wangsixiang
8 months ago
12 changed files with 444 additions and 9 deletions
@ -0,0 +1,38 @@ |
|||
package com.zc.business.controller; |
|||
|
|||
import com.ruoyi.common.core.controller.BaseController; |
|||
import com.ruoyi.common.core.domain.AjaxResult; |
|||
import com.zc.business.domain.DcWarning; |
|||
import com.zc.business.service.IMsmService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import java.util.HashMap; |
|||
|
|||
/** |
|||
* 王思祥 |
|||
* 推送业务 |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("/business/sms") |
|||
public class MsmController extends BaseController { |
|||
@Autowired |
|||
private IMsmService msmService; |
|||
|
|||
/** |
|||
* 短信推送业务,阿里云短信业务 |
|||
*/ |
|||
@PostMapping("/push") |
|||
public Boolean alibabaCloudSms() |
|||
{ |
|||
return msmService.send("19806114248"); |
|||
} |
|||
//调用微信推送
|
|||
@PostMapping("/wenXinPush") |
|||
public AjaxResult commandAndDispatch(){ |
|||
return AjaxResult.success(msmService.wenXinSend("wx9ee0e3babfd8d2af","de2ecb80b30d63135918ba3ae6ffb711")); |
|||
} |
|||
} |
@ -0,0 +1,21 @@ |
|||
package com.zc.business.service; |
|||
|
|||
import com.alibaba.fastjson.JSONArray; |
|||
import com.ruoyi.common.core.domain.AjaxResult; |
|||
|
|||
public interface IMsmService { |
|||
/** |
|||
* 发送短信 |
|||
* |
|||
* @param phone 手机号 |
|||
* @return |
|||
*/ |
|||
public boolean send(String phone); |
|||
/** |
|||
* 微信推送 |
|||
* |
|||
* @return |
|||
*/ |
|||
JSONArray wenXinSend(String appId, String appSecret); |
|||
|
|||
} |
@ -0,0 +1,201 @@ |
|||
package com.zc.business.service.impl; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONArray; |
|||
import com.aliyuncs.DefaultAcsClient; |
|||
import com.aliyuncs.IAcsClient; |
|||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; |
|||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; |
|||
import com.aliyuncs.profile.DefaultProfile; |
|||
import com.ruoyi.common.core.domain.AjaxResult; |
|||
import com.ruoyi.common.core.redis.RedisCache; |
|||
import com.ruoyi.common.utils.spring.SpringUtils; |
|||
import com.zc.business.service.IMsmService; |
|||
import com.zc.business.utils.HttpUtil; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.json.JSONObject; |
|||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.io.InputStream; |
|||
|
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Random; |
|||
import java.util.concurrent.CountDownLatch; |
|||
import java.util.concurrent.ExecutorService; |
|||
import java.util.concurrent.Executors; |
|||
import java.util.concurrent.ThreadPoolExecutor; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
/** |
|||
* 短信业务实现类 |
|||
*/ |
|||
@Service |
|||
public class MsmServiceImpl implements IMsmService { |
|||
private static final String WEIXINTUISONTOKEN = "weixintuisontoken";//redis策略缓存的key
|
|||
|
|||
/** |
|||
* 发送短信 |
|||
* |
|||
* @param phone 手机号 |
|||
* @return |
|||
*/ |
|||
public boolean send(String phone) { |
|||
//地域节点、阿里云的id、秘钥
|
|||
DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", |
|||
"LTAI5tENd3j1tP5t2fF4fxaX", "i86Yenj13XRd2aeZMtybpKxsqI1VRU"); |
|||
IAcsClient client = new DefaultAcsClient(profile); |
|||
SendSmsRequest request = new SendSmsRequest(); |
|||
request.setSysRegionId("cn-qingdao");//地域节点
|
|||
request.putQueryParameter("PhoneNumbers", phone); //手机号,要填绑定测试的手机号码
|
|||
request.putQueryParameter("SignName", "阿里云短信测试"); //阿里云签名名称
|
|||
request.putQueryParameter("TemplateCode", "SMS_154950909"); //阿里云模板code
|
|||
String code = String.format("%04d", new Random().nextInt(10000));//验证码
|
|||
HashMap<Object, Object> map = new HashMap<>(); |
|||
map.put("code", code); //参数加入到map
|
|||
JSONObject json = new JSONObject(map); //map参数转为JSON传入到模板即可
|
|||
request.putQueryParameter("TemplateParam", json); |
|||
try { |
|||
SendSmsResponse response = client.getAcsResponse(request); |
|||
if (response != null && response.getCode() != null && response.getCode().equals("OK")) { |
|||
return true; |
|||
} |
|||
return false; |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
//测试短信推送
|
|||
// public static void main(String[] args) {
|
|||
// MsmServiceImpl msmService = new MsmServiceImpl();
|
|||
// boolean send = msmService.send("19806114248");
|
|||
// System.out.println(send);
|
|||
// }
|
|||
//微信推送
|
|||
@Override |
|||
public JSONArray wenXinSend(String appId, String appSecret) { |
|||
MsmServiceImpl msmService = new MsmServiceImpl(); |
|||
return msmService.sendTemp(appId, appSecret); |
|||
} |
|||
|
|||
//获取微信token
|
|||
public static com.alibaba.fastjson.JSONObject getAccessToken(String appId, String appSecret) { |
|||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret; |
|||
String accessToken = null; |
|||
com.alibaba.fastjson.JSONObject jsonObj = null; |
|||
try { |
|||
URL urlGet = new URL(url); |
|||
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); |
|||
http.setRequestMethod("GET"); // 必须是get方式请求
|
|||
http.setDoInput(true); |
|||
http.connect(); |
|||
InputStream is = http.getInputStream(); |
|||
int size = is.available(); |
|||
byte[] jsonBytes = new byte[size]; |
|||
is.read(jsonBytes); |
|||
accessToken = new String(jsonBytes, "UTF-8"); |
|||
System.err.println(accessToken); |
|||
jsonObj = com.alibaba.fastjson.JSONObject.parseObject(accessToken); |
|||
is.close(); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return jsonObj; |
|||
} |
|||
|
|||
//执行微信推送
|
|||
public JSONArray sendTemp(String appId, String appSecret) { |
|||
JSONArray objects = new JSONArray(); |
|||
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(100); |
|||
MsmServiceImpl msmService = new MsmServiceImpl(); |
|||
RedisCache redisCache = SpringUtils.getBean(RedisCache.class); |
|||
String accessToken = redisCache.getCacheObject(WEIXINTUISONTOKEN); |
|||
if (StringUtils.isBlank(accessToken)) { |
|||
accessToken = msmService.getAccessToken(appId, appSecret).getString("access_token");//获取到了access_token
|
|||
String expiresIn = msmService.getAccessToken(appId, appSecret).getString("expires_in");//token有效秒数
|
|||
redisCache.setCacheObject(WEIXINTUISONTOKEN, accessToken, Integer.parseInt(expiresIn), TimeUnit.SECONDS);//把token存入到redis中,并设置过期时间(时间来自于微信接口返回)
|
|||
} |
|||
JSONArray userListOpenid = msmService.getUserListOpenid(accessToken); |
|||
CountDownLatch latch = new CountDownLatch(userListOpenid.size()); |
|||
for (Object openid : userListOpenid) { |
|||
String token = accessToken; |
|||
executor.execute(() -> { |
|||
// 封装要发送的json
|
|||
Map<String, Object> map = new HashMap(); |
|||
map.put("template_id", "D_yBiN7aDl2pw3bhSqU_Fbi0D8BHXL0qeBjhusWVO68");//模板消息id
|
|||
map.put("touser", openid.toString());//这里的openid是全部关注公众号的openid
|
|||
// 封装data
|
|||
com.alibaba.fastjson.JSONObject data = new com.alibaba.fastjson.JSONObject(); |
|||
com.alibaba.fastjson.JSONObject name = new com.alibaba.fastjson.JSONObject(); |
|||
name.put("value", "做一个身体和心灵都勇敢的人,趁着身体未老,心灵还透明。晚安!"); |
|||
data.put("name", name); |
|||
com.alibaba.fastjson.JSONObject weather = new com.alibaba.fastjson.JSONObject(); |
|||
weather.put("value", "阴天 19°c"); |
|||
data.put("weather", weather); |
|||
com.alibaba.fastjson.JSONObject birthday = new com.alibaba.fastjson.JSONObject(); |
|||
birthday.put("value", "53 天"); |
|||
data.put("birthday", birthday); |
|||
map.put("data", data); |
|||
String r = HttpUtil.getJsonData(com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(map)), |
|||
"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token); //发送模板消息
|
|||
System.out.println("-->" + r); |
|||
try { |
|||
JSONObject jsonObject = new JSONObject(r); |
|||
String errmsg = jsonObject.getString("errmsg"); |
|||
Object errcode = jsonObject.get("errcode"); |
|||
objects.add(errcode); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} finally { |
|||
latch.countDown(); |
|||
} |
|||
}); |
|||
} |
|||
try { |
|||
latch.await(); // 等待所有线程执行完毕
|
|||
} catch (InterruptedException e) { |
|||
Thread.currentThread().interrupt(); |
|||
} finally { |
|||
executor.shutdown(); |
|||
} |
|||
return objects; |
|||
//map.put("url","https://www.vipkes.cn");//用户点击模板消息,要跳转的地址
|
|||
// 封装miniprogram 跳转小程序用,不跳不要填
|
|||
// Map<String, String> mapA = new HashMap<>();
|
|||
// mapA.put("appid", ""); //小程序appid
|
|||
// mapA.put("pagepath", ""); //小程序页面pagepath
|
|||
// map.put("miniprogram", mapA);
|
|||
} |
|||
|
|||
//获取微信公众号全部关注人的openid
|
|||
public static JSONArray getUserListOpenid(String accessToken) { |
|||
String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken; |
|||
; |
|||
com.alibaba.fastjson.JSONObject jsonObj = null; |
|||
try { |
|||
URL urlGet = new URL(url); |
|||
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); |
|||
http.setRequestMethod("GET"); // 必须是get方式请求
|
|||
http.setDoInput(true); |
|||
http.connect(); |
|||
InputStream is = http.getInputStream(); |
|||
int size = is.available(); |
|||
byte[] jsonBytes = new byte[size]; |
|||
is.read(jsonBytes); |
|||
String userOpenid = new String(jsonBytes, "UTF-8"); |
|||
jsonObj = com.alibaba.fastjson.JSONObject.parseObject(userOpenid); |
|||
is.close(); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
com.alibaba.fastjson.JSONObject data = jsonObj.getJSONObject("data"); |
|||
return data.getJSONArray("openid"); |
|||
} |
|||
} |
@ -0,0 +1,83 @@ |
|||
package com.zc.business.utils; |
|||
|
|||
|
|||
import com.alibaba.fastjson.JSONObject; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.InputStream; |
|||
import java.io.InputStreamReader; |
|||
import java.io.OutputStreamWriter; |
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
|
|||
public class HttpUtil { |
|||
|
|||
public static String getJsonData(JSONObject jsonParam, String urls) { |
|||
StringBuffer sb = new StringBuffer(); |
|||
try { |
|||
// 创建url资源
|
|||
URL url = new URL(urls); |
|||
// 建立http连接
|
|||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|||
// 设置允许输出
|
|||
conn.setDoOutput(true); |
|||
// 设置允许输入
|
|||
conn.setDoInput(true); |
|||
// 设置不用缓存
|
|||
conn.setUseCaches(false); |
|||
// 设置传递方式
|
|||
conn.setRequestMethod("POST"); |
|||
// 设置维持长连接
|
|||
conn.setRequestProperty("Connection", "Keep-Alive"); |
|||
// 设置文件字符集:
|
|||
conn.setRequestProperty("Charset", "UTF-8"); |
|||
// 转换为字节数组
|
|||
byte[] data = (jsonParam.toString()).getBytes(); |
|||
// 设置文件长度
|
|||
conn.setRequestProperty("Content-Length", String.valueOf(data.length)); |
|||
// 设置文件类型:
|
|||
conn.setRequestProperty("contentType", "application/json"); |
|||
// 开始连接请求
|
|||
conn.connect(); |
|||
//OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
|
|||
//防止消息乱码
|
|||
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8"); |
|||
// 写入请求的字符串
|
|||
out.write(jsonParam.toString()); |
|||
out.flush(); |
|||
out.close(); |
|||
|
|||
System.out.println(conn.getResponseCode()); |
|||
|
|||
// 请求返回的状态
|
|||
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) { |
|||
System.out.println("连接成功"); |
|||
// 请求返回的数据
|
|||
InputStream in1 = conn.getInputStream(); |
|||
try { |
|||
String readLine = new String(); |
|||
BufferedReader responseReader = new BufferedReader(new InputStreamReader(in1, "UTF-8")); |
|||
while ((readLine = responseReader.readLine()) != null) { |
|||
sb.append(readLine).append("\n"); |
|||
} |
|||
responseReader.close(); |
|||
System.out.println(sb.toString()); |
|||
|
|||
} catch (Exception e1) { |
|||
e1.printStackTrace(); |
|||
} |
|||
} else { |
|||
System.out.println("error++"); |
|||
|
|||
} |
|||
|
|||
} catch (Exception e) { |
|||
|
|||
} |
|||
|
|||
return sb.toString(); |
|||
|
|||
} |
|||
|
|||
|
|||
} |
Loading…
Reference in new issue