import { rsaDecrypt } from '@/utils/jsencrypt'; /** * 显示loading * @param {String|Object} params title或者options。 */ function loading(params) { const options = getOptions(params); uni.showLoading({ title: '加载中', mask: true, // 是否显示透明蒙层,防止触摸穿透,默认true ...options, }); } /** * 传入Promise方法自定加loading * @param {Function} */ async function dispatch(callback) { if (callback.constructor.name === 'AsyncFunction') { // todo callback 拿到的永远是 function,暂无解决方案 // 若方法返回值是promise try { loading(); const res = await callback(); uni.hideLoading(); return res; } catch (e) { uni.hideLoading(); console.error(e); } return; } // 函数返回值不是Promise const res = callback(); return res; } /** * 将11位电话 4-7 变为 * * @param {String} tel 电话号码 * @param {Boolean} isDecrypt 是否解密电话号码 */ function middleTel(tel = '', isDecrypt) { let phoneNum = tel; if (isDecrypt) { // 需要解密tel phoneNum = rsaDecrypt(tel); } const reg = /^(\d{3})\d{4}(\d{4})$/; return phoneNum.replace(reg, '$1****$2'); } /** * @param {String|Object} params title或者options。 * @return {Object} 返回Object */ function getOptions(params) { let options = {}; if (params === undefined || params === null) { return options; } if (params.constructor === String) { options.title = params; } if (params.constructor === Object) { options = { ...params }; } return options; } /** * 显示消息提示框 * @param {String|Object} params title或者options。 * title 提示的内容,长度与 icon 取值有关。 * @return {Promises} 返回Promise */ async function toast(params) { const options = getOptions(params); const duration = options.duration || 1000; // 提示的延迟时间,单位毫秒,默认:2000 return new Promise((resolve) => { uni.showToast({ icon: 'none', duration, ...options, }); setTimeout(() => { resolve(); }, duration); }); } const $c = { loading, dispatch, middleTel, toast, }; export default { install(Vue) { Vue.prototype.$c = $c; uni.$c = $c; }, };