12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 'use strict';
- const Controller = require('egg').Controller;
- const svgCaptcha = require('svg-captcha');
- const md5 = require('md5-node');
- class AdminController extends Controller {
- async login() {
-
- const {ctx, service} = this;
- let {username , password, captcha, checkKey} = ctx.request.body;
- let error = null;
- if( !username ) error = '用户名为空!';
- if( !password ) error = '密码为空!';
- if( !checkKey ) error = 'checkKey为空!';
- if( !captcha ) error = "验证码无效";
- if( error ) {
- ctx.adminFail(error);
- return;
- }
- captcha = captcha.toLowerCase();
- let cacheKey = md5(captcha + checkKey);
- let code = await ctx.service.cache.getString( cacheKey )
- if( !code || code != captcha) {
- ctx.adminFail("验证码无效");
- return;
- }
- // await ctx.service.cache.removeCache( cacheKey );
- let ret = await service.adminUser.login(username, password);
- ctx.adminOK(ret, '登录成功!');
- }
- async loginOut() {
- const { ctx } = this;
- throw "error test";
- // ctx.body = 'admin loginOut';
- }
- async captcha() {
- const { ctx , app} = this;
- const captcha = svgCaptcha.create({
- size: 4,
- fontSize: 50,
- width: 100,
- height: 40,
- bacground: '#cc9966'
- });
- //ctx.session.captcha = captcha.text;
- let key = ctx.params.key;
- if( !key ) {
- ctx.adminFail("no key");
- return;
- }
- let code = captcha.text;
- let lowerCaseCode = code.toLowerCase();
- let realKey = md5(lowerCaseCode+key);
-
- await ctx.service.cache.setString(realKey, lowerCaseCode, 60);
- ctx.adminOK({type: 'image/svg+xml', svg: captcha.data});
- }
- async duplicateCheck(){
- let {ctx, service} = this;
- let query = ctx.request.query;
- let ok = await service.sys.duplicateCheckOk( query );
- if( ok ) {
- ctx.adminOK(true);
- return;
- }
- ctx.adminFail(false);
- }
- }
- module.exports = AdminController;
|