wechat中的一些小问题

This commit is contained in:
Xu Chang 2023-01-30 21:14:31 +08:00
parent 82641f660c
commit 4ab636890f
10 changed files with 186 additions and 2 deletions

View File

@ -5,6 +5,10 @@ import { QiniuUploadInfo } from "oak-frontend-base/lib/types/Upload";
import { Config, Origin } from "../types/Config";
import { BackendRuntimeContext } from "../context/BackendRuntimeContext";
declare type GeneralAspectDict<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>> = {
mergeUser: (params: {
from: string;
to: string;
}, context: Cxt) => Promise<void>;
loginByMobile: (params: {
captcha?: string;
password?: string;

View File

@ -2,7 +2,9 @@ import { loginByMobile, loginWechat, loginWechatMp, syncUserInfoWechatMp, sendCa
import { getUploadInfo } from './extraFile';
import { getApplication } from './application';
import { updateConfig, updateApplicationConfig } from './config';
import { mergeUser } from './user';
export declare const aspectDict: {
mergeUser: typeof mergeUser;
switchTo: typeof switchTo;
loginByMobile: typeof loginByMobile;
loginWechat: typeof loginWechat;

View File

@ -5,7 +5,9 @@ var token_1 = require("./token");
var extraFile_1 = require("./extraFile");
var application_1 = require("./application");
var config_1 = require("./config");
var user_1 = require("./user");
exports.aspectDict = {
mergeUser: user_1.mergeUser,
switchTo: token_1.switchTo,
loginByMobile: token_1.loginByMobile,
loginWechat: token_1.loginWechat,

6
lib/aspects/user.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
import { BackendRuntimeContext } from "../context/BackendRuntimeContext";
import { EntityDict } from "../general-app-domain";
export declare function mergeUser<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>(params: {
from: string;
to: string;
}, context: Cxt, innerLogic?: boolean): Promise<void>;

94
lib/aspects/user.js Normal file
View File

@ -0,0 +1,94 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeUser = void 0;
var tslib_1 = require("tslib");
var types_1 = require("oak-domain/lib/types");
function mergeUser(params, context, innerLogic) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var from, to, schema, _a, _b, _i, entity, entityDesc, attributes, _c, _d, _e, attr, attrDef;
var _f, _g;
return tslib_1.__generator(this, function (_h) {
switch (_h.label) {
case 0:
if (!innerLogic && !context.isRoot()) {
throw new types_1.OakUserUnpermittedException('不允许执行mergeUser操作');
}
from = params.from, to = params.to;
schema = context.getSchema();
_a = [];
for (_b in schema)
_a.push(_b);
_i = 0;
_h.label = 1;
case 1:
if (!(_i < _a.length)) return [3 /*break*/, 8];
entity = _a[_i];
if (['oper', 'modi', 'operEntity', 'modiEntity', 'userEntityGrant', 'wechatQrCode'].includes(entity)) {
return [3 /*break*/, 7];
}
entityDesc = schema[entity];
if (entityDesc.view) {
return [3 /*break*/, 7];
}
attributes = entityDesc.attributes;
_c = [];
for (_d in attributes)
_c.push(_d);
_e = 0;
_h.label = 2;
case 2:
if (!(_e < _c.length)) return [3 /*break*/, 7];
attr = _c[_e];
attrDef = attributes[attr];
if (!(attrDef.type === 'ref' && attrDef.ref === 'user')) return [3 /*break*/, 4];
return [4 /*yield*/, context.operate(entity, {
action: 'update',
data: (_f = {},
_f[attr] = to,
_f),
filter: (_g = {},
_g[attr] = from,
_g)
}, { dontCollect: true, dontCreateOper: true, dontCreateModi: true })];
case 3:
_h.sent();
_h.label = 4;
case 4:
if (!(attr === 'entity' && attributes.hasOwnProperty('entityId'))) return [3 /*break*/, 6];
return [4 /*yield*/, context.operate(entity, {
action: 'update',
data: {
entityId: to,
},
filter: {
entity: 'user',
entityId: from,
}
}, { dontCollect: true, dontCreateOper: true, dontCreateModi: true })];
case 5:
_h.sent();
_h.label = 6;
case 6:
_e++;
return [3 /*break*/, 2];
case 7:
_i++;
return [3 /*break*/, 1];
case 8: return [4 /*yield*/, context.operate('user', {
action: 'merge',
data: {
refId: to,
userState: 'merged',
},
filter: {
id: from,
}
}, {})];
case 9:
_h.sent();
return [2 /*return*/];
}
});
});
}
exports.mergeUser = mergeUser;

View File

@ -111,7 +111,7 @@ function setUserSubscribed(openId, eventKey, context) {
list = _h.sent();
now = Date.now();
data = {
activeAt: now,
// activeAt: now,
};
doUpdate = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var wechatUser;
@ -208,6 +208,13 @@ function setUserSubscribed(openId, eventKey, context) {
case 4: return [4 /*yield*/, context.select('userEntityGrant', {
data: {
id: 1,
granter: {
id: 1,
name: 1,
nickname: 1,
},
expired: 1,
entity: 1,
},
filter: {
id: entityId,

View File

@ -12,6 +12,7 @@ type GeneralAspectDict<
ED extends EntityDict,
Cxt extends BackendRuntimeContext<ED>
> = {
mergeUser: (params: { from: string, to: string }, context: Cxt) => Promise<void>;
loginByMobile: (
params: {
captcha?: string;

View File

@ -9,8 +9,10 @@ import {
import { getUploadInfo } from './extraFile';
import { getApplication } from './application';
import { updateConfig, updateApplicationConfig } from './config';
import { mergeUser } from './user';
export const aspectDict = {
mergeUser,
switchTo,
loginByMobile,
loginWechat,

59
src/aspects/user.ts Normal file
View File

@ -0,0 +1,59 @@
import { OakUserUnpermittedException } from "oak-domain/lib/types";
import { BackendRuntimeContext } from "../context/BackendRuntimeContext";
import { EntityDict } from "../general-app-domain";
export async function mergeUser<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>(params: { from: string, to: string }, context: Cxt, innerLogic?: boolean) {
if (!innerLogic && !context.isRoot()) {
throw new OakUserUnpermittedException('不允许执行mergeUser操作');
}
const { from, to } = params;
const schema = context.getSchema();
for (const entity in schema) {
if (['oper', 'modi', 'operEntity', 'modiEntity', 'userEntityGrant', 'wechatQrCode'].includes(entity)) {
continue;
}
const entityDesc = schema[entity];
if (entityDesc.view) {
continue;
}
const { attributes } = entityDesc;
for (const attr in attributes) {
const attrDef = attributes[attr as keyof typeof attributes];
if (attrDef.type === 'ref' && attrDef.ref === 'user') {
await context.operate(entity, {
action: 'update',
data: {
[attr]: to,
},
filter: {
[attr]: from,
}
} as any, { dontCollect: true, dontCreateOper: true, dontCreateModi: true });
}
if (attr === 'entity' && attributes.hasOwnProperty('entityId')) {
await context.operate(entity, {
action: 'update',
data: {
entityId: to,
},
filter: {
entity: 'user',
entityId: from,
}
} as any, { dontCollect: true, dontCreateOper: true, dontCreateModi: true });
}
}
}
await context.operate('user', {
action: 'merge',
data: {
refId: to,
userState: 'merged',
},
filter: {
id: from,
}
}, {});
}

View File

@ -126,7 +126,7 @@ async function setUserSubscribed(openId: string, eventKey: string, context: BRC)
const now = Date.now();
const data = {
activeAt: now,
// activeAt: now,
};
const doUpdate = async () => {
@ -223,6 +223,13 @@ async function setUserSubscribed(openId: string, eventKey: string, context: BRC)
{
data: {
id: 1,
granter: {
id: 1,
name: 1,
nickname: 1,
},
expired: 1,
entity: 1,
},
filter: {
id: entityId,