ソースを参照

[update] message:

kelei 6 ヶ月 前
コミット
c685ff343c

+ 15 - 0
admin/src/main/java/com/flyer/foster/controller/app/AppUserController.java

@@ -1,7 +1,10 @@
 package com.flyer.foster.controller.app;
 
+import com.flyer.foster.consts.WechatConst;
 import com.flyer.foster.dto.app.AppUserQueryDTO;
 import com.flyer.foster.service.IAppUserService;
+import com.flyer.foster.util.WeChatApiUtil;
+import com.flyer.foster.util.WxAccessToken;
 import com.flyer.util.JwtUtil;
 import com.flyer.util.R;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -25,6 +28,10 @@ public class AppUserController {
     @Autowired
     private IAppUserService iAppUserService;
 
+    @Autowired
+    private WeChatApiUtil weChatApiUtil;
+
+
     @RequestMapping("/login")
     public R login(@RequestBody AppUserQueryDTO dto) {
         return R.ok().result(iAppUserService.login(dto));
@@ -44,4 +51,12 @@ public class AppUserController {
         String birdseyeToken = jwtUtil.createBirdseyeToken(claims, audience, subject);
         return R.ok().result(birdseyeToken);
     }
+
+    @PostMapping("/send-sub-msg")
+    public R sendSubscribeMessage() {
+        WxAccessToken wxAccessToken = WxAccessToken.getInstance(weChatApiUtil, WechatConst.APP_ID, WechatConst.APP_SECRET);
+        String accessToken = wxAccessToken.getAccess_token();
+        Map<String, Object> subMsg = weChatApiUtil.subscribeMessage(accessToken, "oiUkI7dJILmw43YavQ6SD9BuiDsU");
+        return R.ok();
+    }
 }

+ 2 - 0
admin/src/main/java/com/flyer/foster/dto/app/AppTreePosterContentQueryDTO.java

@@ -19,4 +19,6 @@ public class AppTreePosterContentQueryDTO {
     private Integer appUserId;
 
     private Integer treeId;
+
+    private String createdTime;
 }

+ 19 - 6
admin/src/main/java/com/flyer/foster/service/impl/TreePosterContentServiceImpl.java

@@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -57,10 +58,12 @@ public class TreePosterContentServiceImpl extends ServiceImpl<ITreePosterContent
 
         if (posterLib != null) {
             // 查询该时间段是否已经插入海报悄悄话
-            TreePosterContent treePosterContent = this.lambdaQuery().eq(TreePosterContent::getPosterLibId, posterLib.getId()).one();
-            if (treePosterContent == null) {
+            List<TreePosterContent> treePosterContentList = this.lambdaQuery()
+                    .eq(TreePosterContent::getPosterLibId, posterLib.getId())
+                    .list();
+            if (treePosterContentList.size() == 0) {
                 // 2.获取悄悄话,取最新一条记录
-                treePosterContent = new TreePosterContent();
+                TreePosterContent treePosterContent = new TreePosterContent();
                 BeanUtil.copyProperties(addDTO, treePosterContent);
                 treePosterContent.setTenantId(tenantId);
                 treePosterContent.setPosterLibId(posterLib.getId());
@@ -74,7 +77,7 @@ public class TreePosterContentServiceImpl extends ServiceImpl<ITreePosterContent
                 // 新增
                 this.save(treePosterContent);
             }
-            BeanUtil.copyProperties(treePosterContent, respDTO);
+            BeanUtil.copyProperties(treePosterContentList, respDTO);
         }
         return respDTO;
     }
@@ -94,6 +97,7 @@ public class TreePosterContentServiceImpl extends ServiceImpl<ITreePosterContent
         IPage<TreePosterContent> pageInfo = this.lambdaQuery()
                 .eq(TreePosterContent::getAppUserId, queryDTO.getAppUserId())
                 .eq(TreePosterContent::getTreeId, queryDTO.getTreeId())
+                .orderByDesc(TreePosterContent::getCreatedTime)
                 .page(page);
         List<AppTreePosterContentRespDTO> list = new ArrayList<>();
         AppTreePosterContentRespDTO respDTO;
@@ -112,14 +116,23 @@ public class TreePosterContentServiceImpl extends ServiceImpl<ITreePosterContent
         List<AppTreePosterContentRespDTO> list = new ArrayList<>();
         AppTreePosterContentRespDTO respDTO;
 
+        LocalDateTime queryDateTime;
+        if (StrUtil.isNotBlank(queryDTO.getCreatedTime())) {
+            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+            queryDateTime = LocalDateTime.parse(queryDTO.getCreatedTime(), fmt);
+        } else {
+            queryDateTime = LocalDateTime.now();
+        }
         // 当天最小时间
-        LocalDateTime min = LocalDateTime.of(LocalDate.from(LocalDateTime.now()), LocalDateTime.MIN.toLocalTime());
+        LocalDateTime min = LocalDateTime.of(LocalDate.from(queryDateTime), LocalDateTime.MIN.toLocalTime());
         // 当天最大时间
-        LocalDateTime max = LocalDateTime.of(LocalDate.from(LocalDateTime.now()), LocalDateTime.MAX.toLocalTime());
+        LocalDateTime max = LocalDateTime.of(LocalDate.from(queryDateTime), LocalDateTime.MAX.toLocalTime());
+
 
         List<TreePosterContent> treePosterContentList = this.lambdaQuery()
                 .eq(TreePosterContent::getTreeId, queryDTO.getTreeId())
                 .between(TreePosterContent::getCreatedTime, min, max)
+                .orderByDesc(TreePosterContent::getCreatedTime)
                 .list();
         for (TreePosterContent treePosterContent : treePosterContentList) {
             respDTO = new AppTreePosterContentRespDTO();

+ 40 - 1
admin/src/main/java/com/flyer/foster/util/WeChatApiUtil.java

@@ -15,6 +15,8 @@ import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.PostConstruct;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -78,7 +80,7 @@ public class WeChatApiUtil {
         return res;
     }
 
-//    public String getPhoneNumber(String appId, String appSecret, String code) {
+    //    public String getPhoneNumber(String appId, String appSecret, String code) {
 //        String accessToken = this.getAccessToken(appId, appSecret);
 //        String tel = "";
 //        String url = StrUtil.format("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={}", accessToken);
@@ -98,4 +100,41 @@ public class WeChatApiUtil {
 //        }
 //        return tel;
 //    }
+
+    public Map<String, Object> subscribeMessage(String accessToken, String openId) {
+        // 首先获取微信调用凭证
+        if (null == accessToken) {
+            throw new BusinessException("获取微信调用凭证失败");
+        }
+        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
+
+        HashMap<String, Object> param = new HashMap<>();
+        param.put("template_id", "4HrnTVgSa1LU1m5a-2ySDz_1N6Ndi3WnkV6rRsV3ROM");
+        param.put("touser", openId);
+        param.put("page", "pages/subPages/login/index");
+        JSONObject data = new JSONObject();
+        data.put("thing1", setVal("value", "test12399"));
+        data.put("date2", setVal("value", "2024年10月1日"));
+        param.put("data", data);
+        param.put("miniprogram_state", "trial");
+
+        try {
+            // 微信调用返回结果
+            JSONObject res = restTemplate.postForObject(url, param, JSONObject.class);
+            if (res != null && res.get("errcode").equals(0)) {
+                return (Map<String, Object>) res.get("result");
+            } else {
+                log.error(res.get("errmsg").toString());
+                return null;
+            }
+        } catch (RestClientException e) {
+            throw new BusinessException("http客户端请求错误");
+        }
+    }
+
+    private JSONObject setVal(String key, String value) {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put(key, value);
+        return jsonObject;
+    }
 }