// 引用工具类 const utils = require('../common/util.js'); //服务接口访问配置 var config = { // sysweburl: "http://110.188.24.28:50300", // 开发 sysweburl: "http://110.188.24.28:50200", // 测试 // sysweburl: "http://171.94.154.2:50105/hl", // 正式 // sysweburl: "https://new.zg12345.cn/hl", // 正式 upfileurl: "http://110.188.24.28:50120/file/upload", // 测试 // upfileurl: "http://171.94.154.2:50105/hlfs/file/upload", // 正式 // upfileurl: "https://new.zg12345.cn/hlfs/file/upload", // 正式 getfileurl: "http://110.188.24.28:50120", // 测试 // getfileurl: "http://171.94.154.2:50105/hlfs", // 正式 // getfileurl: "https://new.zg12345.cn/hlfs", // 正式 } // 服务器返回字段 var response = { //自定义响应字段 statusName: 'code', //数据状态的字段名称 msgName: 'msg', //状态信息的字段名称 dataName: 'data' ,//数据详情的字段名称 } // http状态码 var statusCode = { ok: 200, //数据状态一切正常的状态码 logout: 401, //登录状态失效的状态码 } //录音对象 const recorderManager = uni.getRecorderManager(); //播放对象 const innerAudioContext = uni.createInnerAudioContext(); // 全局变量请求头 var _token = ''; // token // 登录失效提示方法 const logErrTip = () => { uni.hideLoading(); uni.showModal({ title: '登录提示', content: '您未授权登录,请授权', showCancel: true, confirmText: '确定', success: (e) => { if (e.confirm) { // 获取微信登录授权 uni.switchTab({ url: "/pages/Index/Mine" }) } }, fail: () => {}, complete: () => {} }) } // 自动登录 const autoLogin = (fun) => { reqDirect({ method: 'GET', url: '/api/v1/Identity/third/refresh', data: {openId: uni.getStorageSync('userInfo').openID} }).then(res => { saveUserInfo(res, function(){ typeof fun === 'function' && fun(); }); }, err => { utils.msg(err, function() { typeof fun === 'function' && fun(); }); }); } // 缓存登录用户信息 const saveUserInfo = (data, fun) => { _token = data.token; console.log(data); uni.setStorageSync('userInfo', { openID: data.openId, userName: data.userName || '', userImg: '', userTel: data.phoneNumber || '', userType: data.userType || '1', // 登录用户类型 1:市民 2:网格员 YQCode: data.invitationCode, // 邀请码 isVolunteer: data.isVolunteer // 是否为志愿者 }) typeof fun === 'function' && fun(); } // 调用数据请求入口方法——需验证是否登录 const req = (_options, hideLoading) => { if (!(hideLoading || false)) { uni.showLoading({ title: '请求中...', mask: true }) } // 获取当前缓存的微信openID let openID = uni.getStorageSync('userInfo').openID; // 异步请求数据 return new Promise((resolve, reject) => { if (!openID) { // 检查openID是否存在 // 登录失效提示方法 logErrTip(); reject(); } else { if(!_token){ //检查token是否存在 // 不存在-自动登录 autoLogin(function(){ ajax(resolve, reject, _options); }) }else{ // 存在-直接请求 ajax(resolve, reject, _options); } } }) } // 调用数据请求入口方法——不用验证是否登录 const reqDirect = (_options, hideLoading) => { if (!(hideLoading || false)) { uni.showLoading({ title: '请求中...', mask: true }) } // 异步请求数据 return new Promise((resolve, reject) => { ajax(resolve, reject, _options); }) } /* * * 请求接口数据 * */ const ajax = (resolve, reject, _options) => { uni.request({ method: _options.method || 'POST', url: config.sysweburl + _options.url, data: _options.data || {}, header: { // 'Content-Type': _options.contentType || 'application/x-www-form-urlencoded; charset=UTF-8', 'Authorization': 'Bearer ' + _token }, success: function(res) { console.log(res); uni.hideLoading(); if(res.statusCode == statusCode.ok){ let jsonResult = {}; jsonResult[response.statusName] = res.data.code; jsonResult[response.msgName] = res.data.error; jsonResult[response.dataName] = res.data.result; if (jsonResult[response.statusName] == 0){ resolve(jsonResult[response.dataName]); }else { if (reject) { reject(jsonResult[response.msgName]); } else { utils.msg(jsonResult[response.msgName]); } } }else if (res.statusCode == statusCode.logout){ // 登录失效提示方法 logErrTip() }else { utils.msg("服务异常,请稍后重试"); } }, fail: function() { uni.hideLoading(); utils.msg('服务异常,请稍后重试!'); } }); } // 附件上传 const onUploadFiles = (files, fun) => { uni.showLoading({ title: '上传中,请稍后', mask: true }) let pArr = []; if (files && files.length > 0) { files.forEach((file, i, array) => { pArr.push(new Promise(function(resolve, reject) { onUpLoadSingleFile(file, "uploadkey" + i, resolve, reject); })) }); Promise.all(pArr).then(res => { let strRes = res.toString(); uni.hideLoading(); typeof fun === 'function' && fun(strRes); }, err => { uni.hideLoading(); util.msg(err.msg); }) } else { uni.hideLoading(); typeof fun === 'function' && fun(); } } // 单个附件上传 const onUpLoadSingleFile = (file, key, resolve, reject) => { let fileObj = { fileType: file.fileType, msg: '' }; switch (fileObj.fileType) { case 'vioce': fileObj.msg = '录音上传失败'; break; case 'image': fileObj.msg = '图片上传失败'; break; case 'video': fileObj.msg = '视频上传失败'; break; default: break; } uni.uploadFile({ url: config.upfileurl, filePath: file.tempFilePath, name: key, // formData: { // Code: 'ZGSSP20240102', // Duration: file.duration || '' // }, timeout: 10000, success: res => { if (res.data) { var data = res.data; try { data = JSON.parse(data); if (data[0].code == '1') { return resolve(data[0].data[0].fid); } else { return reject(fileObj) } } catch (d) { return reject(fileObj) } } else { return reject(fileObj) } }, fail: res => { return reject(fileObj) }, }) } //暴露模块 module.exports = { config: config, req: req, reqDirect: reqDirect, onUploadFiles: onUploadFiles, recorderManager: recorderManager, innerAudioContext: innerAudioContext, saveUserInfo: saveUserInfo }