This commit is contained in:
2025-12-04 14:22:04 +08:00
parent 2a55f2d3ef
commit 9e458f4a77
17 changed files with 449 additions and 143 deletions

View File

@@ -68,16 +68,7 @@ export class WxServices {
},
});
// @ts-ignore
if (type === 'open' && user && user.data.wxOpenid !== token.openid) {
user.data = {
...user.data,
// @ts-ignore
wxOpenid: token.openid,
};
user = await user.update({ data: user.data });
console.log('mp-user login openid update=============', token.openid, token.unionid);
// @ts-ignore
} else if (type === 'mp' && user && user.data.wxmpOpenid !== token.openid) {
if (type === 'mp' && user && user.data.wxmpOpenid !== token.openid) {
user.data = {
...user.data,
// @ts-ignore
@@ -94,7 +85,7 @@ export class WxServices {
canChangeUsername: true,
};
user.data = data;
if ((type = 'mp')) {
if (type === 'mp') {
// @ts-ignore
data.wxmpOpenid = token.openid;
} else {

View File

@@ -1,7 +1,7 @@
import { Op } from 'sequelize';
import { User, UserSecret } from '@/models/user.ts';
import { app } from '@/app.ts';
import { redis } from '@/app.ts';
app
.route({
path: 'secret',
@@ -10,7 +10,7 @@ app
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { page = 1, pageSize = 100, search, sort = 'DESC', orgId } = ctx.query;
const { page = 1, pageSize = 100, search, sort = 'DESC', orgId, showToken = false } = ctx.query;
const searchWhere: Record<string, any> = search
? {
[Op.or]: [{ title: { [Op.like]: `%${search}%` } }, { description: { [Op.like]: `%${search}%` } }],
@@ -18,7 +18,10 @@ app
: {};
if (orgId) {
searchWhere.orgId = orgId;
} else {
searchWhere.orgId = null;
}
const excludeFields = showToken ? [] : ['token'];
const { rows: secrets, count } = await UserSecret.findAndCountAll({
where: {
userId: tokenUser.userId,
@@ -27,7 +30,7 @@ app
offset: (page - 1) * pageSize,
limit: pageSize,
attributes: {
exclude: ['token'], // Exclude sensitive token field
exclude: excludeFields, // Exclude sensitive token field
},
order: [['updatedAt', sort]],
});
@@ -166,3 +169,52 @@ app
ctx.body = secret;
})
.addTo(app);
app.route({
path: 'secret',
key: 'wxnotify',
description: '为了微信去缓存需要的数据, unionid是公众号下的用户的unionid',
}).define(async (ctx) => {
const { openid, unionid } = ctx.query;
if (!openid && !unionid) {
// ctx.throw(400, '需要提供 openid 或者 unionid 参数');
ctx.throw(400, '需要提供 unionid 参数');
}
// 最少20为的openid
if (unionid.length < 20) {
ctx.throw(400, 'unionid 是必填的');
}
const redisKey = UserSecret.wxRedisKey(unionid);
const token = await redis.get(redisKey);
if (token) {
ctx.body = 'success'
return;
}
const user = await User.findOne({
where: {
data: {
wxUnionId: unionid
}
}
})
if (!user) {
ctx.throw(404, '请关注公众号《人生可视化助手》后再操作');
return
}
let secretKey = await UserSecret.findOne({
where: {
userId: user.id,
title: 'wxmp-notify-token'
}
});
if (!secretKey) {
secretKey = await UserSecret.createSecret({ id: user.id, title: 'wxmp-notify-token' });
}
const check = await secretKey.checkOnUse();
if (check.code !== 200) {
ctx.throw(check.code, check.message);
}
await redis.set(redisKey, secretKey.token, 'EX', 30 * 24 * 60 * 60); // 30天过期
ctx.body = 'success'
}).addTo(app);