Commit a59aba76 by baochunxin

#G:问题修复:新增推送模板模块

parent ed2928a3
......@@ -19,7 +19,7 @@ public class VerificationCodeParam {
/**
* 业务参数
*/
private HashMap<String, Object> map;
private Map<String, Object> map;
......
......@@ -171,6 +171,10 @@
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
......
package com.ruoyi.common.utils.http;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
......@@ -17,6 +19,12 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
......@@ -189,6 +197,34 @@ public class HttpUtils
return result.toString();
}
public static String sendJsonPost(String url, JSONObject param) {
JSONObject result = new JSONObject();
//定义接收数据
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
//请求参数转JOSN字符串
StringEntity entity = new StringEntity(param.toString(), "UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
try {
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
result.put("error", "连接错误!");
}
//关闭连接
try {
client.close();
} catch (Exception e) {
System.out.println(e.getMessage());
return "";
}
return result.toString();
}
public static String sendSSLPost(String url, String param)
{
StringBuilder result = new StringBuilder();
......
package yangtz.cs.liu.campus.util;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.core.domain.VerificationCodeParam;
import com.ruoyi.common.utils.http.HttpUtils;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yangtz.cs.liu.wechat.utils.CheckUtil;
import yangtz.cs.liu.wechat.controller.api.VxTemlateController;
/**
* 调用模板发送信息
*/
@Service
public class VxTemlateSendApi {
/**
* 发送模板信息
*/
/**
* 测试环境路径
*/
private static String url = "http://urgb7d.natappfree.cc/houduan/vx/temlateses/sendTemlate";
/**
* 签名key
*/
private static String ticketSecrets = "71_0xakqv3T3ORjyXPeP-s2rwRcAqDbMEmmmkfMQeeJJmXBM8g-W4p4oa5BKisL-FYVHMzLJLyzbhgFQUfOkBW8Ufr4SWTTgGpjGs-BmIi8V95ct5hAXtQ47xDetjoLSSiAFAXEE";
@Autowired
VxTemlateController vxTemlateController;
/**
*
* @param map
* toUserOpenId 微信用户id
* applyTime 申请时间
* remark 备注
* applyUser 用户名字
* content 内容
* first 标题
* @return
*/
private static String sendVxTemlate(Map<String,Object> map){
CheckUtil checkUtil = new CheckUtil(ticketSecrets);
checkUtil.setMap(map);
checkUtil.setValue("timeStamp", new Date().getTime()/1000);
VerificationCodeParam verificationCodeParam = new VerificationCodeParam();
verificationCodeParam.setSign(checkUtil.makeSign());
verificationCodeParam.setMap(map);
verificationCodeParam.setTimeStamp(Long.valueOf(new Date().getTime()/1000));
String jsonString = JSON.toJSONString(verificationCodeParam);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
String post = HttpUtils.sendJsonPost(url, jsonObject);
return post;
}
}
package yangtz.cs.liu.wechat.controller.api;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.core.domain.VerificationCodeParam;
import com.ruoyi.common.core.domain.AjaxResult;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
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 yangtz.cs.liu.campus.domain.temp.AuditoriumTemplate;
import yangtz.cs.liu.wechat.service.api.IWxApiService;
......@@ -15,7 +22,8 @@ import yangtz.cs.liu.wechat.utils.TimestampUtil;
/**
* 微信模板推送
*/
@RestController("/vx/temlate")
@RestController()
@RequestMapping("/vx/temlateses")
public class VxTemlateController {
@Autowired
......@@ -31,7 +39,7 @@ public class VxTemlateController {
* @return
*/
@PostMapping("/sendTemlate")
private AjaxResult sendTemlate(VerificationCodeParam param){
private AjaxResult sendTemlate(@RequestBody VerificationCodeParam param){
Boolean flag = false;
String msg = "";
// 验证信息是否被篡改
......@@ -46,17 +54,26 @@ public class VxTemlateController {
msg ="验证时间戳不合法";
}
if (flag){
AjaxResult.error(msg);
return AjaxResult.error(msg);
}
HashMap<String, Object> map = param.getMap();
Map<String, Object> map = param.getMap();
Date date =null;
String applyTime = map.get("applyTime").toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
date = dateFormat.parse(applyTime);
} catch (ParseException e) {
e.printStackTrace();
}
// 发送模板
AuditoriumTemplate auditoriumTemplate = new AuditoriumTemplate();
auditoriumTemplate.setApplyTime((Date)map.get("applyTime"));
auditoriumTemplate.setRemark(map.get("remark").toString());
auditoriumTemplate.setContent(map.get("content").toString());
auditoriumTemplate.setApplyUser(map.get("applyUser").toString());
auditoriumTemplate.setToUserOpenId(map.get("toUserOpenId").toString());
auditoriumTemplate.setFirst(map.get("first").toString());
auditoriumTemplate.setApplyTime(date);
auditoriumTemplate.setRemark(map.get("remark").toString()==null ?"":map.get("remark").toString());
auditoriumTemplate.setContent(map.get("content").toString()==null ?"":map.get("content").toString());
auditoriumTemplate.setApplyUser(map.get("applyUser").toString()==null ?"":map.get("applyUser").toString());
auditoriumTemplate.setToUserOpenId(map.get("toUserOpenId").toString()==null ?"":map.get("toUserOpenId").toString());
auditoriumTemplate.setFirst(map.get("first").toString()==null ?"":map.get("first").toString());
String s = iWxApiService.sendAuditoriumTemplate(auditoriumTemplate);
return AjaxResult.success(s);
}
......
......@@ -222,7 +222,7 @@ public class WxApiServiceImpl implements IWxApiService {
msgTemplate.setTemplate_id(teacherLeaveTemplateId);
// msgTemplate.setUrl("www.baidu.com");
msgTemplate.setTouser(toUserOpenId);
String jsonObject = JSONUtil.parseObj(template).toString();
String jsonObject = JSONUtil.parseObj(msgTemplate).toString();
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String result = HttpUtils.sendPost(sendUrl, jsonObject);
......@@ -250,7 +250,7 @@ public class WxApiServiceImpl implements IWxApiService {
msgTemplate.setTemplate_id(leaveResultTemplateId);
// msgTemplate.setUrl("www.baidu.com");
msgTemplate.setTouser(toUserOpenId);
String jsonObject = JSONUtil.parseObj(template).toString();
String jsonObject = JSONUtil.parseObj(msgTemplate).toString();
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String result = HttpUtils.sendPost(sendUrl, jsonObject);
......@@ -300,7 +300,7 @@ public class WxApiServiceImpl implements IWxApiService {
msgTemplate.setTemplate_id(studentLeaveTemplateId);
// msgTemplate.setUrl("www.baidu.com");
msgTemplate.setTouser(toUserOpenId);
String jsonObject = JSONUtil.parseObj(template).toString();
String jsonObject = JSONUtil.parseObj(msgTemplate).toString();
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String result = HttpUtils.sendPost(sendUrl, jsonObject);
......@@ -331,7 +331,7 @@ public class WxApiServiceImpl implements IWxApiService {
msgTemplate.setTemplate_id(purchaseTemplateId);
// msgTemplate.setUrl("www.baidu.com");
msgTemplate.setTouser(toUserOpenId);
String jsonObject = JSONUtil.parseObj(template).toString();
String jsonObject = JSONUtil.parseObj(msgTemplate).toString();
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String result = HttpUtils.sendPost(sendUrl, jsonObject);
......@@ -359,11 +359,13 @@ public class WxApiServiceImpl implements IWxApiService {
MsgTemplate msgTemplate = new MsgTemplate();
msgTemplate.setData(data);
msgTemplate.setTemplate_id(purchaseTemplateId);
//正式
// msgTemplate.setTemplate_id(purchaseTemplateId);
//测试环境id
msgTemplate.setTemplate_id("uR6Tta4qpv64ZGBByyjEkQ9N52HLMmNn9vHMESDdSF8");
// msgTemplate.setUrl("www.baidu.com");
msgTemplate.setTouser(toUserOpenId);
String jsonObject = JSONUtil.parseObj(template).toString();
String jsonObject = JSONUtil.parseObj(msgTemplate).toString();
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String result = HttpUtils.sendPost(sendUrl, jsonObject);
return result;
......@@ -393,7 +395,7 @@ public class WxApiServiceImpl implements IWxApiService {
msgTemplate.setTemplate_id(purchaseTemplateId);
// msgTemplate.setUrl("www.baidu.com");
msgTemplate.setTouser(toUserOpenId);
String jsonObject = JSONUtil.parseObj(template).toString();
String jsonObject = JSONUtil.parseObj(msgTemplate).toString();
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String result = HttpUtils.sendPost(sendUrl, jsonObject);
......@@ -425,7 +427,7 @@ public class WxApiServiceImpl implements IWxApiService {
msgTemplate.setTemplate_id(officialTemplateId);
// msgTemplate.setUrl("www.baidu.com");
msgTemplate.setTouser(toUserOpenId);
String jsonObject = JSONUtil.parseObj(template).toString();
String jsonObject = JSONUtil.parseObj(msgTemplate).toString();
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String result = HttpUtils.sendPost(sendUrl, jsonObject);
......
......@@ -94,10 +94,8 @@ public class WxLoginServiceImpl implements IWxLoginService {
loginUser.setAvatar(user.getAvatar());
/** 更新老师openId */
SchoolTeacher teacher = new SchoolTeacher();
teacher.setId(schoolTeacher.getId());
teacher.setOpenId(wxLoginBody.getOpenId());
schoolTeacherMapper.updateById(teacher);
schoolTeacher.setOpenId(wxLoginBody.getOpenId());
schoolTeacherMapper.updateById(schoolTeacher);
/** 更新用户表openId */
SysUser sysUser = new SysUser();
......
......@@ -6,6 +6,9 @@ import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -15,6 +18,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author lyric
......@@ -37,18 +41,20 @@ public class AccessTokenUtil {
String token = redisCache.getCacheObject(ACCESS_TOKEN);
if (StringUtils.isEmpty(token)) {
try {
String url = accessTokenUri;
//String url = accessTokenUri;
//测试环境
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx765a55464b308d13&secret=aa6741b17f05203975b17f0a64458741";
// 利用hutool的http工具类请求获取access_token
String result = HttpUtil.get(url);
// 将结果解析为json
JSONObject jsonObject = JSONUtil.parseObj(result);
// 获取access_token
String accessToken = jsonObject.getStr("access_token");
String accessToken = (String) jsonObject.get("access_token");
if (!StringUtils.isEmpty(accessToken)) {
token = accessToken;
token = accessToken;
log.info(accessToken);
// 将access_token存入redis
redisCache.setCacheObject("access_token", accessToken, 2, TimeUnit.HOURS);
redisCache.setCacheObject("access_token", accessToken, 5, TimeUnit.MINUTES);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
......
......@@ -23,6 +23,9 @@ public class CheckUtil {
public void setValue(String key,Object value) {
map.put(key, value);
}
public void setMap(Map<String,Object> maps) {
map.putAll(maps);
}
/**
* 检验签名是否正确
......
......@@ -8,6 +8,7 @@ public class SignUtil {
public static boolean validateMessage(VerificationCodeParam param,String secretKey) {
CheckUtil check = new CheckUtil(secretKey);
check.setMap(param.getMap());
check.setValue("timeStamp", param.getTimeStamp());
boolean result = check.checkSign(param.getSign());
return result;
......
......@@ -201,7 +201,7 @@
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="openId != null">open_id = #{openId},</if>
<if test="openId != null">open_id = #{openId}</if>
</trim>
where id = #{id}
</update>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment