You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
|
3 months ago
|
let ajaxUrl = "https://api.coze.cn/";
|
||
|
|
|
||
|
|
let requestTask;
|
||
|
|
let data = {
|
||
|
|
ajaxUrl: ajaxUrl,
|
||
|
|
startAbort ($bool = 0) {
|
||
|
|
if ($bool == 1) {
|
||
|
|
requestTask.abort();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
coziGet ($url = '', $parmas = {}, callback = () => { }, failback = () => { }) {
|
||
|
|
let that = this,
|
||
|
|
params = {};
|
||
|
|
let promise = new Promise((resolve, reject) => {
|
||
|
|
params = $parmas;
|
||
|
|
resolve();
|
||
|
|
});
|
||
|
|
promise.then(() => {
|
||
|
|
that.postData($url, params, callback, failback, 'GET');
|
||
|
|
}).catch();
|
||
|
|
},
|
||
|
|
coziPost ($url = '', $parmas = {}, callback = () => { }, failback = () => { }) {
|
||
|
|
let that = this,
|
||
|
|
params = {};
|
||
|
|
let promise = new Promise((resolve, reject) => {
|
||
|
|
params = $parmas;
|
||
|
|
resolve();
|
||
|
|
});
|
||
|
|
promise.then(() => {
|
||
|
|
that.postData($url, params, callback, failback, 'POST');
|
||
|
|
}).catch();
|
||
|
|
},
|
||
|
|
|
||
|
|
// 发送请求
|
||
|
|
postData ($url = '', $parmas = {}, callback = () => { }, failback = () => { }, $method) {
|
||
|
|
let that = this,
|
||
|
|
$header = {};
|
||
|
|
if ($parmas == '') {
|
||
|
|
$parmas = {};
|
||
|
|
}
|
||
|
|
if(uni.getStorageSync("cozi_token")){
|
||
|
|
$header['Authorization'] = 'Bearer ' + uni.getStorageSync("cozi_token");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查网络信息, 避免网络引起的错误码
|
||
|
|
uni.getNetworkType({
|
||
|
|
success: function (res) {
|
||
|
|
console.log(res);
|
||
|
|
if (res.networkType != 'none') {
|
||
|
|
requestTask = uni.request({
|
||
|
|
url: ajaxUrl + $url,
|
||
|
|
data: $parmas,
|
||
|
|
header: $header,
|
||
|
|
method: $method,
|
||
|
|
success: (res) => {
|
||
|
|
console.log('res before', res);
|
||
|
|
let resData = res.data;
|
||
|
|
console.log('res after', resData);
|
||
|
|
if($parmas.stream){
|
||
|
|
callback(resData);
|
||
|
|
}else{
|
||
|
|
if (resData.code == 0) {// 正常
|
||
|
|
callback(resData.data, resData.msg);
|
||
|
|
} else {// 其他异常
|
||
|
|
failback(resData.msg);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
fail (error) {
|
||
|
|
console.log('请求失败', error);
|
||
|
|
if (error.errMsg == 'request:fail abort' || error.errMsg == 'request:fail timeout') {
|
||
|
|
|
||
|
|
} else {
|
||
|
|
uni.showToast({
|
||
|
|
title: error.errMsg,
|
||
|
|
icon: "none"
|
||
|
|
})
|
||
|
|
}
|
||
|
|
failback(error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
uni.showToast({
|
||
|
|
title: '网络异常,请检查网络',
|
||
|
|
icon: "none"
|
||
|
|
})
|
||
|
|
}
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
};
|
||
|
|
|
||
|
|
export default data;
|