'use strict'; const Controller = require('../core/base_controller'); const fileName = "CSide"; class CSideController extends Controller { // 比较用户名 async diffName() { try { const ctx = this.ctx; const body = ctx.request.body; const result = await ctx.service.cSide.loginUser(body.name); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result) { this.setResponse(ctx, 404, '该用户名已存在', "none"); } else { this.setResponse(ctx, 200, '验证通过', "none"); } } catch (error) { const blockName = fileName + "--" + "diffName"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 比较电话号码 async diffPhone() { try { const ctx = this.ctx; const body = ctx.request.body; const result = await ctx.service.cSide.diffPhone(body.phone); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result) { this.setResponse(ctx, 404, '该手机号已注册', "none"); } else { this.setResponse(ctx, 200, '验证通过', "none"); } } catch (error) { const blockName = fileName + "--" + "diffPhone"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 比较邮箱 async diffEmail() { try { const ctx = this.ctx; const body = ctx.request.body; const result = await ctx.service.cSide.diffEmail(body.email); if (result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result) { this.setResponse(ctx, 404, '该邮箱已经注册', "none"); } else { this.setResponse(ctx, 200, '验证通过', "none"); } } catch (error) { const blockName = fileName + "--" + "diffEmail"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // C端用户注册 async registered() { const { ctx } = this; try { const salt = this.generateRandomString(); const body = ctx.request.body; const form = { uid: this.generateNumberString('C'), salt: this.encryptRequest(salt), status: 0, role: 0, avatar: 'default', account_name: this.decryptRequest(body.account_name), password: this.shaForm(this.decryptRequest(body.password), salt), user_name: body.user_name == "" ? "" : this.decryptRequest(body.user_name), phone: this.decryptRequest(body.phone), gender: body.gender == "" ? null : this.decryptRequest(body.gender), age: body.age == "" ? null : this.decryptRequest(body.age), email: body.email == "" ? "" : this.decryptRequest(body.email), address: body.address == "" ? "" : this.decryptRequest(body.address), post_code: body.post_code == "" ? "" : this.decryptRequest(body.post_code), created_at: new Date(), updated_at: new Date(), login_at: new Date(), login_status: 0, }; const result = await ctx.service.cSide.registered(form); if(result == "error") { this.setResponse(ctx, 500, '服务器错误', "none"); return; } if (result) { this.setResponse(ctx, result.code, result.message, "none"); } else { this.setResponse(ctx, 404, '注册失败', "none"); } } catch (error) { const blockName = fileName + "--" + "registered"; ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); this.setResponse(ctx, 500, '服务器错误', "none"); } } // 通用服务登录 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.cSide.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.cSide.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.cSide.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"); } } } module.exports = CSideController;