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