Sfoglia il codice sorgente

添加消息处理

yq 3 anni fa
parent
commit
9a0e5a9c7f

+ 10 - 0
src/main/java/com/usky/dxtop/common/utils/DateUtils.java

@@ -130,6 +130,16 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
             return null;
         }
     }
+
+    public static String format(Date date, String pattern) {
+        String formatDate = null;
+        if (StringUtils.isNotBlank(pattern)) {
+            formatDate = DateFormatUtils.format(date, pattern);
+        } else {
+            formatDate = DateFormatUtils.format(date, YYYY_MM_DD_HH_MM_SS);
+        }
+        return formatDate;
+    }
     
     /**
      * 获取服务器启动时间

+ 10 - 3
src/main/java/com/usky/dxtop/controller/web/ChargeController.java

@@ -1,6 +1,8 @@
 package com.usky.dxtop.controller.web;
 
 
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.dxtop.common.core.domain.AjaxResult;
 import com.usky.dxtop.model.Charge;
 import com.usky.dxtop.service.ChargeService;
@@ -49,10 +51,15 @@ public class ChargeController {
         return AjaxResult.success(chargeService.one(seq));
     }
 
-
+    /**
+     * 批量修改消息状态
+     * @return
+     */
     @PutMapping
-    public AjaxResult update(@RequestBody Charge charge){
-        return AjaxResult.success(chargeService.update(charge));
+    public AjaxResult updateList(){
+        LambdaUpdateWrapper<Charge> updateWrapper = Wrappers.lambdaUpdate();
+        updateWrapper.set(Charge::getMessageFlag,true);
+        return AjaxResult.success(chargeService.update(updateWrapper));
     }
 }
 

+ 10 - 3
src/main/java/com/usky/dxtop/controller/web/DishController.java

@@ -1,6 +1,8 @@
 package com.usky.dxtop.controller.web;
 
 
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.dxtop.common.core.domain.AjaxResult;
 import com.usky.dxtop.model.Dish;
 import com.usky.dxtop.service.DishService;
@@ -55,10 +57,15 @@ public class DishController {
         return AjaxResult.success(dishService.collect());
     }
 
-
+    /**
+     * 批量修改消息状态
+     * @return
+     */
     @PutMapping
-    public AjaxResult update(@RequestBody Dish dish){
-        return AjaxResult.success(dishService.update(dish));
+    public AjaxResult update() {
+        LambdaUpdateWrapper<Dish> updateWrapper = Wrappers.lambdaUpdate();
+        updateWrapper.set(Dish::getMessageFlag,true);
+        return AjaxResult.success(dishService.update(updateWrapper));
     }
 
 }

+ 15 - 9
src/main/java/com/usky/dxtop/controller/web/WxController.java

@@ -8,8 +8,6 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.HashMap;
-import java.util.Map;
 import java.util.TreeMap;
 
 /**
@@ -56,12 +54,20 @@ public class WxController {
             return wxApi.getToken(WxApi.APP_ID,WxApi.SECRET);
         }
     }
-    @GetMapping("/testt")
-    public String sendMessageTest(String openId){
-        Map<String, Object> map = new HashMap<>();
-        Map<String, Object> firstMap = new HashMap<>();
-        firstMap.put("value","支付成功");
-        map.put("first",firstMap);
-        return wxApi.sendMessageApi(openId,map,wxApi.getToken(WxApi.APP_ID,WxApi.SECRET));
+
+    /**
+     * 公众号进行消息推送
+     * @param openId
+     * @param data
+     * @param templateId
+     * @param detailUrl
+     * @return
+     */
+    @GetMapping("/offSendMessage")
+    public String offSendMessageT(@RequestParam String openId,
+                                  @RequestParam Object data,
+                                  @RequestParam String templateId,
+                                  @RequestParam(required = false) String detailUrl){
+        return wxApi.sendMessageApi(openId,data,wxApi.getToken(WxApi.APP_ID,WxApi.SECRET),templateId,detailUrl);
     }
 }

+ 1 - 1
src/main/java/com/usky/dxtop/framework/config/SecurityConfig.java

@@ -100,7 +100,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 // 对于登录login 注册register 验证码captchaImage 允许匿名访问
                 .antMatchers("/login", "/register", "/captchaImage","/top/notify","/dxtop/order/topScanPayApi","/loginApi",
                         "/dxtop/charge/page","/dxtop/dish/page","/wxLogin","/wxAccToken","/dxtop/order/callUnifiedPay",
-                        "/dxtop/staff/one","/dxtop/staff/cardBalance","/dxtop/charge/cordPage","/dxtop/charge","/dxtop/dish","/dxtop/staff"
+                        "/dxtop/staff/one","/dxtop/staff/cardBalance","/dxtop/charge/cordPage","/dxtop/charge","/dxtop/dish","/dxtop/staff","offSendMessage"
                         ).permitAll()
                 .antMatchers(
                         HttpMethod.GET,

+ 13 - 6
src/main/java/com/usky/dxtop/service/api/WxApi.java

@@ -53,9 +53,14 @@ public class WxApi {
 
     public static final String SECRET = "b758e45c89162542610509dafd9db7c3";
     /**
-     * 模版id
+     * 充值模版id
      */
-    private static final String TEMPLATE_ID = "Eps4guetElB_6uzu-AYvvGM4xc9LwO7UnEwxNwn0ZFE";
+    public static final String CHARGE_TEMPLATE_ID = "jxzlZgnhRTsBDaFozB7HMqoAuoNQqPjZU7ltSUSaG4k";
+
+    /**
+     * 消费模版id
+     */
+    public static final String DISH_TEMPLATE_ID = "l8PMggrTWBpQoXOGTWFkw2lRkos99Gav06l7Ts1A4nQ";
     /**
      * 公众号的key
      */
@@ -63,6 +68,7 @@ public class WxApi {
 
     private static final String WX_TICKET_KEY = "wxTicketKey";
 
+
     /**
      * 小程序登录
      * @param appId
@@ -88,12 +94,13 @@ public class WxApi {
     }
 
 
-    public String sendMessageApi(String openId,Object data,String token){
+    public String sendMessageApi(String openId, Object data, String token, String templateId,String detailUrl){
         String url = String.format("%s%s%s", SEND_MESSAGE, "?access_token=", token);
         Map<String,Object> map = new HashMap<>();
         map.put("touser",openId);
-        map.put("template_id",TEMPLATE_ID);
+        map.put("template_id",templateId);
         map.put("data",data);
+        map.put("url",detailUrl);
         String result = HttpUtils.sendPost(url, JSONObject.toJSONString(map), null);
         JSONObject jsonObject = JSONObject.parseObject(result);
         return jsonObject.toJSONString();
@@ -122,7 +129,7 @@ public class WxApi {
             throw new CustomException(obj.get("errmsg").toString());
         }
         String s = obj.get("access_token").toString();
-        redisCache.setCacheObject(appId+ACCESS_TOKEN, s, 7200, TimeUnit.MINUTES);
+        redisCache.setCacheObject(appId+ACCESS_TOKEN, s, 7200, TimeUnit.SECONDS);
         return s;
     }
 
@@ -146,7 +153,7 @@ public class WxApi {
             throw new CustomException(obj.get("errmsg").toString());
         }
         String s = obj.get("ticket").toString();
-        redisCache.setCacheObject(WX_TICKET_KEY, s, 7200, TimeUnit.MINUTES);
+        redisCache.setCacheObject(WX_TICKET_KEY, s, 7200, TimeUnit.SECONDS);
         return s;
     }
 

+ 22 - 5
src/main/java/com/usky/dxtop/service/listener/ChargeTransMqListener.java

@@ -1,6 +1,7 @@
 package com.usky.dxtop.service.listener;
 
 import com.alibaba.fastjson.JSONObject;
+import com.usky.dxtop.common.utils.DateUtils;
 import com.usky.dxtop.common.utils.StringUtils;
 import com.usky.dxtop.model.Charge;
 import com.usky.dxtop.model.Staff;
@@ -26,6 +27,8 @@ import java.util.Map;
 @Component
 public class ChargeTransMqListener {
 
+    private static final String URL = "https://smartpark.caih.com/vuepay/#/record?id=1";
+
     @Autowired
     private ChargeService chargeService;
 
@@ -49,11 +52,7 @@ public class ChargeTransMqListener {
             staff.setBalance(charge.getBalance());
             staffService.updateById(staff);
             if (StringUtils.isNotBlank(staff.getOpenId())){
-                Map<String, Object> map = new HashMap<>();
-                Map<String, Object> firstMap = new HashMap<>();
-                firstMap.put("value","支付成功");
-                map.put("first",firstMap);
-                wxApi.sendMessageApi(staff.getOpenId(),map,wxApi.getToken(WxApi.APP_ID,WxApi.SECRET));
+                wxApi.sendMessageApi(staff.getOpenId(),getMessageData(charge),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET),WxApi.CHARGE_TEMPLATE_ID,URL);
             }
         } catch (Exception e) {
             log.info("charge_trans_produce"+"异常信息:" + e.getMessage());
@@ -61,4 +60,22 @@ public class ChargeTransMqListener {
 
         }
     }
+
+
+    public Map<String,Object> getMessageData(Charge charge){
+        Map<String, Object> map = new HashMap<>();
+        map.put("first",getMap("恭喜您,充值成功,以下为相关简讯",null));
+        map.put("keyword1",getMap("余额充值",null));
+        map.put("keyword2",getMap(String.format("%s%s", charge.getAmt()," 元"),null));
+        map.put("keyword3",getMap(DateUtils.format(charge.getCreateTime(),null),null));
+        map.put("remark",getMap("如有疑问,请及时在线联系我们的客服!",null));
+        return map;
+    }
+
+    public Map<String,Object> getMap(Object value,String color){
+        Map<String,Object> map = new HashMap<>();
+        map.put("value",value);
+        map.put("color",color);
+        return map;
+    }
 }

+ 23 - 5
src/main/java/com/usky/dxtop/service/listener/DishTransMqListener.java

@@ -1,6 +1,7 @@
 package com.usky.dxtop.service.listener;
 
 import com.alibaba.fastjson.JSONObject;
+import com.usky.dxtop.common.utils.DateUtils;
 import com.usky.dxtop.common.utils.StringUtils;
 import com.usky.dxtop.model.Dish;
 import com.usky.dxtop.model.Staff;
@@ -26,6 +27,8 @@ import java.util.Map;
 @Component
 public class DishTransMqListener {
 
+
+    private static final String URL = "https://smartpark.caih.com/vuepay/#/record?id=2";
     @Autowired
     private DishService dishService;
     @Autowired
@@ -48,14 +51,29 @@ public class DishTransMqListener {
             staff.setBalance(dish.getBalance());
             staffService.updateById(staff);
             if (StringUtils.isNotBlank(staff.getOpenId())){
-                Map<String, Object> map = new HashMap<>();
-                Map<String, Object> firstMap = new HashMap<>();
-                firstMap.put("value","消费成功");
-                map.put("first",firstMap);
-                wxApi.sendMessageApi(staff.getOpenId(),map,wxApi.getToken(WxApi.APP_ID,WxApi.SECRET));
+                wxApi.sendMessageApi(staff.getOpenId(),getMessageData(dish),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET), WxApi.DISH_TEMPLATE_ID,URL);
             }
         } catch (Exception e) {
             log.info("dish_trade_produce"+"异常信息:" + e.getMessage());
         }
     }
+
+    public Map<String,Object> getMessageData(Dish dish){
+        Map<String, Object> map = new HashMap<>();
+        map.put("first",getMap("尊敬的客户,您的订单已支付成功",null));
+        map.put("keyword1",getMap(dish.getSeq(),null));
+        map.put("keyword2",getMap(dish.getShopName(),null));
+        map.put("keyword3",getMap(String.format("%s%s", dish.getAmt()," 元"),null));
+        map.put("keyword4",getMap(DateUtils.format(dish.getCreateTime(),null),null));
+        map.put("keyword5",getMap(DateUtils.format(dish.getCreateTime(),null),null));
+        map.put("remark",getMap("如有疑问,请及时在线联系我们的客服!",null));
+        return map;
+    }
+
+    public Map<String,Object> getMap(Object value,String color){
+        Map<String,Object> map = new HashMap<>();
+        map.put("value",value);
+        map.put("color",color);
+        return map;
+    }
 }

+ 31 - 0
src/test/java/com/usky/dxtop/SmApiTest.java

@@ -1,7 +1,14 @@
 package com.usky.dxtop;
 
+import com.usky.dxtop.model.Charge;
+import com.usky.dxtop.model.Dish;
+import com.usky.dxtop.service.ChargeService;
+import com.usky.dxtop.service.DishService;
 import com.usky.dxtop.service.MsgLogService;
+import com.usky.dxtop.service.api.WxApi;
 import com.usky.dxtop.service.job.SmJob;
+import com.usky.dxtop.service.listener.ChargeTransMqListener;
+import com.usky.dxtop.service.listener.DishTransMqListener;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,10 +27,34 @@ public class SmApiTest {
     @Autowired
     private MsgLogService msgLogService;
 
+    @Autowired
+    private WxApi wxApi;
+
+    @Autowired
+    private ChargeTransMqListener chargeTransMqListener;
+
+    @Autowired
+    private DishTransMqListener dishTransMqListener;
+
+    @Autowired
+    private DishService dishService;
+    @Autowired
+    private ChargeService chargeService;
+
     @Autowired
     private SmJob smJob;
     @Test
     public void test1(){
         smJob.personApi(null);
     }
+    @Test
+    public void testSendMessage(){
+        Dish dish = dishService.getById(637744684233638175L);
+        wxApi.sendMessageApi("o5P6I5tLj-TjrK7leWUT8EFmTCa4",dishTransMqListener.getMessageData(dish),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET), WxApi.DISH_TEMPLATE_ID,"https://smartpark.caih.com/vuepay/#/record?id=2");
+
+
+        Charge charge = chargeService.getById(637744683598183711L);
+        wxApi.sendMessageApi("o5P6I5tLj-TjrK7leWUT8EFmTCa4",chargeTransMqListener.getMessageData(charge),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET), WxApi.CHARGE_TEMPLATE_ID,"https://smartpark.caih.com/vuepay/#/record?id=2");
+    }
+
 }