修改bug

This commit is contained in:
yangjun 2023-06-13 16:47:12 +08:00
parent 1cbc1e997e
commit b2f21c9cf2
7 changed files with 240 additions and 52 deletions

View File

@ -0,0 +1,149 @@
package org.jeecg.modules.kc.grab.SynchronizationService;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xkcoding.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.kc.KcBdgxbcopy.entity.KcBdgxbcopy;
import org.jeecg.modules.kc.KcBdgxbcopy.service.IKcBdgxbcopyService;
import org.jeecg.modules.kc.grab.SynchronizationService.base.BaseSync;
import org.jeecg.modules.kc.kcMessagelistcopy.entity.KcMessagelistcopy;
import org.jeecg.modules.kc.kcMessagelistcopy.service.IKcMessagelistcopyService;
import org.jeecg.modules.wxgzh.WeChatTemplateMsg;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 服务大厅-本科教学服务评价
*/
@Slf4j
public class WechartSendmessageTask extends BaseSync {
@Autowired
private IKcBdgxbcopyService kcBdgxbcopyService;
@Autowired
private IKcMessagelistcopyService kcMessagelistcopyService;
// appId
// private static final String appId = "wx59920eb69d611d7f";//东师
private static final String appId = "wxafdbb51ba3aa614d";//测试
// appIdSecret
// private static final String appIdSecret = "";//东师
private static final String appIdSecret = "bdf346ae8b986e9fa5d33b2e4495b27e";//测试
// 公众号的模板id(也有相应的接口可以查询到)
// private static final String templateId = "KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys";//东师
private static final String templateId = "cFD7lyjpppIZUB9ipuGjkjZr9ED-BhJZ7bw3ILaFgbg";//测试
//微信通知点击后跳转的页面
private static final String domainTo = "https://zxkccx.webvpn.nenu.edu.cn";
/**
* 若参数变量名修改 QuartzJobController中也需对应修改
*/
@Override
public void execute(JobExecutionContext jobExecutionContext) {
start();
try {
run(getParamMap());
} catch (Exception e) {
e.printStackTrace();
}
end();
}
/**
* 有参定时任务实现
* @param param
*/
public void run(Map<String, Object> param) throws Exception {
// 模板参数
Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>();
String ywTime = (String)param.get("ywTime");
QueryWrapper<KcMessagelistcopy> kcMessagelistcopyQueryWrapper = new QueryWrapper<>();
kcMessagelistcopyQueryWrapper.ne("status","1");
kcMessagelistcopyQueryWrapper.eq("scheduleddatetime", ywTime);
List<KcMessagelistcopy> messqglist = kcMessagelistcopyService.list();
for(KcMessagelistcopy KcMessagelistcopy:messqglist){
// openId代表一个唯一微信用户即微信消息的接收人
String openId = KcMessagelistcopy.getTouser();
// 微信的基础accessToken
String accessToken = getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
/**
* 其他模板可以从模板库中自己添加
* 模板ID
* KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys
* 开发者调用模板消息接口时需提供模板ID
* 标题 上课提醒
* 行业 教育 - 院校
* 详细内容
* {{first.DATA}}
* 课程{{keyword1.DATA}}
* 时间{{keyword2.DATA}}
* 地点{{keyword3.DATA}}
* {{remark.DATA}}
*/
sendMag.put("first", new WeChatTemplateMsg(KcMessagelistcopy.getFirstdata()));
sendMag.put("keyword1", new WeChatTemplateMsg(KcMessagelistcopy.getKeyword1()));
sendMag.put("keyword2", new WeChatTemplateMsg(KcMessagelistcopy.getKeyword2()));
sendMag.put("keyword3", new WeChatTemplateMsg(KcMessagelistcopy.getKeyword3()));
sendMag.put("remark", new WeChatTemplateMsg(KcMessagelistcopy.getRemarkdata()));
RestTemplate restTemplate = new RestTemplate();
//拼接base参数
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("touser", openId); // openId
sendBody.put("url", KcMessagelistcopy.getTourl()); // 点击模板信息跳转地址
sendBody.put("topcolor", "#FF0000"); // 顶色
sendBody.put("data", sendMag); // 模板参数
sendBody.put("template_id", KcMessagelistcopy.getTemplateId()); // 模板Id
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, sendBody, String.class);
log.info("结果是: {}",forEntity.getBody());
JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
// 0
String messageCode = jsonObject.getString("errcode");
// 2431260672639467520
String msgId = jsonObject.getString("msgid");
System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
KcMessagelistcopy.setMsgid(msgId);
KcMessagelistcopy.setStatus("1");
KcMessagelistcopy.setSendtime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
kcMessagelistcopyService.updateById(KcMessagelistcopy);
}
}
public String getAccessToken() throws Exception{
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId +"&secret=" + appIdSecret;
String res = HttpUtil.get(url);
JSONObject jsonObject = JSONObject.parseObject(res);
String accessToken = jsonObject.getString("access_token");
log.info("accessToken{}", accessToken);
return accessToken;
}
/**
* 无参定时任务实现
*/
public void run(){
try {
run(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -61,7 +61,7 @@ public abstract class BaseSync implements Job {
}
}
public abstract void run(Map<String, Object> param);
public abstract void run(Map<String, Object> param) throws Exception;
public abstract void run();

View File

@ -3,6 +3,8 @@ package org.jeecg.modules.kc.kcMessagelistcopy.mapper;
import org.jeecg.modules.kc.kcMessagelistcopy.entity.KcMessagelistcopy;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
/**
* @Description: 订阅信息表
* @Author: jeecg-boot
@ -18,4 +20,5 @@ public interface KcMessagelistcopyMapper extends BaseMapper<KcMessagelistcopy> {
int insertStudentNextDayClass(String skrq, String txrq);
int insertTeacherNextDayClass(String skrq, String txrq);
}

View File

@ -42,7 +42,7 @@
0,
bd.openid,
'KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys',
'https://zxkccx.webvpn.nenu.edu.cn/index',
'https://zxkccx.webvpn.nenu.edu.cn',
CONCAT( kt.skjs, '老师,您有课程即将开始:' ),
'',
kcmc,
@ -75,7 +75,7 @@
0,
bd.openid,
'KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys',
'https://zxkccx.webvpn.nenu.edu.cn/index',
'https://zxkccx.webvpn.nenu.edu.cn',
CONCAT( xk.xm, '同学,您明天的课程安排如下:' ),
'',
concat( '2022-12-20',
@ -110,7 +110,7 @@
0,
bd.openid,
'KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys',
'https://zxkccx.webvpn.nenu.edu.cn/index',
'https://zxkccx.webvpn.nenu.edu.cn',
CONCAT( kt.skjs, '老师,您明天的课程安排如下:' ),
'',
concat( '2022-12-20',
@ -138,4 +138,5 @@
AND skrq = #{ skrq }
GROUP BY bd.openid, kt.skjs, WEEK
</insert>
</mapper>

View File

@ -3,6 +3,7 @@ package org.jeecg.modules.kc.kcMessagelistcopy.service;
import org.jeecg.modules.kc.kcMessagelistcopy.entity.KcMessagelistcopy;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
import java.util.Map;
/**

View File

@ -7,6 +7,8 @@ import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
/**
* @Description: 订阅信息表
* @Author: jeecg-boot

View File

@ -1,9 +1,14 @@
package org.jeecg.modules.wxgzh;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xkcoding.http.HttpUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.kc.KcBdgxbcopy.entity.KcBdgxbcopy;
import org.jeecg.modules.kc.KcBdgxbcopy.service.IKcBdgxbcopyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@ -13,7 +18,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -22,22 +29,27 @@ public class WxgzhController {
private static final Logger log = LoggerFactory.getLogger(WxgzhController.class);
// appId
// private static final String appId = "wx59920eb69d611d7f";
private static final String appId = "wxafdbb51ba3aa614d";
// private static final String appId = "wx59920eb69d611d7f";//东师
private static final String appId = "wxafdbb51ba3aa614d";//测试
// appIdSecret
private static final String appIdSecret = "bdf346ae8b986e9fa5d33b2e4495b27e";
// private static final String appIdSecret = "";//东师
private static final String appIdSecret = "bdf346ae8b986e9fa5d33b2e4495b27e";//测试
// 公众号的模板id(也有相应的接口可以查询到)
private static final String templateId = "KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys";
// private static final String templateId = "KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys";//东师
private static final String templateId = "cFD7lyjpppIZUB9ipuGjkjZr9ED-BhJZ7bw3ILaFgbg";//测试
// 微信申请的域名(提前准备)
private static final String domain = "http://bylwcs.nenu.edu.cn/wxpay";
private static final String domain = "https://zxkccx.webvpn.nenu.edu.cn/wxpay";
// 自定义跳转方法
private static final String redirectMethod = "/weixinoauth";
//微信通知点击后跳转的页面
private static final String domainTo = "http://www.baidu.com";
private static final String domainTo = "https://zxkccx.webvpn.nenu.edu.cn";
@Autowired
private IKcBdgxbcopyService kcBdgxbcopyService;
//1.先查询code
@RequestMapping("/wxpay/getCode")
@ -81,7 +93,19 @@ public class WxgzhController {
JSONObject obj = JSONObject.parseObject(res);
log.info("根据code查询得到openId:{}",openid);
log.info("obj:{}",obj);
return "http://www.baidu.com b";
// KcBdgxbcopy kcBdgxbcopy =
QueryWrapper<KcBdgxbcopy> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("openid",openid);
queryWrapper.eq("userid",state);
List<KcBdgxbcopy> list = kcBdgxbcopyService.list(queryWrapper);
if(list==null||list.size()>0){
KcBdgxbcopy KcBdgxbcopy = new KcBdgxbcopy();
KcBdgxbcopy.setUserid(state);
KcBdgxbcopy.setOpenid(openid);
KcBdgxbcopy.setBdtime(new Date());
kcBdgxbcopyService.save(KcBdgxbcopy);
}
return domainTo;
}
@GetMapping("/wxpay/getToken")
@ -102,52 +126,60 @@ public class WxgzhController {
@GetMapping("/wxpay/sendMessage")
public String sendMessage() {
public Result<String> sendMessage() throws Exception {
// 模板参数
Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>();
// openId代表一个唯一微信用户即微信消息的接收人
String openId = "oNB9p1BpVJEquxxxxxxxxx";
// 微信的基础accessToken
String accessToken = "57_LubK-8NKQc6C7jsLMxvdHaI0ju4x3-HPWEFhh7GKkw9fKbWhuxxoZyX4GaVIn6y4yO7RKfSlCyHdedKJlHUMZkd8457nKm0TOoaVkbzK1HCZ4g4gZdrmAGBylGBOZu9yxxxxxxxxxxxxxxxx";
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String userid = "2016900057";
/**
* 其他模板可以从模板库中自己添加
* 模板ID
* KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys
* 开发者调用模板消息接口时需提供模板ID
* 标题 上课提醒
* 行业 教育 - 院校
* 详细内容
* {{first.DATA}}
* 课程{{keyword1.DATA}}
* 时间{{keyword2.DATA}}
* 地点{{keyword3.DATA}}
* {{remark.DATA}}
*/
sendMag.put("first", new WeChatTemplateMsg("有课程即将开始"));
sendMag.put("keyword1", new WeChatTemplateMsg("线性代数"));
sendMag.put("keyword2", new WeChatTemplateMsg("[1,2节] 08:30-10:00"));
sendMag.put("keyword3", new WeChatTemplateMsg("上课地点N304"));
sendMag.put("remark", new WeChatTemplateMsg("请开发者为用户提供定制提醒的选项,以免打扰。"));
RestTemplate restTemplate = new RestTemplate();
//拼接base参数
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("touser", openId); // openId
sendBody.put("url", domainTo); // 点击模板信息跳转地址
sendBody.put("topcolor", "#FF0000"); // 顶色
sendBody.put("data", sendMag); // 模板参数
sendBody.put("template_id", templateId); // 模板Id
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, sendBody, String.class);
log.info("结果是: {}",forEntity.getBody());
JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
// 0
String messageCode = jsonObject.getString("errcode");
// 2431260672639467520
String msgId = jsonObject.getString("msgid");
System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
return forEntity.getBody();
QueryWrapper<KcBdgxbcopy> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userid",userid);
List<KcBdgxbcopy> list = kcBdgxbcopyService.list(queryWrapper);
for(KcBdgxbcopy kcBdgxbcopyPar:list){
String openId = kcBdgxbcopyPar.getOpenid();
// 微信的基础accessToken
String accessToken = getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
/**
* 其他模板可以从模板库中自己添加
* 模板ID
* KtWw0lwQ8FOgJdKgrmya0eoafGkMfMN8ECdZs8oSJys
* 开发者调用模板消息接口时需提供模板ID
* 标题 上课提醒
* 行业 教育 - 院校
* 详细内容
* {{first.DATA}}
* 课程{{keyword1.DATA}}
* 时间{{keyword2.DATA}}
* 地点{{keyword3.DATA}}
* {{remark.DATA}}
*/
sendMag.put("first", new WeChatTemplateMsg("有课程即将开始"));
sendMag.put("keyword1", new WeChatTemplateMsg("线性代数"));
sendMag.put("keyword2", new WeChatTemplateMsg("[1,2节] 08:30-10:00"));
sendMag.put("keyword3", new WeChatTemplateMsg("上课地点N304"));
sendMag.put("remark", new WeChatTemplateMsg("请开发者为用户提供定制提醒的选项,以免打扰。"));
RestTemplate restTemplate = new RestTemplate();
//拼接base参数
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("touser", openId); // openId
sendBody.put("url", domainTo); // 点击模板信息跳转地址
sendBody.put("topcolor", "#FF0000"); // 顶色
sendBody.put("data", sendMag); // 模板参数
sendBody.put("template_id", templateId); // 模板Id
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, sendBody, String.class);
log.info("结果是: {}",forEntity.getBody());
JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
// 0
String messageCode = jsonObject.getString("errcode");
// 2431260672639467520
String msgId = jsonObject.getString("msgid");
System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
}
return Result.OK("推送成功!");
}
}