diff --git a/es/aspects/AspectDict.d.ts b/es/aspects/AspectDict.d.ts index 3ee628935..422ee9d7e 100644 --- a/es/aspects/AspectDict.d.ts +++ b/es/aspects/AspectDict.d.ts @@ -7,6 +7,10 @@ import { MaterialType } from '../types/WeChat'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { WechatPublicEventData, WechatMpEventData } from 'oak-external-sdk'; export type AspectDict = { + loginWebByMpToken: (params: { + mpToken: string; + env: WebEnv; + }, context: BackendRuntimeContext) => Promise; mergeUser: (params: { from: string; to: string; diff --git a/es/aspects/index.d.ts b/es/aspects/index.d.ts index e5585b6ec..e47a83522 100644 --- a/es/aspects/index.d.ts +++ b/es/aspects/index.d.ts @@ -1,4 +1,4 @@ -import { bindByEmail, bindByMobile, loginByAccount, loginByEmail, loginByMobile, loginWechat, loginWechatMp, loginWechatNative, syncUserInfoWechatMp, sendCaptchaByMobile, sendCaptchaByEmail, switchTo, refreshWechatPublicUserInfo, getWechatMpUserPhoneNumber, logout, loginByWechat, wakeupParasite, refreshToken, verifyPassword } from './token'; +import { bindByEmail, bindByMobile, loginByAccount, loginByEmail, loginByMobile, loginWechat, loginWechatMp, loginWechatNative, syncUserInfoWechatMp, sendCaptchaByMobile, sendCaptchaByEmail, switchTo, refreshWechatPublicUserInfo, getWechatMpUserPhoneNumber, logout, loginByWechat, wakeupParasite, refreshToken, verifyPassword, loginWebByMpToken } from './token'; import { getInfoByUrl } from './extraFile'; import { getApplication, signatureJsSDK, uploadWechatMedia, batchGetArticle, getArticle, batchGetMaterialList, getMaterial, deleteMaterial } from './application'; import { updateConfig, updateApplicationConfig, updateStyle } from './config'; @@ -79,6 +79,7 @@ declare const aspectDict: { getApplicationPassports: typeof getApplicationPassports; removeApplicationPassportsByPIds: typeof removeApplicationPassportsByPIds; verifyPassword: typeof verifyPassword; + loginWebByMpToken: typeof loginWebByMpToken; }; export default aspectDict; export { AspectDict } from './AspectDict'; diff --git a/es/aspects/index.js b/es/aspects/index.js index d1e541871..29b255029 100644 --- a/es/aspects/index.js +++ b/es/aspects/index.js @@ -1,4 +1,4 @@ -import { bindByEmail, bindByMobile, loginByAccount, loginByEmail, loginByMobile, loginWechat, loginWechatMp, loginWechatNative, syncUserInfoWechatMp, sendCaptchaByMobile, sendCaptchaByEmail, switchTo, refreshWechatPublicUserInfo, getWechatMpUserPhoneNumber, logout, loginByWechat, wakeupParasite, refreshToken, verifyPassword, } from './token'; +import { bindByEmail, bindByMobile, loginByAccount, loginByEmail, loginByMobile, loginWechat, loginWechatMp, loginWechatNative, syncUserInfoWechatMp, sendCaptchaByMobile, sendCaptchaByEmail, switchTo, refreshWechatPublicUserInfo, getWechatMpUserPhoneNumber, logout, loginByWechat, wakeupParasite, refreshToken, verifyPassword, loginWebByMpToken, } from './token'; import { getInfoByUrl } from './extraFile'; import { getApplication, signatureJsSDK, uploadWechatMedia, batchGetArticle, getArticle, batchGetMaterialList, getMaterial, deleteMaterial, } from './application'; import { updateConfig, updateApplicationConfig, updateStyle } from './config'; @@ -79,5 +79,6 @@ const aspectDict = { getApplicationPassports, removeApplicationPassportsByPIds, verifyPassword, + loginWebByMpToken, }; export default aspectDict; diff --git a/es/aspects/token.d.ts b/es/aspects/token.d.ts index 45250d704..2c264340f 100644 --- a/es/aspects/token.d.ts +++ b/es/aspects/token.d.ts @@ -114,3 +114,14 @@ export declare function refreshToken(params: { tokenValue: string; applicationId: string; }, context: BRC): Promise; +/** + * 小程序web-view处理token + * @param mpToken + * @param env + * @param context + * @returns + */ +export declare function loginWebByMpToken(params: { + mpToken: string; + env: WebEnv; +}, context: BRC): Promise; diff --git a/es/aspects/token.js b/es/aspects/token.js index 4dbd00063..bac91bc75 100644 --- a/es/aspects/token.js +++ b/es/aspects/token.js @@ -2489,3 +2489,70 @@ export async function refreshToken(params, context) { closeRootMode(); return tokenValue; } +/** + * 使用微信小程序中的token登录web + * @param tokenValue + * @param env + * @param context + * @returns + */ +async function setupWechatMp(mpToken, env, context) { + const [token] = await context.select('token', { + data: { + id: 1, + entity: 1, + entityId: 1, + ableState: 1, + userId: 1, + user: { + id: 1, + userState: 1, + refId: 1, + ref: { + id: 1, + userState: 1, + refId: 1, + }, + wechatUser$user: { + $entity: 'wechatUser', + data: { + id: 1, + }, + }, + userSystem$user: { + $entity: 'userSystem', + data: { + id: 1, + systemId: 1, + }, + }, + }, + }, + filter: { + $or: [{ + value: mpToken, + }, { + oldValue: mpToken, + }] + // ableState: 'enabled', + }, + }, { dontCollect: true }); + assert(token); + const { user } = token; + return await setUpTokenAndUser(env, context, token.entity, token.entityId, undefined, user); +} +/** + * 小程序web-view处理token + * @param mpToken + * @param env + * @param context + * @returns + */ +export async function loginWebByMpToken(params, context) { + const { mpToken, env } = params; + const closeRootMode = context.openRootMode(); + const tokenValue = await setupWechatMp(mpToken, env, context); + await loadTokenInfo(tokenValue, context); + closeRootMode(); + return tokenValue; +} diff --git a/es/features/application.js b/es/features/application.js index 6a9513f1d..3de11cc8e 100644 --- a/es/features/application.js +++ b/es/features/application.js @@ -62,8 +62,11 @@ export class Application extends Feature { appType = 'native'; } else { + const url = new URL(window.location.href); + const param = url.searchParams.get('appType'); + console.log('param appType:',param); if (/MicroMessenger/i.test(window.navigator.userAgent)) { - appType = 'wechatPublic'; + appType = param || 'wechatPublic'; } } const { result } = await this.cache.exec('getApplication', { diff --git a/es/features/token.d.ts b/es/features/token.d.ts index 70e8713fa..64c0abcd8 100644 --- a/es/features/token.d.ts +++ b/es/features/token.d.ts @@ -19,6 +19,7 @@ export declare class Token extends Feature { loginByAccount(account: string, password: string): Promise; bindByMobile(mobile: string, captcha?: string): Promise; bindByEmail(email: string, captcha?: string): Promise; + loginWebByMpToken(mpToken: string): Promise; loginByWechatInWebEnv(wechatLoginId: string): Promise; loginWechat(code: string, params?: { wechatLoginId?: string; diff --git a/es/features/token.js b/es/features/token.js index c351af951..63b4bfd97 100644 --- a/es/features/token.js +++ b/es/features/token.js @@ -142,6 +142,17 @@ export class Token extends Feature { }, undefined, true); this.publish(); } + async loginWebByMpToken(mpToken) { + const env = await this.environment.getEnv(); + const { result } = await this.cache.exec('loginWebByMpToken', { + mpToken, + env, + }, undefined, true); + this.tokenValue = result; + await this.storage.save(LOCAL_STORAGE_KEYS.token, result); + this.publish(); + this.checkNeedSetPassword(); + } async loginByWechatInWebEnv(wechatLoginId) { const env = await this.environment.getEnv(); const { result } = await this.cache.exec('loginByWechat', { diff --git a/lib/aspects/AspectDict.d.ts b/lib/aspects/AspectDict.d.ts index 3ee628935..422ee9d7e 100644 --- a/lib/aspects/AspectDict.d.ts +++ b/lib/aspects/AspectDict.d.ts @@ -7,6 +7,10 @@ import { MaterialType } from '../types/WeChat'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { WechatPublicEventData, WechatMpEventData } from 'oak-external-sdk'; export type AspectDict = { + loginWebByMpToken: (params: { + mpToken: string; + env: WebEnv; + }, context: BackendRuntimeContext) => Promise; mergeUser: (params: { from: string; to: string; diff --git a/lib/aspects/index.d.ts b/lib/aspects/index.d.ts index e5585b6ec..e47a83522 100644 --- a/lib/aspects/index.d.ts +++ b/lib/aspects/index.d.ts @@ -1,4 +1,4 @@ -import { bindByEmail, bindByMobile, loginByAccount, loginByEmail, loginByMobile, loginWechat, loginWechatMp, loginWechatNative, syncUserInfoWechatMp, sendCaptchaByMobile, sendCaptchaByEmail, switchTo, refreshWechatPublicUserInfo, getWechatMpUserPhoneNumber, logout, loginByWechat, wakeupParasite, refreshToken, verifyPassword } from './token'; +import { bindByEmail, bindByMobile, loginByAccount, loginByEmail, loginByMobile, loginWechat, loginWechatMp, loginWechatNative, syncUserInfoWechatMp, sendCaptchaByMobile, sendCaptchaByEmail, switchTo, refreshWechatPublicUserInfo, getWechatMpUserPhoneNumber, logout, loginByWechat, wakeupParasite, refreshToken, verifyPassword, loginWebByMpToken } from './token'; import { getInfoByUrl } from './extraFile'; import { getApplication, signatureJsSDK, uploadWechatMedia, batchGetArticle, getArticle, batchGetMaterialList, getMaterial, deleteMaterial } from './application'; import { updateConfig, updateApplicationConfig, updateStyle } from './config'; @@ -79,6 +79,7 @@ declare const aspectDict: { getApplicationPassports: typeof getApplicationPassports; removeApplicationPassportsByPIds: typeof removeApplicationPassportsByPIds; verifyPassword: typeof verifyPassword; + loginWebByMpToken: typeof loginWebByMpToken; }; export default aspectDict; export { AspectDict } from './AspectDict'; diff --git a/lib/aspects/index.js b/lib/aspects/index.js index 2bfa2b625..f51cc2bb5 100644 --- a/lib/aspects/index.js +++ b/lib/aspects/index.js @@ -81,5 +81,6 @@ const aspectDict = { getApplicationPassports: applicationPassport_1.getApplicationPassports, removeApplicationPassportsByPIds: applicationPassport_1.removeApplicationPassportsByPIds, verifyPassword: token_1.verifyPassword, + loginWebByMpToken: token_1.loginWebByMpToken, }; exports.default = aspectDict; diff --git a/lib/aspects/token.d.ts b/lib/aspects/token.d.ts index 45250d704..2c264340f 100644 --- a/lib/aspects/token.d.ts +++ b/lib/aspects/token.d.ts @@ -114,3 +114,14 @@ export declare function refreshToken(params: { tokenValue: string; applicationId: string; }, context: BRC): Promise; +/** + * 小程序web-view处理token + * @param mpToken + * @param env + * @param context + * @returns + */ +export declare function loginWebByMpToken(params: { + mpToken: string; + env: WebEnv; +}, context: BRC): Promise; diff --git a/lib/aspects/token.js b/lib/aspects/token.js index 4ff16bfe6..ad8f93023 100644 --- a/lib/aspects/token.js +++ b/lib/aspects/token.js @@ -1,6 +1,25 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.refreshToken = exports.wakeupParasite = exports.logout = exports.getWechatMpUserPhoneNumber = exports.switchTo = exports.sendCaptchaByEmail = exports.sendCaptchaByMobile = exports.syncUserInfoWechatMp = exports.loginWechatMp = exports.loginWechat = exports.loginWechatNative = exports.loginByWechat = exports.refreshWechatPublicUserInfo = exports.bindByEmail = exports.bindByMobile = exports.loginByEmail = exports.loginByAccount = exports.verifyPassword = exports.loginByMobile = void 0; +exports.loginByMobile = loginByMobile; +exports.verifyPassword = verifyPassword; +exports.loginByAccount = loginByAccount; +exports.loginByEmail = loginByEmail; +exports.bindByMobile = bindByMobile; +exports.bindByEmail = bindByEmail; +exports.refreshWechatPublicUserInfo = refreshWechatPublicUserInfo; +exports.loginByWechat = loginByWechat; +exports.loginWechatNative = loginWechatNative; +exports.loginWechat = loginWechat; +exports.loginWechatMp = loginWechatMp; +exports.syncUserInfoWechatMp = syncUserInfoWechatMp; +exports.sendCaptchaByMobile = sendCaptchaByMobile; +exports.sendCaptchaByEmail = sendCaptchaByEmail; +exports.switchTo = switchTo; +exports.getWechatMpUserPhoneNumber = getWechatMpUserPhoneNumber; +exports.logout = logout; +exports.wakeupParasite = wakeupParasite; +exports.refreshToken = refreshToken; +exports.loginWebByMpToken = loginWebByMpToken; const tslib_1 = require("tslib"); const uuid_1 = require("oak-domain/lib/utils/uuid"); const WechatSDK_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/WechatSDK")); @@ -534,7 +553,6 @@ async function loginByMobile(params, context) { closeRootMode(); return tokenValue; } -exports.loginByMobile = loginByMobile; async function verifyPassword(params, context) { const { password } = params; const userId = context.getCurrentUserId(); @@ -566,7 +584,6 @@ async function verifyPassword(params, context) { } }, {}); } -exports.verifyPassword = verifyPassword; async function loginByAccount(params, context) { const { account, password, env } = params; let needUpdatePassword = false; @@ -907,7 +924,6 @@ async function loginByAccount(params, context) { closeRootMode(); return tokenValue; } -exports.loginByAccount = loginByAccount; async function loginByEmail(params, context) { const { email, captcha, env, disableRegister } = params; const loginLogic = async () => { @@ -978,7 +994,6 @@ async function loginByEmail(params, context) { closeRootMode(); return tokenValue; } -exports.loginByEmail = loginByEmail; async function bindByMobile(params, context) { const { mobile, captcha, env, } = params; const userId = context.getCurrentUserId(); @@ -1090,7 +1105,6 @@ async function bindByMobile(params, context) { await bindLogic(); closeRootMode(); } -exports.bindByMobile = bindByMobile; async function bindByEmail(params, context) { const { email, captcha, env, } = params; const userId = context.getCurrentUserId(); @@ -1203,7 +1217,6 @@ async function bindByEmail(params, context) { await bindLogic(); closeRootMode(); } -exports.bindByEmail = bindByEmail; async function setupLoginName(name, env, context) { const result2 = await context.select('loginName', { data: { @@ -1457,7 +1470,6 @@ async function refreshWechatPublicUserInfo({}, context) { (0, assert_1.assert)(token.entityId); return await tryRefreshWechatPublicUserInfo(token.entityId, context); } -exports.refreshWechatPublicUserInfo = refreshWechatPublicUserInfo; // 用户在微信端授权登录后,在web端触发该方法 async function loginByWechat(params, context) { const { wechatLoginId, env } = params; @@ -1487,7 +1499,6 @@ async function loginByWechat(params, context) { closeRootMode(); return tokenValue; } -exports.loginByWechat = loginByWechat; async function loginFromWechatEnv(code, env, context, wechatLoginId) { const application = context.getApplication(); const { type, config, systemId } = application; @@ -1831,7 +1842,6 @@ async function loginWechatNative({ code, env, }, context) { closeRootMode(); return tokenValue; } -exports.loginWechatNative = loginWechatNative; /** * 公众号授权登录 * @param param0 @@ -1862,7 +1872,6 @@ async function loginWechat({ code, env, wechatLoginId, }, context) { closeRootMode(); return tokenValue; } -exports.loginWechat = loginWechat; /** * 小程序授权登录 * @param param0 @@ -1876,7 +1885,6 @@ async function loginWechatMp({ code, env, }, context) { closeRootMode(); return tokenValue; } -exports.loginWechatMp = loginWechatMp; /** * 同步从wx.getUserProfile拿到的用户信息 * @param param0 @@ -1930,7 +1938,6 @@ async function syncUserInfoWechatMp({ nickname, avatarUrl, encryptedData, iv, si // 实测发现解密出来的和userInfo完全一致…… await setUserInfoFromWechat(user, { nickname, avatar: avatarUrl }, context); } -exports.syncUserInfoWechatMp = syncUserInfoWechatMp; async function sendCaptchaByMobile({ mobile, env, type: captchaType, }, context) { const { type } = env; let visitorId = mobile; @@ -2092,7 +2099,6 @@ async function sendCaptchaByMobile({ mobile, env, type: captchaType, }, context) return '验证码发送失败'; } } -exports.sendCaptchaByMobile = sendCaptchaByMobile; async function sendCaptchaByEmail({ email, env, type: captchaType, }, context) { const { type } = env; let visitorId = email; @@ -2238,7 +2244,6 @@ async function sendCaptchaByEmail({ email, env, type: captchaType, }, context) { return '验证码发送失败'; } } -exports.sendCaptchaByEmail = sendCaptchaByEmail; async function switchTo({ userId }, context) { const reallyRoot = context.isReallyRoot(); if (!reallyRoot) { @@ -2260,7 +2265,6 @@ async function switchTo({ userId }, context) { }, }, {}); } -exports.switchTo = switchTo; async function getWechatMpUserPhoneNumber({ code, env }, context) { const application = context.getApplication(); const { type, config, systemId } = application; @@ -2276,7 +2280,6 @@ async function getWechatMpUserPhoneNumber({ code, env }, context) { closeRootMode(); return reuslt; } -exports.getWechatMpUserPhoneNumber = getWechatMpUserPhoneNumber; async function logout(params, context) { const { tokenValue } = params; if (tokenValue) { @@ -2299,7 +2302,6 @@ async function logout(params, context) { closeRootMode(); } } -exports.logout = logout; /** * 创建一个当前parasite上的token * @param params @@ -2366,7 +2368,6 @@ async function wakeupParasite(params, context) { closeRootMode(); return tokenValue; } -exports.wakeupParasite = wakeupParasite; /** * todo 检查登录环境一致性,同一个token不能跨越不同设备 * @param env1 @@ -2511,4 +2512,70 @@ async function refreshToken(params, context) { closeRootMode(); return tokenValue; } -exports.refreshToken = refreshToken; +/** + * 使用微信小程序中的token登录web + * @param tokenValue + * @param env + * @param context + * @returns + */ +async function setupWechatMp(mpToken, env, context) { + const [token] = await context.select('token', { + data: { + id: 1, + entity: 1, + entityId: 1, + ableState: 1, + userId: 1, + user: { + id: 1, + userState: 1, + refId: 1, + ref: { + id: 1, + userState: 1, + refId: 1, + }, + wechatUser$user: { + $entity: 'wechatUser', + data: { + id: 1, + }, + }, + userSystem$user: { + $entity: 'userSystem', + data: { + id: 1, + systemId: 1, + }, + }, + }, + }, + filter: { + $or: [{ + value: mpToken, + }, { + oldValue: mpToken, + }] + // ableState: 'enabled', + }, + }, { dontCollect: true }); + (0, assert_1.assert)(token); + const { user } = token; + return await setUpTokenAndUser(env, context, token.entity, token.entityId, undefined, user); +} +/** + * 小程序web-view处理token + * @param mpToken + * @param env + * @param context + * @returns + */ +async function loginWebByMpToken(params, context) { + const { mpToken, env } = params; + const closeRootMode = context.openRootMode(); + const tokenValue = await setupWechatMp(mpToken, env, context); + await loadTokenInfo(tokenValue, context); + closeRootMode(); + return tokenValue; +} diff --git a/lib/features/application.js b/lib/features/application.js index 94eb6db85..a1ed1632c 100644 --- a/lib/features/application.js +++ b/lib/features/application.js @@ -65,8 +65,10 @@ class Application extends Feature_1.Feature { appType = 'native'; } else { + const url = new URL(window.location.href); + const param = url.searchParams.get('appType'); if (/MicroMessenger/i.test(window.navigator.userAgent)) { - appType = 'wechatPublic'; + appType = param || 'wechatPublic'; } } const { result } = await this.cache.exec('getApplication', { diff --git a/lib/features/index.js b/lib/features/index.js index 98cb05892..2b40473b3 100644 --- a/lib/features/index.js +++ b/lib/features/index.js @@ -1,6 +1,7 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.initialize = exports.create = void 0; +exports.create = create; +exports.initialize = initialize; const tslib_1 = require("tslib"); const token_1 = require("./token"); const extraFile_1 = require("./extraFile"); @@ -37,7 +38,6 @@ function create(basicFeatures) { userWechatPublicTag, }; } -exports.create = create; async function initialize(features, access, config, clazzes) { await features.application.initialize((0, appVersion_1.oakGetPackageJsonVersion)(), access.http.hostname, undefined, config?.applicationExtraProjection); if (process.env.OAK_PLATFORM === 'web') { @@ -53,4 +53,3 @@ async function initialize(features, access, config, clazzes) { } } } -exports.initialize = initialize; diff --git a/lib/features/token.d.ts b/lib/features/token.d.ts index 70e8713fa..64c0abcd8 100644 --- a/lib/features/token.d.ts +++ b/lib/features/token.d.ts @@ -19,6 +19,7 @@ export declare class Token extends Feature { loginByAccount(account: string, password: string): Promise; bindByMobile(mobile: string, captcha?: string): Promise; bindByEmail(email: string, captcha?: string): Promise; + loginWebByMpToken(mpToken: string): Promise; loginByWechatInWebEnv(wechatLoginId: string): Promise; loginWechat(code: string, params?: { wechatLoginId?: string; diff --git a/lib/features/token.js b/lib/features/token.js index 8647ba84e..e4e53eb3c 100644 --- a/lib/features/token.js +++ b/lib/features/token.js @@ -145,6 +145,17 @@ class Token extends Feature_1.Feature { }, undefined, true); this.publish(); } + async loginWebByMpToken(mpToken) { + const env = await this.environment.getEnv(); + const { result } = await this.cache.exec('loginWebByMpToken', { + mpToken, + env, + }, undefined, true); + this.tokenValue = result; + await this.storage.save(constants_1.LOCAL_STORAGE_KEYS.token, result); + this.publish(); + this.checkNeedSetPassword(); + } async loginByWechatInWebEnv(wechatLoginId) { const env = await this.environment.getEnv(); const { result } = await this.cache.exec('loginByWechat', { diff --git a/src/aspects/AspectDict.ts b/src/aspects/AspectDict.ts index b6c8383cf..c8ab4be20 100644 --- a/src/aspects/AspectDict.ts +++ b/src/aspects/AspectDict.ts @@ -8,6 +8,13 @@ import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; import { WechatPublicEventData, WechatMpEventData } from 'oak-external-sdk'; export type AspectDict = { + loginWebByMpToken: ( + params: { + mpToken: string; + env: WebEnv; + }, + context: BackendRuntimeContext + ) => Promise; mergeUser: ( params: { from: string; to: string }, context: BackendRuntimeContext @@ -48,10 +55,10 @@ export type AspectDict = { verifyPassword: ( params: { password: string; - env: WebEnv | WechatMpEnv | NativeEnv; + env: WebEnv | WechatMpEnv | NativeEnv; }, context: BackendRuntimeContext - ) => Promise; + ) => Promise; loginByAccount: ( params: { account: string; diff --git a/src/aspects/index.ts b/src/aspects/index.ts index d5bb397a0..9eb402762 100644 --- a/src/aspects/index.ts +++ b/src/aspects/index.ts @@ -18,6 +18,7 @@ import { wakeupParasite, refreshToken, verifyPassword, + loginWebByMpToken, } from './token'; import { getInfoByUrl } from './extraFile'; import { @@ -134,6 +135,7 @@ const aspectDict = { getApplicationPassports, removeApplicationPassportsByPIds, verifyPassword, + loginWebByMpToken, }; export default aspectDict; diff --git a/src/aspects/token.ts b/src/aspects/token.ts index 4b0894265..61fbc36c7 100644 --- a/src/aspects/token.ts +++ b/src/aspects/token.ts @@ -91,9 +91,9 @@ async function makeDistinguishException(userId: string, c userId, !!(password || passwordSha1), idState === 'verified', - !! wechatUser$user?.length, - !! email$user?.length, - !! mobile$user?.length, + !!wechatUser$user?.length, + !!email$user?.length, + !!mobile$user?.length, message ); } @@ -741,12 +741,12 @@ export async function verifyPassword( }, ], } - }, { }); + }, {}); if (!user) { throw new OakUserException('error::user.passwordUnmatch'); } - + await context.operate('user', { id: await generateNewIdAsync(), action: 'update', @@ -1131,7 +1131,7 @@ export async function loginByAccount( assert(applicationPassport?.passport); const tokenValue = await loginLogic(); - const [ tokenInfo ] = await loadTokenInfo(tokenValue, context); + const [tokenInfo] = await loadTokenInfo(tokenValue, context); const { userId } = tokenInfo; await context.operate('user', { @@ -1141,7 +1141,7 @@ export async function loginByAccount( password, passwordSha1: encryptPasswordSha1(password), verifyPasswordAt: Date.now(), - }: { + } : { verifyPasswordAt: Date.now(), }, filter: { @@ -1325,7 +1325,7 @@ export async function bindByMobile( forUpdate: true, } ) - + if (boundMobile) { //用户已绑定的mobile与当前输入的mobile一致 if (boundMobile.mobile === mobile) { @@ -2270,8 +2270,8 @@ async function loginFromWechatEnv( { data: { id: 1, - userState: 1, - refId: 1, + userState: 1, + refId: 1, }, filter: { wechatUser$user: { @@ -3291,3 +3291,84 @@ export async function refreshToken( closeRootMode(); return tokenValue; } + +/** + * 使用微信小程序中的token登录web + * @param tokenValue + * @param env + * @param context + * @returns + */ +async function setupWechatMp(mpToken: string, env: WebEnv, context: BRC) { + const [token] = await context.select( + 'token', + { + data: { + id: 1, + entity: 1, + entityId: 1, + ableState: 1, + userId: 1, + user: { + id: 1, + userState: 1, + refId: 1, + ref: { + id: 1, + userState: 1, + refId: 1, + }, + wechatUser$user: { + $entity: 'wechatUser', + data: { + id: 1, + }, + }, + userSystem$user: { + $entity: 'userSystem', + data: { + id: 1, + systemId: 1, + }, + }, + }, + }, + filter: { + $or: [{ + value: mpToken, + + }, { + oldValue: mpToken, + }] + // ableState: 'enabled', + }, + }, + { dontCollect: true } + ); + assert(token); + const { user } = token; + return await setUpTokenAndUser( + env, + context, + token.entity!, + token.entityId, + undefined, + user as Partial + ); +} + +/** + * 小程序web-view处理token + * @param mpToken + * @param env + * @param context + * @returns + */ +export async function loginWebByMpToken(params: { mpToken: string, env: WebEnv, }, context: BRC): Promise { + const { mpToken, env } = params; + const closeRootMode = context.openRootMode(); + const tokenValue = await setupWechatMp(mpToken, env, context); + await loadTokenInfo(tokenValue, context); + closeRootMode(); + return tokenValue; +} \ No newline at end of file diff --git a/src/features/application.ts b/src/features/application.ts index 3b0428ae3..75a3526ee 100644 --- a/src/features/application.ts +++ b/src/features/application.ts @@ -86,11 +86,13 @@ export class Application extends Feature { appType = 'native'; } else { + const url = new URL(window.location.href); + const param = url.searchParams.get('appType') as AppType; if (/MicroMessenger/i.test(window.navigator.userAgent)) { - appType = 'wechatPublic'; + appType = param || 'wechatPublic'; } } - + const { result } = await this.cache.exec('getApplication', { version, type: appType, diff --git a/src/features/token.ts b/src/features/token.ts index 52c111d72..4c5870422 100644 --- a/src/features/token.ts +++ b/src/features/token.ts @@ -102,7 +102,7 @@ export class Token extends Feature { this.removeToken(true); } } - else { + else { this.checkNeedSetPassword(); } } catch (err) { @@ -223,6 +223,25 @@ export class Token extends Feature { this.publish(); } + async loginWebByMpToken( + mpToken: string, + ) { + const env = await this.environment.getEnv(); + const { result } = await this.cache.exec( + 'loginWebByMpToken', + { + mpToken, + env, + }, + undefined, + true + ); + this.tokenValue = result; + await this.storage.save(LOCAL_STORAGE_KEYS.token, result); + this.publish(); + this.checkNeedSetPassword(); + } + async loginByWechatInWebEnv(wechatLoginId: string) { const env = await this.environment.getEnv(); const { result } = await this.cache.exec('loginByWechat', { @@ -324,7 +343,7 @@ export class Token extends Feature { return this.tokenValue; } - getToken(allowUnloggedIn?: boolean) { + getToken(allowUnloggedIn?: boolean) { if (this.tokenValue === '') { throw new OakUserInfoLoadingException(); }