/* eslint-disable array-callback-return */ 'use strict'; const Controller = require('egg').Controller; class AdminController extends Controller { async login() { const { ctx, app } = this; const { userName, password } = ctx.request.body; const person = require(app.config.filePath.configJson); const data = person.admin.filter(p => p.userName === userName); 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 editPwa() { const { ctx } = this; const { userName, password, newpassword } = ctx.request.body; const person = require(this.app.config.filePath.configJson); const jsaonfilePath = this.app.config.filePath.configJson; const data = person.admin.filter(p => p.userName === userName); let msg; if (data.length <= 0) { msg = { errcode: -1, errmsg: '用户不存在' }; } else { if (data[0].password === password) { person.admin.map(p => { if (p.userName === userName) { p.password = newpassword; } }); const jsonstr = JSON.stringify(person); await this.service.files.write({ filePath: jsaonfilePath, str: jsonstr }); msg = { errcode: 0, errmsg: '' }; } else { msg = { errcode: -1, errmsg: '密码错误' }; } } ctx.body = msg; } } module.exports = AdminController;