需求
- 新增人員
- 請求header中需加token
- 新增直接傳nickname,nickname不重名
- password默認是123456的md5加密密文
- 修改密碼
- 請求header中需加token
- 傳參:新密碼
實現
代碼基本上沒有改動,只需要改動路由(router.js),控制器(controller),服務(service)
- 以下幾項都和前一篇(egg實現登錄鑒權(三):密碼的md5加密及驗證)相同
- 數據庫
- 依賴包
- config/config.default.js
- config/plugin.js
- app/model/user.js
- app/controller/user.js
'use strict'; const Controller = require('egg').Controller; class UserController extends Controller { // 登錄 async login() { const { ctx, app } = this; const data = ctx.request.body; // 判斷該用戶是否存在並且密碼是否正確 const isValidUser = await ctx.service.user.validUser(data.nickname, data.password); if (isValidUser) { const token = app.jwt.sign({ nickname: data.nickname }, app.config.jwt.secret); ctx.body = { code: 200, msg: '登錄成功', token }; } else { ctx.body = { code: 400, msg: '登錄失敗' }; } } // 獲取所有用戶 async index() { const { ctx } = this; const data = await ctx.service.user.getUser(); ctx.body = { code: 200, msg: '查詢成功', data }; } // 通過id獲取用戶 async show() { const { ctx } = this; const data = await ctx.service.user.getUser(ctx.params.id); ctx.body = { code: 200, msg: '查詢成功', data }; } // 創建人員 async create() { const { ctx } = this; const { nickname } = ctx.request.body; await ctx.service.user.addUser(nickname); ctx.body = { code: 200, msg: '新增成功' }; } // 修改密碼 async updatePwd() { const { ctx } = this; const { password } = ctx.request.body; await ctx.service.user.editPwd(ctx.params.id, password); ctx.body = { code: 200, msg: '修改成功' }; } } module.exports = UserController;
- app/service/user.js
'use strict'; const Service = require('egg').Service; const crypto = require('crypto'); // 設置默認密碼 const DEFAULT_PWD = '123456'; function toInt(str) { if (typeof str === 'number') return str; if (!str) return str; return parseInt(str, 10) || 0; } class UserService extends Service { // 查詢user表,驗證密碼和花名 async validUser(nickname, password) { const data = await this.getUser(); const pwd = this.getMd5Data(password); for (const item of data) { if (item.nickname === nickname && item.password === pwd) return true; } return false; } // 獲取用戶,不傳id則查詢所有 async getUser(id) { const { ctx } = this; const query = { limit: toInt(ctx.query.limit), offset: toInt(ctx.query.offset) }; if (id) { return await ctx.model.User.findByPk(toInt(id)); } return await ctx.model.User.findAll(query); } // 新增人員 async addUser(nickname) { const { ctx } = this; const password = this.getMd5Data(DEFAULT_PWD); await ctx.model.User.create({ nickname, password }); } // 修改密碼 async editPwd(id, password) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); const newPwd = this.getMd5Data(password); await user.update({ password: newPwd }); } // 專門對數據進行md5加密的方法,輸入明文返回密文 getMd5Data(data) { return crypto.createHash('md5').update(data).digest('hex'); } } module.exports = UserService;
- app/router.js
'use strict'; /** * @param {Egg.Application} app - egg application */ module.exports = app => { const { router, controller, jwt } = app; router.get('/', controller.home.index); router.post('/user/login', controller.user.login); // 查詢 router.get('/user', controller.user.index); router.get('/user/:id', jwt, controller.user.show); // 新增 router.put('/user', jwt, controller.user.create); // 修改密碼 router.post('/user/:id', jwt, controller.user.updatePwd); };
- package.json
{
"name": "jwt", "version": "1.0.0", "description": "", "private": true, "egg": { "declarations": true }, "dependencies": { "egg": "^2.15.1", "egg-cors": "^2.2.3", "egg-jwt": "^3.1.7", "egg-scripts": "^2.11.0", "egg-sequelize": "^5.2.0", "mysql2": "^2.0.2" }, "devDependencies": { "autod": "^3.0.1", "autod-egg": "^1.1.0", "egg-bin": "^4.11.0", "egg-ci": "^1.11.0", "egg-mock": "^3.21.0", "eslint": "^5.13.0", "eslint-config-egg": "^7.1.0" }, "engines": { "node": ">=10.0.0" }, "scripts": { "start": "egg-scripts start --daemon --title=egg-server-jwt", "stop": "egg-scripts stop --title=egg-server-jwt", "dev": "egg-bin dev", "debug": "egg-bin debug", "test": "npm run lint -- --fix && npm run test-local", "test-local": "egg-bin test", "cov": "egg-bin cov", "lint": "eslint .", "ci": "npm run lint && npm run cov", "autod": "autod" }, "ci": { "version": "10" }, "repository": { "type": "git", "url": "" }, "author": "", "license": "MIT" }
自測
- 新增人員
- 修改密碼