366 lines
8.6 KiB
JavaScript
366 lines
8.6 KiB
JavaScript
import globalData from '@/common/js/globalData.js';
|
||
|
||
export default {
|
||
address: 'https://www.opsoul.com',
|
||
// 异步接口拦截
|
||
addInterceptor() {
|
||
let _this = this;
|
||
uni.addInterceptor('request', {
|
||
invoke(args) {
|
||
let userInfo = _this.getCurUserInfo();
|
||
// request 触发前拼接 url
|
||
// args.url = 'https://www.opsoul.com' + args.url;
|
||
args.url = 'http://127.0.0.1:80' + args.url;
|
||
// args.url = 'http://10.45.110.174:80' + args.url;
|
||
if (!args.data) {
|
||
args.data = {}
|
||
}
|
||
args.data.deptId = globalData.deptId;
|
||
args.data.from = globalData.from;
|
||
if (!args.data.workerId) {
|
||
args.data.workerId = userInfo.workerId;
|
||
}
|
||
if (!args.header) {
|
||
args.header = {}
|
||
}
|
||
args.header.deptId = globalData.deptId;
|
||
args.header.from = globalData.from;
|
||
// console.log("停止触发");
|
||
// return false;
|
||
},
|
||
success(res) {
|
||
if (res.data.code !== 0) {
|
||
uni.showToast({
|
||
title: '系统开小差啦T_T,请稍后重试',
|
||
icon: 'error'
|
||
})
|
||
}
|
||
},
|
||
fail(err) {
|
||
uni.showToast({
|
||
title: '系统开小差啦T_T,请稍后重试',
|
||
icon: 'error'
|
||
})
|
||
},
|
||
complete(res) {
|
||
console.log('interceptor-complete', res)
|
||
}
|
||
})
|
||
},
|
||
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();
|
||
}
|
||
}
|
||
})
|
||
},
|
||
async login() {
|
||
let userInfo = await wx.getUserProfile({
|
||
desc: '用于小程序登录'
|
||
});
|
||
console.log("从微信获取基本用户信息:" + userInfo);
|
||
// 获取微信登录凭证
|
||
const wxLoginRes = await wx.login();
|
||
console.log(wxLoginRes)
|
||
// 获取openid
|
||
const wxAuthRes = await uni.request({
|
||
url: '/wx/auth',
|
||
header: {
|
||
code: wxLoginRes.code
|
||
}
|
||
})
|
||
console.log(wxAuthRes)
|
||
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,
|
||
status: 0
|
||
});
|
||
if (registerRes.code === 0) {
|
||
wxGetUserRes = await this.qryUserInfo(openId);
|
||
}
|
||
}
|
||
if (!wxGetUserRes.data) {
|
||
userInfo = null;
|
||
} else {
|
||
userInfo = {
|
||
...wxGetUserRes.data,
|
||
wxLoginCode: wxLoginRes.code
|
||
}
|
||
}
|
||
console.log("通过后台服务获取用户信息:" + userInfo);
|
||
if (!userInfo || userInfo.workerId == null || userInfo.workerId == undefined) {
|
||
uni.showToast({
|
||
icon: 'none',
|
||
title: '微信用户信息获取失败,请退出小程序重试'
|
||
})
|
||
return false;
|
||
}
|
||
// 页面存储用户登录有效信息,以便其他页面调用
|
||
uni.setStorageSync('userProfile', userInfo);
|
||
return true;
|
||
},
|
||
async storageExistUser() {
|
||
uni.clearStorageSync('userProfile');
|
||
// 获取微信登录凭证
|
||
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.setStorageSync('userProfile', userInfo);
|
||
},
|
||
async registerUser(params = {}) {
|
||
let res = await uni.request({
|
||
url: '/wx/addWorker',
|
||
method: 'POST',
|
||
data: params
|
||
})
|
||
return res[1].data;
|
||
},
|
||
async qryUserInfo(openId) {
|
||
let res = await uni.request({
|
||
url: '/wx/getWorkerInfo',
|
||
method: 'POST',
|
||
data: {
|
||
openId: openId
|
||
}
|
||
})
|
||
return res[1].data;
|
||
},
|
||
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();
|
||
this.updateUserPhone({
|
||
workerId: userInfo.workerId,
|
||
account: phoneRes.data,
|
||
phone: phoneRes.data
|
||
})
|
||
return true;
|
||
}
|
||
},
|
||
getCurUserInfo() {
|
||
let userProfile = uni.getStorageSync('userProfile');
|
||
return userProfile;
|
||
},
|
||
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 uploadFile(filePath) {
|
||
let res = await uni.uploadFile({
|
||
url: this.address + '/tool/qiniu/upload',
|
||
filePath: filePath,
|
||
name: 'uploadFile'
|
||
})
|
||
let resStr = res[1].data;
|
||
let resObj = null;
|
||
if (resStr != null && resStr.length > 0) {
|
||
try {
|
||
resObj = JSON.parse(resStr);
|
||
} catch(e) {
|
||
console.log(e)
|
||
}
|
||
}
|
||
if (!resObj || resObj.code !== 0) {
|
||
uni.showToast({
|
||
icon: 'error',
|
||
title: '上传失败'
|
||
})
|
||
return '';
|
||
}
|
||
return resObj.url;
|
||
},
|
||
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/list',
|
||
method: 'POST',
|
||
data: params
|
||
})
|
||
return res[1].data;
|
||
},
|
||
async bindBankCard(params) {
|
||
let res = await uni.request({
|
||
url: '/worker/bank/bind',
|
||
method: 'POST',
|
||
data: params
|
||
})
|
||
return res[1].data;
|
||
},
|
||
async getBindBankCardByWorkerId(params) {
|
||
let res = await uni.request({
|
||
url: '/worker/bank/getByWorkerId',
|
||
method: 'POST',
|
||
data: params
|
||
})
|
||
return res[1].data;
|
||
},
|
||
async addGoods(params) {
|
||
let res = await uni.request({
|
||
url: '/goods/goods/addGoods',
|
||
method: 'POST',
|
||
data: params
|
||
})
|
||
return res[1].data;
|
||
},
|
||
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',
|
||
data: params,
|
||
header: {
|
||
pageNum: params.pageNum,
|
||
pageSize: params.pageSize
|
||
}
|
||
})
|
||
return res[1].data;
|
||
},
|
||
async updateWorker(params = {}) {
|
||
let res = await uni.request({
|
||
url: '/worker/update',
|
||
method: 'POST',
|
||
data: params
|
||
})
|
||
// 更新用户信息的方法顺便更新本地缓存
|
||
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/list',
|
||
method: 'POST',
|
||
data: params
|
||
})
|
||
return res[1].data;
|
||
},
|
||
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;
|
||
}
|
||
}
|