blob: db568befc2c20c77df70ca3092f9eb99f2d8e257 [file] [log] [blame]
guangchao.xu1bc4ccc2021-01-19 17:09:26 +08001import config from './config.js'
2const base_url = config.base_api
3console.log(base_url)
guangchao.xu070005a2020-12-07 09:56:40 +08004const install = (Vue, vm) => {
5 // 此为自定义配置参数,具体参数见上方说明
6 Vue.prototype.$u.http.setConfig({
guangchao.xu1bc4ccc2021-01-19 17:09:26 +08007 baseUrl: base_url,
guangchao.xu070005a2020-12-07 09:56:40 +08008 // 设置为json,返回后会对数据进行一次JSON.parse()
9 dataType: 'json',
10 // sslVerify:false,
11 showLoading: true, // 是否显示请求中的loading
12 loadingText: '请求中...', // 请求loading中的文字提示
13 loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
14 originalData: true, // 是否在拦截器中返回服务端的原始数据
15 loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
16 // 配置请求头信息
17 header: {
18 'content-type': 'application/x-www-form-urlencoded'
19 },
20 });
21 // 请求拦截部分,如配置,每次请求前都会执行
22 Vue.prototype.$u.http.interceptor.request = (config) => {
23 const token = uni.getStorageSync('token');
24 const tenantid = uni.getStorageSync('tenantid');
25 if (token) {
26 config.header['Authorization'] = "Bearer " + token;
guangchao.xu1bc4ccc2021-01-19 17:09:26 +080027 } else {
guangchao.xu070005a2020-12-07 09:56:40 +080028 config.header['Authorization'] = ''
29 }
30 //console.log(config.url,config.header['Authorization'])
31 config.header["X-TENANT-ID"] = tenantid;
guangchao.xu1bc4ccc2021-01-19 17:09:26 +080032 if (config.url == "/i/activity" || config.url == "/v1/feedback/release" ||
33 config.url == '/medicineapi/medicalcard/add' || config.url == '/medicalapi/pay' ||
34 config.url == '/v1/security/save' || config.url == '/i/security/check' || config.url == '/i/security/pwdset') {
guangchao.xu070005a2020-12-07 09:56:40 +080035 config.header['content-type'] = "application/json";
36 } else {
37 config.header['content-type'] = "application/x-www-form-urlencoded";
38 }
39 return config;
40 }
41 // 响应拦截,如配置,每次请求结束都会执行本方法
42 Vue.prototype.$u.http.interceptor.response = (res) => {
43 // console.log(res)
44 if (res.statusCode == 200) {
45 // res为服务端返回值,可能有code,result等字段
46 // 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
47 // 如果配置了originalData为true,请留意这里的返回值
48 if (res.data.code == 200) {
49 return res.data
guangchao.xu1bc4ccc2021-01-19 17:09:26 +080050 } else if (res.data.code == 500) {
guangchao.xu070005a2020-12-07 09:56:40 +080051 uni.showModal({
52 title: "提示",
53 content: res.data.code + ":" + res.data.msg,
54 showCancel: false,
55 })
56 return false;
57 } else {
58 uni.showModal({
59 title: "错误",
60 content: res.data.code + ":" + res.data.msg,
61 showCancel: false,
62 })
63 return false;
64 }
65 } else if (res.statusCode == 401) {
66 uni.showToast({
67 title: "登录状态失效,请重新登录",
68 icon: "none",
guangchao.xu1bc4ccc2021-01-19 17:09:26 +080069 mask: true,
guangchao.xu070005a2020-12-07 09:56:40 +080070 duration: 1500,
71 complete(res) {
72 setTimeout(() => {
73 uni.reLaunch({
74 url: "/pages/sub_basic/login/index"
75 })
76 }, 1500)
77 }
78 })
79
80 return false;
81 } else {
82 // 如果返回false,则会调用Promise的reject回调,
83 // 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
guangchao.xu1bc4ccc2021-01-19 17:09:26 +080084 if (res.errMsg.indexOf('fail') == -1) {
guangchao.xu070005a2020-12-07 09:56:40 +080085 uni.showModal({
86 title: "错误",
87 content: res.data.status + ":" + res.data.message,
88 // content: `${res.data.status}:${res.data.message}`,
89 showCancel: false,
90 })
guangchao.xu1bc4ccc2021-01-19 17:09:26 +080091 } else {
guangchao.xu50e42382021-01-04 17:53:47 +080092 uni.showModal({
93 title: "错误",
94 content: res.errMsg,
95 // content: `${res.data.status}:${res.data.message}`,
96 showCancel: false,
97 })
guangchao.xu070005a2020-12-07 09:56:40 +080098 }
99 return false;
100 }
101 }
102
103}
104
105
106// export default install
107module.exports = install