| import config from './config.js' |
| const base_url = config.base_api |
| console.log(base_url) |
| const install = (Vue, vm) => { |
| // 此为自定义配置参数,具体参数见上方说明 |
| Vue.prototype.$u.http.setConfig({ |
| baseUrl: base_url, |
| // 设置为json,返回后会对数据进行一次JSON.parse() |
| dataType: 'json', |
| // sslVerify:false, |
| showLoading: true, // 是否显示请求中的loading |
| loadingText: '请求中...', // 请求loading中的文字提示 |
| loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms |
| originalData: true, // 是否在拦截器中返回服务端的原始数据 |
| loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透 |
| // 配置请求头信息 |
| header: { |
| 'content-type': 'application/x-www-form-urlencoded' |
| }, |
| }); |
| // 请求拦截部分,如配置,每次请求前都会执行 |
| Vue.prototype.$u.http.interceptor.request = (config) => { |
| const token = uni.getStorageSync('token'); |
| const tenantid = uni.getStorageSync('tenantid'); |
| if (token) { |
| config.header['Authorization'] = "Bearer " + token; |
| } else { |
| config.header['Authorization'] = '' |
| } |
| //console.log(config.url,config.header['Authorization']) |
| config.header["X-TENANT-ID"] = tenantid; |
| if (config.url == "/i/activity" || config.url == "/v1/feedback/release" || |
| config.url == '/medicineapi/medicalcard/add' || config.url == '/medicalapi/pay' || |
| config.url == '/v1/security/save' || config.url == '/i/security/check' || config.url == '/i/security/pwdset') { |
| config.header['content-type'] = "application/json"; |
| } else { |
| config.header['content-type'] = "application/x-www-form-urlencoded"; |
| } |
| return config; |
| } |
| // 响应拦截,如配置,每次请求结束都会执行本方法 |
| Vue.prototype.$u.http.interceptor.response = (res) => { |
| // console.log(res) |
| if (res.statusCode == 200) { |
| // res为服务端返回值,可能有code,result等字段 |
| // 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到 |
| // 如果配置了originalData为true,请留意这里的返回值 |
| if (res.data.code == 200) { |
| return res.data |
| } else if (res.data.code == 500) { |
| uni.showModal({ |
| title: "提示", |
| content: res.data.code + ":" + res.data.msg, |
| showCancel: false, |
| }) |
| return false; |
| } else { |
| uni.showModal({ |
| title: "错误", |
| content: res.data.code + ":" + res.data.msg, |
| showCancel: false, |
| }) |
| return false; |
| } |
| } else if (res.statusCode == 401) { |
| uni.showToast({ |
| title: "登录状态失效,请重新登录", |
| icon: "none", |
| mask: true, |
| duration: 1500, |
| complete(res) { |
| setTimeout(() => { |
| uni.reLaunch({ |
| url: "/pages/sub_basic/login/index" |
| }) |
| }, 1500) |
| } |
| }) |
| |
| return false; |
| } else { |
| // 如果返回false,则会调用Promise的reject回调, |
| // 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值 |
| if (res.errMsg.indexOf('fail') == -1) { |
| uni.showModal({ |
| title: "错误", |
| content: res.data.status + ":" + res.data.message, |
| // content: `${res.data.status}:${res.data.message}`, |
| showCancel: false, |
| }) |
| } else { |
| uni.showModal({ |
| title: "错误", |
| content: res.errMsg, |
| // content: `${res.data.status}:${res.data.message}`, |
| showCancel: false, |
| }) |
| } |
| return false; |
| } |
| } |
| |
| } |
| |
| |
| // export default install |
| module.exports = install |