纸通宝商家版本的小程序,原生微信开发。
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.
 

104 lines
3.3 KiB

import { mPost } from "./request"
import { zconfig } from "./ztb"
import { hconfig } from "./chain"
const util = require('../utils/util')
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/authorize/by-oauth2', params, config)
// ********************** 通过微信的方式登录******************************
// 通过微信的方式登录,onGotPhoneNumber中统一处理/uec/authorize/by-wechat-applet-mobile
function gotPhoneNumber(form, resolve, reject) {
wx.request({
header: { 'X-APP-ID': app.xAppId },
url: config.baseUrl + '/uec/authorize/by-wechat-applet-mobile',
data: form,
method: 'POST',
success: function (result) {
// 绑定手机号码,进行登录
wx.hideLoading()
if (result.data.code != 0) {
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
return
}
result.data.tmpAuthToken = form.tmpAuthToken
resolve(result.data)
},
fail: function (res) {
wx.hideLoading()
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
}
})
}
function phoneWechat(form) {
let promise = new Promise(function (resolve, reject) {
if (!form.encryptedData) {
util.showToast('授权被拒绝,登录失败')
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/authorize/by-oauth2',
data: { authCode: res.code, authMethod: 'WeChatMiniProgram' },
method: 'POST',
success: function (result) {
if (result.data.code == 0) {
if(result.data.data.tmpAuthToken){
form.tmpAuthToken = result.data.data.tmpAuthToken
// 获取手机号码
gotPhoneNumber(form, resolve, reject)
} else if(result.data.data.token){
wx.hideLoading()
resolve(result.data)
}
} else if (result.data.code == 200101 && result.data.data.tmpAuthToken) {
form.tmpAuthToken = result.data.data.tmpAuthToken
gotPhoneNumber(form, resolve, reject)
} else {
wx.hideLoading()
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
}
},
fail: function (res) {
wx.hideLoading()
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
}
})
},
fail: function (res) {
wx.hideLoading()
util.showToast('登录失败,请稍后再试')
reject('登录失败,请稍后再试')
}
})
})
return promise
}
function finalizeToken(){
config.header = null
zconfig.header = null
hconfig.header = null
}
export {
config,
finalizeToken,
wechatApplet,
phoneWechat,
}