From 91f05653ae0336048d211eab9579b30898ebc5d3 Mon Sep 17 00:00:00 2001 From: dongfengtao Date: Wed, 13 Mar 2024 15:59:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=BF=90=E8=A1=8C=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E6=94=B9=E5=AE=8C=E8=87=AA=E5=8A=A8=E8=AF=86=E5=88=AB=E7=9A=84?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E7=94=A8=E6=88=B7=E5=9C=A8=E4=BC=A0?= =?UTF-8?q?=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miniapp-taro/src/pages/index/index.tsx | 16 +++--- .../miniapp-uniapp/src/pages/index/index.vue | 49 +++++++++++-------- packages/miniapp/src/Authing.ts | 12 ++--- packages/miniapp/src/helpers/utils.ts | 15 ++++++ packages/miniapp/src/types.ts | 9 ++-- 5 files changed, 65 insertions(+), 36 deletions(-) diff --git a/examples/miniapp-taro/src/pages/index/index.tsx b/examples/miniapp-taro/src/pages/index/index.tsx index 7b4a72a2..8f293c85 100644 --- a/examples/miniapp-taro/src/pages/index/index.tsx +++ b/examples/miniapp-taro/src/pages/index/index.tsx @@ -9,7 +9,6 @@ import { Authing } from '@authing/miniapp-taro' // import { encryptFunction } from '@authing/miniapp-jsencrypt' import { encryptFunction } from '@authing/miniapp-sm2encrypt' -const RUN_PLATFORM = 'wx'; //运行平台默认 wx const EXT_IDP_CONNIDENTIFIER = "EXT_IDP_CONNIDENTIFIER" const APP_SECRET = 'APP_SECRET'; @@ -19,7 +18,6 @@ const authing = new Authing({ appId: 'appId', host: 'host', userPoolId: 'userPoolId', - platform: RUN_PLATFORM, encryptFunction }) @@ -103,14 +101,18 @@ export default class Index extends Component { const res = await authing.loginByPhone({ extIdpConnidentifier: EXT_IDP_CONNIDENTIFIER, - douyinMiniProgramCodeAndPhonePayload: { + // 之前的暂时保留 + wechatMiniProgramCodeAndPhonePayload: { + wxPhoneInfo: { + code + } + }, + // 通用参数 + miniProgramCodeAndPhonePayload: { phoneParams: { - code, //这个 code 需要修改一下 encryptedData, iv, - } - }, - wechatMiniProgramCodeAndPhonePayload: { + }, wxPhoneInfo: { code } diff --git a/examples/miniapp-uniapp/src/pages/index/index.vue b/examples/miniapp-uniapp/src/pages/index/index.vue index f5032eae..3fbe67e1 100644 --- a/examples/miniapp-uniapp/src/pages/index/index.vue +++ b/examples/miniapp-uniapp/src/pages/index/index.vue @@ -45,14 +45,12 @@ import { Authing } from '/Users/mac/Desktop/www/authing-js-sdk/packages/miniapp-uniapp/dist/bundle-uniapp' import { encryptFunction } from '@authing/miniapp-jsencrypt' const AUTHING_EXT_IDP_CONN_IDENTIFIER = "AUTHING_EXT_IDP_CONN_IDENTIFIER" - const RUN_PLATFORM = 'wx'; //运行平台默认抖音 const APP_SECRET = 'APP_SECRET'; const APP_ID = "APP_ID"; const authing = new Authing({ appId: "appId", host: 'host', userPoolId: 'userPoolId', - platform: RUN_PLATFORM, encryptFunction }) @@ -71,26 +69,37 @@ }, methods: { - /** - * 需要在真机上测试,微信开发者工具不会返回 code - * @param {*} e - */ - async loginByPhone (e) { - const { code } = e.detail - console.log('e: ', e) - const res = await authing.loginByPhone({ - extIdpConnidentifier: AUTHING_EXT_IDP_CONN_IDENTIFIER, - wechatMiniProgramCodeAndPhonePayload: { - wxPhoneInfo: { - code - } + /** + * 需要在真机上测试,微信开发者工具不会返回 code + * @param {*} e + */ + async loginByPhone(e) { + const { code, iv, encryptedData } = e.detail; + console.log('e: ', e) + const res = await authing.loginByPhone({ + extIdpConnidentifier: AUTHING_EXT_IDP_CONN_IDENTIFIER, + // 之前的暂时保留 + // wechatMiniProgramCodeAndPhonePayload: { + // wxPhoneInfo: { + // code + // } + // }, + // 通用参数 + miniProgramCodeAndPhonePayload: { + phoneParams: { + encryptedData, + iv, }, - options: { - scope: 'openid profile offline_access' + wxPhoneInfo: { + code } - }) - console.log('authing.loginByPhone res: ', res) - }, + }, + options: { + scope: 'openid profile offline_access' + } + }) + console.log('authing.loginByPhone res: ', res) + }, async loginByCode () { const res = await authing.loginByCode({ diff --git a/packages/miniapp/src/Authing.ts b/packages/miniapp/src/Authing.ts index 30909654..cdb9602c 100644 --- a/packages/miniapp/src/Authing.ts +++ b/packages/miniapp/src/Authing.ts @@ -47,6 +47,7 @@ import { getLoginStateKey, getPlatformLoginCodeKey, request, + getCurrentMiniProgram, StorageProvider } from './helpers' @@ -61,7 +62,7 @@ export class Authing { this.options = { ...options, host: options.host || 'https://core.authing.cn', - platform: options.platform || 'wx' + platform: options.platform || getCurrentMiniProgram() || 'wx' } this.storage = new StorageProvider() @@ -335,7 +336,7 @@ export class Authing { const { extIdpConnidentifier, wechatMiniProgramCodeAndPhonePayload, - douyinMiniProgramCodeAndPhonePayload, + miniProgramCodeAndPhonePayload, options } = data @@ -350,8 +351,7 @@ export class Authing { switch (this.options.platform) { case PlatformsMenu.wx: - const wxPhoneInfo = wechatMiniProgramCodeAndPhonePayload?.wxPhoneInfo - + const wxPhoneInfo = wechatMiniProgramCodeAndPhonePayload?.wxPhoneInfo || miniProgramCodeAndPhonePayload?.wxPhoneInfo if (!wxPhoneInfo || !wxPhoneInfo.code) { return returnError({ message: 'wxPhoneInfo.code is required' @@ -384,7 +384,7 @@ export class Authing { break case PlatformsMenu.tt: - const phoneParams = douyinMiniProgramCodeAndPhonePayload?.phoneParams + const phoneParams = miniProgramCodeAndPhonePayload?.phoneParams if (!phoneParams) { return returnError({ @@ -397,7 +397,7 @@ export class Authing { if (!loginParamsCode) { return returnError({ - message: 'get tt login params error' + message: 'get tt login params error ,please use the miniProgramCodeAndPhonePayload' }) } diff --git a/packages/miniapp/src/helpers/utils.ts b/packages/miniapp/src/helpers/utils.ts index 43a4590f..a26fb195 100644 --- a/packages/miniapp/src/helpers/utils.ts +++ b/packages/miniapp/src/helpers/utils.ts @@ -1,5 +1,6 @@ import { Platforms, + PlatformsMenu, } from '../types' @@ -11,3 +12,17 @@ export function getPlatformLoginCodeKey (appId: string,platform: Platforms | und return ['authing', appId, `${platform ? platform : 'wx' }-login-code`].join(':') } + +declare const wx: any +declare const tt: any + +export function getCurrentMiniProgram():PlatformsMenu { + if (typeof tt!== 'undefined' && typeof tt.getSystemInfo !== 'undefined') { + return PlatformsMenu.tt + } else if (typeof wx !== 'undefined' && typeof wx.getSystemInfo !== 'undefined') { + return PlatformsMenu.wx + } else { + return PlatformsMenu.wx + } + +} diff --git a/packages/miniapp/src/types.ts b/packages/miniapp/src/types.ts index ced61da9..2708f6ae 100644 --- a/packages/miniapp/src/types.ts +++ b/packages/miniapp/src/types.ts @@ -137,12 +137,15 @@ export interface LoginByPhoneOptions { code: string } } - douyinMiniProgramCodeAndPhonePayload?: { - phoneParams: { - code: string + miniProgramCodeAndPhonePayload?: { + phoneParams?: { + code?: string iv: string encryptedData: string } + wxPhoneInfo?: { + code: string + } } options?: LoginOptions }