admin.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* eslint-disable array-callback-return */
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. class AdminController extends Controller {
  5. async login() {
  6. const { ctx, app } = this;
  7. const { userName, password } = ctx.request.body;
  8. const person = require(app.config.filePath.configJson);
  9. const data = person.admin.filter(p => p.userName === userName);
  10. let msg;
  11. if (data.length <= 0) {
  12. msg = { errcode: -1, errmsg: '用户不存在' };
  13. } else {
  14. if (data[0].password === password) {
  15. const token = app.jwt.sign({ userName, name: data[0].name }, app.config.jwt.secret, {
  16. expiresIn: 60 * 60 * 60,
  17. });
  18. msg = { errcode: 0, errmsg: '', token, userName, name: data[0].name };
  19. } else {
  20. msg = { errcode: -1, errmsg: '密码错误' };
  21. }
  22. }
  23. ctx.body = msg;
  24. }
  25. async editPwa() {
  26. const { ctx } = this;
  27. const { userName, password, newpassword } = ctx.request.body;
  28. const person = require(this.app.config.filePath.configJson);
  29. const jsaonfilePath = this.app.config.filePath.configJson;
  30. const data = person.admin.filter(p => p.userName === userName);
  31. let msg;
  32. if (data.length <= 0) {
  33. msg = { errcode: -1, errmsg: '用户不存在' };
  34. } else {
  35. if (data[0].password === password) {
  36. person.admin.map(p => {
  37. if (p.userName === userName) {
  38. p.password = newpassword;
  39. }
  40. });
  41. const jsonstr = JSON.stringify(person);
  42. await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr });
  43. msg = { errcode: 0, errmsg: '' };
  44. } else {
  45. msg = { errcode: -1, errmsg: '密码错误' };
  46. }
  47. }
  48. ctx.body = msg;
  49. }
  50. }
  51. module.exports = AdminController;