/*网络请求*/ import ENV_CONFIG from '@/config/env.js'; import ApiConstant from "@/common/ApiConstant"; // vuex 的使用 详情参考官网 https://uniapp.dcloud.io/vue-vuex export default class Request { http(param) { // 请求参数 var url = param.url, method = param.method, header = { 'X-Access-Token':param.token || "", }, data = param.data || {}, showLoading = true; if (param.showLoad===false) { showLoading = false; } //拼接完整请求地址 var requestUrl = ENV_CONFIG[process.env.NAME].requestBaseUrl + url; //拼接完整请求地址(根据环境切换) //请求方式:GET或POST(POST需配置 if (method) { if(process.env.NAME){ if(process.env.NAME === 'dy_tiyu_prod'){ data['programType'] = '体育志愿宝' }else{ data['programType'] = '艺体志愿宝' } } method = method.toUpperCase(); //小写改为大写 if (method === "POST") { header = {"content-type":'application/json',...header} } else { header = {"content-type":'application/x-www-form-urlencoded',...header} } } //加载圈 if (showLoading) { uni.showLoading({ title: '加载中...' }); } // 返回promise return new Promise((resolve, reject) => { // 请求 uni.request({ url: requestUrl, data: data, method: method, header: header, success: (res) => { console.log('执行http请求') let jsons= JSON.stringify(res) //如果 nginx启动了,后端服务没有启动 if (jsons.includes('502 Bad Gateway')) { //跳转到维护页面 wx.reLaunch({ url: '/pages/error/serviceDown', }) return; } if (res.data && res.data.code === 401) { try { uni.clearStorageSync(); } catch (e) {} // 先显示提示框 uni.showModal({ title: '提示', content: '该功能需要登录后使用喔~', confirmText: '去登录', cancelText: '取消', success: (res) => { if (res.confirm) { // 用户点击去登录 wx.reLaunch({ url: '/pages/zyb/login', }) }else{ uni.navigateBack(); } } }); return; } // 将结果抛出 resolve(res.data) }, //请求失败 fail: (e) => { console.log('e:') console.log(e) if (e && e.errMsg === 'request:fail ') { //服务未启动 //跳转到维护页面 wx.reLaunch({ url: '/pages/error/serviceDown', }) return; }else{ uni.showToast({ title: "" + e.data.msg, icon: 'none' }); } resolve(e.data); }, //请求完成 complete() { //隐藏加载 if (showLoading) { uni.hideLoading(); } resolve(); return; }, }) }) } get(url,data,options={},header={}){ options.method='get'; options.data=data; options.url=url; options.token=uni.getStorageSync('token'); return this.http(options); } get2(url,data,headers={},options={}){ options.method='get'; options.data=data; options.url=url; options.token=uni.getStorageSync('token'); return this.http(options); } post(url,data,options={}){ options.method='post'; options.data=data; options.url=url; options.token=uni.getStorageSync('token'); return this.http(options); } post(url,data,showLoad,options={}){ options.method='post'; options.data=data; options.url=url; options.showLoad = showLoad; options.token=uni.getStorageSync('token'); return this.http(options); } post(url,data,headers={},options={}){ options.method='post'; options.data=data; options.url=url; options.token=uni.getStorageSync('token'); return this.http(options); } delete(url,data,options={}){ options.method='delete'; options.data=data; options.url=url; options.token=uni.getStorageSync('token'); return this.http(options); } getUserScore(){ this.get(ApiConstant.Score.getScore, {},{showLoad:false}).then(res => { if (res.success && res.result) { let scoreInfo = res.result.scoreInfo uni.removeStorageSync('scoreInfo') uni.removeStorageSync('fillVolunteer')//清除可报志愿数量 uni.removeStorageSync('volunteer')//清除志愿单 if(scoreInfo){ uni.setStorageSync('scoreInfo', scoreInfo) } if(res.result.fillVolunteer){ uni.setStorageSync('fillVolunteer',res.result.fillVolunteer) } if(res.result.volunteer){ uni.setStorageSync('volunteer',res.result.volunteer) } } }).catch(err => { }).finally(() => { }); } goto(e){ uni.navigateTo({ url :e }) } }