123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- /* 常用工具函数 */
- export const msg = (title, duration = 1500, mask = true, icon = 'none') => {
- //统一提示方便全局修改
- if (Boolean(title) === false) {
- return
- }
- uni.showToast({
- title,
- duration,
- mask,
- icon
- })
- }
- export const modal = (title, content, confirmText, cancelText, showCancel = false, callBack) => {
- uni.showModal({
- title,
- content,
- confirmText,
- cancelText,
- confirmColor: '#E15616',
- showCancel,
- success: function(res) {
- if (res.confirm) {
- callBack()
- }
- }
- })
- }
- export const json = type => {
- //模拟异步请求数据
- return new Promise(resolve => {
- setTimeout(() => {
- // console.log(resolve+'======='+type);
- resolve(Json[type])
- }, 500)
- })
- }
- export const prePage = () => {
- let pages = getCurrentPages()
- let prePage = pages[pages.length - 2]
- // #ifdef H5
- return prePage
- // #endif
- return prePage.$vm
- }
- /** 时间格式化
- * @param {dateTime} date 标准时间格式 -> new Date()
- * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
- * @returns {string} 格式化后的时间 '2017-01-01 01:00:00'
- */
- export const dateFormat = (date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') => {
- var o = {
- 'M+': date.getMonth() + 1, // month
- 'd+': date.getDate(), // day
- 'h+': date.getHours(), // hour
- 'm+': date.getMinutes(), // minute
- 's+': date.getSeconds(), // second
- 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
- S: date.getMilliseconds(), // millisecond
- }
- if (/(y+)/.test(format)) {
- format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
- }
- for (var k in o) {
- if (new RegExp('(' + k + ')').test(format)) {
- format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k])
- .length))
- }
- }
- return format
- }
- // 两个简单数组是否交集
- export const isIntersect = (arr1 = [], arr2 = []) => {
- // console.log(arr1, arr2)
- return arr1.some(v => arr2.indexOf(v.toString()) > -1)
- }
- // 将对象转为querystring参数
- export const makeQueryStr = (obj) => {
- const strs = []
- for (let key in obj) {
- strs.push(key + '=' + obj[key])
- }
- return strs.join('&')
- }
- /**
- * 倒计时
- * @param diff 倒计时时间/s
- * @param loadTime 运行时的当前时间
- * @param item 倒计时对象
- * @param callback 回调
- */
- const countDown = function(diff, loadTime, item, callback) {
- function round($diff) {
- let dd = parseInt($diff / 1000 / 60 / 60 / 24, 10) // 计算剩余的天数
- let hh = parseInt(($diff / 1000 / 60 / 60) % 24, 10) // 计算剩余的小时数
- let mm = parseInt(($diff / 1000 / 60) % 60, 10) // 计算剩余的分钟数
- let ss = parseInt(($diff / 1000) % 60, 10) // 计算剩余的秒数
- function checkTime(_a) {
- let a = _a
- if (a < 10) {
- a = '0' + a
- }
- return a.toString()
- }
- item.conttainer = {
- dd: checkTime(dd),
- hh: checkTime(hh),
- mm: checkTime(mm),
- ss: checkTime(ss),
- }
- if (item.conttainer.dd > 0 || item.conttainer.hh > 0 || item.conttainer.mm > 0 || item.conttainer.ss > 0) {
- item.t = setTimeout(() => {
- callback && callback(item)
- round($diff - 1000)
- }, 1000)
- } else {
- callback && callback(item)
- }
- }
- round(diff - loadTime)
- }
- const install = (Vue, vm) => {
- console.log('初始化挂载($util)工具方法 util.js ')
- Vue.prototype.$util = { msg, prePage, modal, makeQueryStr, countDown }
- }
- export default install
|