From 5c3b009b1aa325e4cf1f90f75231d36760662f8e Mon Sep 17 00:00:00 2001 From: chenruilong Date: Wed, 21 Sep 2022 18:13:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E6=94=AF=E6=8C=81=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=96=B0=E7=89=88=E6=9C=AC=E6=8E=A5=E5=8F=A3=20phonenumber.get?= =?UTF-8?q?PhoneNumber,=20=E5=A2=9E=E5=8A=A0=20code=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-id-co/lib/utils/weixin.js | 14 +++- .../module/relate/bind-mobile-by-mp-weixin.js | 65 ++++++++++++++----- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/weixin.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/weixin.js index 8ff178f..2a0f609 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/weixin.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/weixin.js @@ -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 = { decryptWeixinData, getWeixinPlatform, generateWeixinCache, getWeixinCache, - saveWeixinUserKey + saveWeixinUserKey, + getWeixinAccessToken } diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/relate/bind-mobile-by-mp-weixin.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/relate/bind-mobile-by-mp-weixin.js index 5da7259..2e9a991 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/relate/bind-mobile-by-mp-weixin.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/relate/bind-mobile-by-mp-weixin.js @@ -7,8 +7,10 @@ const { } = require('../../common/constants') const { decryptWeixinData, - getWeixinCache + getWeixinCache, getWeixinAccessToken } = require('../../lib/utils/weixin') +const { initWeixin } = require('../../lib/third-party') +const { ERROR } = require('../../common/error') /** * 通过微信绑定手机号 @@ -16,6 +18,7 @@ const { * @param {Object} params * @param {String} params.encryptedData 微信获取手机号返回的加密信息 * @param {String} params.iv 微信获取手机号返回的初始向量 + * @param {String} params.code 微信获取手机号返回的code * @returns */ module.exports = async function (params = {}) { @@ -26,30 +29,58 @@ module.exports = async function (params = {}) { * 因此此接口不应直接使用客户端login获取的code,只能使用缓存的sessionKey */ const schema = { - encryptedData: 'string', - iv: 'string' + encryptedData: { + required: false, + type: 'string' + }, + iv: { + required: false, + type: 'string' + }, + code: { + required: false, + type: 'string' + } } const { encryptedData, - iv + iv, + code } = params this.middleware.validate(params, schema) + + if ((!encryptedData && !iv) && !code) { + return { + errCode: ERROR.INVALID_PARAM + } + } + const uid = this.authInfo.uid - const sessionKey = await getWeixinCache.call(this, { - uid, - key: 'session_key' - }) - if (!sessionKey) { - throw new Error('Session key not found') + let mobile + if (code) { + // 区分客户端类型 小程序还是App + const accessToken = await getWeixinAccessToken.call(this) + const weixinApi = initWeixin.call(this) + 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 = { mobile -- GitLab