request copy.js
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import host from '@/utils/ip-config.js';
import { t } from '@/plugins/index.js'
let requestFlag = true
// 请求封装
export default function request(options, ipType = 'ip') {
// 根据不同的类型选择不同的ip
// // #ifdef APP-PLUS
// const ipTypeMap = {
// ip: uni.getStorageSync('serverIp'),
// assetsIp: `${uni.getStorageSync('serverIp')}/images`
// }
// // #endif
// // #ifndef APP-PLUS
// const ipTypeMap = {
// ip: host.ip,
// assetsIp: host.assetsIp
// }
// // #endif
// 声明部分接口必须要用store.keliuyun.com服务
const ipTypeMap = {
ip: host.ip,
assetsIp: host.assetsIp
}
// const url = (ipType === 'store.keliuyun' ? host.ip : ipTypeMap[ipType]) + options.url
const url = ipTypeMap[ipType] + options.url
return new Promise((resolve, reject) => {
const systemInfo = uni.getSystemInfoSync()
// 请求方式
const method = options.method?.toUpperCase() || 'GET';
// 根据method类型调整header,GET为?a=x&b=y,POST为json
const headerContentType = method === 'GET' ? 'application/x-www-form-urlencoded' : 'application/json';
const _data = Array.isArray(options.data) ? options.data : {
...(options.data || {}),
};
// const _data = {
// ...(options.data || {}),
// // _t:Date.now()
// }
const languageObj = {
'mall_CN': 'zh-CN,zh;q=0.9',
'zh_CN': 'zh-CN,zh;q=0.9',
'en_US': 'en-US,en;q=0.5',
'zh_TW': 'zh-TW,zh;q=0.3',
'ja_JP':'ja-JP,ja;q=0.9'
}
const lang = uni.getStorageSync('lang') || host.defaultLang
const setting = {
url,
data: _data,
method,
timeout: 60 * 1000,
header: {
'content-type': headerContentType,
// 请求体中添加设备信息
'Device-OS': systemInfo.platform || 'unknown', // 直接使用同步获取的信息
'Device-Model': systemInfo.model || 'unknown',
'Authorization': uni.getStorageSync('Authorization') || '',
'Accept-Language': languageObj[lang] || 'en-US,en;q=0.5',
},
success(res) {
// 如果是静态资源,直接返回
if (ipType === 'assetsIp') {
resolve(res.data);
return;
}
// 否则判断res.data内部的code
if (res.data.ecode === 401 && ipType !== 'store.keliuyun') {
console.log(1);
if (requestFlag) {
requestFlag = false
uni.showModal({
title: t('message.prompt'),
showCancel: false,
content: t('Message.atokeIsDisabled'),
confirmText: t('app.login.reLogin'),
success: function(resModal) {
if (resModal.confirm) {
requestFlag = true
uni.removeStorageSync('Authorization')
// uni.clearStorageSync()
uni.reLaunch({
url: '/pages/login/index'
})
} else if (resModal.cancel) {
console.log('用户点击取消');
}
}
})
}
} else if (res.data.code !== 200) {
if(!options.hiddenMsg){
uni.showToast({
icon: 'none',
title: res?.data?.msg || 'Error'
})
}
reject(res?.data?.msg || 'Error')
} else {
resolve(res.data);
}
},
fail(err) {
uni.showToast({
icon: 'none',
title: t('app.login.networkError')
})
reject(err);
},
}
if (options.header) {
setting.header = Object.assign(setting.header, options.header);
}
uni.request(setting);
})
}