admin
2021-07-30 a66b556036c2b3936a51fd7b7e54a204eb31dc14
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
package com.yeshi.buwan.job;
 
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.buwan.log.LogHelper;
import com.yeshi.buwan.util.HttpUtil;
import com.yeshi.buwan.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
 
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
@Component
public class SMSJob {
 
    /**
     * 发送聚合短信
     *
     * @param mobiles
     * @throws Exception
     */
    public void sendJuHeSMS(List<String> mobiles) throws Exception {
        String url = String.format("http://v.juhe.cn/shortLetter/send.php?key=%s&tplId=%s&mobile=%s", "3f1ed706b36412dec6b5cd3cf1d0e5e3", "4558", URLEncoder.encode(StringUtil.join(mobiles, ",")));
        String result = HttpUtil.get(url);
        JSONObject resultJSON = JSONObject.fromObject(result);
        if (resultJSON.optInt("error_code") != 0) {
            throw new Exception(result);
        }
    }
 
    /**
     * 获取电话号码
     *
     * @return
     */
    private List<String> getMobiles() {
        Scanner scanner = new Scanner(SMSJob.class.getClassLoader().getResourceAsStream("电话.txt"));
        List<String> mobileList = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String phone = scanner.nextLine();
            if (!StringUtil.isNullOrEmpty(phone)) {
                mobileList.add(phone.trim());
            }
        }
        scanner.close();
        return mobileList;
    }
 
    @XxlJob("sms-s11hongbao")
    public ReturnT<String> sendSMS(String param) throws Exception {
        List<String> phoneList = getMobiles();
        int page = phoneList.size() % 1000 == 0 ? phoneList.size() / 1000 : phoneList.size() / 1000 + 1;
        for (int i = 0; i < page; i++) {
            List<String> phones = phoneList.subList(1000 * i, (1000 * i + 1000) >= phoneList.size() ? phoneList.size() : (1000 * i + 1000));
            try {
                sendJuHeSMS(phones);
            } catch (Exception e) {
                LogHelper.error("短信发送出错,page:" + i);
            }
        }
        return ReturnT.SUCCESS;
    }
 
 
}