-
Notifications
You must be signed in to change notification settings - Fork 41
Tiny pro sever egg #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3161693
feat: egg API getUserInfo
wjiangwang 8f22805
feat: egg API login register
wjiangwang bd11b2d
Merge branch 'dev' of github.com:wjiangwang/tiny-cli into tinypro-sever
wjiangwang e01693f
fix: fix err code
wjiangwang 8e7497e
fix: fix PR suggestion
wjiangwang ba7eb41
fix: fix database name
wjiangwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/toolkits/pro/template/server/eggJs/app/controller/user.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { Controller } from 'egg'; | ||
| import BcryptUtils from '../utils/bcrypt-utils'; | ||
|
|
||
| export default class UserController extends Controller { | ||
| // 注册用户 | ||
| public async registerUser() { | ||
| const { ctx, app } = this; | ||
| const payload = ctx.request.body || {}; | ||
| const transaction = await ctx.model.transaction(); | ||
| try { | ||
| this.logger.info('[ controller | user ] registerUser : 进入registerUser方法'); | ||
| // 校验参数 | ||
| const registerUserRule = { | ||
| user_name: { type: 'email' }, | ||
| password: { | ||
| type: 'string', | ||
| format: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/, | ||
| }, | ||
| }; | ||
| const err = app.validator.validate(registerUserRule, payload); | ||
| if (err?.length) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'InvalidParameter'); | ||
| return; | ||
| } | ||
| const { user_name, password } = payload; | ||
| // 判断用户是否已经存在 | ||
| const user = await ctx.service.user.getUserByName(user_name); | ||
| if (user) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'UserAlreadyExist'); | ||
| return; | ||
| } | ||
| const hash = BcryptUtils.genHash(password); | ||
| // 创建用户 | ||
| const { id } = await ctx.service.user.createUser({ user_name, password: hash }, { transaction }); | ||
| const userInfo = await ctx.service.user.createUserInfo({ user_name, user_id: id }, { transaction }); | ||
| await transaction.commit(); | ||
| ctx.helper.commonJson(ctx, userInfo, 200); | ||
| } catch (error) { | ||
| await transaction.rollback(); | ||
| ctx.helper.commonJson(ctx, {}, 500, 'InterError'); | ||
| } | ||
| } | ||
|
|
||
| // 获取用户信息 | ||
h-ivy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public async getUserInfo() { | ||
| const { ctx } = this; | ||
| const { id } = ctx.params; | ||
|
|
||
| try { | ||
| this.logger.info('[ controller | user ] getUserInfo : 进入getUserInfo方法'); | ||
| const userInfo = await ctx.service.user.getUserInfoById(id); | ||
| if (!userInfo) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'UserNotFound'); | ||
| return; | ||
| } | ||
| ctx.helper.commonJson(ctx, userInfo, 200); | ||
| } catch (error) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'InterError'); | ||
| } | ||
| } | ||
|
|
||
| // 登录 | ||
| public async login() { | ||
| const { ctx, app } = this; | ||
| const payload = ctx.request.body || {}; | ||
| try { | ||
| this.logger.info('[ controller | user ] login : 进入login方法'); | ||
| // 校验参数格式 | ||
| const err = app.validator.validate( | ||
| { | ||
| user_name: { type: 'email' }, | ||
| password: { type: 'string' }, | ||
| }, | ||
| payload, | ||
| ); | ||
| if (err?.length) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'InvalidParameter'); | ||
| return; | ||
| } | ||
|
|
||
| // 用户是否存在 | ||
| const user = await ctx.service.user.getUserByName(payload.user_name); | ||
| if (!user) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'UserNotFound'); | ||
| return; | ||
| } | ||
|
|
||
| // 密码是否正确 | ||
| const match = BcryptUtils.compare(payload.password, user.password); | ||
| if (!match) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'ErrorPassword'); | ||
| return; | ||
| } | ||
|
|
||
| // 生成Token | ||
| const { secret, sign } = this.app.config.jwt; | ||
| const userInfo = await ctx.service.user.getUserInfoById(user.id); | ||
| const token = this.app.jwt.sign(userInfo, secret, sign); | ||
| ctx.helper.commonJson(ctx, { token }, 200); | ||
h-ivy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| ctx.helper.commonJson(ctx, {}, 500, 'InterError'); | ||
| } | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
packages/toolkits/pro/template/server/eggJs/app/model/registerUser.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* indent size: 4 */ | ||
|
|
||
| module.exports = (app: any) => { | ||
| const DataTypes = app.Sequelize; | ||
|
|
||
| const RegisterUser = app.model.define( | ||
| 'RegisterUser', | ||
| { | ||
| id: { | ||
| type: DataTypes.INTEGER(20).UNSIGNED, | ||
| allowNull: false, | ||
| primaryKey: true, | ||
| autoIncrement: true, | ||
| }, | ||
| user_name: { | ||
| type: DataTypes.STRING(32), | ||
| allowNull: false, | ||
| }, | ||
| password: { | ||
| type: DataTypes.STRING(60), | ||
| allowNull: false, | ||
| }, | ||
| register_type: { | ||
| type: DataTypes.ENUM('email'), | ||
| allowNull: false, | ||
| defaultValue: 'email', | ||
| }, | ||
| }, | ||
| { | ||
| tableName: 'registeruser', | ||
| underscored: false, | ||
| freezeTableName: true, | ||
| omitNull: false, | ||
| timestamps: false, | ||
| paranoid: false, | ||
| }, | ||
| ); | ||
|
|
||
| return RegisterUser; | ||
| }; |
76 changes: 76 additions & 0 deletions
76
packages/toolkits/pro/template/server/eggJs/app/model/userInfo.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* indent size: 4 */ | ||
|
|
||
| module.exports = (app: any) => { | ||
| const DataTypes = app.Sequelize; | ||
|
|
||
| const UserInfo = app.model.define( | ||
| 'UserInfo', | ||
| { | ||
| id: { | ||
| type: DataTypes.INTEGER(20).UNSIGNED, | ||
| allowNull: false, | ||
| primaryKey: true, | ||
| autoIncrement: true, | ||
| }, | ||
| user_name: { | ||
| type: DataTypes.STRING(32), | ||
| allowNull: false, | ||
| }, | ||
| user_id: { | ||
| type: DataTypes.INTEGER(20), | ||
| allowNull: false, | ||
| }, | ||
| department: { | ||
| type: DataTypes.STRING(32), | ||
| allowNull: false, | ||
| defaultValue: '', | ||
| }, | ||
| employee_type: { | ||
| type: DataTypes.STRING(32), | ||
| defaultValue: null, | ||
| }, | ||
| roles: { | ||
| type: DataTypes.STRING(32), | ||
| defaultValue: null, | ||
| }, | ||
| probation_start: { | ||
| type: DataTypes.DATE, | ||
| defaultValue: null, | ||
| }, | ||
| probation_end: { | ||
| type: DataTypes.DATE, | ||
| defaultValue: null, | ||
| }, | ||
| probation_duration: { | ||
| type: DataTypes.INTEGER(11).UNSIGNED, | ||
| defaultValue: null, | ||
| }, | ||
| protocol_start: { | ||
| type: DataTypes.DATE, | ||
| defaultValue: null, | ||
| }, | ||
| protocol_end: { | ||
| type: DataTypes.DATE, | ||
| defaultValue: null, | ||
| }, | ||
| address: { | ||
| type: DataTypes.STRING(32), | ||
| defaultValue: null, | ||
| }, | ||
| status: { | ||
| type: DataTypes.STRING(32), | ||
| defaultValue: null, | ||
| }, | ||
| }, | ||
| { | ||
| tableName: 'userinfo', | ||
| underscored: false, | ||
| freezeTableName: true, | ||
| omitNull: false, | ||
| timestamps: false, | ||
| paranoid: false, | ||
| }, | ||
| ); | ||
|
|
||
| return UserInfo; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/toolkits/pro/template/server/eggJs/app/service/user.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { Service } from 'egg'; | ||
|
|
||
| export default class User extends Service { | ||
| public async getUserInfoById(user_id: string): Promise<any> { | ||
| return this.ctx.model.UserInfo.findOne({ where: { user_id }, raw: true }); | ||
| } | ||
|
|
||
| public async getUserByName(user_name: string): Promise<any> { | ||
| return this.ctx.model.RegisterUser.findOne({ where: { user_name } }); | ||
| } | ||
|
|
||
| public async createUser(params: { user_name: string; password: string }, options?): Promise<any> { | ||
| return this.ctx.model.RegisterUser.create(params, options); | ||
| } | ||
|
|
||
| public async createUserInfo(info, options?): Promise<any> { | ||
| return this.ctx.model.UserInfo.create(info, options); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/toolkits/pro/template/server/eggJs/app/utils/bcrypt-utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import * as bcrypt from 'bcrypt'; | ||
|
|
||
| export default class BcryptUtils { | ||
| private static saltRounds = 10; | ||
| public static genHash(password: string): string { | ||
| return bcrypt.hashSync(password, this.saltRounds); | ||
| } | ||
| public static compare(password: string, encrypted: string): boolean { | ||
| return bcrypt.compareSync(password, encrypted); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.