123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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;
- },
- };
|