oak-general-business/lib/aspects/wechatUser.js

71 lines
2.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unbindingWechat = unbindingWechat;
const types_1 = require("oak-domain/lib/types");
const uuid_1 = require("oak-domain/lib/utils/uuid");
const assert_1 = require("oak-domain/lib/utils/assert");
async function unbindingWechat(params, context) {
const { wechatUserId, captcha, mobile } = params;
const fn = async () => {
const userId = context.getCurrentUserId();
// 校验传入的wechatUser.userId 是否和当前登录者的id相等
const [wechatUser] = await context.select('wechatUser', {
data: {
id: 1,
userId: 1,
},
filter: {
id: wechatUserId,
}
}, {});
(0, assert_1.assert)(wechatUser.userId === userId, '已绑定微信的用户与当前登录者不一致');
await context.operate('wechatUser', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
userId: null,
},
filter: {
id: wechatUserId,
}
}, {
dontCollect: true,
});
};
if (mobile && captcha) {
const result = await context.select('captcha', {
data: {
id: 1,
expired: 1,
},
filter: {
origin: 'mobile',
content: mobile,
code: captcha,
// TODO: 这里的type暂未确定如果需要发验证码需要再添加一个验证码类型
},
sorter: [{
$attr: {
$$createAt$$: 1,
},
$direction: 'desc',
}],
indexFrom: 0,
count: 1,
}, { dontCollect: true });
if (result.length > 0) {
const [captchaRow] = result;
if (captchaRow.expired) {
throw new types_1.OakUserException('验证码已经过期');
}
await fn();
}
else {
throw new types_1.OakUserException('验证码无效');
}
}
else {
await fn();
}
}