text: const router = require('express').Router();
  
  const User = require('../models/User');
  const Rooms = require('../models/Rooms');
  const { checkForm } = require('../utils/validate');
  const { hp } = require('../utils/wrapper');
  
  /**
   * home page
   */
  router.get('/', async (req, res) => {
  
    const userInfoInSession = req.session.user;
    const userInfo = {
      avatarUrl: userInfoInSession['portrait'] || 'https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/b8014a90f603738d7c8316c6b11bb051f919ec0d.jpg',
      coin: userInfoInSession['coin'],
      username: userInfoInSession['name'],
      signature: userInfoInSession['signature'] || '[未设置签名]',
    };
  
    // 
    let [_, rooms] = await hp(Rooms.findAll());
  
    rooms = rooms.map((room, /* idx */) => {
      room.isEnable = !(userInfo.coin > room.max_user_coin || userInfo.coin < room.min_user_coin);
      return room;
    });
  
    res.render('index', {
      title: 'Att Card',
      userInfo,
      rooms,
    });
  });
  
  /**
   * login and register page
   * GET /signup
   */
  router.get('/login', (req, res) => {
    res.render('login', {
      title: 'Login - Att Card',
    });
  });
  
  /**
   * register page
   * GET /register
   */
  router.get('/register', (req, res) => {
    res.render('register', {
      title: 'Register - Att Card',
    });
  });
  
  /**
   * GET /logout
   */
  router.get('/logout', (req, res) => {
    if (req.cookies.user) {
      res.clearCookie();
    }
    if (req.session) {
      req.session.destroy();
    }
    res.redirect('/login');
  });
  
  /**
   * POST /signin
   */
  router.post('/login', async (req, res) => {
    // 检测是否存在必须数据
    if (!req.body || !req.body.email || !req.body.password) {
      res.status().json({ code: 1, desc: '您没有指定登录数据' });
      return;
    }
    const { email, password } = req.body;
  
    // 检测是否有这个用户
    const [err, userData] = await hp(User.find(email));
  
    // 查询存在错误，视为服务器内部错误
    if (err) {
      console.log(err.message);
      res.json({ code: 2, desc: 'Server internal error' });
      return;
    }
  
    // 服务器没错，检测是否查询到用户
    console.log(userData);
    if (!userData) {
      res.json({ code: 1, desc: 'No account' });
      return;
    }
  
    // 不存在错误，判断密码是否正确
    // 密码错误，返回提示，正确返回一个链接
    if (User.encryptPwd(userData.pwd_salt, password) !== userData.password) {
      res.json({ code: 1, desc: 'wrong password' });
      return;
    }
    // 密码正确，设置session，返回数据
    req.session.user = userData;
    res.json({ code: 0, data: { next: '/' } });
  });
  
  
  /**
   * POST /signup
   */
  router.post('/register', async (req, res) => {
    if (!req.body || !req.body.email || !req.body.name || !req.body.password) {
      res.json({ code: 2, desc: '没有传递注册参数' });
      return;
    }
    const { email, name, password } = req.body;
  
    // 检测格式是否正确
    if (!checkForm(email, name, password)) {
      res.json({ code: 1, desc: '输入数据格式错误' });
      return;
    }
  
    const [findErr, user] = await hp(User.find(email));
  
    // 存在err代表服务器内部错误
    if (findErr) {
      res.json({ code: 2, desc: 'server internal error' });
      return;
    }
  
    // 已经存在该用户
    if (user) {
      res.json({ code: 1, desc: 'the email has been token' });
      return;
    }
  
    // 新建用户
    const [errCreate] = await hp(User.create(req.body));
    if (errCreate) {
      console.log(errCreate);
      res.json({ code: 1, desc: 'server internal error' });
      return;
    }
  
    res.json({ code: 0, data: { next: '/' } });
  });
  
  module.exports = router;
  