wang-hao-jie
2022-01-07 0f2202dfcea3309c1b0e7515f6db5fbdc83d51d1
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
130
131
132
133
134
package cn.exrick.xboot.core.common.sms;
 
import cn.exrick.xboot.core.common.constant.SettingConstant;
import cn.exrick.xboot.core.common.exception.XbootException;
import cn.exrick.xboot.core.entity.Setting;
import cn.exrick.xboot.core.service.SettingService;
import cn.exrick.xboot.core.vo.SmsSetting;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
/**
 * @author Exrickx
 */
@Component
@Slf4j
public class SmsUtil {
 
    @Autowired
    private SettingService settingService;
 
    public String getSmsUsed() {
 
        Setting setting = settingService.get(SettingConstant.SMS_USED);
        if (setting == null || StrUtil.isBlank(setting.getValue())) {
            throw new XbootException("您还未配置短信服务");
        }
        String type = setting.getValue();
        return type;
    }
 
    public SmsSetting getSmsSetting() {
 
        Setting sms = settingService.get(getSmsUsed());
        return new Gson().fromJson(sms.getValue(), SmsSetting.class);
    }
 
    /**
     * 获得对应完整短信模版Key
     * @param type
     * @return
     */
    public String getTemplate(Integer type) {
 
        return getSmsUsed() + "_" + getTemplateSuffix(type);
    }
 
    /**
     * 获得对应模版Key后缀
     * @param type
     * @return
     */
    public static String getTemplateSuffix(Integer type) {
 
        switch (type) {
            case 0:
                return SettingConstant.SMS_COMMON;
            case 1:
                return SettingConstant.SMS_REGIST;
            case 2:
                return SettingConstant.SMS_LOGIN;
            case 3:
                return SettingConstant.SMS_CHANGE_MOBILE;
            case 4:
                return SettingConstant.SMS_CHANG_PASS;
            case 5:
                return SettingConstant.SMS_RESET_PASS;
            case 6:
                return SettingConstant.SMS_ACTIVITI;
            default:
                return SettingConstant.SMS_COMMON;
        }
    }
 
    public String getTemplateCode(Integer type){
 
        Setting setting = settingService.get(getTemplate(type));
        if (StrUtil.isBlank(setting.getValue())) {
            throw new XbootException("系统还未配置短信服务或相应短信模版");
        }
        return setting.getValue();
    }
 
    /**
     * 发送验证码 模版变量为单个 code 无需模版编号
     * @param mobile
     * @param code
     * @param type   0通用模版 1注册 2登录 3修改手机 4修改密码 5重置密码 6工作流模版
     * @return
     */
    public void sendCode(String mobile, String code, Integer type) {
 
        sendCode(mobile, code, getTemplateCode(type));
    }
 
    /**
     * 发送验证码 模版变量为单个 code 需传入模版编号
     * @param mobile
     * @param code
     * @param templateCode
     * @return
     */
    public void sendCode(String mobile, String code, String templateCode) {
 
        sendSms(mobile, "{code:" + code + "}", templateCode);
    }
 
    /**
     * 发送工作流消息 模版变量为 content
     * @param mobile
     * @param content
     * @return
     */
    public void sendActMessage(String mobile, String content) {
 
        // 获取工作流消息模板
        sendSms(mobile, "{content:" + content + "}", getTemplateCode(6));
    }
 
    /**
     * 发送短信
     * @param mobile       手机号 多个,逗号分隔 若为11位国内手机号无需加国家区号86
     *                     国际号码需加上区号 [国家或地区码][手机号] 如8109012345678、86为日本、09012345678为手机号
     * @param params       参数 JSON格式,如{"code": "1234"}
     *                     若启用腾讯短信会自动按顺序转换为逗号分隔的数组值如[1234]
     * @param templateCode 短信模板code/id
     */
    public void sendSms(String mobile, String params, String templateCode) {
 
        //smsFactory.getSms().sendSms(mobile, params, templateCode);
    }
}