| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- const userModel = require('../model/user');
- const jwt = require('jsonwebtoken');
- const bcrypt = require('bcrypt');
- const secret = require('../config/secret');
- const Response = require('../util/response');
- class UserController {
- /**
- * 登录
- * @param ctx
- * @returns {Promise.<void>}
- */
- static async login(ctx) {
- const data = ctx.request.body;
- const user = await userModel.findByPhone(data.phone)
- if (user) {
- const confirmRes = bcrypt.compareSync(data.password, user.password);
- if(!confirmRes){
- ctx.body = Response.failed('密码错误,请重试')
- }else{
- // console.log('ctx',ctx);
- // console.log('ctx.cookies.redirect_uri',ctx.cookies.get('redirect_uri'));
- let redirect_uri = ctx.cookies.get('redirect_uri');
- let data = {
- iss : 'p.seecoder.cn',
- sub: encodeURIComponent(user.id + "&" + user.name + "&" + user.phone),
- aud: ctx.cookies.get('client_id'),
- auth_time: Math.floor(new Date().getTime() / 1000),
- iat: Math.floor(new Date().getTime() / 1000),
- exp: Math.floor(new Date().getTime() / 1000 + 24 * 60 * 60),
- };
- const token = jwt.sign(data, secret.sign);
- //ctx.redirect(redirect_uri + "?id_token=" + token);
- ctx.body = Response.success({token: token, name: user.name, path:redirect_uri});
- }
- } else {
- ctx.body = Response.failed('此账号不存在');
- }
- }
- /**
- * 注册
- * @param ctx
- * @returns {Promise.<void>}
- */
- static async register(ctx) {
- const data = ctx.request.body;
- const hashPassword = bcrypt.hashSync(data.password, 10);
- const user = await userModel.create(data.name, hashPassword, data.phone);
- if (user) {
- ctx.body = Response.success({name: user.name});
- } else {
- ctx.body = Response.failed('此手机号已注册');
- }
- }
- }
- module.exports = UserController;
|