package com.oying.modules.message.rest;
|
|
import com.alibaba.fastjson2.JSONObject;
|
import com.oying.annotation.Log;
|
import com.oying.modules.message.domain.MessageSystem;
|
import com.oying.modules.message.domain.dto.SendMessageRequestDto;
|
import com.oying.modules.message.service.MessageSystemService;
|
import com.oying.modules.message.domain.dto.MessageSystemQueryCriteria;
|
import com.oying.modules.system.domain.User;
|
import com.oying.modules.system.domain.UserSubscribe;
|
import com.oying.utils.HttpRequest;
|
import lombok.Data;
|
import lombok.RequiredArgsConstructor;
|
|
import java.sql.Timestamp;
|
import java.util.Date;
|
import java.util.List;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
import io.swagger.annotations.*;
|
import java.io.IOException;
|
import java.util.Map;
|
import javax.servlet.http.HttpServletResponse;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.oying.utils.PageResult;
|
import com.oying.modules.security.service.WeiXinService;
|
import com.oying.utils.R;
|
/**
|
* @author 李萌
|
* @date 2025-05-20
|
**/
|
@RestController
|
@RequiredArgsConstructor
|
@Api(tags = "消息_系统")
|
@RequestMapping("/api/message/messageSystem")
|
public class MessageSystemController {
|
|
private static final Logger log = LoggerFactory.getLogger(MessageSystemController.class);
|
private final MessageSystemService messageSystemService;
|
private final WeiXinService weiXinService;
|
|
|
|
// @ApiOperation("导出数据")
|
// @GetMapping(value = "/download")
|
// @PreAuthorize("@el.check('messageSystem:list')")
|
// public void exportMessageSystem(HttpServletResponse response, MessageSystemQueryCriteria criteria) throws IOException {
|
// messageSystemService.download(messageSystemService.queryAll(criteria), response);
|
// }
|
|
@GetMapping
|
@ApiOperation("查询消息_系统")
|
@PreAuthorize("@el.check('messageSystem:list')")
|
public R<PageResult<MessageSystem>> queryMessageSystem(MessageSystemQueryCriteria criteria){
|
Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
|
// return new ResponseEntity<>(messageSystemService.queryAll(criteria,page),HttpStatus.OK);
|
return R.success(messageSystemService.queryAll(criteria,page));
|
}
|
|
// @PostMapping
|
// @Log("新增消息_系统")
|
// @ApiOperation("新增消息_系统")
|
// @PreAuthorize("@el.check('messageSystem:add')")
|
// public ResponseEntity<Object> createMessageSystem(@Validated @RequestBody MessageSystem resources){
|
// messageSystemService.create(resources);
|
// return new ResponseEntity<>(HttpStatus.CREATED);
|
// }
|
|
// @PutMapping
|
// @Log("修改消息_系统")
|
// @ApiOperation("修改消息_系统")
|
// @PreAuthorize("@el.check('messageSystem:edit')")
|
// public ResponseEntity<Object> updateMessageSystem(@Validated @RequestBody MessageSystem resources){
|
// messageSystemService.update(resources);
|
// return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
// }
|
|
// @DeleteMapping
|
// @Log("删除消息_系统")
|
// @ApiOperation("删除消息_系统")
|
// @PreAuthorize("@el.check('messageSystem:del')")
|
// public ResponseEntity<Object> deleteMessageSystem(@ApiParam(value = "传ID数组[]") @RequestBody List<Integer> ids) {
|
// messageSystemService.deleteAll(ids);
|
// return new ResponseEntity<>(HttpStatus.OK);
|
// }
|
|
//查询一条系统消息
|
@GetMapping("/{id}")
|
@ApiOperation("查询一条系统消息")
|
public R<MessageSystem> getMessageSystem(@PathVariable Integer id){
|
MessageSystem messageSystem = messageSystemService.getById(id);
|
// return new ResponseEntity<>(messageSystem,HttpStatus.OK);
|
return R.success(messageSystem);
|
}
|
//插入一条系统消息 带两个参数start与end
|
@PostMapping()
|
@ApiOperation("插入一条系统消息")
|
public R<Object> insertMessageSystem(
|
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
|
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
|
|
// 创建 MessageSystem 对象
|
MessageSystem messageSystem = new MessageSystem();
|
messageSystem.setStartTime(new Timestamp(start.getTime()));
|
messageSystem.setEndTime(new Timestamp(end.getTime()));
|
|
// 调用服务层保存数据
|
messageSystemService.save(messageSystem);
|
|
// return new ResponseEntity<>(HttpStatus.OK);
|
return R.success("插入成功");
|
}
|
|
/**
|
* POST 该接口用于发送订阅消息。
|
* data 请求参数
|
* openid 用户openId
|
* templateId 订阅模板id
|
* page 小程序跳转链接
|
*/
|
// public void sendMessage(Map<String, Object> data, String openid, String templateId, String page) {
|
// JSONObject jsonObject = new JSONObject();
|
// if (wxEnabled) {
|
// String url = weiXinProperties.getSendMessage();
|
// url = url.replace("{accessToken}", getStableAccessToken());
|
// Map<String, Object> map = getSendMessageDto(data, openid, templateId, page);
|
// jsonObject = HttpRequest.exchangeJsonObject(HttpMethod.POST, url, map);
|
// } else {
|
// jsonObject.put("message", "测试环境");
|
// }
|
// User user = userService.findByOpenid(openid);
|
// UserSubscribe sub = new UserSubscribe();
|
// sub.setSubType(templateId);
|
// sub.setUserId(user.getId());
|
// sub.setOpenid(openid);
|
// sub.setUsername(user.getUsername());
|
// sub.setSendMessage(JSONObject.toJSONString(data));
|
// sub.setSubMessage(jsonObject.toJSONString());
|
// subscribeService.save(sub);
|
// }
|
//调用微信发送消息接口sendMessage
|
|
//示例
|
// {
|
// "touser": "OPENID",
|
// "template_id": "TEMPLATE_ID",
|
// "page": "index",
|
// "data":{
|
// "phrase3": {
|
// "value": "审核通过"
|
// },
|
// "name1": {
|
// "value": "订阅"
|
// },
|
// "date2": {
|
// "value": "2019-12-25 09:42"
|
// }
|
// }
|
// }
|
//todo 发送订阅消息 数据库暂未存储
|
@PostMapping("/sendMessage")
|
@ApiOperation("发送订阅消息")
|
public R<SendMessageRequestDto> sendMessage(@RequestBody SendMessageRequestDto request){
|
//打印request
|
//SendMessageRequestDto(touser=OPENID, template_id=TEMPLATE_ID, page=index, data={date2={"value":"2019-12-25 09:42"}, phrase3={"value":"审核通过"}, name1={"value":"订阅"}})
|
log.info("request:{}", request);
|
try {
|
weiXinService.sendMessage(request.getData(), request.getTouser(), request.getTemplate_id(), request.getPage());
|
return R.success(request);
|
} catch (Exception e) {
|
// 如果捕获到异常,返回错误信息
|
log.error("发送订阅消息失败", e);
|
return R.fail(request,"发送订阅消息失败 用真实数据:" + e.getMessage());
|
}
|
}
|
|
|
|
}
|