diff --git a/src/app/controller/account.ts b/src/app/controller/account.ts index cbacde2..5af0b76 100644 --- a/src/app/controller/account.ts +++ b/src/app/controller/account.ts @@ -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) + } } } diff --git a/src/app/controller/room.ts b/src/app/controller/room.ts index 4c28e04..28c93e3 100644 --- a/src/app/controller/room.ts +++ b/src/app/controller/room.ts @@ -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) diff --git a/src/app/controller/user.ts b/src/app/controller/user.ts index 153b1ba..ef1644e 100644 --- a/src/app/controller/user.ts +++ b/src/app/controller/user.ts @@ -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') } } } diff --git a/src/config/config.default.ts b/src/config/config.default.ts index 06d357e..22ce5a9 100644 --- a/src/config/config.default.ts +++ b/src/config/config.default.ts @@ -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); }, }; diff --git a/src/interface/IAccountInfo.ts b/src/interface/IAccountInfo.ts index 052ca7e..d2a80f4 100644 --- a/src/interface/IAccountInfo.ts +++ b/src/interface/IAccountInfo.ts @@ -1,5 +1,6 @@ export interface IAccountInfo { userAccount: string; password: string; + nickName?: string; } diff --git a/src/interface/IAccountService.ts b/src/interface/IAccountService.ts index 1011d26..826bb67 100644 --- a/src/interface/IAccountService.ts +++ b/src/interface/IAccountService.ts @@ -4,4 +4,5 @@ import {ILoginResult} from "./ILoginResult"; export interface IAccountService { login(accountInfo: IAccountInfo): Promise; authUser(userInfo: IAccountInfo): Promise; + register(accountInfo: IAccountInfo): Promise; } diff --git a/src/interface/IRoom.ts b/src/interface/IRoom.ts index c6b8aa0..b70facd 100644 --- a/src/interface/IRoom.ts +++ b/src/interface/IRoom.ts @@ -3,6 +3,7 @@ export interface IRoom { } export interface IRoomService { - findById(uid: string): IRoom; + findById(uid: string): Promise; + findByRoomNumber(roomNumber: number): Promise add(): Promise; } diff --git a/src/interface/IUser.ts b/src/interface/IUser.ts index c899097..3a1a64c 100644 --- a/src/interface/IUser.ts +++ b/src/interface/IUser.ts @@ -1,5 +1,5 @@ export interface IUser { nickName: string; account: string; - password: string; + password?: string; } diff --git a/src/interface/IUserService.ts b/src/interface/IUserService.ts new file mode 100644 index 0000000..3f5b88b --- /dev/null +++ b/src/interface/IUserService.ts @@ -0,0 +1,8 @@ +import { IAccountInfo } from './IAccountInfo'; +import { IUser } from './IUser'; + +export interface IUserService { + findById(uid: string): Promise; + findByAccount(account: string): Promise; + addUser(accountInfo: IAccountInfo): Promise; +} diff --git a/src/service/account.ts b/src/service/account.ts index 28175bd..5c74a7e 100644 --- a/src/service/account.ts +++ b/src/service/account.ts @@ -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 { + 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 { + 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; } diff --git a/src/service/room.ts b/src/service/room.ts index 649580d..8ef73f3 100644 --- a/src/service/room.ts +++ b/src/service/room.ts @@ -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 { + return await this.mysql.get('room', { id: uid}); } - async add() { + async findByRoomNumber(number: string): Promise { + 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' + } } } diff --git a/src/service/user.ts b/src/service/user.ts index 5a81fd3..0b76080 100644 --- a/src/service/user.ts +++ b/src/service/user.ts @@ -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{ + 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 { + return await this.mysql.insert('user', { + account: accountInfo.userAccount, + password: accountInfo.password, + nick_name: accountInfo.nickName + }); } }