fix: add wx-login check

This commit is contained in:
2025-04-09 01:23:08 +08:00
parent 197d6415d3
commit e5111227fd
9 changed files with 188 additions and 18 deletions

View File

@@ -5,7 +5,7 @@
"main": "index.js",
"basename": "/root/wx-app-services",
"app": {
"key": "wx-app",
"key": "wx-app-services",
"entry": "dist/app.mjs",
"type": "system-app",
"files": [

View File

@@ -24,10 +24,10 @@ export type WxToken = {
* @param code
* @returns
*/
export const fetchToken = async (code: string): Promise<WxToken> => {
export const fetchToken = async (code: string, type: 'open' | 'mp' = 'open'): Promise<WxToken> => {
let appId = config.wxOpen.appId;
let appSecret = config.wxOpen.appSecret;
if (!appId && !appSecret) {
if (type === 'mp') {
appId = config.wx.appId;
appSecret = config.wx.appSecret;
}

View File

@@ -68,7 +68,7 @@ app
const code = ctx.query.code;
try {
const wx = new WxServices();
const token = await wx.login(code);
const token = await wx.login(code, 'mp');
const redis = useContextKey('redis');
await redis.set(`wx:mp:login:${state}`, token, 'EX', 10000); // 30秒过期
ctx.body = {
@@ -81,6 +81,24 @@ app
})
.addTo(app);
app
.route({
path: 'wx',
key: 'mp-get-openid',
isDebug: true,
})
.define(async (ctx) => {
const code = ctx.query.code;
if (!code) {
ctx.throw(400, 'code is required');
return;
}
const wx = new WxServices();
const mpInfo = await wx.getOpenid(code, 'mp');
ctx.body = mpInfo;
})
.addTo(app);
app
.route({
path: 'wx',

View File

@@ -36,8 +36,22 @@ export class WxServices {
}
return random;
}
async login(code: string) {
const token = await fetchToken(code);
/**
* 获取openid
* @param code
* @returns
*/
async getOpenid(code: string, type: 'mp' | 'open' = 'open') {
const token = await fetchToken(code, type);
console.log('login token', token);
return {
openid: token.openid,
scope: token.scope,
unionid: token.unionid,
};
}
async login(code: string, type: 'mp' | 'open' = 'open') {
const token = await fetchToken(code, type);
console.log('login token', token);
if (!token.unionid) {
throw new CustomError(400, 'code is invalid, wxdata can not be found');
@@ -52,32 +66,49 @@ export class WxServices {
},
});
// @ts-ignore
if (user && user.data.wxOpenid !== token.openid) {
if (type === 'open' && user && user.data.wxOpenid !== token.openid) {
user.data = {
...user.data,
// @ts-ignore
wxOpenid: token.openid,
};
await user.update({ data: user.data });
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) {
user.data = {
...user.data,
// @ts-ignore
wxmpOpenid: token.openid,
};
user = await user.update({ data: user.data });
}
if (!user) {
const username = await this.randomUsername();
user = await User.createUser(username, nanoid(10));
user.data = {
let data = {
...user.data,
// @ts-ignore
wxOpenid: token.openid,
wxUnionId: unionid,
};
await user.save({ fields: ['data'] });
user.data = data;
if ((type = 'mp')) {
// @ts-ignore
data.wxmpOpenid = token.openid;
} else {
// @ts-ignore
data.wxOpenid = token.openid;
}
this.user = await user.save({ fields: ['data'] });
this.getUserInfo();
this.isNew = true;
}
this.user = user;
const tokenInfo = await user.createToken(null, 'plugin', {
wx: {
openid: token.openid,
unionid: unionid,
type,
},
});
this.webToken = tokenInfo.accessToken;
@@ -107,7 +138,12 @@ export class WxServices {
} catch (error) {
console.error('Error downloading or converting image:', error);
}
await this.user.save();
const data = {
...this.user.data,
wxUserInfo: userInfo,
};
this.user.data = data;
await this.user.save({ fields: ['data', 'nickname', 'avatar'] });
} catch (error) {
console.error('Error getting user info:', error);
}
@@ -121,7 +157,7 @@ export class WxServices {
return await downloadImag(url);
} catch (error) {
console.error('Error downloading or converting image:', error);
throw error;
return '';
}
}
}