admin
2021-11-27 4f015b8c624484e0c3b2d88b944163ce43a48d1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.yeshi.location.app.controller.client.api;
 
import com.github.qcloudsms.SmsSingleSenderResult;
import com.yeshi.location.app.entity.config.SystemConfigKey;
import com.yeshi.location.app.service.inter.config.SystemConfigService;
import com.yeshi.location.app.service.manager.VerifyCodeManager;
import com.yeshi.location.app.vo.AcceptData;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.sms.TencentSMSUtil;
import org.yeshi.utils.sms.VerifyCodeFactory;
 
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
 
/**
 * @author hxh
 * @title: UserController
 * @description: 用户接口
 * @date 2021/11/16 17:37
 */
@Controller
@RequestMapping("api/v1/sms")
public class SMSController {
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private VerifyCodeManager verifyCodeManager;
 
    @Resource
    private RedisTemplate<String, String> redisTemplate;
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 发送验证码
     * @date 18:31 2021/11/16
     * @param: acceptData
     * @param: phone
     **/
    @ResponseBody
    @RequestMapping("sendSMS")
    public String sendSMS(AcceptData acceptData, String phone) {
        if (!StringUtil.isMobile(phone)) {
            return JsonUtil.loadFalseResult("手机号格式错误");
        }
        String key = "sendsms-" + phone;
 
        if (redisTemplate.opsForValue().setIfAbsent(key, "1", 60, TimeUnit.SECONDS)) {
            String code = VerifyCodeFactory.createNumber(6);
            String msg = systemConfigService.getValueCache(acceptData.getSystem(), SystemConfigKey.tencentVerifySMSTemplate);
            String appId = systemConfigService.getValueCache(acceptData.getSystem(), SystemConfigKey.tencentSMSAppId);
            String appKey = systemConfigService.getValueCache(acceptData.getSystem(), SystemConfigKey.tencentSMSAppKey);
            SmsSingleSenderResult result =
                    TencentSMSUtil.sendSingleMsg(Integer.parseInt(appId), appKey, phone, msg.replace("{验证码}", code));
            if (result.result == 0) {
                verifyCodeManager.sendSMSSuccess(phone, code);
                return JsonUtil.loadTrueResult("");
            } else {
                return JsonUtil.loadFalseResult(result.errMsg);
            }
        } else {
            return JsonUtil.loadFalseResult("服务器繁忙,请稍后再试");
        }
 
 
    }
 
 
}