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.
104 lines
3.1 KiB
104 lines
3.1 KiB
1 year ago
|
import urlConfig from './config.js'
|
||
|
// import store from '@/store/index'
|
||
|
|
||
|
function service(options = {}) {
|
||
|
let contentType = localStorage.token_type ? "application/json" : "application/x-www-form-urlencoded";
|
||
|
options.header = {
|
||
|
"content-type": contentType,
|
||
|
"Authorization": localStorage.token_type,
|
||
|
'withCredentials': true,
|
||
|
// 'Blade-Auth':'bearer '+store.state.token,
|
||
|
'token_type': localStorage.token_type
|
||
|
};
|
||
|
return new Promise((resolve, reject) => {
|
||
|
options.success = (res) => {
|
||
|
console.log("请求成功", res);
|
||
|
// 如果请求回来的状态码不是200则执行以下操作
|
||
|
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') {
|
||
|
return null;
|
||
|
} else {
|
||
|
reject(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为空")
|
||
|
}
|
||
|
};
|
||
|
options.fail = (err) => {
|
||
|
reject(err);
|
||
|
};
|
||
|
uni.request(options);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export default service;
|