129 lines
3.2 KiB
TypeScript
129 lines
3.2 KiB
TypeScript
import { app } from '@/app.ts';
|
||
import { useContextKey } from '@kevisual/use-config/context';
|
||
import { WxServices } from './services.ts';
|
||
import { config } from '@/modules/config.ts';
|
||
export const createCookie = async (token: any, ctx: any) => {
|
||
if (!config.domain) {
|
||
return;
|
||
}
|
||
//TODO, 获取访问的 hostname, 如果访问的和 domain 的不一致,也创建cookie
|
||
const browser = ctx.req.headers['user-agent'];
|
||
const isBrowser = browser.includes('Mozilla'); // 浏览器
|
||
if (isBrowser && ctx.res.cookie) {
|
||
// const reqDomain = ctx.req?.headers?.host;
|
||
// if (reqDomain !== config.domain) {
|
||
// const redis = await useContextKey('redis');
|
||
// if (!redis) {
|
||
// console.error('redis is not set');
|
||
// return;
|
||
// }
|
||
// const getCacheToken = await redis.get(`login:check:domain:${reqDomain}`);
|
||
// if (getCacheToken) {
|
||
// ctx.res.cookie('token', getCacheToken, {
|
||
// maxAge: 7 * 24 * 60 * 60 * 1000, // 过期时间, 设置7天
|
||
// domain: config.domain,
|
||
// sameSite: 'lax',
|
||
// httpOnly: true,
|
||
// });
|
||
// }
|
||
// }
|
||
ctx.res.cookie('token', token.accessToken || token?.token, {
|
||
maxAge: 7 * 24 * 60 * 60 * 1000, // 过期时间, 设置7天
|
||
domain: config.domain,
|
||
sameSite: 'lax',
|
||
httpOnly: true,
|
||
});
|
||
}
|
||
};
|
||
app
|
||
.route({
|
||
path: 'wx',
|
||
key: 'checkLogin',
|
||
})
|
||
.define(async (ctx) => {
|
||
const state = ctx.query.state;
|
||
if (!state) {
|
||
ctx.throw(400, 'state is required');
|
||
return;
|
||
}
|
||
const redis = useContextKey('redis');
|
||
const token = await redis.get(`wx:mp:login:${state}`);
|
||
if (!token) {
|
||
ctx.throw(400, 'Invalid state');
|
||
return;
|
||
}
|
||
try {
|
||
ctx.body = JSON.parse(token);
|
||
} catch (error) {
|
||
ctx.throw(500, 'Invalid token get');
|
||
}
|
||
})
|
||
.addTo(app);
|
||
|
||
app
|
||
.route({
|
||
path: 'wx',
|
||
key: 'mplogin',
|
||
})
|
||
.define(async (ctx) => {
|
||
const state = ctx.query.state;
|
||
const code = ctx.query.code;
|
||
try {
|
||
const wx = new WxServices();
|
||
const token = await wx.login(code, 'mp');
|
||
const redis = useContextKey('redis');
|
||
await redis.set(`wx:mp:login:${state}`, JSON.stringify(token), 'EX', 10000); // 30秒过期
|
||
ctx.body = {
|
||
token,
|
||
};
|
||
} catch (error) {
|
||
console.error(error);
|
||
ctx.throw(500, 'Invalid code');
|
||
}
|
||
})
|
||
.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',
|
||
key: 'open-login',
|
||
isDebug: true,
|
||
})
|
||
.define(async (ctx) => {
|
||
const code = ctx.query.code;
|
||
const wx = new WxServices();
|
||
if (!code) {
|
||
ctx.throw(400, 'code is required');
|
||
return;
|
||
}
|
||
try {
|
||
const token = await wx.login(code);
|
||
ctx.body = token;
|
||
if (!token.accessToken) {
|
||
ctx.throw(500, 'Invalid code');
|
||
}
|
||
} catch (error) {
|
||
console.error(error);
|
||
ctx.throw(500, 'Invalid code');
|
||
}
|
||
})
|
||
.addTo(app);
|