|
|
export default { /** * 图片处理-预览图片 * @param {Array} urls - 图片列表 * @param {Number} current - 首个预览下标 */ previewImage(urls = [], current = 0) { uni.previewImage({ urls: urls, current: current, indicator: 'default', loop: true, fail(err) { console.log('previewImage出错', urls, err) }, }) },
/** * 打电话 * @param {String<Number>} phoneNumber - 数字字符串 */ callPhone(phoneNumber = '') { let num = phoneNumber.toString() uni.makePhoneCall({ phoneNumber: num, fail(err) { console.log('makePhoneCall出错', err) }, }); }, /** * @description: 封装弹窗交互 * @param {String} msg 提示信息 * @param {Function} succ 点确认执行函数 * @return {Promise} */ showModal(msg,succ){ let that = this return new Promise((rs,rj)=>{ uni.showModal({ title: '温馨提示', content: msg, success: function (res) { if (res.confirm) { succ() rs(true) } else if (res.cancel) { that.showNone("已取消") rj(false) } } }); }) }, /** * @description: 弹窗封装 * @param {String} msg * @return: {*} */ showNone(msg){ uni.showToast({ title: msg, icon: 'none', duration:2000 }); }, /** * @description: 自动封装promise * @param {Function} api * @return {Promise API} */ promisify(api){ return (options, ...params) => { return new Promise((resolve, reject) => { api(Object.assign({}, options, { success: resolve, fail: reject }), ...params); }); } }, /** * @description: 获取URL拼接 * @param {JSON Object} data * @param {String} url * @return {String} */ getWebURL(data,url){ let result = "" for(var i in data){ result+=`&${i}=${data[i]}` } return url+"?"+result.slice(1) }, /** * @description: 休眠指定时间 * @param {Int} time * @return: {*} */ async sleep(time){ const res = await new Promise(resolve => { setTimeout(() => resolve("asyncSetTimeOut"), time||1000); }); return res }, /** * @description: 解析onload传递参数,兼容多种传参方式 * @param {String ,JSON Object } param * @return: {*} */ getDecodeObj(param) { //解析传递参数 - 兼容开发者工具快速调试参数,并做promise封装
return new Promise((rs, rj) => { let res = decodeURIComponent(param) try { res = JSON.parse(res) } catch (e) { //TODO handle the exception
res = JSON.parse(decodeURIComponent(res)) || res } finally { if (typeof(res) == 'object') rs(res) rj(res) } }) }, /** * @description: 防抖函数封装 * @param {Function} func * @param {Int} wait * @param {Bool} immediate * @return {debounce Function} */ debounce(func, wait, immediate) { let timeout, args, context, timestamp, result; const later = function() { // 据上一次触发时间间隔
const last = +new Date() - timestamp; // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
if (last < wait && last > 0) { timeout = setTimeout(later, wait - last); } else { timeout = null; // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
if (!immediate) { result = func.apply(context, args); if (!timeout) context = args = null; } } } return function(...args) { context = this; timestamp = +new Date(); const callNow = immediate && !timeout; // 如果延时不存在,重新设定延时
if (!timeout) timeout = setTimeout(later, wait); if (callNow) { result = func.apply(context, args); context = args = null; } return result; } }, /** * @description: 路由跳转,uni版(未兼容路由) * @param {String Path} url * @param {String } type * @return: {*} */ routeTo(url,type){ switch(type){ case 'nT': uni.navigateTo({url}); break case 'rT': uni.redirectTo({url}); break case 'rL': uni.reLaunch({url}); break case 'sT': uni.switchTab({url}); break default: uni.navigateBack({delta: 1}) break } }, route(url,type){ // 无再次封装必要,参考
// https://hhyang.cn/v2/api/routerInsatll.html#router-aftereach
}, }
|