use mysql to user and room data
This commit is contained in:
@@ -22,5 +22,19 @@ export class Account extends BaseController {
|
||||
this.ctx.logger.error('login-----:', e);
|
||||
this.fail(e)
|
||||
}
|
||||
};
|
||||
|
||||
@post('/register')
|
||||
async register() {
|
||||
try {
|
||||
const { body } = this.getRequestBody();
|
||||
const { userAccount, password, nickName } = body;
|
||||
const accountInfo: IAccountInfo = {userAccount, password, nickName};
|
||||
const result = await this.service.register(accountInfo);
|
||||
this.success(result)
|
||||
} catch (e) {
|
||||
this.ctx.logger.error('login-----:', e);
|
||||
this.fail(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,17 +12,26 @@ export class RoomController extends BaseController {
|
||||
@inject('RoomService')
|
||||
roomService: IRoomService;
|
||||
/**
|
||||
* 处理ocr数据转发
|
||||
*
|
||||
*/
|
||||
@post('/')
|
||||
async index() {
|
||||
try {
|
||||
const result = await this.roomService.add();
|
||||
if(result.affectedRows === 1) {
|
||||
this.success({ username: 'cai'});
|
||||
} else {
|
||||
this.fail('create room error');
|
||||
}
|
||||
this.success(result);
|
||||
} catch (e) {
|
||||
this.fail('create room error');
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
@post('/find')
|
||||
async find() {
|
||||
try {
|
||||
const { body } = this.getRequestBody();
|
||||
const result = await this.roomService.findByRoomNumber(body.roomNumber);
|
||||
console.log(result, 'roomNumber');
|
||||
this.success(result);
|
||||
} catch (e) {
|
||||
this.fail('create room error');
|
||||
console.log(e)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Context, inject, controller, post, provide } from 'midway';
|
||||
import { Context, inject, controller, post, provide, plugin } from 'midway';
|
||||
import BaseController from '../../lib/baseController';
|
||||
import { IUserService } from '../../interface/IUserService';
|
||||
|
||||
@provide()
|
||||
@controller('/node/user')
|
||||
@@ -7,18 +8,25 @@ export class UserController extends BaseController {
|
||||
|
||||
@inject()
|
||||
ctx: Context;
|
||||
|
||||
@plugin()
|
||||
jwt: any;
|
||||
|
||||
@inject('UserService')
|
||||
user: IUserService;
|
||||
/**
|
||||
* 处理ocr数据转发
|
||||
*/
|
||||
@post('/')
|
||||
async index() {
|
||||
try {
|
||||
const { body } = this.getRequestBody();
|
||||
const token: string = this.ctx.get('Authorization') || '';
|
||||
console.log(token, body, this.ctx.state)
|
||||
this.success({ username: 'cai'})
|
||||
const userInfo = await this.jwt.verify(token);
|
||||
const user = this.user.findByAccount(userInfo.userAccount);
|
||||
this.success(user);
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
this.fail('server error')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ export default (appInfo: EggAppInfo) => {
|
||||
secret: "123456",
|
||||
enable: true,
|
||||
match(ctx: Context) {
|
||||
const reg = /login/;
|
||||
const reg = /login|register/;
|
||||
return !reg.test(ctx.originalUrl);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export interface IAccountInfo {
|
||||
userAccount: string;
|
||||
password: string;
|
||||
nickName?: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,4 +4,5 @@ import {ILoginResult} from "./ILoginResult";
|
||||
export interface IAccountService {
|
||||
login(accountInfo: IAccountInfo): Promise<ILoginResult>;
|
||||
authUser(userInfo: IAccountInfo): Promise<boolean>;
|
||||
register(accountInfo: IAccountInfo): Promise<string>;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ export interface IRoom {
|
||||
}
|
||||
|
||||
export interface IRoomService {
|
||||
findById(uid: string): IRoom;
|
||||
findById(uid: string): Promise<IRoom>;
|
||||
findByRoomNumber(roomNumber: number): Promise<boolean>
|
||||
add(): Promise<any>;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export interface IUser {
|
||||
nickName: string;
|
||||
account: string;
|
||||
password: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { IAccountInfo } from './IAccountInfo';
|
||||
import { IUser } from './IUser';
|
||||
|
||||
export interface IUserService {
|
||||
findById(uid: string): Promise<IUser>;
|
||||
findByAccount(account: string): Promise<IUser>;
|
||||
addUser(accountInfo: IAccountInfo): Promise<any>;
|
||||
}
|
||||
+42
-12
@@ -1,8 +1,10 @@
|
||||
import BaseService from '../lib/baseService';
|
||||
import {Context, inject, provide, plugin, config} from "midway";
|
||||
import {IAccountInfo} from "../interface/IAccountInfo";
|
||||
import {IAccountService} from "../interface/IAccountService";
|
||||
import {ILoginResult} from "../interface/ILoginResult";
|
||||
import { Context, inject, provide, plugin, config } from 'midway';
|
||||
import { IAccountInfo } from '../interface/IAccountInfo';
|
||||
import { IAccountService } from '../interface/IAccountService';
|
||||
import { ILoginResult } from '../interface/ILoginResult';
|
||||
import { IUserService } from '../interface/IUserService';
|
||||
import { IUser } from '../interface/IUser';
|
||||
|
||||
@provide('AccountService')
|
||||
export class AccountService extends BaseService implements IAccountService {
|
||||
@@ -13,6 +15,9 @@ export class AccountService extends BaseService implements IAccountService {
|
||||
@plugin()
|
||||
jwt: any;
|
||||
|
||||
@inject('UserService')
|
||||
user: IUserService;
|
||||
|
||||
@config('jwt')
|
||||
protected jwtConfig: any;
|
||||
|
||||
@@ -23,27 +28,52 @@ export class AccountService extends BaseService implements IAccountService {
|
||||
// 校验用户信息
|
||||
const isAuth = await this.authUser(accountInfo);
|
||||
if (isAuth) {
|
||||
token = await this.getToken(accountInfo.userAccount)
|
||||
token = await this.getToken(accountInfo.userAccount);
|
||||
}
|
||||
const result: ILoginResult = {token};
|
||||
resolve(result)
|
||||
const result: ILoginResult = { token };
|
||||
resolve(result);
|
||||
} catch (e) {
|
||||
this.ctx.logger.error('login service error:', e);
|
||||
reject(e);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
public async register(accountInfo: IAccountInfo): Promise<string> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const hasUser = await this.checkHasUser(accountInfo.userAccount);
|
||||
if (!hasUser) {
|
||||
let result = await this.user.addUser(accountInfo);
|
||||
if (result.affectedRow === 1) {
|
||||
resolve('user create successful');
|
||||
}
|
||||
} else {
|
||||
reject('User already exists');
|
||||
}
|
||||
} catch (e) {
|
||||
this.ctx.logger.error('register service error:', e);
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async authUser(accountInfo: IAccountInfo) {
|
||||
let valid = accountInfo.userAccount === 'cai' && accountInfo.password === '123';
|
||||
const user: IUser = await this.checkHasUser(accountInfo.userAccount);
|
||||
let valid = user.password === accountInfo.password;
|
||||
if (!valid) {
|
||||
throw 'incorrect user account or password.'
|
||||
throw 'incorrect user account or password.';
|
||||
}
|
||||
return accountInfo.userAccount === 'cai' && accountInfo.password === '123';
|
||||
return valid;
|
||||
}
|
||||
|
||||
private async checkHasUser(userAccount: string): Promise<IUser> {
|
||||
return await this.user.findByAccount(userAccount);
|
||||
}
|
||||
|
||||
private getToken(userAccount: string) {
|
||||
const token = this.jwt.sign({userName: userAccount}, this.jwtConfig.secret, { expiresIn: 60 * 60 });
|
||||
const token = this.jwt.sign({ userAccount: userAccount },
|
||||
this.jwtConfig.secret, { expiresIn: 60 * 60 });
|
||||
this.ctx.logger.info(`AccountService getToken token--${token}`);
|
||||
return token;
|
||||
}
|
||||
|
||||
+21
-5
@@ -1,5 +1,6 @@
|
||||
import BaseService from '../lib/baseService';
|
||||
import {Context, inject, provide, plugin} from "midway";
|
||||
import { IRoom } from '../interface/IRoom';
|
||||
|
||||
@provide('RoomService')
|
||||
export class RoomService extends BaseService {
|
||||
@@ -10,14 +11,29 @@ export class RoomService extends BaseService {
|
||||
@plugin()
|
||||
mysql: any;
|
||||
|
||||
async findById(uid: string){
|
||||
const room = await this.mysql.get('room', { id: uid});
|
||||
return { room }
|
||||
@plugin()
|
||||
redis: any;
|
||||
|
||||
async findById(uid: string): Promise<IRoom> {
|
||||
return await this.mysql.get('room', { id: uid});
|
||||
}
|
||||
|
||||
async add() {
|
||||
async findByRoomNumber(number: string): Promise<boolean> {
|
||||
const redis = await this.redis.get(`room:${number}`);
|
||||
console.log(redis, 'redis', number);
|
||||
return redis
|
||||
}
|
||||
|
||||
async add(expires: number = 10) {
|
||||
const number = Math.floor(Math.random() * (1000000 - 100000)) + 100000;
|
||||
return await this.mysql.insert('room', { roomNumber: number });
|
||||
const result = await this.mysql.insert(`room`, { room_number: number });
|
||||
const roomRedis = await this.redis.set(`room:${number}`, `${number}`, 'ex', expires);
|
||||
console.log(roomRedis, 'roomRedis-----------');
|
||||
if(result.affectedRows === 1 && roomRedis === 'OK') {
|
||||
return { roomNumber: number }
|
||||
} else {
|
||||
throw 'room add error'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-10
@@ -1,8 +1,10 @@
|
||||
import {Context, inject, provide, plugin} from "midway";
|
||||
import { IUser } from '../interface/IUser';
|
||||
import { IUserService } from '../interface/IUserService';
|
||||
import { IAccountInfo } from '../interface/IAccountInfo';
|
||||
|
||||
@provide('UserService')
|
||||
export class UserService {
|
||||
export class UserService implements IUserService{
|
||||
|
||||
@inject()
|
||||
ctx: Context;
|
||||
@@ -10,19 +12,20 @@ export class UserService {
|
||||
@plugin()
|
||||
mysql: any;
|
||||
|
||||
async findById(uid: string){
|
||||
const user = await this.mysql.get('user', { id: uid});
|
||||
return { user }
|
||||
async findById(uid: string): Promise<IUser>{
|
||||
return await this.mysql.get('user', { id: uid});
|
||||
}
|
||||
|
||||
async findByAccount(account: string){
|
||||
const user = await this.mysql.get('user', { account: account});
|
||||
return { user }
|
||||
async findByAccount(account: string) {
|
||||
return await this.mysql.get('user', { account: account});
|
||||
}
|
||||
|
||||
async addUser(data: IUser) {
|
||||
const result = await this.mysql.insert('user', data)
|
||||
return result
|
||||
async addUser(accountInfo: IAccountInfo): Promise<any> {
|
||||
return await this.mysql.insert('user', {
|
||||
account: accountInfo.userAccount,
|
||||
password: accountInfo.password,
|
||||
nick_name: accountInfo.nickName
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user