guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame] | 1 | const install = (Vue, vm) => { |
| 2 | // 此为自定义配置参数,具体参数见上方说明 |
| 3 | Vue.prototype.$u.http.setConfig({ |
| 4 | // baseUrl: 'https://yy.dlsmk.cn/payapi/mobileapi', // 大理智警域名 |
| 5 | baseUrl:'https://yy.dlsmk.cn/portal/mobileapi', //正式地址 |
| 6 | // baseUrl:'http://yy.dlsmk.cn:8080/portal/mobileapi', //测试地址 |
| 7 | // baseUrl: "http://172.28.43.20:8089/portal/mobileapi",//本地地址 |
| 8 | // method: 'POST', |
| 9 | // 设置为json,返回后会对数据进行一次JSON.parse() |
| 10 | dataType: 'json', |
| 11 | // sslVerify:false, |
| 12 | showLoading: true, // 是否显示请求中的loading |
| 13 | loadingText: '请求中...', // 请求loading中的文字提示 |
| 14 | loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms |
| 15 | originalData: true, // 是否在拦截器中返回服务端的原始数据 |
| 16 | loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透 |
| 17 | // 配置请求头信息 |
| 18 | header: { |
| 19 | 'content-type': 'application/x-www-form-urlencoded' |
| 20 | }, |
| 21 | }); |
| 22 | // 请求拦截部分,如配置,每次请求前都会执行 |
| 23 | Vue.prototype.$u.http.interceptor.request = (config) => { |
| 24 | const token = uni.getStorageSync('token'); |
| 25 | const tenantid = uni.getStorageSync('tenantid'); |
| 26 | if (token) { |
| 27 | config.header['Authorization'] = "Bearer " + token; |
| 28 | }else{ |
| 29 | config.header['Authorization'] = '' |
| 30 | } |
| 31 | //console.log(config.url,config.header['Authorization']) |
| 32 | config.header["X-TENANT-ID"] = tenantid; |
| 33 | if (config.url == "/i/activity" || config.url == "/v1/feedback/release" || config.url == '/medicineapi/medicalcard/add') { |
| 34 | config.header['content-type'] = "application/json"; |
| 35 | } else { |
| 36 | config.header['content-type'] = "application/x-www-form-urlencoded"; |
| 37 | } |
| 38 | return config; |
| 39 | } |
| 40 | // 响应拦截,如配置,每次请求结束都会执行本方法 |
| 41 | Vue.prototype.$u.http.interceptor.response = (res) => { |
| 42 | // console.log(res) |
| 43 | if (res.statusCode == 200) { |
| 44 | // res为服务端返回值,可能有code,result等字段 |
| 45 | // 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到 |
| 46 | // 如果配置了originalData为true,请留意这里的返回值 |
| 47 | if (res.data.code == 200) { |
| 48 | return res.data |
| 49 | } else if(res.data.code == 500){ |
| 50 | uni.showModal({ |
| 51 | title: "提示", |
| 52 | content: res.data.code + ":" + res.data.msg, |
| 53 | showCancel: false, |
| 54 | }) |
| 55 | return false; |
| 56 | } else { |
| 57 | uni.showModal({ |
| 58 | title: "错误", |
| 59 | content: res.data.code + ":" + res.data.msg, |
| 60 | showCancel: false, |
| 61 | }) |
| 62 | return false; |
| 63 | } |
| 64 | } else if (res.statusCode == 401) { |
| 65 | uni.showToast({ |
| 66 | title: "登录状态失效,请重新登录", |
| 67 | icon: "none", |
| 68 | mask:true, |
| 69 | duration: 1500, |
| 70 | complete(res) { |
| 71 | setTimeout(() => { |
| 72 | uni.reLaunch({ |
| 73 | url: "/pages/sub_basic/login/index" |
| 74 | }) |
| 75 | }, 1500) |
| 76 | } |
| 77 | }) |
| 78 | |
| 79 | return false; |
| 80 | } else { |
| 81 | // 如果返回false,则会调用Promise的reject回调, |
| 82 | // 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值 |
| 83 | if(res.errMsg.indexOf('fail') == -1){ |
| 84 | uni.showModal({ |
| 85 | title: "错误", |
| 86 | content: res.data.status + ":" + res.data.message, |
| 87 | // content: `${res.data.status}:${res.data.message}`, |
| 88 | showCancel: false, |
| 89 | }) |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |
| 97 | |
| 98 | // export default install |
| 99 | module.exports = install |
| 100 | |