/* 常用工具函数 */ 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('&') } const install = (Vue, vm) => { console.log('初始化挂载($util)工具方法 util.js ') Vue.prototype.$util = { msg, prePage, modal, makeQueryStr } } export default install