package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.MemberValidateUtil;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.AliMessageConfig;
import com.hfkj.config.MessageConfig;
import com.hfkj.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/26 23:08
 */
@Controller
@RequestMapping(value = "/sms")
@Api(value = "短信服务")
public class SmsController {

    private static Logger log = LoggerFactory.getLogger(SmsController.class);

    @Resource
    private RedisUtil redisUtil;

    @RequestMapping(value = "/sendLoginSMSCode", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取登录验证码")
    public ResponseData sendLoginSMSCode(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("phone"))) {
                log.error("LoginController --> phone() error!", "请求参数校验失败");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            String phone = body.getString("phone");
            // 校验手机号格式
            if (MemberValidateUtil.validatePhone(phone) == false) {
                log.error("LoginController --> phone() error!", "请输入正确的手机号");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号");
            }
            // 生成随机6位验证码
            String smsCode = String.valueOf(new Random().nextInt(899999) + 100000);
            AliMessageConfig.sendSmsCode(phone,smsCode);

            // 验证码缓存5分钟
            redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5);

            return ResponseMsgUtil.success("短信发送成功");

        } catch (Exception e) {
            log.error("SMSController --> getLoginSMSCode() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/sendBindCardSMSCode", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取绑定卡片验证码")
    public ResponseData sendBindCardSMSCode(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("phone"))) {
                log.error("LoginController --> phone() error!", "请求参数校验失败");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            String phone = body.getString("phone");
            // 校验手机号格式
            if (MemberValidateUtil.validatePhone(phone) == false) {
                log.error("LoginController --> phone() error!", "请输入正确的手机号");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号");
            }
            // 生成随机6位验证码
            String smsCode = String.valueOf(new Random().nextInt(899999) + 100000);
            MessageConfig.req(phone,smsCode, MessageConfig.HWMSG_ID7);

            // 验证码缓存5分钟
            redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5);

            return ResponseMsgUtil.success("短信发送成功");

        } catch (Exception e) {
            log.error("SMSController --> getLoginSMSCode() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }



}

