纸通宝SAAS仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

251 lines
8.5 KiB

/**
* Copyright © 2020-present LiuDanYang. All rights Reserved.
*/
import { mGet, mPost } from "./request"
import { sconfig } from "./saas"
import { pconfig } from "./payment"
import { zconfig } from "./ztb"
import { mconfig } from "./moment"
const util = require('../utils/util')
const storage = require('../utils/storage')
const app = getApp()
const urls = [`https://api-client-uec-dev.qniao.cn`, `https://api-client-uec-test.qniao.cn`, `https://api-client-uec.qniao.cn`]
const config = {
baseUrl: urls[app.evn]
}
const wechatApplet = (params) => mPost('/uec/get/wechat-applet-session', params, config)
const loginCaptcha = (params) => mPost(`/uec/authorize/by-captcha`, params, config)
const loginPwd = (params) => mPost(`/uec/authorize/by-password`, params, config)
const postCaptcha = (params) => mPost(`/uec/get/auth-captcha`, params, config)
const setPassword = (params) => mPost(`/uec/user/set/sign-in-password`, params, config)
const modifyMobile = (params) => mPost(`/uec/user/modify/mobile`, params, config)
const updateUserInfo = (params) => mPost('/uec/user/update/user-info', params, config)
const getAuthSession = () => mGet('/uec/create/identity-auth-session', null, config)
const certificateImage = (params) => mPost('/uec/recognize/certificate-img', params, config)
const certificateIdentity = (params) => mPost('/uec/identify/identity', params, config)
const generateCode = (params) => mPost('/uec/generate/qrcode', params, config)
const createQrcode = (params) => mPost('/uec/create/qrcode-record', params, config)
const verifyCode = (code, params) => mGet(`/uec/verify/qrcode${code}`, params, config)
const applyEmplyee = (params) => mPost(`/uec/apply/to/be/employee/of/enterprise-department`, params, config)
const getCheckList = (params) => mGet(`/uec/get/employee-joining-department-application-list`, params, config)
const auditEmploye = (params) => mPost(`/uec/audit/employee-joining-department-application`, params, config)
const deleteEmploye = (params) => mPost(`/uec/remove/employees`, params, config)
const editEmploye = (params) => mPost(`/uec/save/self-department-member`, params, config)
// ****************************************************************************************************************************************
// /authorize/by-wechat-applet小程序登录
function loginWechat(form, loading) {
let promise = new Promise(function (resolve, reject) {
if (!form.encryptedData) {
util.showToast('授权被拒绝,获取微信用户失败')
reject('授权被拒绝,获取微信用户失败')
return
}
if(loading) {
wx.showLoading({ title: '登录中', mask: true })
}
var tempToken = storage.get('tmpAuthToken')
if(!util.isEmpty(tempToken)){
form.tmpAuthToken = tempToken
unionWechat(form, resolve, reject, loading)
} else {
wx.login({
success: res => {
// 获取openId
wx.request({
header: { 'X-APP-ID': app.xAppId },
url: config.baseUrl + '/uec/get/wechat-applet-session',
data: { authCode: res.code },
method: 'POST',
success: function (result) {
if (result.data.code == 0) {
form.tmpAuthToken = result.data.data.tmpAuthToken
storage.put('tmpAuthToken', form.tmpAuthToken, 98)
// 微信登录,获取unionId
unionWechat(form, resolve, reject, loading)
} else {
if(loading) {
wx.hideLoading()
}
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
}
},
fail: function (err) {
if(loading) {
wx.hideLoading()
}
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
}
})
},
fail: function (err) {
if(loading) {
wx.hideLoading()
}
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
}
})
}
})
return promise
}
// POST ​/uec​/authorize​/by-wechat-applet 通过小程序的方式进行认证
function unionWechat(form, resolve, reject, loading) {
wx.request({
header: { 'X-APP-ID': app.xAppId },
url: config.baseUrl + '/uec/authorize/by-wechat-applet',
data: form,
method: 'POST',
success: function (result) {
if (result.data.code == 0) {
storage.remove('tempToken')
resolve(result.data)
} else {
reject(result.data)
}
},
fail: function () {
if(loading) {
wx.hideLoading()
}
wx.showToast({ title: '获取用户信息,请稍后再试', icon: "none" })
reject('请求错误!')
}
})
}
// ****************************************************************************************************************************************
// 通过微信的方式登录,onGotPhoneNumber中统一处理/uec/decrypt/wechat-applet-mobile
function gotPhoneNumber(form, resolve, reject) {
// 获取手机号码 /authorize/encrypt/mobile
wx.request({
header: { 'X-APP-ID': app.xAppId },
url: config.baseUrl + '/uec/decrypt/wechat-applet-mobile',
data: form,
method: 'POST',
success: function (result) {
// 绑定手机号码,进行登录
if(loading) {
wx.hideLoading()
}
if (result.data.code != 0) {
wx.showToast({ title: '登录失败,请稍后再试', icon: "none" })
reject('登录失败,请稍后再试')
return
}
result.data.tmpAuthToken = form.tmpAuthToken
resolve(result.data)
},
fail: function (res) {
if(loading) {
wx.hideLoading()
}
wx.showToast({ title: '登录失败,请稍后再试', icon: "none" })
reject('登录失败,请稍后再试')
}
})
}
function phoneWechat(form, loading) {
let promise = new Promise(function (resolve, reject) {
if (!form.encryptedData) {
wx.showToast({ title: '授权被拒绝,登录失败', icon: "none" })
reject('授权被拒绝,登录失败')
return
}
if(loading) {
wx.showLoading({ title: '登录中', mask: true })
}
// 没有获取到openId,获取之后进行登录
wx.login({
success: res => {
// 获取openId
wx.request({
header: { 'X-APP-ID': app.xAppId },
url: config.baseUrl + '/uec/get/wechat-applet-session',
data: { authCode: res.code },
method: 'POST',
success: function (result) {
if (result.data.code == 0) {
form.tmpAuthToken = result.data.data.tmpAuthToken
// 获取手机号码
gotPhoneNumber(form, resolve, reject, loading)
} else {
if(loading) {
wx.hideLoading()
}
wx.showToast({ title: '登录失败,请稍后再试', icon: "none" })
reject('登录失败,请稍后再试')
}
},
fail: function (res) {
if(loading) {
wx.hideLoading()
}
wx.showToast({ title: '登录失败,请稍后再试', icon: "none" })
reject('登录失败,请稍后再试')
}
})
},
fail: function (res) {
if(loading) {
wx.hideLoading()
}
wx.showToast({ title: '登录失败,请稍后再试', icon: "none" })
reject('登录失败,请稍后再试')
}
})
})
return promise
}
function initToken(token){
sconfig.header = { 'Authorization': 'QNT ' + token }
pconfig.header = { 'Authorization': 'QNT ' + token }
zconfig.header = { 'Authorization': 'QNT ' + token }
mconfig.header = { 'Authorization': 'QNT ' + token }
}
function headerFactoryId(factoryId){
sconfig.header['X-FACTORY-ID'] = factoryId
pconfig.header['X-FACTORY-ID'] = factoryId
zconfig.header['X-FACTORY-ID'] = factoryId
mconfig.header['X-FACTORY-ID'] = factoryId
}
function finalizeToken(){
config.header = null
sconfig.header = null
pconfig.header = null
zconfig.header = null
mconfig.header = null
}
export {
config,
initToken,
headerFactoryId,
finalizeToken,
wechatApplet,
updateUserInfo,
getAuthSession,
certificateImage,
certificateIdentity,
postCaptcha,
loginPwd,
loginCaptcha,
phoneWechat,
loginWechat,
setPassword,
modifyMobile,
verifyCode,
generateCode,
createQrcode,
applyEmplyee,
getCheckList,
auditEmploye,
deleteEmploye,
editEmploye
}