小秘书消息定时推送【搬迁音萌】

This commit is contained in:
liaozetao
2023-08-04 18:19:37 +08:00
parent f2641495d5
commit 300d99fcf5
12 changed files with 553 additions and 292 deletions

View File

@@ -3,21 +3,21 @@ package com.accompany.admin.service;
import com.accompany.admin.common.BusinessException;
import com.accompany.admin.service.base.BaseService;
import com.accompany.admin.vo.message.RecallQO;
import com.accompany.business.model.MsgPushRecord;
import com.accompany.business.model.MsgPushRecordExample;
import com.accompany.business.mybatismapper.MsgPushRecordMapper;
import com.accompany.business.mybatismapper.UsersMapperExpend;
import com.accompany.business.param.neteasepush.*;
import com.accompany.business.service.SendSysMsgService;
import com.accompany.business.service.MsgPushService;
import com.accompany.business.service.user.UsersService;
import com.accompany.business.util.ReplaceDomainUtil;
import com.accompany.common.config.SystemConfig;
import com.accompany.common.constant.Attach;
import com.accompany.common.constant.Constant;
import com.accompany.core.enumeration.BusinessStatusCodeEnum;
import com.accompany.core.model.Users;
import com.accompany.core.model.UsersExample;
import com.accompany.core.mybatismapper.UsersMapper;
import com.alibaba.fastjson.JSONObject;
import com.accompany.core.vo.BaseResponseVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,7 +25,8 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.Date;
import java.util.List;
/**
* 66小秘书给前台发消息
@@ -34,17 +35,27 @@ import java.util.*;
@Service
public class MsgPushAdminService extends BaseService {
// 消息发送者
private static final Byte SECRETARY = 0;
private static final Byte OFFICIAL = 1;
private static final Byte ACTIVITY = 2;
@Autowired
private MsgPushRecordMapper msgPushRecordMapper;
@Autowired
private UsersMapperExpend usersMapperExpend;
@Autowired
private UsersMapper usersMapper;
@Autowired
private SendSysMsgService sendSysMsgService;
@Autowired
private UsersService usersService;
@Autowired
private MsgPushService msgPushService;
public PageInfo<MsgPushRecord> getMsgList(Integer pageNumber, Integer pageSize, Byte msgType, String erbanNos) {
MsgPushRecordExample msgPushRecordExample = new MsgPushRecordExample();
msgPushRecordExample.setOrderByClause("crate_time DESC");
@@ -67,24 +78,25 @@ public class MsgPushAdminService extends BaseService {
// 保存信息
public int saveMsg(String erbanNos, Byte msgType, Byte toObjType, String words, String pic, String title,
String desc, String picUrl, String webUrl, Byte skipType, String adminId, String skipContent,String appId) {
String desc, String picUrl, String webUrl, Byte skipType, String adminId, String skipContent, String appId, Date pushTime, Byte msgFrom) {
MsgPushRecord msgPushRecord = new MsgPushRecord();
// 新增房间跳转逻辑
if(Constant.SecretarySkipType.ROOM.equals(skipType)){
if(StringUtils.isEmpty(skipContent)){
if (Constant.SecretarySkipType.ROOM.equals(skipType)) {
if (StringUtils.isEmpty(skipContent)) {
logger.error("参数错误");
throw new BusinessException("参数错误");
}
Users users = usersService.getUserByErbanNo(Long.parseLong(skipContent));
if(users == null){
if (users == null) {
logger.error("查询不到该用户房间");
throw new BusinessException("查询不到该用户房间");
}
msgPushRecord.setSkipUri(users.getUid().toString());
}else{
} else {
msgPushRecord.setSkipUri(skipContent);
}
msgPushRecord.setFromAccid(Long.parseLong(SystemConfig.secretaryUid));
msgPushRecord.setMsgFrom(SECRETARY);
msgPushRecord.setMsgType(msgType);
msgPushRecord.setToObjType(toObjType);
msgPushRecord.setToErbanNos(erbanNos);
@@ -92,7 +104,7 @@ public class MsgPushAdminService extends BaseService {
msgPushRecord.setAdminId(adminId);
msgPushRecord.setSkipType(skipType);
msgPushRecord.setAppId(appId);
StringBuffer sbf = new StringBuffer();
StringBuilder sbf = new StringBuilder();
if (!StringUtils.isEmpty(erbanNos)) {
for (String erbanNo : erbanNos.split(",")) {
Long uid = erbanNoToUid(erbanNo.trim());
@@ -102,16 +114,20 @@ public class MsgPushAdminService extends BaseService {
} else {
msgPushRecord.setToAccids("0");
}
saveMsgType(msgPushRecord,msgType, words, pic, title, desc, picUrl, webUrl);//存消息类型
sendSysMsgToUsers(msgPushRecord);//发送消息
saveMsgType(msgPushRecord, msgType, words, pic, title, desc, picUrl, webUrl);//存消息类型
// 有推送时间则不立刻发送
if (pushTime == null) {
msgPushService.sendSysMsgToUsers(msgPushRecord);//发送消息
} else {
msgPushRecord.setPushTime(pushTime);
msgPushRecord.setPushStatus(Constant.MsgPushStatus.NOT_PUST);
}
msgPushRecordMapper.insert(msgPushRecord);
return 1;
}
//保存消息类型
public void saveMsgType(MsgPushRecord msgPushRecord,Byte msgType, String words, String pic, String title, String desc, String picUrl, String webUrl) {
public void saveMsgType(MsgPushRecord msgPushRecord, Byte msgType, String words, String pic, String title, String desc, String picUrl, String webUrl) {
if (!StringUtils.isEmpty(msgType)) {
msgPushRecord.setMsgType(msgType);
switch (msgType) {
@@ -136,170 +152,6 @@ public class MsgPushAdminService extends BaseService {
}
/**
* 发送文字、图片、图文 目前仅支持跳H5 和 跳转房间 (跳转房间仅支持文字))
* @param msgPushRecord
*/
public void sendSysMsgToUsers(MsgPushRecord msgPushRecord) {
switch (msgPushRecord.getToObjType().intValue()){
case Constant.PushMsgType.special :// 指定接收用户
NeteaseSendMsgBatchParam neteaseSendMsgBatchParam = new NeteaseSendMsgBatchParam();
neteaseSendMsgBatchParam.setFromAccid(String.valueOf(msgPushRecord.getFromAccid()));
this.buildNeteaseSendMsgBatchParam(neteaseSendMsgBatchParam,msgPushRecord);
sendSysMsgService.sendBatchMsgMsg(neteaseSendMsgBatchParam);
break;
case Constant.PushMsgType.all ://群发消息(高级群)
/**
* 1、分页查询每次查询50个uid
* 2、循环处理每次50累计满5次休眠10s
*/
sendGroupMsg(msgPushRecord);
break;
}
}
/**
* 构建批量发送消息参数体
* @param neteaseSendMsgBatchParam
* @param msgPushRecord
*/
private void buildNeteaseSendMsgBatchParam(NeteaseSendMsgBatchParam neteaseSendMsgBatchParam,MsgPushRecord msgPushRecord){
// 0 表示文本消息,1 表示图片100 自定义消息类型
switch (msgPushRecord.getMsgType()){
case 0 :
if(StringUtils.isEmpty(msgPushRecord.getSkipUri())){
// 批量发送文字(不带跳转链接)
neteaseSendMsgBatchParam.setContent(msgPushRecord.getMsgDesc());
}else if(Constant.SecretarySkipType.H5.equals(msgPushRecord.getSkipType())){
// 批量发送文字(带跳转链接)自定义类型
neteaseSendMsgBatchParam.setType(Constant.DefineProtocol.CUSTOM_MESS_DEFINE);
Body body = new Body();
body.setData(this.buildAttach(msgPushRecord,Constant.SecretarySkipType.H5));
neteaseSendMsgBatchParam.setBody(body);
}else{
// 批量发送文字(带跳转房间)自定义类型
neteaseSendMsgBatchParam.setType(Constant.DefineProtocol.CUSTOM_MESS_DEFINE);
Body body = new Body();
body.setData(this.buildAttach(msgPushRecord,Constant.SecretarySkipType.ROOM));
neteaseSendMsgBatchParam.setBody(body);
}
break;
case 1 :
// 发图片
neteaseSendMsgBatchParam.setType(1);
neteaseSendMsgBatchParam.setContent( gson.toJson(this.buildPicture(msgPushRecord.getPicUrl())));
break;
case 100 :
// 图文 (带跳转)
sendBatchPicAndWords(neteaseSendMsgBatchParam,msgPushRecord);
break;
default:
logger.error("参数不正确");
break;
}
if(!msgPushRecord.getToAccids().equals("0")){
List<String> uidList = com.accompany.common.utils.StringUtils.splitToList(msgPushRecord.getToAccids(),",");
neteaseSendMsgBatchParam.setToAccids(uidList);
}
Payload payload = new Payload();
payload.setSkiptype(msgPushRecord.getSkipType());
payload.setData(msgPushRecord.getSkipUri());
neteaseSendMsgBatchParam.setPayload(payload);
}
/**
* 构建自定义文字带跳转功能的参数体
* @param msgPushRecord
* @return
*/
private Attach buildAttach(MsgPushRecord msgPushRecord,Byte skipType){
Attach attach = new Attach();
attach.setFirst(Constant.DefineProtocol.CUSTOM_MESS_HEAD_SECRETARY);
attach.setSecond(Constant.DefineProtocol.CUSTOM_MESS_SUB_SECRETARY_INTRACTION);
JSONObject jsonObject = new JSONObject();
jsonObject.put("title", msgPushRecord.getMsgDesc());
jsonObject.put("msg", msgPushRecord.getMsgDesc());
jsonObject.put("routerType", skipType);
jsonObject.put("routerValue", msgPushRecord.getSkipUri());
attach.setData(jsonObject);
return attach;
}
/**
* 构建图片参数体
* @param picUrl
* @return
*/
private Picture buildPicture(String picUrl){
Picture picture = new Picture();
picture.setName("图片");
picture.setExt("jpg");
picture.setMd5(String.valueOf(System.currentTimeMillis()));
picture.setUrl(picUrl);
picture.setH(6814);
picture.setW(2332);
picture.setSize(388245);
return picture;
}
//群发图文
public void sendBatchPicAndWords(NeteaseSendMsgBatchParam neteaseSendMsgBatchParam,MsgPushRecord msgPushRecord) {
neteaseSendMsgBatchParam.setType(msgPushRecord.getMsgType());
Body body = new Body();
body.setFirst(Constant.DefMsgType.PointToPointMsg);
body.setSecond(Constant.DefMsgType.PushPicWordMsg);
PicWordMsgAttach picWordMsgAttach = new PicWordMsgAttach();
picWordMsgAttach.setDesc(msgPushRecord.getMsgDesc());
picWordMsgAttach.setPicUrl(msgPushRecord.getPicUrl());
picWordMsgAttach.setTitle(msgPushRecord.getTitle());
if (msgPushRecord.getWebUrl() != null) {
picWordMsgAttach.setWebUrl(msgPushRecord.getWebUrl());
}
if (msgPushRecord.getSkipUri() != null) {
picWordMsgAttach.setWebUrl(msgPushRecord.getSkipUri());
}
body.setData(picWordMsgAttach);
neteaseSendMsgBatchParam.setBody(body);
}
//群发文字或者图片
public void sendGroupMsg(MsgPushRecord msgPushRecord) {
int page = 0;
int batchSize = 500;
List<String> list = new ArrayList<>();
Map<String, Object> param = new HashMap<String, Object>();
param.put("appId", msgPushRecord.getAppId());
List<Users> users = new ArrayList<Users>();
long num = usersMapperExpend.selectCount(param);//查询总数
int m = num % batchSize == 0 ? (int) num / batchSize : (int) (num / batchSize) + 1;
int j = 1;
for (; j <= m; j++) {//控制次数
NeteaseSendMsgBatchParam neteaseSendMsgBatchParam = new NeteaseSendMsgBatchParam();
list.clear();
page = (j - 1) * batchSize;
param.put("currentPage", page);//当前页数
param.put("pageSize", batchSize);//每一页条数
users = usersMapperExpend.selectByPage(param);//获取200条记录
if (!CollectionUtils.isEmpty(users)) {
for (Users user : users) {
list.add(String.valueOf(user.getUid()));
}
}
neteaseSendMsgBatchParam.setToAccids(list);
neteaseSendMsgBatchParam.setFromAccid(msgPushRecord.getFromAccid().toString());
this.buildNeteaseSendMsgBatchParam(neteaseSendMsgBatchParam,msgPushRecord);
sendSysMsgService.sendBatchMsgMsg(neteaseSendMsgBatchParam);
}
logger.debug("共发送了{}次信息",j-1);
}
private Long erbanNoToUid(String erBanNo) {
Long uids = 0L;
if (!StringUtils.isEmpty(erBanNo)) {
@@ -317,5 +169,17 @@ public class MsgPushAdminService extends BaseService {
}
public BaseResponseVO<Void> recall(RecallQO qo) {
logger.info("消息撤销 qo: {}", qo);
Integer recordId = qo.getRecordId();
MsgPushRecord record = msgPushRecordMapper.selectByPrimaryKey(recordId);
Byte pushStatus = record.getPushStatus();
if (Constant.MsgPushStatus.PUSH.equals(pushStatus)) {
return new BaseResponseVO<>(500, "该消息已推送,不可撤销。");
}
int delete = msgPushRecordMapper.deleteByPrimaryKey(recordId);
logger.info("已删除待发送消息 delete: {}", delete);
return new BaseResponseVO<>(BusinessStatusCodeEnum.SUCCESS);
}
}

View File

@@ -0,0 +1,19 @@
package com.accompany.admin.vo.message;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* @author: Harry Zhang
* @since: 2022/12/26 16:08
*/
@Data
@Accessors(chain = true)
public class RecallQO {
@NotNull
private Integer recordId;
}

View File

@@ -1,6 +1,8 @@
package com.accompany.admin.controller.feedback;
import com.accompany.admin.vo.message.RecallQO;
import com.accompany.core.vo.BaseResponseVO;
import com.alibaba.fastjson.JSONObject;
import com.accompany.admin.controller.BaseController;
import com.accompany.admin.service.MsgPushAdminService;
@@ -13,20 +15,21 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
/**
* 后台消息管理
* Created by fxw on 2018/01/02
*/
@Controller
@RequestMapping("/admin")
public class MessageAdminController extends BaseController{
@Controller
@RequestMapping("/admin")
public class MessageAdminController extends BaseController {
@Autowired
private MsgPushAdminService msgPushAdminService;
@Autowired
@@ -35,73 +38,81 @@ import org.springframework.web.multipart.MultipartFile;
private SendSysMsgService sendSysMsgService;
private static final Logger logger = LoggerFactory.getLogger(MessageAdminController.class);
/**
* 消息列表
*/
@RequestMapping(value="/msgAdmin/getMsgList")
@RequestMapping(value = "/msgAdmin/getMsgList")
@ResponseBody
public void getMsgList(Byte msgType,String erbanNOs){
PageInfo<MsgPushRecord> pageInfo = msgPushAdminService.getMsgList(getPageNumber(),getPageSize(),msgType,erbanNOs);
public void getMsgList(Byte msgType, String erbanNOs) {
PageInfo<MsgPushRecord> pageInfo = msgPushAdminService.getMsgList(getPageNumber(), getPageSize(), msgType, erbanNOs);
JSONObject jsonObject = new JSONObject();
jsonObject.put("total",pageInfo.getTotal());
jsonObject.put("rows",pageInfo.getList());
jsonObject.put("total", pageInfo.getTotal());
jsonObject.put("rows", pageInfo.getList());
writeJson(jsonObject.toJSONString());
}
/**
* 保存消息
*
* @param
* @return
*/
@RequestMapping(value = "messageAdmin/saveMessage")
@ResponseBody
public int saveMsg(String erbanNos,Byte msgType, Byte toObjType,String words, String pic,String title,String desc,
String picUrl,String webUrl,Byte skipType,String skipContent,String appId){
public int saveMsg(String erbanNos, Byte msgType, Byte toObjType, String words, String pic, String title, String desc,
String picUrl, String webUrl, Byte skipType, String skipContent, String appId, Date pushTime, Byte msgFrom) {
int adminId = getAdminId();
if(adminId == -1){
return 2;
if (adminId == -1) {
return 2;
}
return msgPushAdminService.saveMsg(erbanNos,msgType,toObjType,words,pic,title,desc,picUrl,webUrl,skipType,
String.valueOf(adminId),skipContent,appId);
}
@RequestMapping(value="msg/headimg")
@ResponseBody
public void uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile){
String msg = null;
JSONObject jsonObject = new JSONObject();
if(!uploadFile.isEmpty()){
try{
String filepath = qiuniuService.uploadByStream(uploadFile.getInputStream());
if(!BlankUtil.isBlank(filepath)){
jsonObject.put("path",qiuniuService.mergeUrlAndSlim(filepath));
}
} catch (Exception e){
logger.error("upload fail, "+ e.getMessage());
msg = "上传失败I/O流异常";
}
}else{
msg = "上传失败,表单类型不正确!";
}
jsonObject.put("msg",msg);
writeJson(jsonObject.toJSONString());
return msgPushAdminService.saveMsg(erbanNos, msgType, toObjType, words, pic, title, desc, picUrl, webUrl, skipType,
String.valueOf(adminId), skipContent, appId, pushTime, msgFrom);
}
/**
* 消息撤销
*/
@PostMapping(value = "messageAdmin/recall")
@ResponseBody
public BaseResponseVO recall(@RequestBody @Validated RecallQO qo) {
return msgPushAdminService.recall(qo);
}
@RequestMapping(value = "msg/headimg")
@ResponseBody
public void uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile) {
String msg = null;
JSONObject jsonObject = new JSONObject();
if (!uploadFile.isEmpty()) {
try {
String filepath = qiuniuService.uploadByStream(uploadFile.getInputStream());
if (!BlankUtil.isBlank(filepath)) {
jsonObject.put("path", qiuniuService.mergeUrlAndSlim(filepath));
}
} catch (Exception e) {
logger.error("upload fail, " + e.getMessage());
msg = "上传失败I/O流异常";
}
} else {
msg = "上传失败,表单类型不正确!";
}
jsonObject.put("msg", msg);
writeJson(jsonObject.toJSONString());
}
//广播接口
@RequestMapping(value = "messageAdmin/broadCastMsg")
@ResponseBody
public void broadCastMsg(String from,String param){
sendSysMsgService.broadCastMsg(from,param);
public void broadCastMsg(String from, String param) {
sendSysMsgService.broadCastMsg(from, param);
}
}

View File

@@ -47,10 +47,11 @@
<option value="1">图片</option>
<option value="100">图文</option>
</select>
<label for="erbanNum" class="col-sm-2 control-label">平台号:</label>
<div class="col-sm-3">
<span><input type="text" class="form-control validate[required]" name="erbanNo" id="erbanNum" ></span>
</div>
<label for="erbanNum" class="col-sm-2 control-label">平台号:</label>
<div class="col-sm-3">
<span><input type="text" class="form-control validate[required]" name="erbanNo"
id="erbanNum"></span>
</div>
<button id="btnSearch" class="btn btn-sm btn-primary">查询</button>
<button id="add" class="btn btn-default">
<i class="glyphicon glyphicon-plus"></i>增加
@@ -65,7 +66,8 @@
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">x</span></button>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">x</span></button>
<h4 class="modal-title" id="addModalLabel">新增消息</h4>
</div>
<div class="modal-body">
@@ -81,12 +83,14 @@
</div>
<div class="form-group" id="appIdDiv">
<label for="appId" class="col-sm-3 control-label">app</label>
<label for="appId" class="col-sm-3 control-label">渠道</label>
<div class="col-sm-8">
<select name="appId" id="appId" class="col-sm-3">
<option value="">全部</option>
<option value="yinyou">平台</option>
<option value="qingxun">66</option>
<option value="official">安卓官方包</option>
<option value="google">安卓谷歌包</option>
<option value="peko_Enterprise">ios企业包</option>
<option value="youmi_appstore">ios商店包</option>
</select>
</div>
</div>
@@ -94,71 +98,76 @@
<div class="form-group">
<label for="msgType" class="col-sm-3 control-label">消息类型:</label>
<div class="col-sm-8">
<select name="msgTypeList" id="msgType" class="col-sm-3">
<option value="0">文本</option>
<option value="1">图片</option>
<option value="100">图文</option>
</select>
<select name="msgTypeList" id="msgType" class="col-sm-3">
<option value="0">文本</option>
<option value="1">图片</option>
<option value="100">图文</option>
</select>
</div>
</div>
<div class="form-group" id ="skipUrlDiv">
<label for="skip" class="col-sm-3 control-label">跳转来源:</label>
<div class="form-group" id="skipUrlDiv">
<label for="skip" class="col-sm-3 control-label">跳转来源</label>
<div class="col-sm-8">
<select name="skip" id="skip" class="col-sm-3">
<option value=""></option>
<option value="2">跳H5页面</option>
<option value="1" id ="skipRoomType">跳转房间</option>
<option value="1" id="skipRoomType">跳转房间</option>
</select>
</div>
<div class="col-sm-6">
<input type="text" class="form-control" name="skipUri" id="skipUrlContent">
</div>
</div>
<div class="form-group" id ="mmm">
<label for="aimErbanNo" class="col-sm-3 control-label">请输入指定平台号,多个平台号用逗号隔开:</label>
<div class="form-group" id="mmm">
<label for="aimErbanNo"
class="col-sm-3 control-label">请输入指定平台号,多个平台号用逗号隔开:</label>
<div class="col-sm-8">
<input class="form-control validate[required]" name="aimErbanNo" id="aimErbanNo">
</div>
</div>
<div class="form-group" id ="nnn">
<div class="form-group" id="nnn">
<label for="words" class="col-sm-3 control-label">文本:</label>
<div class="col-sm-8">
<input class="form-control validate[required]" name="words" id="words">
</div>
</div>
<div class="form-group" id ="ppp">
<div class="form-group" id="ppp">
<label for="pic" class="col-sm-3 control-label">图片:</label>
<div class="col-sm-8">
<img src="" id="imgUrl" style="width:250px;height:90px;" alt="">
<input type="file" id="uploadFile" name="uploadFile" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg">
<input type="file" id="uploadFile" name="uploadFile"
accept="image/gif,image/jpeg,image/jpg,image/png,image/svg">
<button class="btn btn-success" type="button" id="uploadBtn">上传</button>
<span class="attention">注意:图片尺寸请注意在750*250,选择图片后请点击上传按钮</span>
<input type="hidden" id="pic" name="pic" class="form-control validate[required]"/>
</div>
</div>
<div class="form-group" id ="ooo">
<div class="form-group" id="ooo">
<label for="wordsAndPic1" class="col-sm-3 control-label">图文:</label>
<div class="col-sm-8">
标题:<input class="form-control validate[required]" name="title" id="title">
内容:<input class="form-control validate[required]" name="desc" id="desc">
<!--封面图-->
<img src="" id="imgUrl1" style="width:250px;height:90px;" alt="">
<input type="file" id="uploadFile1" name="uploadFile" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg">
<input type="file" id="uploadFile1" name="uploadFile"
accept="image/gif,image/jpeg,image/jpg,image/png,image/svg">
<button class="btn btn-success" type="button" id="uploadBtn1">上传</button>
封面图:<span class="attention">尺寸小于750*250,选择图片后请点击上传按钮</span>
<input type="hidden" id="wordsAndPic1" name="wordsAndPic1" class="form-control validate[required]"/>
<input type="hidden" id="wordsAndPic1" name="wordsAndPic1"
class="form-control validate[required]"/>
</div>
</div>
<!--<div class="form-group">
<label for="msgDesc" class="col-sm-3 control-label">消息内容:</label>
<div class="col-sm-8">
<input type="text" class="form-control validate[required]" name="msgDesc" id="msgDesc">
<div class="form-group">
<label for="pushTime" class="col-sm-3 control-label">推送时间(非必填)</label>
<div class="col-sm-9">
<input type="text" class="input-sm form-control datetime" name="pushTime" id="pushTime">
</div>
</div>-->
</div>
</form>
</div>
<div class="modal-footer">
@@ -170,7 +179,6 @@
</div>
<script>
$(function () {
@@ -260,19 +268,46 @@
return '-';
}
}
}
/*,{
},
{
field: 'pushTime',
title: '发送时间',
align: 'center',
width: '10%',
valign: 'middle',
formatter: function (val, row, index) {
if (val) {
var date = new Date(val);
return date.format("yyyy-MM-dd hh:mm");
} else {
return '-';
}
}
},
{
field: 'tmp',
title: '操作',
align: 'center',
width: '20%',
width: '12%',
valign: 'middle',
formatter: function (val, row, index) {
var key = row.blockId;
return "<button class='btn btn-sm btn-success opt-release' data-id=" + key + ">解封</button>&nbsp;&nbsp;"+
"<button class='btn btn-sm btn-success opt-edit' data-id=" + key + " >编辑</button>";
var key = row.recordId;
var pushTime = row.pushTime;
var date = new Date(pushTime);
let format = date.format("yyyy-MM-dd hh:mm");
let now = new Date().format("yyyy-MM-dd hh:mm");
console.log("format: " + format)
console.log("now: " + now)
let condition = format > now;
console.log("condition: " + condition)
if (condition) {
var str = "<button class='btn btn-sm btn-danger opt-del' data-id=" + key + ">撤销</button>";
return str;
} else {
return '-';
}
}
}*/
}
],
cache: false,
striped: true,
@@ -315,12 +350,11 @@
/*保存*/
$('#addSave').on('click', function () {
if ($('#addMsgForm').validationEngine('validate')) {
if($("#msgType").val()==100 && ($("#skipUrlContent").val() == undefined || $("#skipUrlContent").val().length <= 0)){
if($("#msgType").val() == 100 && ($("#skipUrlContent").val() == undefined || $("#skipUrlContent").val().length <= 0)){
alert("未填写跳转链接");
}else{
$('#addSave').attr("disabled",true);
if($("#msgType").val()==1){
} else {
$('#addSave').attr("disabled", true);
if($("#msgType").val() == 1) {
$('#skipUrlContent').val('');
}
$.ajax({
@@ -337,7 +371,8 @@
picUrl:$('#wordsAndPic1').val(),
skipType:$('#skip').val(),
skipContent:$('#skipUrlContent').val(),
appId:$("#appId").val()
appId:$("#appId").val(),
pushTime:$("#pushTime").val()
},
dataType: 'json',
success: function (data) {
@@ -359,7 +394,38 @@
}
})
}
}
})
// 撤销
$('#table').on('click', '.opt-del', function () {
var recordId = $(this).attr("data-id");
console.log('recordId', recordId)
if (recordId == 'undefined' || !recordId) {
return;
}
if (confirm("确认撤销?")) {
$.ajax({
type: 'post',
url: "/admin/messageAdmin/recall.action",
data: JSON.stringify({
'recordId': recordId
}),
dataType: "json",
contentType: "application/json",
success: function (res) {
if (res.code == 200) {
$("#tipMsg").text("撤销成功");
$("#tipModal").modal('show');
TableHelper.doRefresh("#table");
} else {
$("#tipMsg").text("撤销失败,错误码:" + res.code + ", 错误信息: " + res.message);
$("#tipModal").modal('show');
}
}
});
}
})
@@ -475,9 +541,6 @@
})
</script>

View File

@@ -5861,5 +5861,13 @@ public class Constant {
public static final Byte NORMAL = 1;
public static final Byte HIGH = 2;
}
public static class MsgPushStatus {
// 未推送
public static final Byte NOT_PUST = 0;
// 已推送
public static final Byte PUSH = 1;
}
}

View File

@@ -13,5 +13,7 @@ public interface AccountLoginRecordMapperExpand {
List<String> listUserLoginDevice(Long uid);
Integer countLoginRecord(@Param("uid")Long uid);
Integer countLoginRecord(@Param("uid") Long uid);
List<Long> getUidByRecent60Day(@Param("channel") String channel, @Param("index") Integer index, @Param("offset") Integer offset);
}

View File

@@ -1,22 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.accompany.core.mybatismapper.AccountLoginRecordMapperExpand" >
<select id="countLoginDay" resultType="java.lang.Long">
select COUNT(1) from (
select left(create_time, 10) as days from account_login_record
where uid=#{uid} and create_time BETWEEN #{startDay} AND #{endDay}
GROUP BY days) as a
</select>
<select id="getRecentLoginUids" resultType="java.lang.Long">
SELECT DISTINCT(uid) FROM account_login_record WHERE create_time &gt;= #{startDay}
</select>
<select id="listUserLoginDevice" resultType="java.lang.String">
select device_id from account_login_record where uid = #{uid} group by device_id;
<mapper namespace="com.accompany.core.mybatismapper.AccountLoginRecordMapperExpand">
<select id="countLoginDay" resultType="java.lang.Long">
select COUNT(1) from (
select left(create_time, 10) as days from account_login_record
where uid=#{uid} and create_time BETWEEN #{startDay} AND #{endDay}
GROUP BY days) as a
</select>
<select id="getRecentLoginUids" resultType="java.lang.Long">
SELECT DISTINCT(uid) FROM account_login_record WHERE create_time &gt;= #{startDay}
</select>
<select id="listUserLoginDevice" resultType="java.lang.String">
select device_id from account_login_record where uid = #{uid} group by device_id;
</select>
<select id="countLoginRecord" resultType="java.lang.Integer" parameterType="java.lang.Long">
select count(1) from account_login_record where uid = #{uid};
</select>
<select id="getUidByRecent60Day" resultType="java.lang.Long">
select distinct alr.uid from account_login_record as alr
left join users as u on u.uid = alr.uid
where alr.create_time >= DATE_SUB(CURDATE(), INTERVAL 60 DAY)
<if test="channel != null and channel != ''">
and u.channel = #{channel}
</if>
limit #{index}, #{offset}
</select>
<select id="countLoginRecord" resultType="java.lang.Integer" parameterType="java.lang.Long">
select count(1) from account_login_record where uid = #{uid};
</select>
</mapper>

View File

@@ -7,6 +7,8 @@ public class MsgPushRecord {
private Long fromAccid;
private Byte msgFrom;
private Byte toObjType;
private Byte msgType;
@@ -33,6 +35,10 @@ public class MsgPushRecord {
private String appId;
private Date pushTime;
private Byte pushStatus;
public Integer getRecordId() {
return recordId;
}
@@ -152,4 +158,28 @@ public class MsgPushRecord {
public void setAppId(String appId) {
this.appId = appId == null ? null : appId.trim();
}
public Date getPushTime() {
return pushTime;
}
public void setPushTime(Date pushTime) {
this.pushTime = pushTime;
}
public Byte getPushStatus() {
return pushStatus;
}
public void setPushStatus(Byte pushStatus) {
this.pushStatus = pushStatus;
}
public Byte getMsgFrom() {
return msgFrom;
}
public void setMsgFrom(Byte msgFrom) {
this.msgFrom = msgFrom;
}
}

View File

@@ -0,0 +1,16 @@
package com.accompany.business.mybatismapper;
import com.accompany.business.model.MsgPushRecord;
import java.util.List;
/**
* @author: liaozetao
* @date: 2023/8/4 16:53
* @description:
*/
public interface MsgPushRecordMapperExpand {
List<MsgPushRecord> getNeedPushRecordList();
}

View File

@@ -0,0 +1,191 @@
package com.accompany.business.service;
import cn.hutool.core.collection.CollectionUtil;
import com.accompany.business.model.MsgPushRecord;
import com.accompany.business.mybatismapper.MsgPushRecordMapper;
import com.accompany.business.mybatismapper.UsersMapperExpend;
import com.accompany.business.param.neteasepush.*;
import com.accompany.common.constant.Attach;
import com.accompany.common.constant.Constant;
import com.accompany.core.mybatismapper.AccountLoginRecordMapperExpand;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class MsgPushService {
@Autowired
private MsgPushRecordMapper msgPushRecordMapper;
@Autowired
private UsersMapperExpend usersMapperExpend;
@Autowired
private AccountLoginRecordMapperExpand accountLoginRecordMapperExpand;
@Autowired
private SendSysMsgService sendSysMsgService;
/**
* 发送文字、图片、图文 目前仅支持跳H5 和 跳转房间 (跳转房间仅支持文字))
*
* @param msgPushRecord
*/
public void sendSysMsgToUsers(MsgPushRecord msgPushRecord) {
switch (msgPushRecord.getToObjType().intValue()) {
// 指定接收用户
case Constant.PushMsgType.special:
NeteaseSendMsgBatchParam neteaseSendMsgBatchParam = new NeteaseSendMsgBatchParam();
neteaseSendMsgBatchParam.setFromAccid(String.valueOf(msgPushRecord.getFromAccid()));
this.buildNetEaseSendMsgBatchParam(neteaseSendMsgBatchParam, msgPushRecord);
sendSysMsgService.sendBatchMsgMsg(neteaseSendMsgBatchParam);
break;
//群发消息(高级群)
case Constant.PushMsgType.all:
//1、分页查询每次查询50个uid
//2、循环处理每次50累计满5次休眠10s
sendGroupMsg(msgPushRecord);
break;
}
msgPushRecord.setPushStatus(Constant.MsgPushStatus.PUSH);
msgPushRecordMapper.updateByPrimaryKey(msgPushRecord);
}
/**
* 群发文字或者图片
*
* @param msgPushRecord
*/
public void sendGroupMsg(MsgPushRecord msgPushRecord) {
int page = 1;
int batchSize = 500;
for (List<Long> uidList = accountLoginRecordMapperExpand.getUidByRecent60Day(msgPushRecord.getAppId(), 0, batchSize);
uidList != null && !uidList.isEmpty();
uidList = accountLoginRecordMapperExpand.getUidByRecent60Day(msgPushRecord.getAppId(), (++page - 1) * batchSize, batchSize)) {
NeteaseSendMsgBatchParam neteaseSendMsgBatchParam = new NeteaseSendMsgBatchParam();
neteaseSendMsgBatchParam.setToAccids(uidList.stream().map(String::valueOf).collect(Collectors.toList()));
neteaseSendMsgBatchParam.setFromAccid(msgPushRecord.getFromAccid().toString());
buildNetEaseSendMsgBatchParam(neteaseSendMsgBatchParam, msgPushRecord);
sendSysMsgService.sendBatchMsgMsg(neteaseSendMsgBatchParam);
}
log.debug("共发送了{}次信息", page);
}
/**
* 构建批量发送消息参数体
*
* @param neteaseSendMsgBatchParam
* @param msgPushRecord
*/
private void buildNetEaseSendMsgBatchParam(NeteaseSendMsgBatchParam neteaseSendMsgBatchParam, MsgPushRecord msgPushRecord) {
// 0 表示文本消息,1 表示图片100 自定义消息类型
switch (msgPushRecord.getMsgType()) {
case 0:
if (StringUtils.isEmpty(msgPushRecord.getSkipUri())) {
// 批量发送文字(不带跳转链接)
neteaseSendMsgBatchParam.setContent(msgPushRecord.getMsgDesc());
} else if (Constant.SecretarySkipType.H5.equals(msgPushRecord.getSkipType())) {
// 批量发送文字(带跳转链接)自定义类型
neteaseSendMsgBatchParam.setType(Constant.DefineProtocol.CUSTOM_MESS_DEFINE);
Body body = new Body();
body.setData(this.buildAttach(msgPushRecord, Constant.SecretarySkipType.H5));
neteaseSendMsgBatchParam.setBody(body);
} else {
// 批量发送文字(带跳转房间)自定义类型
neteaseSendMsgBatchParam.setType(Constant.DefineProtocol.CUSTOM_MESS_DEFINE);
Body body = new Body();
body.setData(this.buildAttach(msgPushRecord, Constant.SecretarySkipType.ROOM));
neteaseSendMsgBatchParam.setBody(body);
}
break;
case 1:
// 发图片
neteaseSendMsgBatchParam.setType(1);
neteaseSendMsgBatchParam.setContent(new Gson().toJson(this.buildPicture(msgPushRecord.getPicUrl())));
break;
case 100:
// 图文 (带跳转)
sendBatchPicAndWords(neteaseSendMsgBatchParam, msgPushRecord);
break;
default:
log.error("参数不正确");
break;
}
if (!msgPushRecord.getToAccids().equals("0")) {
List<String> uidList = com.accompany.common.utils.StringUtils.splitToList(msgPushRecord.getToAccids(), ",");
neteaseSendMsgBatchParam.setToAccids(uidList);
}
Payload payload = new Payload();
payload.setSkiptype(msgPushRecord.getSkipType());
payload.setData(msgPushRecord.getSkipUri());
neteaseSendMsgBatchParam.setPayload(payload);
}
/**
* 构建自定义文字带跳转功能的参数体
*
* @param msgPushRecord
* @return
*/
private Attach buildAttach(MsgPushRecord msgPushRecord, Byte skipType) {
Attach attach = new Attach();
attach.setFirst(Constant.DefineProtocol.CUSTOM_MESS_HEAD_SECRETARY);
attach.setSecond(Constant.DefineProtocol.CUSTOM_MESS_SUB_SECRETARY_INTRACTION);
JSONObject jsonObject = new JSONObject();
jsonObject.put("title", msgPushRecord.getMsgDesc());
jsonObject.put("msg", msgPushRecord.getMsgDesc());
jsonObject.put("routerType", skipType);
jsonObject.put("routerValue", msgPushRecord.getSkipUri());
attach.setData(jsonObject);
return attach;
}
/**
* 构建图片参数体
*
* @param picUrl
* @return
*/
private Picture buildPicture(String picUrl) {
Picture picture = new Picture();
picture.setName("图片");
picture.setExt("jpg");
picture.setMd5(String.valueOf(System.currentTimeMillis()));
picture.setUrl(picUrl);
picture.setH(6814);
picture.setW(2332);
picture.setSize(388245);
return picture;
}
//群发图文
public void sendBatchPicAndWords(NeteaseSendMsgBatchParam neteaseSendMsgBatchParam, MsgPushRecord msgPushRecord) {
neteaseSendMsgBatchParam.setType(msgPushRecord.getMsgType());
Body body = new Body();
body.setFirst(Constant.DefMsgType.PointToPointMsg);
body.setSecond(Constant.DefMsgType.PushPicWordMsg);
PicWordMsgAttach picWordMsgAttach = new PicWordMsgAttach();
picWordMsgAttach.setDesc(msgPushRecord.getMsgDesc());
picWordMsgAttach.setPicUrl(msgPushRecord.getPicUrl());
picWordMsgAttach.setTitle(msgPushRecord.getTitle());
if (msgPushRecord.getWebUrl() != null) {
picWordMsgAttach.setWebUrl(msgPushRecord.getWebUrl());
}
if (msgPushRecord.getSkipUri() != null) {
picWordMsgAttach.setWebUrl(msgPushRecord.getSkipUri());
}
body.setData(picWordMsgAttach);
neteaseSendMsgBatchParam.setBody(body);
}
}

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.accompany.business.mybatismapper.MsgPushRecordMapperExpand">
<select id="getNeedPushRecordList" resultType="com.accompany.business.model.MsgPushRecord">
select * from msg_push_record where push_status = 0 and push_time &lt; now();
</select>
</mapper>

View File

@@ -0,0 +1,44 @@
package com.accompany.scheduler.task.msg;
import com.accompany.business.model.MsgPushRecord;
import com.accompany.business.mybatismapper.MsgPushRecordMapper;
import com.accompany.business.mybatismapper.MsgPushRecordMapperExpand;
import com.accompany.business.service.MsgPushService;
import com.accompany.scheduler.base.BaseTask;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Component
public class MsgPushTask extends BaseTask {
@Autowired
private MsgPushRecordMapper msgPushRecordMapper;
@Autowired
private MsgPushRecordMapperExpand msgPushRecordMapperExpand;
@Autowired
private MsgPushService msgPushService;
@Scheduled(cron = "0 0/5 * * * ?")
public void msgPushTask() {
log.info("==== msgPushTask start ====");
List<MsgPushRecord> list = msgPushRecordMapperExpand.getNeedPushRecordList();
for (MsgPushRecord record : list) {
try {
msgPushService.sendSysMsgToUsers(record);
} catch (Exception e) {
log.error("msgPushTask error e:{}", e.getMessage(), e);
}
}
List<Integer> recordIdList = list.stream().map(MsgPushRecord::getRecordId).collect(Collectors.toList());
log.info("--------------------------------msgPushTask end listId:{}", JSONObject.toJSONString(recordIdList));
}
}