yq 3 éve
szülő
commit
d73694b670

+ 3 - 1
src/main/java/com/usky/dxtop/controller/web/business/WxController.java

@@ -87,7 +87,9 @@ public class WxController {
                     if (StringUtils.isBlank(s.getOpenId())){
                         throw new CustomException("用户没有绑定openid,绑定后再进行操作");
                     }
-                    return ApiResult.success(wxApi.sendMessageApi(staff.getOpenId(), data, wxApi.getToken(WxApi.APP_ID, WxApi.SECRET), templateId, detailUrl));
+            String result = wxApi.sendMessageApi(staff.getOpenId(), data, wxApi.getToken(WxApi.APP_ID, WxApi.SECRET), templateId, detailUrl);
+            result = wxApi.againSendMessage(result,staff.getOpenId(), data, templateId, detailUrl,WxApi.APP_ID,WxApi.SECRET);
+            return ApiResult.success(result);
                 }).orElseThrow(() -> new CustomException("用户信息不存在"));
     }
 

+ 21 - 3
src/main/java/com/usky/dxtop/service/api/WxApi.java

@@ -106,13 +106,23 @@ public class WxApi {
         map.put("url",detailUrl);
         String result = HttpUtils.sendPost(url, JSONObject.toJSONString(map), null);
         JSONObject jsonObject = JSONObject.parseObject(result);
-        if (!"0".equals(jsonObject.get("errcode").toString())){
-            throw new CustomException(String.format("发送异常:code:%s,message:%s",jsonObject.get("errcode"),jsonObject.get("errmsg")));
-        }
         return jsonObject.toJSONString();
 
     }
 
+    public String againSendMessage(String result,String openId, Object data, String templateId,String detailUrl,String appId,String appSecret) {
+        JSONObject jsonObject = JSONObject.parseObject(result);
+        if (!"0".equals(jsonObject.get("errcode").toString())){
+            if ("40001".equals(jsonObject.get("errcode").toString())){
+                String token = reloadToken(appId, appSecret);
+                result = sendMessageApi(openId,data,token,templateId,detailUrl);
+            }else {
+                throw new CustomException(String.format("发送异常:code:%s,message:%s",jsonObject.get("errcode"),jsonObject.get("errmsg")));
+            }
+        }
+        return result;
+    }
+
     /**
      * 获取token
      * @param appId
@@ -139,6 +149,14 @@ public class WxApi {
         return s;
     }
 
+    public String reloadToken(String appId,String appSecret){
+        boolean b = redisCache.deleteObject(appId + ACCESS_TOKEN);
+        if (!b){
+            throw new CustomException("删除缓存失败请联系管理员");
+        }
+        return getToken(appId,appSecret);
+    }
+
     /**
      * 获取ticket
      * @param token

+ 2 - 0
src/main/java/com/usky/dxtop/service/job/CenterUserJob.java

@@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
@@ -116,6 +117,7 @@ public class CenterUserJob {
 
     public SysUser generateSysUser(Staff staff,List<SysDept> depts){
         SysUser sysUser = new SysUser();
+        sysUser.setCreateTime(new Date());
         sysUser.setCenterUserId(staff.getId());
         sysUser.setUserName(staff.getUsername());
         String pwd = "";

+ 2 - 1
src/main/java/com/usky/dxtop/service/listener/ChargeTransMqListener.java

@@ -52,7 +52,8 @@ public class ChargeTransMqListener {
             staff.setBalance(charge.getBalance());
             staffService.updateById(staff);
             if (StringUtils.isNotBlank(staff.getOpenId())){
-                wxApi.sendMessageApi(staff.getOpenId(),getMessageData(charge),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET),WxApi.CHARGE_TEMPLATE_ID,URL);
+                String  result = wxApi.sendMessageApi(staff.getOpenId(),getMessageData(charge),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET),WxApi.CHARGE_TEMPLATE_ID,URL);
+                wxApi.againSendMessage(result,staff.getOpenId(), getMessageData(charge), WxApi.CHARGE_TEMPLATE_ID, URL,WxApi.APP_ID,WxApi.SECRET);
             }
         } catch (Exception e) {
             log.info("charge_trans_produce"+"异常信息:" + e.getMessage());

+ 2 - 1
src/main/java/com/usky/dxtop/service/listener/DishTransMqListener.java

@@ -51,7 +51,8 @@ public class DishTransMqListener {
             staff.setBalance(dish.getBalance());
             staffService.updateById(staff);
             if (StringUtils.isNotBlank(staff.getOpenId())){
-                wxApi.sendMessageApi(staff.getOpenId(),getMessageData(dish),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET), WxApi.DISH_TEMPLATE_ID,URL);
+                String result = wxApi.sendMessageApi(staff.getOpenId(),getMessageData(dish),wxApi.getToken(WxApi.APP_ID,WxApi.SECRET), WxApi.DISH_TEMPLATE_ID,URL);
+                wxApi.againSendMessage(result,staff.getOpenId(), getMessageData(dish), WxApi.DISH_TEMPLATE_ID, URL,WxApi.APP_ID,WxApi.SECRET);
             }
         } catch (Exception e) {
             log.info("dish_trade_produce"+"异常信息:" + e.getMessage());

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

@@ -45,9 +45,13 @@ public class SmApiTest {
         centerUserJob.personApi(null);
     }
 
+    private static final String GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";
     @Autowired
     private BCryptPasswordEncoder bCryptPasswordEncoder;
 
+    @Autowired
+    private WxApi wxApi;
+
     @Test
     public void test2(){
         String admin = bCryptPasswordEncoder.encode("admin");
@@ -55,6 +59,15 @@ public class SmApiTest {
         System.out.println("密码是否一直:"+bCryptPasswordEncoder.matches("admin", admin));
     }
 
+    @Test
+    public void test3(){
+        TreeMap<String,String> tokenMap = new TreeMap<>();
+        tokenMap.put("grant_type","client_credential");
+        tokenMap.put("appid",WxApi.APP_ID);
+        tokenMap.put("secret",WxApi.SECRET);
+        String token = HttpUtils.sendGet(GET_TOKEN, WxApi.joinParam(tokenMap), null);
+    }
+