添加机构邀请员工推送微信通知功能

This commit is contained in:
yangjun 2025-09-17 10:24:36 +08:00
parent 4f9a50d54e
commit 640ddde62a
7 changed files with 100 additions and 30 deletions

View File

@ -13,4 +13,6 @@ public interface IWxNoticeApi {
int unreadCount(String openId, String clientType);
void upRead(WxNoticeEntity dto);
void sendWechartMinMessage(WxNoticeEntity wxNoticeEntity);
}

View File

@ -95,7 +95,8 @@ public class NuEmployeesAdvisoryInfoServiceImpl extends ServiceImpl<NuEmployeesA
String accessToken = WechatMiniProgramUtils.getAccessToken();
// 2. 准备模板消息数据
String templateId = "CJ6NDNV4mTTyOdYhbksyA_YjDORVemJRmzEVAUZMBis";
// String templateId = "CJ6NDNV4mTTyOdYhbksyA_YjDORVemJRmzEVAUZMBis";
String templateId = "cWVzXm1C-iitx1gNFn1nZdijq9R_3fCv8vLbiqs9zww";
String page = "pages/index/index"; // 点击消息跳转的页面
Map<String, Object> data = new HashMap<>();
@ -108,9 +109,9 @@ public class NuEmployeesAdvisoryInfoServiceImpl extends ServiceImpl<NuEmployeesA
} else if (StringUtils.equals("3", dto.getAuditStatus())) {
auditStatus = "审核驳回";
}
data.put("thing5", content); // 对应模板中的字段
data.put("phrase6", auditStatus);
data.put("date2", DateUtils.now());
data.put("thing2", content); // 对应模板中的字段
data.put("phrase1", auditStatus);
data.put("date3", DateUtils.now());
// 3. 发送模板消息
String resInfo = TemplateMessageSender.sendTemplateMessage(accessToken, dto.getOpenId(), templateId, page, data);

View File

@ -274,7 +274,8 @@ public class OrgApplyInfoServiceImpl extends ServiceImpl<OrgApplyInfoMapper, Org
String accessToken = WechatMiniProgramUtils.getAccessToken();
// 2. 准备模板消息数据
String templateId = "CJ6NDNV4mTTyOdYhbksyA_YjDORVemJRmzEVAUZMBis";
// String templateId = "CJ6NDNV4mTTyOdYhbksyA_YjDORVemJRmzEVAUZMBis";
String templateId = "cWVzXm1C-iitx1gNFn1nZdijq9R_3fCv8vLbiqs9zww";
String page = "pages/index/index"; // 点击消息跳转的页面
Map<String, Object> data = new HashMap<>();
@ -285,9 +286,12 @@ public class OrgApplyInfoServiceImpl extends ServiceImpl<OrgApplyInfoMapper, Org
} else if (StringUtils.equals("2", type)) {
auditStatus = "审核驳回";
}
data.put("thing5", content); // 对应模板中的字段
data.put("phrase6", auditStatus);
data.put("date2", DateUtils.now());
// data.put("thing5", content); // 对应模板中的字段
// data.put("phrase6", auditStatus);
// data.put("date2", DateUtils.now());
data.put("thing2", content); // 对应模板中的字段
data.put("phrase1", auditStatus);
data.put("date3", DateUtils.now());
// 3. 发送模板消息
String resInfo = TemplateMessageSender.sendTemplateMessage(accessToken, openId, templateId, page, data);

View File

@ -46,13 +46,17 @@ public class WechatMiniController {
String accessToken = WechatMiniProgramUtils.getAccessToken();
// 2. 准备模板消息数据
String templateId = "CJ6NDNV4mTTyOdYhbksyA_YjDORVemJRmzEVAUZMBis";
// String templateId = "CJ6NDNV4mTTyOdYhbksyA_YjDORVemJRmzEVAUZMBis";
String templateId = "cWVzXm1C-iitx1gNFn1nZdijq9R_3fCv8vLbiqs9zww";
String page = "pages/index/index"; // 点击消息跳转的页面
Map<String, Object> data = new HashMap<>();
data.put("thing5", "详情请咨询客服进行了解"); // 对应模板中的字段
data.put("phrase6", "审核通过");
data.put("date2", DateUtils.now());
// data.put("thing5", "详情请咨询客服进行了解"); // 对应模板中的字段
// data.put("phrase6", "审核通过");
// data.put("date2", DateUtils.now());
data.put("thing2", "详情请咨询客服进行了解"); // 对应模板中的字段
data.put("phrase1", "审核通过");
data.put("date3", DateUtils.now());
// 3. 发送模板消息
String resInfo = TemplateMessageSender.sendTemplateMessage( accessToken, openId, templateId, page, data);

View File

@ -110,26 +110,26 @@ public class WeixinController {
System.out.println("-------------------444444444444444----------"+tokenNode);
accessToken = tokenNode.get("session_key").asText();
System.out.println("-------------------55555555555555----------"+accessToken);
// String openId = tokenNode.get("openid").asText();
// System.out.println("-------------------666666666666666----------"+openId);
//
// String userInfoUrl = String.format(
// "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN",
// accessToken, openId
// );
// System.out.println("-------------------777777777777777----------"+userInfoUrl);
//
// String userInfoResponse = restTemplate.getForObject(userInfoUrl, String.class);
// System.out.println("-------------------888888888888888----------"+userInfoResponse);
// JsonNode userInfoNode = objectMapper.readTree(userInfoResponse);
// System.out.println("-------------------999999999999999----------"+userInfoNode);
//
// model.addAttribute("openid", openId);
// System.out.println("-------------------999999999999999----------"+openId);
String openId = tokenNode.get("openid").asText();
System.out.println("-------------------666666666666666----------"+openId);
String userInfoUrl = String.format(
"https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN",
accessToken, openId
);
System.out.println("-------------------777777777777777----------"+userInfoUrl);
String userInfoResponse = restTemplate.getForObject(userInfoUrl, String.class);
System.out.println("-------------------888888888888888----------"+userInfoResponse);
JsonNode userInfoNode = objectMapper.readTree(userInfoResponse);
System.out.println("-------------------999999999999999----------"+userInfoNode);
model.addAttribute("openid", openId);
System.out.println("-------------------999999999999999----------"+openId);
// model.addAttribute("nickname", userInfoNode.get("nickname").asText());
// model.addAttribute("headimgurl", userInfoNode.get("headimgurl").asText());
// model.addAttribute("code", code);
// retJson = tokenNode;
model.addAttribute("code", code);
retJson = tokenNode;
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("openid", "Error parsing JSON response");

View File

@ -1,21 +1,32 @@
package com.nu.modules.wxnotice.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nu.entity.WxNoticeEntity;
import com.nu.modules.weixin.utils.TemplateMessageSender;
import com.nu.modules.weixin.utils.WechatMiniProgramUtils;
import com.nu.modules.wxnotice.IWxNoticeApi;
import com.nu.modules.wxnotice.entity.WxNotice;
import com.nu.modules.wxnotice.mapper.WxNoticeMapper;
import com.nu.modules.wxnotice.service.IWxNoticeService;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.base.service.BaseCommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description: 小程序消息通知
@ -26,6 +37,11 @@ import java.util.List;
@Service
public class WxNoticeServiceImpl extends ServiceImpl<WxNoticeMapper, WxNotice> implements IWxNoticeService, IWxNoticeApi {
@Autowired
private BaseCommonService baseCommonService;
@Autowired
private ISysBaseAPI sysBaseAPI;
@Override
public void addNotice(WxNoticeEntity dto) {
WxNotice wxNotice = new WxNotice();
@ -68,4 +84,45 @@ public class WxNoticeServiceImpl extends ServiceImpl<WxNoticeMapper, WxNotice> i
wxNotice.setIzRead("0");
baseMapper.update(wxNotice, uw);
}
@Override
public void sendWechartMinMessage(WxNoticeEntity wxNoticeEntity) {
try {
// 1. 获取access token
String accessToken = WechatMiniProgramUtils.getAccessToken();
// 2. 准备模板消息数据
// String templateId = "CJ6NDNV4mTTyOdYhbksyA_YjDORVemJRmzEVAUZMBis";
String templateId = "yvvhlRft1CAdtU-dWlCW8U63VnTwRU8w3gR0BUdqcWw";
String page = "pages/oldmanindex/companyyaoqing"; // 点击消息跳转的页面
com.alibaba.fastjson.JSONObject orgInfo = sysBaseAPI.getOrgInfo(wxNoticeEntity.getOrgCode());
String companyName = orgInfo.getString("departName");
String openId = wxNoticeEntity.getOpenId();
Map<String, Object> data = new HashMap<>();
String tips = "请及时参加邀约哦";
data.put("thing1", companyName); // 对应模板中的字段
data.put("thing4", tips);
data.put("date3", DateUtils.now());
// 3. 发送模板消息
String resInfo = TemplateMessageSender.sendTemplateMessage(accessToken, openId, templateId, page, data);
if (com.alibaba.cloud.commons.lang.StringUtils.equals(resInfo, "error")) {
baseCommonService.addLog("机构邀请员工入驻,发送通知失败-发送消息异常112 " + resInfo, CommonConstant.LOG_TYPE_2, 1);
}
JSONObject jsonObject = new JSONObject(resInfo);
boolean success = jsonObject.getInt("errcode") == 0;
if (success) {
System.out.println("模板消息发送成功");
baseCommonService.addLog("机构邀请员工入驻,发送通知成功 ", CommonConstant.LOG_TYPE_2, 1);
} else {
baseCommonService.addLog("机构邀请员工入驻,发送通知失败-发送消息异常121 " + resInfo, CommonConstant.LOG_TYPE_2, 1);
System.out.println("模板消息发送失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -169,5 +169,7 @@ public class EmployeesMQListener {
wxNoticeEntity.setClientType(WxClientType.EMPLOYEE.getCode());
wxNoticeApi.addNotice(wxNoticeEntity);
wxNoticeApi.sendWechartMinMessage(wxNoticeEntity);
}
}