/** * Copyright © 2020-present LiuDanYang. All rights Reserved. */ import { mGet, mPost, mDelete } from "./request" // import { mconfig } from "./mall" // import { cconfig } from "./cloud" import { pconfig } from "./payment" const util = require('../../utils/util') const storage = require('../../utils/storage') const app = getApp() const config = { baseUrl: app.release ? 'https://uec.qniao.cn' : 'http://47.107.97.166:9000', header: { 'Authorization': `QNT ` + storage.get('Authorization') } } const checkTestCode = (params) => mGet(`/uec/get/check/qr-code`, params, config) ///uec/get/auth-captcha const verifyCode = (code, params) => mGet(`/uec/verify/qrcode${code}`, params, config) const qrcodeEmploye = (params) => mPost(`/uec/generate/qrcode`, params, config) // /uec/get/wechat-applet-session const wechatApplet = (params) => mPost('/uec/get/wechat-applet-session', params, config) const authCaptcha = (params) => mPost(`/uec/get/auth-captcha`, params, config) const verifyCaptcha = (params) => mPost(`/uec/authorize/by-captcha`, params, config) const getUserInfo = (params) => mGet('/uec/get/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 bindingAdmin = (params) => mPost(`/uec/bind/primary-account`, params, config) const bindingAccount = (params) => mPost(`/uec/apply/to/be/emplyee/of/enterprise`, params, config) const getCheckList = (params) => mGet(`/uec/get/auditing-joining-enterprise-application-list`, params, config) const getEmployeList = (params) => mGet(`/uec/get/passed-employees`, params, config) const auditEmploye = (params) => mPost(`/uec/audit/employee-joining-application`, params, config) const deleteEmploye = (params) => mPost(`/uec/remove/employees`, params, config) const adminEmploye = (params) => mPost(`/uec/grant/admin-role/to/employees`, params, config) const getAddressList = () => mGet(`/uec/get/enterprise-shipping-address-list/by-userId`, null, config) const getAddressInfo = (id) => mGet(`/uec/get/enterprise-shipping-address-list/by-userId`, null, config) const saveAddress = (params) => mPost(`/uec/save/enterprise-shipping-address`, params, config) // /uec/delete/enterprise-shipping-address/{id} const deleteAddress = (id) => mDelete(`/uec/delete/enterprise-shipping-address/${id}`, null, config) // **************************************************************************************************************************************** // /authorize/by-wechat-applet小程序登录 function loginWechat(form) { let promise = new Promise(function (resolve, reject) { if (!form.encryptedData) { util.showToast('授权被拒绝,获取微信用户失败') reject('授权被拒绝,获取微信用户失败') return } wx.showLoading({ title: '登录中', mask: true }) var tempToken = storage.get('tmpAuthToken') if(!util.isEmpty(tempToken)){ form.tmpAuthToken = tempToken unionWechat(form, resolve, reject) } 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) } else { wx.hideLoading() util.showToast('登录失败,请稍后再试') reject('登录失败,请稍后再试') } }, fail: function (err) { wx.hideLoading() util.showToast('登录失败,请稍后再试') reject('登录失败,请稍后再试') } }) }, fail: function (err) { wx.hideLoading() util.showToast('登录失败,请稍后再试') reject('登录失败,请稍后再试') } }) } }) return promise } // POST ​/uec​/authorize​/by-wechat-applet 通过小程序的方式进行认证 function unionWechat(form, resolve, reject) { wx.request({ header: { 'X-APP-ID': app.xAppId }, url: config.baseUrl + '/uec/authorize/by-wechat-applet', data: form, method: 'POST', success: function (result) { wx.hideLoading() if (result.data.code == 0) { storage.remove('tempToken') resolve(result.data) } else { reject(result.data) } }, fail: function () { 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) { // 绑定手机号码,进行登录 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) { wx.hideLoading() wx.showToast({ title: '登录失败,请稍后再试', icon: "none" }) reject('登录失败,请稍后再试') } }) } function phoneWechat(form) { let promise = new Promise(function (resolve, reject) { if (!form.encryptedData) { wx.showToast({ title: '授权被拒绝,登录失败', icon: "none" }) reject('授权被拒绝,登录失败') return } 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) } else { wx.hideLoading() wx.showToast({ title: '登录失败,请稍后再试', icon: "none" }) reject('登录失败,请稍后再试') } }, fail: function (res) { wx.hideLoading() wx.showToast({ title: '登录失败,请稍后再试', icon: "none" }) reject('登录失败,请稍后再试') } }) }, fail: function (res) { wx.hideLoading() wx.showToast({ title: '登录失败,请稍后再试', icon: "none" }) reject('登录失败,请稍后再试') } }) }) return promise } function finalizeToken() { config.header = null mconfig.header = null pconfig.header = null cconfig.header = null } export { config, qrcodeEmploye, verifyCode, checkTestCode, wechatApplet, authCaptcha, verifyCaptcha, getUserInfo, getAuthSession, certificateImage, certificateIdentity, phoneWechat, loginWechat, bindingAdmin, bindingAccount, getCheckList, getEmployeList, auditEmploye, deleteEmploye, adminEmploye, getAddressList, saveAddress, getAddressInfo, deleteAddress, finalizeToken } // module.exports = { // phoneWechat: phoneWechat, // loginWechat: loginWechat // }