temp
This commit is contained in:
@@ -1,8 +1,30 @@
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
import { app } from './index.ts';
|
||||
|
||||
import { useContext } from '@kevisual/use-config/context';
|
||||
import { sequelize, User, OrgInit, UserInit } from '@kevisual/code-center-module';
|
||||
const config = useConfig();
|
||||
|
||||
const context = useContext();
|
||||
|
||||
// http://localhost:3006/api/router?path=user&key=list
|
||||
setTimeout(() => {
|
||||
const org = OrgInit();
|
||||
console.log('org', org);
|
||||
const user = UserInit();
|
||||
console.log('user', user);
|
||||
}, 2000);
|
||||
app
|
||||
.route({
|
||||
path: 'user',
|
||||
key: 'list',
|
||||
isDebug: true,
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const u = await User.findOne();
|
||||
ctx.body = u;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app.listen(config.port, () => {
|
||||
console.log(`server is running at http://localhost:${config.port}`);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { app } from './app.ts';
|
||||
import './demo-route.ts';
|
||||
import './routes/wx/login.ts';
|
||||
|
||||
export { app };
|
||||
44
app/src/routes/wx/login.ts
Normal file
44
app/src/routes/wx/login.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { useContextKey } from '@kevisual/use-config/context';
|
||||
import { WxServices } from './services.ts';
|
||||
app
|
||||
.route({
|
||||
path: 'wx',
|
||||
key: 'checkLogin',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const state = ctx.query.state;
|
||||
const redis = useContextKey('redis');
|
||||
const token = await redis.get(`wx:mp:login:${state}`);
|
||||
if (!token) {
|
||||
ctx.throw(400, 'Invalid state');
|
||||
return;
|
||||
}
|
||||
ctx.body = {
|
||||
token,
|
||||
};
|
||||
})
|
||||
.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);
|
||||
const redis = useContextKey('redis');
|
||||
await redis.set(`wx:mp:login:${state}`, token, 'EX', 10000); // 30秒过期
|
||||
ctx.body = {
|
||||
token,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
ctx.throw(500, 'Invalid code');
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
@@ -1,13 +1,15 @@
|
||||
import { WxTokenResponse, fetchToken, getUserInfo } from '@/modules/wx.ts';
|
||||
import { useContextKey } from '@kevisual/use-config/context';
|
||||
|
||||
import { UserModel } from '@kevisual/code-center-module';
|
||||
import { Buffer } from 'buffer';
|
||||
const User = useContextKey<typeof User>('user');
|
||||
|
||||
const User = useContextKey<typeof UserModel>('UserModel');
|
||||
export class WxServices {
|
||||
token: WxTokenResponse;
|
||||
// 创建一个webToken,用户登录
|
||||
webToken: string;
|
||||
isNew: boolean;
|
||||
// @ts-ignore
|
||||
user: User;
|
||||
constructor() {
|
||||
//
|
||||
@@ -24,9 +26,10 @@ export class WxServices {
|
||||
user = await User.createUser(unionid, unionid.slice(0, 8));
|
||||
this.isNew = true;
|
||||
}
|
||||
const tokenInfo = await user.createToken();
|
||||
const tokenInfo = await user.createToken(null, 'plugin');
|
||||
this.webToken = tokenInfo.token;
|
||||
this.user = user;
|
||||
return this.webToken;
|
||||
}
|
||||
|
||||
async checkHasUser() {}
|
||||
|
||||
Reference in New Issue
Block a user