/**
 * 短信相关控制器
 *
 * @author EllisRan
 * @date 2018/4/17
 * @version
 */
import SmsModel from "../models/sms_model";

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();
            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);
    }
}

export default SmsController;
