'use strict'; const Controller = require('../core/base_controller'); const fileName = "BSide"; const checkUid = async (thisPlace) => { const ctx = thisPlace.ctx; const token = ctx.request.header.authorization; const redis = ctx.app.redis; let rdmcode = await redis.get('db0').get(token); if (!rdmcode) { return false; } return rdmcode; }; class BSideController extends Controller { // B端登录 async login() { const { ctx } = this; try { const body = ctx.request.body; const name = this.decryptRequest(body.name); const password = this.decryptRequest(body.password); const user = await ctx.service.bSide.loginUser(name); if(user == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } const client = await ctx.service.verify.loginClient(body.client_id); if(client == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } // await ctx.service.bSide.subscribe(user.dataValues, client.dataValues); if (user && client && user.dataValues.status !== 2 && user.dataValues.password == this.shaForm(password, this.decryptRequest(user.dataValues.salt))) { const result = await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/user/authorize', { dataType: 'json', contentType: 'application/x-www-form-urlencoded', method: 'POST', timeout: 3000, data: { state: 'xyz', response_type: 'code', username: user.dataValues.uid, password: password, client_id: client.dataValues.client_id, client_secret: client.dataValues.client_secret, redirect_uri: 'http://127.0.0.1:' + ctx.app.server.address().port + '/getToken/' }, }); const timestamp = new Date().getTime(); const data = { data: this.encryptRequest(user.dataValues.uid + '?' + timestamp), redirect: client.dataValues.client_url + '?' + result.headers.location.split('?')[1].split('&')[0].split('=')[1] }; await ctx.service.bSide.loginStatus(user.dataValues.uid, 1); this.setResponse(ctx, 200, '登录成功', "deconstruction", data); } else { const data = user.dataValues.status == 2 ? { msg: '该账号已被封禁,请联系管理员申诉', code: 444 } : { msg: '登录失败', code: 404 }; this.setResponse(ctx, 404, '登录失败', "deconstruction", data); } } catch (error) { const blockName = fileName + "--" + "login"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } //B端管理员修改用户基本信息 async editUserManager() { const ctx = this.ctx; try { let rdmcode = await checkUid(this); if (!rdmcode) { this.setResponse(ctx, 401, 'token过期或错误', "none"); return; } const uid = JSON.parse(rdmcode).uid; const body = ctx.request.body; const Manager = await ctx.service.bSide.uidfinder(uid); if (Manager == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (Manager && Manager.dataValues.role == '1') { const userData = await ctx.service.bSide.editUserManager(body.editMsg, body.userMsg); if (userData == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (userData[0] !== 0) { this.setResponse(ctx, 200, '修改用户基本信息成功', "none"); } else { this.setResponse(ctx, 404, '修改用户基本信息失败', "none"); } } else { this.setResponse(ctx, 404, '修改用户基本信息失败', "none"); } } catch (error) { const blockName = fileName + "--" + "editUserManager"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } } module.exports = BSideController;