增加使用小程序tokenValue登录web(小程序web-view使用)
This commit is contained in:
parent
e05ccb9243
commit
f42737c5db
|
|
@ -7,6 +7,10 @@ import { MaterialType } from '../types/WeChat';
|
|||
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
|
||||
import { WechatPublicEventData, WechatMpEventData } from 'oak-external-sdk';
|
||||
export type AspectDict<ED extends EntityDict> = {
|
||||
loginWebByMpToken: (params: {
|
||||
mpToken: string;
|
||||
env: WebEnv;
|
||||
}, context: BackendRuntimeContext<ED>) => Promise<string>;
|
||||
mergeUser: (params: {
|
||||
from: string;
|
||||
to: string;
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -114,3 +114,14 @@ export declare function refreshToken<ED extends EntityDict>(params: {
|
|||
tokenValue: string;
|
||||
applicationId: string;
|
||||
}, context: BRC<ED>): Promise<string>;
|
||||
/**
|
||||
* 小程序web-view处理token
|
||||
* @param mpToken
|
||||
* @param env
|
||||
* @param context
|
||||
* @returns
|
||||
*/
|
||||
export declare function loginWebByMpToken<ED extends EntityDict>(params: {
|
||||
mpToken: string;
|
||||
env: WebEnv;
|
||||
}, context: BRC<ED>): Promise<string>;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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', {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ export declare class Token<ED extends EntityDict> extends Feature {
|
|||
loginByAccount(account: string, password: string): Promise<void>;
|
||||
bindByMobile(mobile: string, captcha?: string): Promise<void>;
|
||||
bindByEmail(email: string, captcha?: string): Promise<void>;
|
||||
loginWebByMpToken(mpToken: string): Promise<void>;
|
||||
loginByWechatInWebEnv(wechatLoginId: string): Promise<void>;
|
||||
loginWechat(code: string, params?: {
|
||||
wechatLoginId?: string;
|
||||
|
|
|
|||
|
|
@ -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', {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@ import { MaterialType } from '../types/WeChat';
|
|||
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
|
||||
import { WechatPublicEventData, WechatMpEventData } from 'oak-external-sdk';
|
||||
export type AspectDict<ED extends EntityDict> = {
|
||||
loginWebByMpToken: (params: {
|
||||
mpToken: string;
|
||||
env: WebEnv;
|
||||
}, context: BackendRuntimeContext<ED>) => Promise<string>;
|
||||
mergeUser: (params: {
|
||||
from: string;
|
||||
to: string;
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -114,3 +114,14 @@ export declare function refreshToken<ED extends EntityDict>(params: {
|
|||
tokenValue: string;
|
||||
applicationId: string;
|
||||
}, context: BRC<ED>): Promise<string>;
|
||||
/**
|
||||
* 小程序web-view处理token
|
||||
* @param mpToken
|
||||
* @param env
|
||||
* @param context
|
||||
* @returns
|
||||
*/
|
||||
export declare function loginWebByMpToken<ED extends EntityDict>(params: {
|
||||
mpToken: string;
|
||||
env: WebEnv;
|
||||
}, context: BRC<ED>): Promise<string>;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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', {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ export declare class Token<ED extends EntityDict> extends Feature {
|
|||
loginByAccount(account: string, password: string): Promise<void>;
|
||||
bindByMobile(mobile: string, captcha?: string): Promise<void>;
|
||||
bindByEmail(email: string, captcha?: string): Promise<void>;
|
||||
loginWebByMpToken(mpToken: string): Promise<void>;
|
||||
loginByWechatInWebEnv(wechatLoginId: string): Promise<void>;
|
||||
loginWechat(code: string, params?: {
|
||||
wechatLoginId?: string;
|
||||
|
|
|
|||
|
|
@ -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', {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,13 @@ import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
|
|||
import { WechatPublicEventData, WechatMpEventData } from 'oak-external-sdk';
|
||||
|
||||
export type AspectDict<ED extends EntityDict> = {
|
||||
loginWebByMpToken: (
|
||||
params: {
|
||||
mpToken: string;
|
||||
env: WebEnv;
|
||||
},
|
||||
context: BackendRuntimeContext<ED>
|
||||
) => Promise<string>;
|
||||
mergeUser: (
|
||||
params: { from: string; to: string },
|
||||
context: BackendRuntimeContext<ED>
|
||||
|
|
@ -48,10 +55,10 @@ export type AspectDict<ED extends EntityDict> = {
|
|||
verifyPassword: (
|
||||
params: {
|
||||
password: string;
|
||||
env: WebEnv | WechatMpEnv | NativeEnv;
|
||||
env: WebEnv | WechatMpEnv | NativeEnv;
|
||||
},
|
||||
context: BackendRuntimeContext<ED>
|
||||
) => Promise<void>;
|
||||
) => Promise<void>;
|
||||
loginByAccount: (
|
||||
params: {
|
||||
account: string;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -91,9 +91,9 @@ async function makeDistinguishException<ED extends EntityDict>(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<ED extends EntityDict>(
|
|||
},
|
||||
],
|
||||
}
|
||||
}, { });
|
||||
}, {});
|
||||
|
||||
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<ED extends EntityDict>(
|
|||
assert(applicationPassport?.passport);
|
||||
|
||||
const tokenValue = await loginLogic();
|
||||
const [ tokenInfo ] = await loadTokenInfo<ED>(tokenValue, context);
|
||||
const [tokenInfo] = await loadTokenInfo<ED>(tokenValue, context);
|
||||
const { userId } = tokenInfo;
|
||||
|
||||
await context.operate('user', {
|
||||
|
|
@ -1141,7 +1141,7 @@ export async function loginByAccount<ED extends EntityDict>(
|
|||
password,
|
||||
passwordSha1: encryptPasswordSha1(password),
|
||||
verifyPasswordAt: Date.now(),
|
||||
}: {
|
||||
} : {
|
||||
verifyPasswordAt: Date.now(),
|
||||
},
|
||||
filter: {
|
||||
|
|
@ -1325,7 +1325,7 @@ export async function bindByMobile<ED extends EntityDict>(
|
|||
forUpdate: true,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if (boundMobile) {
|
||||
//用户已绑定的mobile与当前输入的mobile一致
|
||||
if (boundMobile.mobile === mobile) {
|
||||
|
|
@ -2270,8 +2270,8 @@ async function loginFromWechatEnv<ED extends EntityDict>(
|
|||
{
|
||||
data: {
|
||||
id: 1,
|
||||
userState: 1,
|
||||
refId: 1,
|
||||
userState: 1,
|
||||
refId: 1,
|
||||
},
|
||||
filter: {
|
||||
wechatUser$user: {
|
||||
|
|
@ -3291,3 +3291,84 @@ export async function refreshToken<ED extends EntityDict>(
|
|||
closeRootMode();
|
||||
return tokenValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用微信小程序中的token登录web
|
||||
* @param tokenValue
|
||||
* @param env
|
||||
* @param context
|
||||
* @returns
|
||||
*/
|
||||
async function setupWechatMp<ED extends EntityDict>(mpToken: string, env: WebEnv, context: BRC<ED>) {
|
||||
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<ED>(
|
||||
env,
|
||||
context,
|
||||
token.entity!,
|
||||
token.entityId,
|
||||
undefined,
|
||||
user as Partial<ED['user']['Schema']>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序web-view处理token
|
||||
* @param mpToken
|
||||
* @param env
|
||||
* @param context
|
||||
* @returns
|
||||
*/
|
||||
export async function loginWebByMpToken<ED extends EntityDict>(params: { mpToken: string, env: WebEnv, }, context: BRC<ED>): Promise<string> {
|
||||
const { mpToken, env } = params;
|
||||
const closeRootMode = context.openRootMode();
|
||||
const tokenValue = await setupWechatMp<ED>(mpToken, env, context);
|
||||
await loadTokenInfo<ED>(tokenValue, context);
|
||||
closeRootMode();
|
||||
return tokenValue;
|
||||
}
|
||||
|
|
@ -86,11 +86,13 @@ export class Application<ED extends EntityDict> 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,
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ export class Token<ED extends EntityDict> extends Feature {
|
|||
this.removeToken(true);
|
||||
}
|
||||
}
|
||||
else {
|
||||
else {
|
||||
this.checkNeedSetPassword();
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
@ -223,6 +223,25 @@ export class Token<ED extends EntityDict> 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<ED extends EntityDict> extends Feature {
|
|||
return this.tokenValue;
|
||||
}
|
||||
|
||||
getToken(allowUnloggedIn?: boolean) {
|
||||
getToken(allowUnloggedIn?: boolean) {
|
||||
if (this.tokenValue === '') {
|
||||
throw new OakUserInfoLoadingException();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue