import request from './axios'; import store from '../store'; /** * 字典工具 */ const $dict = { dicts() { return uni.getStorageSync('dicts') || []; }, load(...code) { return request.instance.post('/admin/dictionary/queryValsByCodeList?codeList=' + code.join(','), null, { withoutToken: true }).then((res) => { if (res && res.data) { let cacheDicts = {}, meta = {}; $dict.dicts().map((e) => (cacheDicts[e.key] = e)); res.data.map((e) => (meta[e.key] = e)); let dicts = { ...cacheDicts, ...meta }; uni.setStorageSync('dicts', Object.values(dicts)); } }); }, getDict(key) { if ($dict.dicts().length) { let dict = $dict.dicts().find((e) => e.key == key); return dict ? dict.values : []; } else return []; }, getValue(key, label) { if ($dict.dicts().length) { let dict = $dict.dicts().find((e) => e.key == key); if (dict) { let item = dict.values.find((v) => v.dictName == label); return item ? item.dictValue : label; } else return label; } else return label; }, getLabel(key, value) { if ($dict.dicts().length) { let dict = $dict.dicts().find((e) => e.key == key); if (dict) { let item = dict.values.find((v) => v.dictValue == value); return item ? item.dictName : value; } else return value ? value: ''; } else return value ? value: ''; }, getColor(key, value) { if ($dict.dicts().length) { let dict = $dict.dicts().find((e) => e.key == key); if (dict) { let item = dict.values.find((v) => v.dictValue == value); return item ? item.dictColor : value; } else return value; } else return value; } }; const $toast = (obj) => { let params = { title: '', duration: 2000, icon: 'none' }; if (typeof obj == 'string') { params.title = obj; } else { Object.assign(params, obj); } uni.showToast(params); }; const $loading = (title) => { uni.showLoading({ title: title ? title : '加载中', mask: true }); }; const $hideLoading = () => { uni.hideLoading(); }; const $dialog = { alert: (params) => { return new Promise((resolve) => { uni.showModal({ title: '温馨提示', showCancel: false, confirmColor: '#197DF0', confirmText: params.confirmButtonText ? params.confirmButtonText : '确定', ...params, success: (res) => { if (res.confirm) { resolve(); } } }); }); }, confirm: (params) => { return new Promise((resolve, reject) => { uni.showModal({ title: '温馨提示', showCancel: true, confirmColor: '#197DF0', cancelText: params.cancelButtonText ? params.cancelButtonText : '取消', confirmText: params.confirmButtonText ? params.confirmButtonText : '确定', ...params, success: (res) => { if (res.confirm) { resolve(); } else if (res.cancel) { reject(); } } }); }); } }; const $linkTo = (url) => { uni.navigateTo({ url }); }; const $formatName = (name) => { if (name == undefined) { return; } return name.substr(name.length - 2, name.length > 2 ? name.length - 1 : name.length); }; const $previewImage = (list, index, urlName) => { uni.previewImage({ current: list[index][urlName], urls: list.map((v) => v[urlName]) }); }; const $getLoginCode = () => { return new Promise(function(resolve, reject) { uni.login({ success: function(res) { if (res.code) { resolve(res); } else { reject(res); } }, fail: function() { reject(false); } }); }); }; const $autoLogin = (params) => { params = params? params: {}; return new Promise(function(resolve, reject) { $getLoginCode().then((res) => { let body = { ...params, code: res.code }; store.commit('getToken', { ...body, then: (v1) => { if (v1) { store.commit('getUserInfo', (v2) => { if (v2) { resolve(v2); } else { reject(v2); } }); } else { reject(v1); } } }); }); }); }; const $getUserProfile = () => { return new Promise(function(resolve, reject) { wx.getUserProfile({ desc: '用于完善会员资料', lang: 'zh_CN', success: (data) => { resolve(data); } }); }); }; export default { $dict, $toast, $loading, $hideLoading, $dialog, $linkTo, $formatName, $previewImage, $getLoginCode, $getUserProfile, $autoLogin, };