import { WxServices } from "./modules/wx-services.ts"; import { app, redis } from "@/app.ts"; app .route({ path: 'wx', key: 'checkLogin', description: '微信网页登录后获取登录结果(遗弃)', }) .define(async (ctx) => { const state = ctx.query.state; if (!state) { ctx.throw(400, 'state is required'); return; } 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', description: '微信网页登录后提交code去登录(遗弃)', }) .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'); 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', description: '微信公众平台获取openid', }) .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', description: '微信开放平台登录', }) .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); app.route({ path: 'wx', key: 'get-qrcode-ticket', description: '获取微信二维码ticket', }) .define(async (ctx) => { const wx = new WxServices(); const res = await wx.getQrCodeTicket(); if (!res) { ctx.throw(500, 'Get qrcode ticket failed'); return; } const key = `wx:mp:login:qrcode:${res.ticket}`; await redis.set(key, '-', 'EX', 360); // 6分钟过期 ctx.body = res; }) .addTo(app); app.route({ path: 'wx', key: 'check-qrcode-login', description: '检查微信二维码登录状态', }) .define(async (ctx) => { const ticket = ctx.query.ticket; if (!ticket) { ctx.throw(400, 'ticket is required'); return; } const token = await redis.get(`wx:mp:login:qrcode:${ticket}`); if (!token) { ctx.throw(400, 'Invalid ticket'); return; } if (token === '-') { ctx.throw(400, 'Not scanned yet'); return; } try { // remove the token after getting it await redis.del(`wx:mp:login:qrcode:${ticket}`); ctx.body = JSON.parse(token); } catch (error) { ctx.throw(500, 'Invalid token get'); } }) .addTo(app); app.route({ path: 'wx', key: 'login-by-ticket', description: '通过ticket登录微信(扫码登录回调)', }) .define(async (ctx) => { const { openid, ticket } = ctx.query || {}; if (!openid || !ticket) { console.error('openid and ticket are required'); ctx.throw(400, 'openid and ticket are required'); return; } const wx = new WxServices(); const result = await wx.loginByTicket({ openid, ticket }); ctx.body = result; }) .addTo(app);