shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.push.support;
 
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.PhoneNumberUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.push.AbstractPushObject;
import com.walker.push.Notification;
import com.walker.push.NotificationChannel;
import com.walker.push.SmsMessage;
 
import java.util.ArrayList;
import java.util.List;
 
public abstract class AbstractSmsPush extends AbstractPushObject<SmsMessage> {
 
    @Override
    public NotificationChannel getNotificationChannel() {
        return NotificationChannel.Sms;
    }
 
    @Override
    public List<SmsMessage> translateToTarget(Notification notification) {
        List<SmsMessage> data = new ArrayList<>(4);
        SmsMessage smsMessage = new SmsMessage();
        String userPhoneNum = null;
        for(String user: notification.getReceiverList()){
 
            // 这里需要特殊处理,如果传入的就是手机号,无需转换。例如:验证码场景,因为登录时还不知道用户信息。
            // 因此,只能使用手机号。2023-04-25
            if(PhoneNumberUtils.isCellPhoneNumber(user)){
                userPhoneNum = user;
            } else {
                userPhoneNum = this.getUserMobile(user);
            }
            if(StringUtils.isEmpty(userPhoneNum)){
                logger.warn("未查找到用户手机号,user = {}", user);
                continue;
            }
            smsMessage.addMobile(userPhoneNum);
        }
 
        try {
            SmsMessage message = JsonUtils.jsonStringToObject(notification.getContent(), SmsMessage.class);
            smsMessage.setTemplateId(message.getTemplateId());
            smsMessage.setTemplateParam(message.getTemplateParam());
        } catch (Exception e) {
            throw new RuntimeException("消息内容json转换错误:" + e.getMessage(), e);
        }
        data.add(smsMessage);
        return data;
    }
 
    /**
     * 获取用户手机号
     * @param userId
     * @return
     */
    protected abstract String getUserMobile(String userId);
}