blob: 0748b4b4d983f75725cf74c3181b87a2b1e329e7 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001const install = (Vue, vm) => {
2 // 此为自定义配置参数,具体参数见上方说明
3 Vue.prototype.$u.http.setConfig({
4 // baseUrl: 'https://yy.dlsmk.cn/payapi/mobileapi', // 大理智警域名
guangchao.xuc43cf972021-01-18 13:37:55 +08005 // baseUrl:'https://yy.dlsmk.cn/portal/mobileapi', //正式地址
guangchao.xu070005a2020-12-07 09:56:40 +08006 // baseUrl:'http://yy.dlsmk.cn:8080/portal/mobileapi', //测试地址
guangchao.xu50e42382021-01-04 17:53:47 +08007 // baseUrl:'/api',
guangchao.xuc43cf972021-01-18 13:37:55 +08008 baseUrl: "http://172.28.43.20:8089/portal/mobileapi",//本地地址
guangchao.xu070005a2020-12-07 09:56:40 +08009 // 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.xu50e42382021-01-04 17:53:47 +080034 if (config.url == "/i/activity" || config.url == "/v1/feedback/release"
guangchao.xuc43cf972021-01-18 13:37:55 +080035 || config.url == '/medicineapi/medicalcard/add' || config.url == '/medicalapi/pay'
36 ||config.url == '/v1/security/save' || config.url == '/i/security/check' || config.url == '/i/security/pwdset') {
guangchao.xu070005a2020-12-07 09:56:40 +080037 config.header['content-type'] = "application/json";
38 } else {
39 config.header['content-type'] = "application/x-www-form-urlencoded";
40 }
41 return config;
42 }
43 // 响应拦截,如配置,每次请求结束都会执行本方法
44 Vue.prototype.$u.http.interceptor.response = (res) => {
45 // console.log(res)
46 if (res.statusCode == 200) {
47 // res为服务端返回值,可能有code,result等字段
48 // 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
49 // 如果配置了originalData为true,请留意这里的返回值
50 if (res.data.code == 200) {
51 return res.data
52 } else if(res.data.code == 500){
53 uni.showModal({
54 title: "提示",
55 content: res.data.code + ":" + res.data.msg,
56 showCancel: false,
57 })
58 return false;
59 } else {
60 uni.showModal({
61 title: "错误",
62 content: res.data.code + ":" + res.data.msg,
63 showCancel: false,
64 })
65 return false;
66 }
67 } else if (res.statusCode == 401) {
68 uni.showToast({
69 title: "登录状态失效,请重新登录",
70 icon: "none",
71 mask:true,
72 duration: 1500,
73 complete(res) {
74 setTimeout(() => {
75 uni.reLaunch({
76 url: "/pages/sub_basic/login/index"
77 })
78 }, 1500)
79 }
80 })
81
82 return false;
83 } else {
84 // 如果返回false,则会调用Promise的reject回调,
85 // 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
86 if(res.errMsg.indexOf('fail') == -1){
87 uni.showModal({
88 title: "错误",
89 content: res.data.status + ":" + res.data.message,
90 // content: `${res.data.status}:${res.data.message}`,
91 showCancel: false,
92 })
guangchao.xu50e42382021-01-04 17:53:47 +080093 }else{
94 uni.showModal({
95 title: "错误",
96 content: res.errMsg,
97 // content: `${res.data.status}:${res.data.message}`,
98 showCancel: false,
99 })
guangchao.xu070005a2020-12-07 09:56:40 +0800100 }
101 return false;
102 }
103 }
104
105}
106
107
108// export default install
109module.exports = install
110