You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.9 KiB
123 lines
3.9 KiB
import urlConfig from './config.js'
|
|
// import store from '@/store/index'
|
|
const request = {}
|
|
|
|
request.globalRequest = (url, options = {}, power) => {
|
|
/* 权限判断 因为有的接口请求头可能需要添加的参数不一样,所以这里做了区分
|
|
1 == 不通过access_token校验的接口
|
|
2 == 文件下载接口列表
|
|
3 == 验证码登录 */
|
|
|
|
let contentType = localStorage.token_type ? "application/json" : "application/x-www-form-urlencoded";
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: url,
|
|
...options, //data,method...
|
|
header: {
|
|
"content-type": contentType,
|
|
"Authorization": localStorage.token_type,
|
|
'withCredentials': true,
|
|
// 'Blade-Auth':'bearer '+store.state.token,
|
|
'token_type': localStorage.token_type,
|
|
...options.header,
|
|
},
|
|
success: (res) => {
|
|
if (res != null) {
|
|
if (res.statusCode != null) {
|
|
let statusCode = res.statusCode.toString();
|
|
// 状态码类型 2开头的全是成功
|
|
let code = statusCode.substring(0, 1);
|
|
if (statusCode == '200') {
|
|
resolve(res.data);
|
|
} else if (statusCode == '204') {
|
|
if (options.method == 'get') {
|
|
reject("没查到")
|
|
} else {
|
|
resolve(res.data)
|
|
}
|
|
} else if (statusCode == '400') {
|
|
if (res.data.error) {
|
|
if (res.data.error.message) {
|
|
reject(res.data.error.message);
|
|
}
|
|
} else if (res.data.errors) {
|
|
let errors = res.data.errors;
|
|
let errorMsg = '请求参数错误:\n';
|
|
let keys = Object.keys(errors);
|
|
for (var i = 0; i < keys.length; i++) {
|
|
let key = keys[i];
|
|
let value = errors[key];
|
|
errorMsg += i + 1 + ':' + value + '\n';
|
|
}
|
|
reject(errorMsg);
|
|
}
|
|
} else if (statusCode == '404') {
|
|
if (res.data === '') {
|
|
reject('未找到接口');
|
|
} else {
|
|
reject(res.data);
|
|
}
|
|
} else if (statusCode == '403') {
|
|
let message = res.data.error.message;
|
|
if (message) {
|
|
let temp;
|
|
try {
|
|
temp = JSON.parse(message)
|
|
} catch (err) {
|
|
reject(message);
|
|
}
|
|
if (temp) {
|
|
var hintError = temp.error;
|
|
var hintErrorDes = temp.error_description;
|
|
if (hintError == 'invalid_grant') {
|
|
if (hintErrorDes.includes(
|
|
'Invalid username or password!')) {
|
|
reject('用户名或密码错误');
|
|
} else if (hintErrorDes.includes(
|
|
'The user account has been locked out due to invalid login attempts'
|
|
)) {
|
|
reject("账号已被锁定,请稍后再试");
|
|
}
|
|
} else {
|
|
reject(message)
|
|
}
|
|
}
|
|
} else {
|
|
reject(res.statusCode + "错误")
|
|
}
|
|
} else {
|
|
let message = res.data.error.message;
|
|
if (message != undefined) {
|
|
reject(res.data.error.message)
|
|
} else {
|
|
reject(res.statusCode + "错误")
|
|
}
|
|
}
|
|
} else {
|
|
reject('options.url' + "返回的状态码类型为空")
|
|
}
|
|
} else {
|
|
reject('options.url' + "返回的res为空")
|
|
}
|
|
},
|
|
fail: (eror) => {
|
|
reject(eror)
|
|
}
|
|
})
|
|
})
|
|
// .catch(params => {
|
|
// reject(params);
|
|
// })
|
|
}
|
|
|
|
|
|
|
|
|
|
export default request.globalRequest
|
|
|
|
// 最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:
|
|
|
|
// 对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18。
|
|
// 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会进行 JSON 序列化。
|
|
// 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string。
|
|
|