import { BACKEND_37_CODE } from '@consts';
import { Controller } from 'egg';

export default class LoginController extends Controller {

  public async login() {
    const { ctx } = this;
    const {username, password} = ctx.request.body;
    ctx.body = await ctx.service.gmUser.login(username, password);
  }

  public async currentUser() {
    const { ctx } = this;
    ctx.body = await ctx.service.gmUser.currentUser();
  }

  public async channelLogin() {
    const { ctx } = this;
    const { uname, time, sign } = ctx.query;
    
    let result = await ctx.service.gmUser.channelLogin(uname, time, sign);
    if(result.code == BACKEND_37_CODE.SUCCESS.code) {
      ctx.redirect(`/welcome?token=${result.data.token}`);
    } else {
      ctx.body = result.code;
    }
  }

  public async changeMyPass() {
    const { ctx } = this;
    const {uid, password} = ctx.request.body;
    ctx.body = await ctx.service.gmUser.changeMyPass(uid, password);
  }
}
