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;
|
}
|
|
|
}
|