dingdong-master/common/js/request.js

611 lines
14 KiB
JavaScript
Raw Normal View History

2022-05-14 14:57:20 +08:00
import globalData from '@/common/js/globalData.js';
2022-05-16 00:38:19 +08:00
export default {
address: 'https://www.opsoul.com',
2022-05-14 14:57:20 +08:00
// 异步接口拦截
2022-06-01 18:18:33 +08:00
addInterceptor() {
let _this = this;
2022-05-14 14:57:20 +08:00
uni.addInterceptor('request', {
2022-06-01 18:18:33 +08:00
invoke(args) {
2022-06-08 18:05:15 +08:00
uni.showLoading({
mask: true,
title: '加载中'
})
2022-06-01 18:18:33 +08:00
let userInfo = _this.getCurUserInfo();
2022-05-14 14:57:20 +08:00
// request 触发前拼接 url
2022-09-13 23:37:00 +08:00
// args.url = 'https://www.opsoul.com' + args.url;
args.url = 'http://127.0.0.1:80' + args.url;
2022-06-18 02:03:07 +08:00
// args.url = 'http://192.168.2.42:80' + args.url;
2022-06-01 18:18:33 +08:00
if (!args.data) {
args.data = {}
2022-05-23 17:38:52 +08:00
}
2022-06-18 02:03:07 +08:00
if (args.data instanceof Object) {
args.data.deptId = globalData.deptId;
args.data.from = globalData.from;
2022-08-16 23:26:51 +08:00
if (args.data.workerId === undefined && userInfo && userInfo.workerId) {
2022-06-18 02:03:07 +08:00
args.data.workerId = userInfo.workerId;
}
2022-06-01 18:18:33 +08:00
}
2022-06-18 02:03:07 +08:00
2022-06-01 18:18:33 +08:00
if (!args.header) {
args.header = {}
}
args.header.deptId = globalData.deptId;
args.header.from = globalData.from;
2022-05-14 14:57:20 +08:00
// console.log("停止触发");
// return false;
},
success(res) {
if (!res || !res.data || res.data.code !== 0) {
2022-05-14 14:57:20 +08:00
uni.showToast({
title: '系统开小差啦T_T请稍后重试',
icon: 'error'
})
}
},
fail(err) {
uni.showToast({
title: '系统开小差啦T_T请稍后重试',
icon: 'error'
})
},
complete(res) {
2022-06-08 18:05:15 +08:00
uni.hideLoading();
2022-05-14 14:57:20 +08:00
}
})
},
checkAndAuth() {
let _this = this;
// 通过 wx.getSetting 先查询一下用户是否授权了 "scope.userInfo" 这个 scope
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userInfo']) {
// 用户授权
wx.authorize({
scope: 'scope.userInfo',
success() {
// 用户已经同意, 后续调用此接口不会弹窗询问
_this.login();
},
fail() {
// 用户已经拒绝过授权
wx.openSetting({
success(res) {
if (res['scope.userInfo']) {
_this.checkAndAuth();
}
}
})
}
})
} else {
_this.login();
}
}
})
},
2022-05-23 17:38:52 +08:00
async login() {
let userInfo = await wx.getUserProfile({
2022-05-19 22:12:16 +08:00
desc: '用于小程序登录'
});
2022-08-16 23:26:51 +08:00
// console.log("从微信获取基本用户信息:" + userInfo);
2022-05-19 22:12:16 +08:00
// 获取微信登录凭证
const wxLoginRes = await wx.login();
2022-08-16 23:26:51 +08:00
// console.log(wxLoginRes)
2022-05-19 22:12:16 +08:00
// 获取openid
const wxAuthRes = await uni.request({
url: '/wx/auth',
header: {
code: wxLoginRes.code
}
})
2022-08-16 23:26:51 +08:00
// console.log(wxAuthRes)
2022-05-19 22:12:16 +08:00
const openId = wxAuthRes[1].data.data.openid;
// 第一次从服务端获取用户信息
let wxGetUserRes = await this.qryUserInfo(openId);
// 获取失败则获取微信信息再调用注册接口
if (!wxGetUserRes.data) {
// 注册完成后再次从服务端获取用户信息
let registerRes = await this.registerUser({
openId: openId,
name: userInfo.userInfo.nickName,
workerLogoUrl: userInfo.userInfo.avatarUrl
2022-05-19 22:12:16 +08:00
});
if (registerRes.code === 0) {
wxGetUserRes = await this.qryUserInfo(openId);
}
}
if (!wxGetUserRes.data) {
userInfo = null;
} else {
userInfo = {
...wxGetUserRes.data,
wxLoginCode: wxLoginRes.code
}
}
2022-08-16 23:26:51 +08:00
// console.log("通过后台服务获取用户信息:" + userInfo);
2022-05-23 17:38:52 +08:00
if (!userInfo || userInfo.workerId == null || userInfo.workerId == undefined) {
2022-05-19 22:12:16 +08:00
uni.showToast({
2022-05-23 17:38:52 +08:00
icon: 'none',
2022-05-19 22:12:16 +08:00
title: '微信用户信息获取失败,请退出小程序重试'
})
return false;
}
// 页面存储用户登录有效信息,以便其他页面调用
uni.setStorageSync('userProfile', userInfo);
2022-05-14 14:57:20 +08:00
return true;
2022-05-19 22:12:16 +08:00
},
2022-08-10 23:01:52 +08:00
async getWxUserInfo() {
// 获取微信登录凭证
const wxLoginRes = await wx.login();
// 获取openid
const wxAuthRes = await uni.request({
url: '/wx/auth',
header: {
code: wxLoginRes.code
}
})
const openId = wxAuthRes[1].data.data.openid;
// 从服务端获取用户信息
let wxGetUserRes = await this.qryUserInfo(openId);
let userInfo = null;
if (wxGetUserRes.data) {
userInfo = {
...wxGetUserRes.data,
wxLoginCode: wxLoginRes.code
}
}
return userInfo;
},
2022-05-24 18:03:36 +08:00
async storageExistUser() {
// 获取微信登录凭证
const wxLoginRes = await wx.login();
// 获取openid
const wxAuthRes = await uni.request({
url: '/wx/auth',
header: {
code: wxLoginRes.code
}
})
const openId = wxAuthRes[1].data.data.openid;
// 从服务端获取用户信息
let wxGetUserRes = await this.qryUserInfo(openId);
let userInfo = null;
if (wxGetUserRes.data) {
userInfo = {
...wxGetUserRes.data,
wxLoginCode: wxLoginRes.code
}
}
uni.clearStorageSync('userProfile');
2022-05-24 18:03:36 +08:00
uni.setStorageSync('userProfile', userInfo);
},
2022-05-19 22:12:16 +08:00
async registerUser(params = {}) {
let res = await uni.request({
2022-05-23 17:38:52 +08:00
url: '/wx/addWorker',
2022-05-19 22:12:16 +08:00
method: 'POST',
data: params
})
return res[1].data;
},
2022-08-09 20:11:13 +08:00
async sendVertifyCode(params = {}) {
let res = await uni.request({
url: '/tool/sms/send',
method: 'POST',
data: params
})
return res[1].data;
},
async realRegisterUser(params = {}) {
let res = await uni.request({
url: '/worker/register',
method: 'POST',
data: params
})
return res[1].data;
},
async appLogin(params = {}) {
let res = await uni.request({
url: '/worker/login',
method: 'POST',
data: params
})
return res[1].data;
},
2022-08-10 23:01:52 +08:00
async qryUserInfo(openId, phone) {
2022-05-19 22:12:16 +08:00
let res = await uni.request({
2022-05-23 17:38:52 +08:00
url: '/wx/getWorkerInfo',
2022-05-19 22:12:16 +08:00
method: 'POST',
data: {
2022-08-10 23:01:52 +08:00
openId: openId,
phone: phone
2022-05-19 22:12:16 +08:00
}
})
return res[1].data;
2022-05-24 18:03:36 +08:00
},
async qryUserPhone(code) {
let res = await uni.request({
url: '/wx/auth/phone',
method: 'GET',
header: {
code: code
}
})
return res[1].data;
},
async updateUserPhone(params = {}) {
let res = await uni.request({
url: '/worker/update',
method: 'POST',
data: params
})
// 更新用户信息的方法顺便更新本地缓存
if (res[1].data.code === 0) {
let userInfo = this.getCurUserInfo();
userInfo.phone = params.phone;
userInfo.account = params.account;
uni.setStorageSync('userProfile', userInfo);
}
return res[1].data;
},
async storagePhoneIntoUserInfo(code) {
let phoneRes = await this.qryUserPhone(code)
if (phoneRes && phoneRes.data) {
let userInfo = this.getCurUserInfo();
await this.updateUserPhone({
2022-05-24 18:03:36 +08:00
workerId: userInfo.workerId,
account: phoneRes.data,
phone: phoneRes.data
})
return true;
}
2022-05-15 01:46:26 +08:00
},
2022-05-16 00:38:19 +08:00
getCurUserInfo() {
let userProfile = uni.getStorageSync('userProfile');
return userProfile;
2022-05-22 21:52:14 +08:00
},
2022-08-16 23:26:51 +08:00
updateCache(cacheKey, cacheVal) {
uni.setStorageSync(cacheKey, cacheVal);
},
2022-05-22 21:52:14 +08:00
async getCurUserNoCache() {
let userInfo = this.getCurUserInfo();
let wxGetUserRes = await this.qryUserInfo(userInfo.openId);
if (!wxGetUserRes.data) {
userInfo = null;
} else {
userInfo = {
...wxGetUserRes.data,
wxLoginCode: userInfo.wxLoginCode
}
}
return userInfo;
},
async updateCurUserCache() {
let curUserInfo = await this.getCurUserNoCache();
uni.setStorageSync('userProfile', curUserInfo);
return true;
2022-05-16 00:38:19 +08:00
},
async uploadFile(filePath) {
let res = await uni.uploadFile({
url: this.address + '/tool/qiniu/upload',
filePath: filePath,
name: 'uploadFile'
})
let resStr = res[1].data;
2022-06-01 00:37:46 +08:00
let resObj = null;
2022-05-16 00:38:19 +08:00
if (resStr != null && resStr.length > 0) {
2022-06-01 00:37:46 +08:00
try {
resObj = JSON.parse(resStr);
} catch(e) {
console.log(e)
}
2022-05-16 00:38:19 +08:00
}
2022-06-01 00:37:46 +08:00
if (!resObj || resObj.code !== 0) {
uni.showToast({
icon: 'error',
title: '上传失败'
})
return '';
}
return resObj.url;
},
2022-06-16 23:14:43 +08:00
async qrySpecialSkillList(params = {goodsCategoryId : null}) {
let res = await uni.request({
url: '/special/skill/list',
method: 'POST',
data: params
})
return res[1].data;
},
async listByStep(params = {goodsCategoryId : null}) {
let res = await uni.request({
url: '/goods/deptcategory/listByStep',
method: 'POST',
data: params
})
return res[1].data;
},
async areaListByStep(params = { parentCode: null }) {
let res = await uni.request({
url: '/system/area/app/list',
method: 'POST',
data: params
})
return res[1].data;
2022-05-26 20:25:04 +08:00
},
async bindBankCard(params) {
let res = await uni.request({
2022-05-28 00:32:09 +08:00
url: '/worker/bank/bind',
method: 'POST',
data: params
})
return res[1].data;
},
async getBindBankCardByWorkerId(params) {
let res = await uni.request({
url: '/worker/bank/getByWorkerId',
2022-05-26 20:25:04 +08:00
method: 'POST',
data: params
})
return res[1].data;
2022-06-01 00:37:46 +08:00
},
async addGoods(params) {
let res = await uni.request({
url: '/goods/goods/addGoods',
method: 'POST',
data: params
})
return res[1].data;
2022-06-01 18:18:33 +08:00
},
async addTeam(params = {}) {
let res = await uni.request({
url: '/worker/team/addTeam',
method: 'POST',
data: params
})
return res[1].data;
},
async getTeamPage(params = {}) {
let res = await uni.request({
url: '/worker/team/getTeamList',
method: 'POST',
2022-06-02 17:39:45 +08:00
data: params,
header: {
pageNum: params.pageNum,
pageSize: params.pageSize
}
2022-06-01 18:18:33 +08:00
})
return res[1].data;
},
async updateWorker(params = {}) {
let res = await uni.request({
url: '/worker/update',
method: 'POST',
data: params
})
2022-06-01 23:58:24 +08:00
// 更新用户信息的方法顺便更新本地缓存
if (res[1].data.code === 0) {
let userInfo = this.getCurUserInfo();
let qryUserInfoRes = await this.qryUserInfo(userInfo.openId);
let newUserInfo = {
...qryUserInfoRes.data,
wxLoginCode: userInfo.wxLoginCode
};
uni.setStorageSync('userProfile', newUserInfo);
}
return res[1].data;
},
async updateWorkerTeam(params = {}) {
let res = await uni.request({
url: '/worker/team/updateTeam',
method: 'POST',
data: params
})
return res[1].data;
},
async qryFinancialCount(params = {}) {
let res = await uni.request({
url: '/financial/detail/count',
method: 'POST',
data: params
})
return res[1].data;
},
async qryFinancialDetail(params = {}) {
let res = await uni.request({
url: '/financial/detail/app/list',
2022-06-01 23:58:24 +08:00
method: 'POST',
data: params
})
2022-06-01 18:18:33 +08:00
return res[1].data;
2022-06-02 17:39:45 +08:00
},
async qryMasterOrderPage(params = {}) {
let res = await uni.request({
url: '/order/master/app/list',
method: 'POST',
data: params,
header: {
pageNum: params.pageNum,
pageSize: params.pageSize
}
})
return res[1].data;
2022-06-13 16:13:10 +08:00
},
async getOrderMasterDetail(params = {}) {
let res = await uni.request({
url: '/order/master/app/detail',
method: 'POST',
data: params
})
return res[1].data;
},
2022-09-12 00:21:19 +08:00
async qryMixOrderList(params = {}) {
let res = await uni.request({
url: '/order/app/mix/order/list',
method: 'POST',
data: params
})
return res[1].data;
},
2022-06-13 16:13:10 +08:00
async updateOrder(params = {}) {
let res = await uni.request({
url: '/order/master/editOrderMaster',
method: 'POST',
data: params
})
return res[1].data;
},
async getCanAssignList(params = {}) {
let res = await uni.request({
url: '/order/can/assign',
method: 'POST',
data: params
})
return res[1].data;
},
async assign(params = {}) {
let res = await uni.request({
url: '/order/assign',
method: 'POST',
data: params
})
return res[1].data;
},
async qryDetailOrderPage(params = {}) {
let res = await uni.request({
2022-06-16 23:14:43 +08:00
url: '/order/detail/app/list',
method: 'POST',
data: params,
header: {
pageNum: params.pageNum,
pageSize: params.pageSize
}
})
return res[1].data;
2022-06-16 23:14:43 +08:00
},
async updateDetailOrder(params = {}) {
let res = await uni.request({
url: '/order/detail/app/edit',
method: 'POST',
data: params
})
return res[1].data;
},
async applyFinishOrder(params = {}) {
let res = await uni.request({
url: '/order/detail/app/applyFinishOrder',
method: 'POST',
data: params
})
return res[1].data;
},
2022-06-16 23:14:43 +08:00
async getOrderDetail(params = {}) {
let res = await uni.request({
url: '/order/detail/app/detail',
method: 'POST',
data: params
})
return res[1].data;
},
async workerSettled(params = {}) {
let res = await uni.request({
url: '/worker/settled',
method: 'POST',
data: params
})
return res[1].data;
},
async qryProductPage(params = {}) {
let res = await uni.request({
url: '/goods/goods/app/list',
method: 'POST',
data: params,
header: {
pageNum: params.pageNum,
pageSize: params.pageSize
}
})
return res[1].data;
},
async updateGoodsStatus(params = {}) {
let res = await uni.request({
url: '/goods/goods/app/updateStatus',
method: 'POST',
data: params
})
return res[1].data;
},
async getWorkerArea(params = {}) {
let res = await uni.request({
url: '/worker/area/worker',
method: 'GET',
data: params
})
return res[1].data;
},
async getWorkerGoodsCategory(params = {}) {
let res = await uni.request({
url: '/worker/goods/category/worker',
method: 'GET',
data: params
})
return res[1].data;
},
async getWorkerSpecialSkill(params = {}) {
let res = await uni.request({
url: '/worker/special/skill/' + params.workerId,
method: 'POST'
})
return res[1].data;
2022-06-18 02:03:07 +08:00
},
async qrPay(params = {}) {
let res = await uni.request({
url: '/pay/ali/qr',
method: 'POST',
data: params.orderMasterId
})
return res[1].data;
2022-06-22 22:38:07 +08:00
},
async updateMasterOrder(params = {}) {
let res = await uni.request({
url: '/order/master/editOrderMaster',
method: 'POST',
data: params
})
return res[1].data;
2022-07-07 15:38:30 +08:00
},
async addWorkerCertify(params = {}) {
let res = await uni.request({
url: '/worker/certification/app/add',
method: 'POST',
data: params
})
return res[1].data;
},
async getWorkerCertify(params = {}) {
let res = await uni.request({
url: '/worker/certification/app/getByWorkerId',
method: 'POST',
data: params
})
return res[1].data;
2022-07-13 21:38:54 +08:00
},
async orderStatistics(params = {}) {
let res = await uni.request({
url: '/order/app/statistics',
method: 'POST',
data: params
})
return res[1].data;
},
async rejectDetailOrder(params = {}) {
let res = await uni.request({
url: '/order/detail/app/order/reject',
method: 'POST',
data: params
})
return res[1].data;
}
2022-05-14 14:57:20 +08:00
}