纸通宝小程序
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.
 

196 lines
6.6 KiB

/**
* Copyright © 2020-present LiuDanYang. All rights Reserved.
*/
import { mGet, mPost, mDelete } from "./request"
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'
}
// /uec/get/wechat-applet-session
const wechatApplet = (params) => mPost('/uec/get/wechat-applet-session', params, config)
// /uec/authorize/by-captcha
const loginCaptcha = (params) => mPost(`/uec/authorize/by-captcha`, params, config)
// /uec/get/auth-captcha
const postCaptcha = (params) => mPost(`/uec/get/auth-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 getAddressList = () => 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) {
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(){
}
export {
config,
finalizeToken,
wechatApplet,
getUserInfo,
postCaptcha,
loginCaptcha,
getAuthSession,
certificateImage,
certificateIdentity,
phoneWechat,
loginWechat,
getAddressList,
saveAddress,
deleteAddress
}