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.
172 lines
5.5 KiB
172 lines
5.5 KiB
1 year ago
|
import mrcGlobal from './global';
|
||
|
import { agentId, miniAppId } from './global';
|
||
|
import * as userTools from './user';
|
||
|
import * as myapi from './myapi';
|
||
|
const whiteUrls = mrcGlobal.AUTH_URL;
|
||
|
export function request(url, method = 'GET', headers = {}, data = {}) {
|
||
|
let tempHeaders = {
|
||
|
token: userTools.token(),
|
||
|
Authorization: userTools.token(),
|
||
|
'Content-Type': 'application/json;charset=UTF-8',
|
||
|
version: mrcGlobal.APP_VERSION
|
||
|
};
|
||
|
// 遍历白名单
|
||
|
if (!whiteUrls.includes(url)) {
|
||
|
Object.assign(tempHeaders, {
|
||
|
Authorization: userTools.token()
|
||
|
});
|
||
|
}
|
||
|
// uni.showLoading();
|
||
|
let requestUrl = '';
|
||
|
if (url.indexOf('https://restapi.amap.com') != -1) {
|
||
|
requestUrl = url;
|
||
|
} else {
|
||
|
if (process.env.NODE_ENV !== 'development') {
|
||
|
requestUrl = mrcGlobal.SERVER_URL + url;
|
||
|
} else {
|
||
|
requestUrl = url;
|
||
|
}
|
||
|
}
|
||
|
const promise = new Promise((resolve, reject) => {
|
||
|
uni.request({
|
||
|
url: requestUrl,
|
||
|
// 拼接请求地址
|
||
|
method: method,
|
||
|
// 请求方式
|
||
|
header: tempHeaders,
|
||
|
// 请求头
|
||
|
data: method == 'GET' ? data : JSON.stringify(data),
|
||
|
dataType: 'json',
|
||
|
success: (res) => {
|
||
|
uni.hideLoading();
|
||
|
if (res.data.code == 200 || res.data.code == 0 || res.data.infocode == 10000) {
|
||
|
let replaceStr = JSON.stringify(res.data);
|
||
|
let reg = new RegExp('http://172.20.46.177', 'g');
|
||
|
replaceStr = replaceStr.replace(reg, 'https://lantu.ytyanhuo.com/');
|
||
|
resolve(JSON.parse(replaceStr));
|
||
|
} else if (res.data.code == 10005 || res.data.code == 10006 || res.data.code == 10007) {
|
||
|
uni.showToast({
|
||
|
icon: 'error',
|
||
|
title: res.data.msg
|
||
|
});
|
||
|
} else {
|
||
|
uni.showToast({
|
||
|
icon: 'error',
|
||
|
title: `${res.data.msg}\n${res.data.internalMsg}`,
|
||
|
duration: 2000
|
||
|
});
|
||
|
}
|
||
|
},
|
||
|
fail: (err) => {
|
||
|
console.log(err);
|
||
|
/*
|
||
|
error 描述
|
||
|
11 无权跨域
|
||
|
12 网络出错
|
||
|
13 超时
|
||
|
14 解码失败
|
||
|
19 HTTP错误
|
||
|
*/
|
||
|
uni.showToast({
|
||
|
type: 'none',
|
||
|
title: '网络错误',
|
||
|
duration: 1000
|
||
|
});
|
||
|
console.log(`请求失败,url: ${url}, 参数:${JSON.stringify(data)} 错误码:${err.error}`);
|
||
|
// reject(`请求失败,url: ${url}, 参数:${JSON.stringify(data)} 错误码:${err.error}`)
|
||
|
// uni.hideLoading();
|
||
|
},
|
||
|
|
||
|
complete: () => {
|
||
|
uni.stopPullDownRefresh();
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
return promise;
|
||
|
}
|
||
|
export function requestGET(url, data = {}) {
|
||
|
const promise = new Promise((resolve, _) => {
|
||
|
request(url, 'GET', {}, data).then((res) => {
|
||
|
resolve(res);
|
||
|
});
|
||
|
});
|
||
|
return promise;
|
||
|
}
|
||
|
export function requestPOST(url, data = {}) {
|
||
|
const promise = new Promise((resolve, _) => {
|
||
|
request(url, 'POST', {}, data).then((res) => {
|
||
|
resolve(res);
|
||
|
});
|
||
|
});
|
||
|
return promise;
|
||
|
}
|
||
|
export function upload(url, filePath, fileName = '', fileType = '', formData = {}, header = {}) {
|
||
|
let tempHeaders = {
|
||
|
'Content-Type': 'multipart/form-data',
|
||
|
token: userTools.token(),
|
||
|
version: mrcGlobal.APP_VERSION
|
||
|
};
|
||
|
// 遍历白名单
|
||
|
if (!whiteUrls.includes(url)) {
|
||
|
Object.assign(tempHeaders, {
|
||
|
token: userTools.token()
|
||
|
});
|
||
|
}
|
||
|
return new Promise((resolve, reject) => {
|
||
|
uni.uploadFile({
|
||
|
url: url,
|
||
|
// 开发者服务器地址
|
||
|
header: Object.assign(tempHeaders, header),
|
||
|
formData: formData,
|
||
|
filePath: filePath,
|
||
|
// 要上传文件资源的本地定位符
|
||
|
fileName: fileName,
|
||
|
// 文件名,即对应的 key, 开发者在服务器端通过这个 key 可以获取到文件二进制内容
|
||
|
fileType: fileType,
|
||
|
// 文件类型,image / video / audio
|
||
|
success: (res) => {
|
||
|
resolve(res);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
export function uploadFile(url, filePath, params) {
|
||
|
return new Promise((resolve, _) => {
|
||
|
upload(url, filePath, params, (fileName = 'filecontent'), (fileType = 'file'), params, {}).then((res) => {
|
||
|
resolve(res);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 免登
|
||
|
export function fetchToken() {
|
||
|
return new Promise((resolve, _) => {
|
||
|
myapi.getAuthCode().then((res) => {
|
||
|
requestPOST(whiteUrls, {
|
||
|
authCode: res.authCode,
|
||
|
miniAppId
|
||
|
}).then((res) => {
|
||
|
if (!res.error) {
|
||
|
userTools.setStoreUserInfo(res.data);
|
||
|
}
|
||
|
resolve(res);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 授权登录
|
||
|
// export function fetchToken() {
|
||
|
// return new Promise((resolve, _) => {
|
||
|
// requestPOST(whiteUrls, {
|
||
|
// authCode: myapi.getStorageSync("authCode"),
|
||
|
// miniAppId
|
||
|
// }).then(res => {
|
||
|
// if (!res.error) {
|
||
|
// userTools.setStoreUserInfo(res.result);
|
||
|
// }
|
||
|
// resolve(res);
|
||
|
// });
|
||
|
// });
|
||
|
// }
|