From 735296b0f9945da6ed4df4e12cce6f6d155b6ced Mon Sep 17 00:00:00 2001
From: huangdeliang <huangdeliang@skieer.com>
Date: Thu, 13 May 2021 20:01:15 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=8E=A5=E5=8F=A3=E5=BC=80?=
 =?UTF-8?q?=E5=8F=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../custom/WechatAppletController.java        |  8 ++
 .../stdiet/custom/domain/SysWxUserInfo.java   |  1 -
 .../domain/wechat/WxPostRecipesMessage.java   | 17 ++++
 .../custom/service/IWechatAppletService.java  |  8 ++
 .../service/impl/WeChartAppletServiceImp.java | 84 +++++++++++++++++++
 .../stdiet/framework/config/ServerConfig.java |  8 ++
 6 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/domain/wechat/WxPostRecipesMessage.java
 create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/service/IWechatAppletService.java
 create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/service/impl/WeChartAppletServiceImp.java

diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
index 5e058dc19..a1c48c6de 100644
--- a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
+++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/WechatAppletController.java
@@ -66,6 +66,8 @@ public class WechatAppletController extends BaseController {
     private ISysOrderPauseService sysOrderPauseService;
     @Autowired
     private ISysDictTypeService iSysDictTypeService;
+    @Autowired
+    private IWechatAppletService iWechatAppletService;
 
     /**
      * 查询微信小程序中展示的客户案例
@@ -472,6 +474,7 @@ public class WechatAppletController extends BaseController {
         }
 
         curWxUserInfo.setCustomerId(AesUtils.encrypt(curWxUserInfo.getCusId().toString()));
+        curWxUserInfo.setCusId(null);
 
         // 并返回一系列登录后的数据
         return AjaxResult.success(curWxUserInfo);
@@ -530,6 +533,11 @@ public class WechatAppletController extends BaseController {
         return request.getParameter("echostr");
     }
 
+    @GetMapping("/getToken")
+    public String getToken(@RequestParam String appId) {
+        return iWechatAppletService.getAccessToken(appId);
+    }
+
 
 }
 
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysWxUserInfo.java b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysWxUserInfo.java
index 83ce90fd4..a5e9a47dc 100644
--- a/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysWxUserInfo.java
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/domain/SysWxUserInfo.java
@@ -20,7 +20,6 @@ public class SysWxUserInfo {
      */
     private String openid;
 
-    @JsonIgnore
     private Long cusId;
 
     private String customerId;
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/domain/wechat/WxPostRecipesMessage.java b/stdiet-custom/src/main/java/com/stdiet/custom/domain/wechat/WxPostRecipesMessage.java
new file mode 100644
index 000000000..53d2a024c
--- /dev/null
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/domain/wechat/WxPostRecipesMessage.java
@@ -0,0 +1,17 @@
+package com.stdiet.custom.domain.wechat;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class WxPostRecipesMessage {
+
+    String name;
+
+    Date startDate;
+
+    Date endDate;
+
+    String remark;
+}
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/IWechatAppletService.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/IWechatAppletService.java
new file mode 100644
index 000000000..af555b9c1
--- /dev/null
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/IWechatAppletService.java
@@ -0,0 +1,8 @@
+package com.stdiet.custom.service;
+
+public interface IWechatAppletService {
+
+    public String getAccessToken(String appId);
+
+    public void postRecipesMessage(String appId, String openId, String name, String startDate, String endDate, String remark);
+}
diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/WeChartAppletServiceImp.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/WeChartAppletServiceImp.java
new file mode 100644
index 000000000..300d512bf
--- /dev/null
+++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/WeChartAppletServiceImp.java
@@ -0,0 +1,84 @@
+package com.stdiet.custom.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.google.gson.JsonObject;
+import com.stdiet.common.core.redis.RedisCache;
+import com.stdiet.common.utils.StringUtils;
+import com.stdiet.custom.service.IWechatAppletService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+@Service
+public class WeChartAppletServiceImp implements IWechatAppletService {
+
+    @Autowired
+    private RedisCache redisCache;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Override
+    public String getAccessToken(String appId) {
+        String accessToken = redisCache.getCacheObject(appId);
+        if (StringUtils.isNull(accessToken)) {
+            String appSecret = "";
+            if (appId.equals("wx26be9b2aa525fc1e")) {
+                appSecret = "de436c17e42e6fc0637bd0de169ea0c1";
+            }
+            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appId}&secret={appSecret}";
+            Map<String, String> param = new HashMap<>();
+            param.put("appId", appId);
+            param.put("appSecret", appSecret);
+
+            ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class, param);
+
+            JSONObject resultObj = JSONObject.parseObject(entity.getBody());
+
+            if (resultObj.getInteger("errcode") == 0) {
+                accessToken = resultObj.getString("access_token");
+                Integer expiresIn = resultObj.getInteger("expires_in");
+                redisCache.setCacheObject(appId, accessToken, expiresIn, TimeUnit.SECONDS);
+            }
+        }
+        return accessToken;
+    }
+
+    @Override
+    public void postRecipesMessage(String appId, String openId, String name, String startDate, String endDate, String remark) {
+        String accessToken = getAccessToken(appId);
+        if (StringUtils.isNull(accessToken)) {
+            return;
+        }
+        String tmpId = "";
+        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
+
+
+        JsonObject param = new JsonObject();
+        param.addProperty("access_token", accessToken);
+        param.addProperty("touser", openId);
+        param.addProperty("template_id", tmpId);
+        param.addProperty("page", "pages/recipes/index");
+
+        JsonObject dataParam = new JsonObject();
+        dataParam.addProperty("key1", name);
+        dataParam.addProperty("key2", startDate);
+        dataParam.addProperty("key3", endDate);
+        dataParam.addProperty("key4", remark);
+
+        param.add("data", dataParam);
+
+        ResponseEntity<String> entity = restTemplate.postForEntity(url, param, String.class);
+
+        JSONObject resultObj = JSONObject.parseObject(entity.getBody());
+
+        System.out.println(resultObj.toJSONString());
+//        Integer errcode = resultObj.getInteger("errcode");
+
+    }
+}
diff --git a/stdiet-framework/src/main/java/com/stdiet/framework/config/ServerConfig.java b/stdiet-framework/src/main/java/com/stdiet/framework/config/ServerConfig.java
index f215a3b4e..5251a550d 100644
--- a/stdiet-framework/src/main/java/com/stdiet/framework/config/ServerConfig.java
+++ b/stdiet-framework/src/main/java/com/stdiet/framework/config/ServerConfig.java
@@ -1,8 +1,11 @@
 package com.stdiet.framework.config;
 
 import javax.servlet.http.HttpServletRequest;
+
+import org.springframework.context.annotation.Bean;
 import org.springframework.stereotype.Component;
 import com.stdiet.common.utils.ServletUtils;
+import org.springframework.web.client.RestTemplate;
 
 /**
  * 服务相关配置
@@ -29,4 +32,9 @@ public class ServerConfig
         String contextPath = request.getServletContext().getContextPath();
         return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
     }
+
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
 }