'use strict';

/**
 * 用户管理业务类
 *
 * @author Mai
 * @date 2018/4/19
 * @version
 */

// 加密类
const crypto = require('crypto');
module.exports = app => {
    class Customer extends app.BaseService {
        async validLogin (data) {
            let result = false;

            // 验证本地用户
            if (data.account === 'admin') {
                result = data.password === 'admin';
            } else {
                result = false;
            }

            if (result) {
                // 成功后写入session
                const currentTime = new Date().getTime();
                const sessionToken = crypto.createHmac('sha1', currentTime + '').update(data.account)
                    .digest().toString('base64');
                const userSession = {
                    username: data.account,
                    loginTime: currentTime,
                    sessionToken,
                };
                this.ctx.session.userSession = userSession;
            }

            return result;
        }
    }

    return Customer;
}