package com.ebts.web.controller.sms;

import com.ebts.common.constant.Constants;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.core.redis.RedisCache;
import com.ebts.framework.config.SMSConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * @author: Clay
 * @date: 2021/8/8 22:51
 * @description：SmsController
 * @email: clay@hchyun.com
 */

@Api(value = "小程序获取短信", tags = "小程序获取短信")
@RestController
@RequestMapping("/applet/sms")
public class SmsController {
    private final Logger logger = LoggerFactory.getLogger(SmsController.class);


    @Autowired
    private RedisCache redisCache;



    @ApiOperation("获取设备信息")
    @GetMapping("/{phone}")
    public AjaxResult getSms(@PathVariable("phone") String phone) {
        try {
            if (null==phone||"".equals(phone)){
                return AjaxResult.error("电话号码不能为空!");
            }
            String regard = "^1[0-9]{10}$";
            if (!Pattern.matches(regard, phone)){
                return AjaxResult.error("手机号输入有误,请重新输入");
            }
            String code = redisCache.getCacheObject(Constants.PHONE_CODE_KEY+phone);
            if (code!=null){
                return AjaxResult.info("重复获取验证码");
            }
            code = SMSConfig.code();
            // todo 设置验证码时效性
           redisCache.setCacheObject(Constants.PHONE_CODE_KEY+phone,code,10, TimeUnit.MINUTES);
            // redisCache.setCacheObject(Constants.PHONE_CODE_KEY+phone,code);
            boolean state = SMSConfig.smsCode(phone,code);
            if (state){
                return AjaxResult.success("验证码发送成功");
            }else {
                return AjaxResult.error("获取失败,请重新获取");
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error();
        }
    }
    @ApiOperation("获取设备信息")
    @PostMapping("/val")
    public AjaxResult smsVal(@RequestBody Map<String,String> params){
        try {
            if (null == params.get("phone")|| "".equals(params.get("phone"))){
                return AjaxResult.error("手机号码为空");
            }
            if (null == params.get("code")|| "".equals(params.get("code"))){
                return AjaxResult.error("手机验证码为空");
            }
            String code = redisCache.getCacheObject(Constants.PHONE_CODE_KEY+params.get("phone"));
            if (code==null){
                return AjaxResult.info("验证码已过期,请重新获取");
            }
            if (code.equals(params.get("code"))){
                redisCache.deleteObject(Constants.PHONE_CODE_KEY+params.get("phone"));
                return AjaxResult.success("验证成功!");
            }else {
                return AjaxResult.error("验证码错误!");
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error();
        }
    }

}

