import config from '@/config' import { getToken } from '@/utils/auth' import errorCode from '@/utils/errorCode' let timeout = 10000 const baseUrl = config.baseUrl /** * 显示消息提示框 * @param content 提示的标题 */ export function toast(content) { uni.showToast({ icon: 'none', title: content }) } /** * 显示模态弹窗 * @param content 提示的标题 */ export function showConfirm(content) { return new Promise((resolve, reject) => { uni.showModal({ title: '提示', content: content, cancelText: '取消', confirmText: '确定', success: function(res) { resolve(res) } }) }) } // 字典值匹配 export function selectDictValue(datas, value) { var actions = []; Object.keys(datas).some((key) => { if (datas[key].dictValue == ('' + value)) { actions.push(datas[key].dictLabel); return true; } }) return actions.join(''); } // export function selectDictLabel(datas, value) { // var actions = []; // Object.keys(datas).some((key) => { // if (datas[key].dictLabel == ('' + value)) { // actions.push(datas[key].dictValue); // return true; // } // }) // return actions.join(''); // } export function selectValueKey(datas, value) { var actions = []; var idx=0; Object.keys(datas).some((key) => { if (datas[key].dictValue == ('' + value)) { idx=key; actions.push(datas[key].dictLabel); return true; } }) var newObj={ actions:actions.join(''), key:idx } return newObj } // export function selectValue(datas, value) { // var actions = []; // var idx=0; // Object.keys(datas).some((key) => { // if (datas[key].value == ('' + value)) { // actions.push(datas[key].label); // return true; // } // }) // return actions.join('') // } // export function selectValuetext(datas, value) { // var actions = []; // var idx=0; // Object.keys(datas).some((key) => { // if (datas[key].value == ('' + value)) { // actions.push(datas[key].text); // return true; // } // }) // return actions.join('') // } export function geocodeAddress(address, key) { return new Promise((resolve, reject) => { // H5 和 App 平台使用高德地图 JavaScript API if (uni.getSystemInfoSync().platform === 'h5' || uni.getSystemInfoSync().platform === 'android') { const url = `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(address)}&key=${key}`; uni.request({ url, success: (res) => { console.log(res) if (res.data.status === '1' && res.data.geocodes.length > 0) { const location = res.data.geocodes[0].location; // 返回格式:经度,纬度 const [longitude, latitude] = location.split(','); console.log(223) resolve({ latitude: parseFloat(latitude), longitude: parseFloat(longitude) }); } else { reject(new Error('未找到地址对应的经纬度')); } }, fail: (err) => { reject(err); }, }); } // 小程序平台使用高德地图小程序 SDK else if (uni.getSystemInfoSync().platform === 'mp-weixin') { wx.request({ url: `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(address)}&key=${key}`, success: (res) => { if (res.data.status === '1' && res.data.geocodes.length > 0) { const location = res.data.geocodes[0].location; // 返回格式:经度,纬度 const [longitude, latitude] = location.split(','); resolve({ latitude: parseFloat(latitude), longitude: parseFloat(longitude) }); } else { reject(new Error('未找到地址对应的经纬度')); } }, fail: (err) => { reject(err); }, }); } else { reject(new Error('不支持的平台')); } }); } /** * 参数处理 * @param params 参数 */ export function tansParams(params) { let result = '' for (const propName of Object.keys(params)) { const value = params[propName] var part = encodeURIComponent(propName) + "=" if (value !== null && value !== "" && typeof (value) !== "undefined") { if (typeof value === 'object') { for (const key of Object.keys(value)) { if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') { let params = propName + '[' + key + ']' var subPart = encodeURIComponent(params) + "=" result += subPart + encodeURIComponent(value[key]) + "&" } } } else { result += part + encodeURIComponent(value) + "&" } } } return result } //上传图片(本地地址识别一张) export function uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback) { const isToken = (config.headers || {}).isToken === false config.header = config.header || {} if (getToken() && !isToken) { config.header['Authorization'] = 'Bearer ' + getToken() } // get请求映射params参数 if (config.params) { let url = config.url + '?' + tansParams(config.params) url = url.slice(0, -1) config.url = url } uni.showLoading({ title: '上传中' }) var failfile = []; uni.uploadFile({ timeout: config.timeout || timeout, url: baseUrl + api, //仅为示例,非真实的接口地址 filePath: filePaths[i], name: 'file', header: config.header, formData: config.formData, success: function(resp) { uni.hideLoading(); let result = JSON.parse(resp.data) const code = result.code || 200 const msg = errorCode[code] || result.msg || errorCode['default'] // console.log(result.fileName,8) if (result.code == 200) { successUp++; files[i] = result; } else if(result.code==401) { showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => { if (res.confirm) { store.dispatch('LogOut').then(res => { uni.reLaunch({ url: '/pages/login/login' }) }) } }) callback('无效的会话,或者会话已过期,请重新登录。'); }else{ failfile = failfile.concat(filePaths[i]) failUp++; } }, fail: function(res) { uni.hideLoading(); failfile = failfile.concat(filePaths[i]) failUp++; }, complete: function(rsp) { // console.log(rsp, filePaths[i]) uni.hideLoading(); i++; if (i == length) { uni.showToast({ title: '上传成功', icon: 'none', duration: 2000 }); callback(files); } else { //递归调用upload函数 uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback); } } }); } //上传图片 export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) { const isToken = (config.headers || {}).isToken === false config.header = config.header || {} if (getToken() && !isToken) { config.header['Authorization'] = 'Bearer ' + getToken() } // get请求映射params参数 if (config.params) { let url = config.url + '?' + tansParams(config.params) url = url.slice(0, -1) config.url = url } uni.showLoading({ title: '上传中' }) var failfile = []; uni.uploadFile({ timeout: config.timeout || timeout, url: baseUrl + api, //仅为示例,非真实的接口地址 filePath: filePaths[i], name: 'file', header: config.header, formData: config.formData, success: function(resp) { uni.hideLoading(); let result = JSON.parse(resp.data) const code = result.code || 200 const msg = errorCode[code] || result.msg || errorCode['default'] // console.log(result.fileName,8) if (result.code == 200) { successUp++; files[i] = result.fileName; } else if(result.code==401) { showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => { if (res.confirm) { store.dispatch('LogOut').then(res => { uni.reLaunch({ url: '/pages/login/login' }) }) } }) callback('无效的会话,或者会话已过期,请重新登录。'); }else{ failfile = failfile.concat(filePaths[i]) failUp++; } }, fail: function(res) { uni.hideLoading(); failfile = failfile.concat(filePaths[i]) failUp++; }, complete: function(rsp) { uni.hideLoading(); i++; if (i == length) { uni.showToast({ title: '上传成功', icon: 'none', duration: 2000 }); callback(files); } else { //递归调用upload函数 uploadmore(api, filePaths, successUp, failUp, i, length, files, callback); } } }); }