Ver Fonte

修改获取accessToken

yusm há 3 semanas atrás
pai
commit
6b1dc90dfd

+ 23 - 1
fhKeeper/formulahousekeeper/management-crm-qrcode/src/main/java/com/management/platform/controller/WechatCallbackController.java

@@ -1,5 +1,6 @@
 package com.management.platform.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.management.platform.entity.Custom;
@@ -20,6 +21,8 @@ import org.apache.http.util.EntityUtils;
 import org.dom4j.Document;
 import org.dom4j.DocumentHelper;
 import org.dom4j.Element;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.RestTemplate;
 
@@ -50,6 +53,12 @@ public class WechatCallbackController {
     @Resource
     private WechatAccountService wechatAccountService;
 
+    @Value(value = "${wxqr.app_id}")
+    private String appId;
+
+    @Value(value = "${wxqr.app_secret}")
+    private String appsecret;
+
     private final ObjectMapper objectMapper = new ObjectMapper();
 
     // 微信配置验证接口(GET请求)
@@ -225,7 +234,8 @@ public class WechatCallbackController {
         if (StringUtils.isNotEmpty(userId)){
             accessToken= getAccessToken(userId);
         }else {
-            accessToken= getAccessToken();
+            accessToken= getAccessToken(appId,appsecret);
+            log.info("accessToken==>"+accessToken);
         }
         if (accessToken == null) {
             return null;
@@ -271,4 +281,16 @@ public class WechatCallbackController {
         return wechatAccountService.getAccessToken(wechatAccount.getCompanyId(), wechatAccount.getAppId());
     }
 
+    public String getAccessToken(String appId, String appSecret) {
+        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
+        log.info("url==>"+url);
+        try {
+            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
+            JSONObject json = JSONObject.parseObject(response.getBody());
+            return json.getString("access_token");
+        } catch (Exception e) {
+            throw new RuntimeException("获取access_token失败", e);
+        }
+    }
+
 }

+ 5 - 0
fhKeeper/formulahousekeeper/management-crm-qrcode/src/main/resources/application.yml

@@ -181,3 +181,8 @@ weixin:
      QR_STR_SCENE: QR_STR_SCENE
      QR_LIMIT_SCENE: QR_LIMIT_SCENE
      QR_LIMIT_STR_SCENE: QR_LIMIT_STR_SCENE
+
+
+wxqr:
+  app_id: wx749c84daac654e1e
+  app_secret: aacbd046ec1c790836f4f684c96fe585