// 用户登录 /* eslint-disable array-callback-return */ 'use strict'; const Controller = require('egg').Controller; const filePath = require('../../config/filespath'); class AdminController extends Controller { async login() { const { ctx, app } = this; const { userName, password } = ctx.request.body; const configJson = require(filePath.configJson); const data = configJson.admin.filter(p => p.userName === userName); console.log(data); let msg; if (data.length <= 0) { msg = { errcode: -1, errmsg: '用户不存在' }; } else { if (data[0].password === password) { const token = app.jwt.sign({ userName, name: data[0].name }, app.config.jwt.secret, { expiresIn: 60 * 60 * 60, }); msg = { errcode: 0, errmsg: '', token, userName, name: data[0].name }; } else { msg = { errcode: -1, errmsg: '密码错误' }; } } ctx.body = msg; } async editPwd() { const { ctx } = this; const { userName, password, newpassword } = ctx.request.body; const configJson = require(filePath.configJson); const data = configJson.admin.filter(p => p.userName === userName); let msg; try { if (data.length <= 0) { msg = { errcode: -1, errmsg: '用户不存在' }; } else { if (data[0].password === password) { configJson.admin.map(p => { if (p.userName === userName) { p.password = newpassword; } }); const jsonstr = JSON.stringify(configJson); await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr }); msg = { errcode: 0, errmsg: '' }; } else { msg = { errcode: -1, errmsg: '密码错误' }; } } ctx.body = msg; } catch (error) { const body = { errcode: -1003, errmsg: '密码修改失败', error }; throw new Error(JSON.stringify(body)); } } } module.exports = AdminController;