project init

This commit is contained in:
2023-01-04 21:06:34 +08:00
parent 5b0e3d2583
commit abc4f65a8e
10 changed files with 391 additions and 2 deletions

237
utils/api.js Normal file
View File

@@ -0,0 +1,237 @@
//testURL
const BASE_URL = 'http://192.168.73.1:10393/mock/68a4cea5-9a96-4c33-b7b9-0f32269ef34f/test?apipost_id=872f12';
//prodURL
// const BASE_URL = 'https://api.hongyutiyu.top';
function buildURL(url, needToken) {
let token = wx.getStorageSync('accessToken');
if (!needToken) {
return token ? url + (url.indexOf('?') >= 0 ? '&' : '?') + "access_token=" + token : url
}
return url + (url.indexOf('?') >= 0 ? '&' : '?');
}
export function fetchPost(url, params, needToken, multiple) {
url = buildURL(url, needToken);
// 如果url不存在返回错误 TODO
// if (!url) {
// return new Promise((resolve, reject) => {
// reject();
// })
// }
console.log("网络请求", BASE_URL + url, params);
// 如果上传图片
if (multiple) {
return new Promise((resolve, reject) => {
wx.uploadFile({
url: BASE_URL + url,
filePath: params.filePath,
name: 'image',
formData: {},
success: function(response) {
console.log(response.data);
let res = JSON.parse(response.data);
console.log(res);
if (response.statusCode == 200) {
if (res.err_code == 0) {
// wx.hideLoading();
resolve(res);
} else {
if (res.err_code == 10003 || res.err_code == 10006 || res.err_code == 20006) {
// wx.removeStorageSync('accessToken');
// wx.redirectTo({
// url: '/pages/login/index',
// })
isUnLogin();
reject(res);
return;
} else if (res.err_code == 20005) {
wx.removeStorageSync('accessToken');
wx.removeStorageSync('history');
wx.hideLoading();
wx.showToast({
title: res.err_msg,
icon: 'none',
duration: 2000
})
reject(res);
} else {
wx.hideLoading();
wx.showToast({
title: res.err_msg,
icon: 'none',
duration: 2000
})
reject(res);
}
}
} else {
wx.hideLoading();
wx.showToast({
title: '网络错误',
icon: 'none',
})
reject(response);
}
},
fail: function(err) {
console.log(err);
wx.hideLoading();
wx.showToast({
title: '网络错误',
icon: 'none',
})
reject(response);
},
})
})
}
// 获取POST数据
return new Promise((resolve, reject) => {
wx.request({
url: BASE_URL + url,
data: params,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'POST',
success: function(res) {
console.log("POST返回数据", url,res);
if (res.data.err_code == 0) {
wx.hideLoading();
resolve(res.data);
} else {
if (res.data.err_code == 10003 || res.data.err_code == 10006 || res.data.err_code == 20006) {
// wx.removeStorageSync('accessToken');
// wx.redirectTo({
// url: '/pages/login/index',
// })
reject(res);
return;
} else if (res.data.err_code == 30022) {
reject(res);
} else if (res.data.err_code == 20005) {
wx.removeStorageSync('accessToken');
wx.removeStorageSync('history');
wx.hideLoading();
wx.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 2000
})
reject(res);
} else {
wx.hideLoading();
wx.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 2000
})
reject(res);
}
}
},
fail: function(res) {
wx.hideLoading();
wx.showToast({
title: '网络错误',
icon: 'none',
duration: 2000
})
reject(res);
},
})
})
}
// get请求
export function fetchGet(url, params, needToken) {
url = buildURL(url, needToken);
// 如果url不存在返回错误 TODO
// if (!url) {
// return new Promise((resolve, reject) => {
// reject();
// })
// }
console.log("网络请求", BASE_URL + url, params);
return new Promise((resolve, reject) => {
wx.request({
url: BASE_URL + url,
data: params,
method: 'GET',
success: function(res) {
console.log("GET返回数据", url, res);
if (res.statusCode == 200) {
if (res.data.err_code == 0) {
wx.hideLoading();
resolve(res.data);
} else {
if (res.data.err_code == 10003 || res.data.err_code == 10006 || res.data.err_code == 20006) {
// isUnLogin();
reject(res);
return;
} else if (res.data.err_code == 20005) {
wx.removeStorageSync('accessToken');
wx.removeStorageSync('history');
wx.hideLoading();
wx.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 2000
})
reject(res);
} else {
wx.hideLoading();
wx.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 2000
})
reject(res);
}
}
} else {
wx.hideLoading();
reject(res)
wx.showToast({
title: '网络错误',
icon: 'none',
duration: 2000
})
}
},
fail: function(res) {
wx.hideLoading();
reject(res)
wx.showToast({
title: '网络错误',
icon: 'none',
duration: 2000
})
},
})
})
}
// 暴露接口
export default {
/**
* 任务清单
*/
taskList(params) {
return new Promise(x => {
let job = {data:{over:true}};
x(job)
console.log(x + '请求成功');
})
// return fetchGet('', params, false);
},
}