/**
 * 短信相关控制器
 *
 * @author EllisRan
 * @date 2018/4/17
 * @version
 */
import UserModel from "../models/user_model";
import SmsModel from "../models/sms_model";
const SMS = require('../models/sms');

class SmsController {

    /**
     * 发送短信验证码
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async code(request, response) {
        let responseData = {
            err: 0,
            msg: ''
        };
        let mobile = request.body.mobile;
        let type = request.body.type;
        try {
            let smsModel = new SmsModel();
            if (parseInt(type) === 3) {
                const Sms = new SMS();
                const code = Sms.generateRandomString(6,2);
                await Sms.send(mobile, code);
                // console.log(JSON.stringify(returnStatus));
                // if (parseInt(JSON.stringify(returnStatus).statusCode) !== 200) {
                //     throw '短信发送失败!';
                // }
                request.session.code = mobile + '_' + code + '_' + Date.parse(new Date())/1000;
            } else {
                let returnStatus = await smsModel.sendSmsFromSSO(mobile, type);
                if (returnStatus === null) {
                    throw '获取数据失败!';
                }
                if(returnStatus != 1){
                    responseData.err = 1;
                    responseData.msg = await smsModel.getStatusMsg(returnStatus);
                }
            }
        } catch (error) {
            console.log(error);
            responseData.err = 1;
            responseData.msg = error;
        }

        response.json(responseData);
    }

    /**
     * 手机和验证码验证并更新sso
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async setMobile(request, response) {
        let responseData = {
            err: 0,
            msg: ''
        };
        let ssoId = request.body.ssoId;
        let mobile = request.body.mobile;
        let code = request.body.code;
        try {
            let smsModel = new SmsModel();
            let returnStatus = await smsModel.checkCodeFromSSO(ssoId, mobile, code);
            if (returnStatus === null) {
                throw '获取数据失败!';
            }
            if(returnStatus != 1){
                responseData.err = 1;
                responseData.msg = await smsModel.getStatusMsg(returnStatus);
            }
        } catch (error) {
            console.log(error);
            responseData.err = 1;
            responseData.msg = error;
        }

        response.json(responseData);
    }

    /**
     * 检测通行账号是否已存在该手机号码注册
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async checkMobile (request, response) {
        let responseData = {
            err: 0,
            msg: ''
        };
        let mobile = request.body.mobile;
        try {
            let userModel = new UserModel();
            let responseData = await userModel.getInfoFromSSOMobile(mobile, 0);
            if ( responseData === '-2') {
                throw '参数有误或手机号码不正确';
            }
            responseData = JSON.parse(responseData);
            if (typeof responseData !== 'object') {
                throw '该手机号未注册通行账号';
            }
            responseData.msg = 'success';
        } catch (error) {
            console.log(error);
            responseData.err = 1;
            responseData.msg = error;
        }

        response.json(responseData);
    }
}

export default SmsController;
