Files
quinn-accounts/utils/api.js
2023-02-11 00:39:10 +08:00

261 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import utils from './util'
//testURL
const BASE_URL = 'http://127.0.0.1:8080/wx';
//devURL
// const BASE_URL = 'https://lmqhznn.goho.co/wx';
//prodURL
// const BASE_URL = 'https://www.qnforever.top/wx';
function buildURL(url, needToken) {
let userId = wx.getStorageSync('userId');
if (!userId && url != '/user/login') {
wx.redirectTo({
url: '../myself/index'
})
wx.showToast({
title: '请先登录',
mask: true,
duration: 2000
})
return;
}
if (url != '/user/login'){
url = url + (url.indexOf('?') >= 0 ? '&' : '?') + "userId=" + userId
}
if (needToken) {
let token = utils.getToken()
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不存在返回错误
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 {
wx.hideLoading();
wx.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 2000
})
reject(res);
}
} else {
wx.hideLoading();
wx.showToast({
title: '服务器异常',
icon: 'none',
duration: 4000
})
reject(response);
}
},
fail: function(err) {
console.log(err);
wx.hideLoading();
wx.showToast({
title: '网络错误',
icon: 'none',
duration: 4000
})
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 {
wx.hideLoading();
wx.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 4000
})
reject(res);
}
},
fail: function(res) {
wx.hideLoading();
wx.showToast({
title: '网络错误',
icon: 'none',
duration: 4000
})
reject(res);
},
})
})
}
// get请求
export function fetchGet(url, params, needToken) {
url = buildURL(url, needToken);
// 如果url不存在返回错误
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 {
reject(res);
}
} else {
wx.hideLoading();
reject(res)
wx.showToast({
title: '服务器异常',
icon: 'none',
duration: 4000
})
}
},
fail: function(res) {
wx.hideLoading();
reject(res)
wx.showToast({
title: '网络错误',
icon: 'none',
duration: 4000
})
},
})
})
}
// 暴露接口
export default {
/**
* 用户设置
* @param params
* @returns {Promise<unknown>}
*/
loginAccount(params) {
return fetchPost('/user/login',params,true,false);
},
getSetting(params) {
return fetchPost('/user/setting/' + params,null,true,false);
},
editSetting(params) {
return fetchPost('/user/settings/edit',params,true,false);
},
deleteSetting(params) {
return fetchPost('/user/settings/del/' + params,null,true,false);
},
getSettings() {
return fetchPost('/user/settings', null, true, false);
},
getSettingsExpend(){
return fetchPost('/user/settings/expend',null,true,false);
},
getSettingsIncome(){
return fetchPost('/user/settings/income',null,true,false);
},
editAccount(params){
return fetchPost('/user/settings/edit/bill',params,true,false);
},
/**
* 账单处理
*/
listBillsToday() {
return fetchPost('/user/bills/today', null,true,false);
},
editBill(params) {
return fetchPost('/user/bill/edit',params,true,false);
},
getBill(params) {
return fetchPost('/user/bill/' + params,null,true,false);
},
deleteBill(params) {
return fetchPost('/user/bill/del/' + params,null,true,false);
},
/**
* 预算处理
*/
getBudgetIndex(){
return fetchPost('/user/budget/index',null,true,false);
},
getBudget(){
return fetchPost('/user/budget',null,true,false);
},
editBudget(params){
return fetchPost('/user/budget/edit',params,true,false);
},
/**
* 账单统计
*/
listBillByAMonth(params){
return fetchPost('/user/bills/month' ,params,true,false);
},
listExpendOrderBills(params){
return fetchPost('/user/bills/expend/order' ,params,true,false);
},
listIncomeOrderBills(params){
return fetchPost('/user/bills/income/order' ,params,true,false);
},
listBillByAccount(params){
return fetchPost('/user/bills/account' ,params,true,false);
},
listBillGroup(params){
return fetchPost('/user/bills/group/list' ,params,true,false);
},
listGroupDetail(params){
return fetchPost('/user/bills/group/detail' ,params,true,false);
},
listBillChart(params) {
return fetchPost('/user/bills/line/chart' ,params,true,false);
}
}