提交 5c3b009b 编写于 作者: C chenruilong

优化 绑定手机号支持微信新版本接口 phonenumber.getPhoneNumber, 增加 code 参数

上级 0d126160
...@@ -177,10 +177,22 @@ async function getWeixinCache ({ ...@@ -177,10 +177,22 @@ async function getWeixinCache ({
}) })
} }
async function getWeixinAccessToken () {
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
const cache = await this.uniOpenBridge.getAccessToken({
dcloudAppid: appId,
platform: 'weixin-' + weixinPlatform
})
return cache.access_token
}
module.exports = { module.exports = {
decryptWeixinData, decryptWeixinData,
getWeixinPlatform, getWeixinPlatform,
generateWeixinCache, generateWeixinCache,
getWeixinCache, getWeixinCache,
saveWeixinUserKey saveWeixinUserKey,
getWeixinAccessToken
} }
...@@ -7,8 +7,10 @@ const { ...@@ -7,8 +7,10 @@ const {
} = require('../../common/constants') } = require('../../common/constants')
const { const {
decryptWeixinData, decryptWeixinData,
getWeixinCache getWeixinCache, getWeixinAccessToken
} = require('../../lib/utils/weixin') } = require('../../lib/utils/weixin')
const { initWeixin } = require('../../lib/third-party')
const { ERROR } = require('../../common/error')
/** /**
* 通过微信绑定手机号 * 通过微信绑定手机号
...@@ -16,6 +18,7 @@ const { ...@@ -16,6 +18,7 @@ const {
* @param {Object} params * @param {Object} params
* @param {String} params.encryptedData 微信获取手机号返回的加密信息 * @param {String} params.encryptedData 微信获取手机号返回的加密信息
* @param {String} params.iv 微信获取手机号返回的初始向量 * @param {String} params.iv 微信获取手机号返回的初始向量
* @param {String} params.code 微信获取手机号返回的code
* @returns * @returns
*/ */
module.exports = async function (params = {}) { module.exports = async function (params = {}) {
...@@ -26,30 +29,58 @@ module.exports = async function (params = {}) { ...@@ -26,30 +29,58 @@ module.exports = async function (params = {}) {
* 因此此接口不应直接使用客户端login获取的code,只能使用缓存的sessionKey * 因此此接口不应直接使用客户端login获取的code,只能使用缓存的sessionKey
*/ */
const schema = { const schema = {
encryptedData: 'string', encryptedData: {
iv: 'string' required: false,
type: 'string'
},
iv: {
required: false,
type: 'string'
},
code: {
required: false,
type: 'string'
}
} }
const { const {
encryptedData, encryptedData,
iv iv,
code
} = params } = params
this.middleware.validate(params, schema) this.middleware.validate(params, schema)
if ((!encryptedData && !iv) && !code) {
return {
errCode: ERROR.INVALID_PARAM
}
}
const uid = this.authInfo.uid const uid = this.authInfo.uid
const sessionKey = await getWeixinCache.call(this, { let mobile
uid, if (code) {
key: 'session_key' // 区分客户端类型 小程序还是App
}) const accessToken = await getWeixinAccessToken.call(this)
if (!sessionKey) { const weixinApi = initWeixin.call(this)
throw new Error('Session key not found') const res = await weixinApi.getPhoneNumber(accessToken, code)
mobile = res.purePhoneNumber
} else {
const sessionKey = await getWeixinCache.call(this, {
uid,
key: 'session_key'
})
if (!sessionKey) {
throw new Error('Session key not found')
}
const res = decryptWeixinData.call(this, {
encryptedData,
sessionKey,
iv
})
mobile = res.purePhoneNumber
} }
const {
purePhoneNumber: mobile
} = decryptWeixinData.call(this, {
encryptedData,
sessionKey,
iv
})
const bindAccount = { const bindAccount = {
mobile mobile
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册