puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.sinata.rest.modular.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinata.common.enums.EnumRemoteSet;
import com.sinata.rest.common.ApiUtils;
import com.sinata.rest.core.aliyun.sms.AliSmsApi;
import com.sinata.rest.core.aliyun.sms.HwSmsApi;
import com.sinata.rest.core.util.ToolUtil;
import com.sinata.rest.modular.system.model.RemoteSet;
import com.sinata.rest.modular.system.model.SmsRecord;
import com.sinata.rest.modular.system.service.IRemoteSetService;
import com.sinata.rest.modular.system.service.ISmsRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.time.LocalDateTime;
 
@Slf4j
@RestController
@RequestMapping("/sms")
@Api(tags = "验证码相关", description = "验证码相关")
public class SmsController {
 
    @Autowired
    ISmsRecordService smsRecordService;
 
    @Autowired
    IRemoteSetService remoteSetService;
 
    private static final String note = "发送验证码 type = 1 注册; type = 2 登录 type = 3 修改密码 type = 4 修改手机号 type = 5修改交易密码";
 
    @GetMapping(value = "/send")
    @ApiOperation(value = "发送验证码", notes = note, response = ApiUtils.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "验证码类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
    })
    public Object sendSms(Integer type, String phone) {
        if (type == null) {
            return ApiUtils.returnNG(null, "发送失败");
        }
        if (StringUtils.isEmpty(phone)) {
            return ApiUtils.returnNG(null, "发送失败");
        }
        // 条件查询
        LambdaQueryWrapper<SmsRecord> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SmsRecord::getCodeType, type).eq(SmsRecord::getPhone, phone);
        SmsRecord r = smsRecordService.getOne(wrapper);
        RemoteSet set = remoteSetService.getById(EnumRemoteSet.SMS_SWITCH.index);
        boolean open = false;
        if (set != null) {
            open = Integer.parseInt(set.getValueStr()) != 0;
        }
        if (r == null) {
            r = new SmsRecord();
            r.setPhone(phone);
            r.setCodeType(type);
            r.setCode(generateCode(open));
            doSendCode(open, phone, r.getCode());
            smsRecordService.save(r);
        } else {
            if (r.getIsCheck() == 0) {
                if (r.getCreateTime().plusMinutes(1).isAfter(LocalDateTime.now())) {
                    return ApiUtils.returnNG(null, "发送过于频繁了,请稍后再试");
                }
            }
            r.setCreateTime(LocalDateTime.now());
            r.setCode(generateCode(open));
            doSendCode(open, phone, r.getCode());
            r.setIsCheck(0);
            smsRecordService.updateById(r);
        }
        return ApiUtils.returnOK(null, "发送成功!");
    }
 
    @GetMapping(value = "/check")
    @ApiOperation(value = "验证验证码", notes = note, response = ApiUtils.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "验证码类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query")
    })
    public Object checkSms(Integer type, String phone, String code) {
        if (type == null) {
            return ApiUtils.returnNG(null, "请求失败");
        }
        if (StringUtils.isEmpty(phone)) {
            return ApiUtils.returnNG(null, "请求失败");
        }
        if (StringUtils.isEmpty(code)) {
            return ApiUtils.returnNG(null, "请求失败");
        }
        String msg = smsRecordService.justVerifyCode(type, phone, code);
        if (msg.isEmpty()) {
            return ApiUtils.returnOK(null, "验证成功!");
        } else {
            return ApiUtils.returnNG(null, msg);
        }
    }
 
    //生成验证码
    private String generateCode(boolean open) {
        if (open) {
            return ToolUtil.getRandomNumber(4);
        } else {
            return "2020";
        }
    }
 
    //发送验证码
    private void doSendCode(boolean open, String phone, String code) {
        if (open) {
            try{
                HwSmsApi.sendSms("+86", phone, code);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
 
}