index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const svgCaptcha = require('svg-captcha');
  4. const md5 = require('md5-node');
  5. class AdminController extends Controller {
  6. async login() {
  7. const {ctx, service} = this;
  8. let {username , password, captcha, checkKey} = ctx.request.body;
  9. let error = null;
  10. if( !username ) error = '用户名为空!';
  11. if( !password ) error = '密码为空!';
  12. if( !checkKey ) error = 'checkKey为空!';
  13. if( !captcha ) error = "验证码无效";
  14. if( error ) {
  15. ctx.adminFail(error);
  16. return;
  17. }
  18. captcha = captcha.toLowerCase();
  19. let cacheKey = md5(captcha + checkKey);
  20. let code = await ctx.service.cache.getString( cacheKey )
  21. if( !code || code != captcha) {
  22. ctx.adminFail("验证码无效");
  23. return;
  24. }
  25. // await ctx.service.cache.removeCache( cacheKey );
  26. let ret = await service.adminUser.login(username, password);
  27. ctx.adminOK(ret, '登录成功!');
  28. }
  29. async loginOut() {
  30. const { ctx } = this;
  31. throw "error test";
  32. // ctx.body = 'admin loginOut';
  33. }
  34. async captcha() {
  35. const { ctx , app} = this;
  36. const captcha = svgCaptcha.create({
  37. size: 4,
  38. fontSize: 50,
  39. width: 100,
  40. height: 40,
  41. bacground: '#cc9966'
  42. });
  43. //ctx.session.captcha = captcha.text;
  44. let key = ctx.params.key;
  45. if( !key ) {
  46. ctx.adminFail("no key");
  47. return;
  48. }
  49. let code = captcha.text;
  50. let lowerCaseCode = code.toLowerCase();
  51. let realKey = md5(lowerCaseCode+key);
  52. await ctx.service.cache.setString(realKey, lowerCaseCode, 60);
  53. ctx.adminOK({type: 'image/svg+xml', svg: captcha.data});
  54. }
  55. async duplicateCheck(){
  56. let {ctx, service} = this;
  57. let query = ctx.request.query;
  58. let ok = await service.sys.duplicateCheckOk( query );
  59. if( ok ) {
  60. ctx.adminOK(true);
  61. return;
  62. }
  63. ctx.adminFail(false);
  64. }
  65. }
  66. module.exports = AdminController;