Просмотр исходного кода

微信公众号服务配置-账号迁移

chao 3 лет назад
Родитель
Сommit
8ec586991b

+ 11 - 34
src/main/java/com/caimei365/wechat/controller/WechatDebugApi.java

@@ -1,11 +1,8 @@
 package com.caimei365.wechat.controller;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
 import com.caimei365.wechat.utils.HttpUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -24,11 +21,6 @@ public class WechatDebugApi {
     @Value("${wechat.apiUrl}")
     private String wechatApiUrl;
 
-    @Value("${wechat.caimei.appid}")
-    private String caimeiAppid;
-    @Value("${wechat.caimei.secret}")
-    private String caimeiSecret;
-
     /**
      * 获取微信 access_token
      */
@@ -45,38 +37,23 @@ public class WechatDebugApi {
      * @param openid 初始openid,不填默认从头开始拉取
      */
     @GetMapping("/wechat/debug/user/list")
-    public String getUserList(String appid, String secret, String  openid) {
-        String tokenUrl = wechatApiUrl + "/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
-        String jsonResult = HttpUtil.httpRequest(tokenUrl, "GET", null);
-        log.info("【微信接口调试】获取access_token:{}", jsonResult);
-        if(StringUtils.hasLength(jsonResult)){
-            JSONObject jsonObject = JSON.parseObject(jsonResult);
-            String accessToken = jsonObject.getString("access_token");
-            String userUrl = wechatApiUrl + "/cgi-bin/user/get?access_token="+ accessToken + "&next_openid="+ openid;
-            String result = HttpUtil.httpRequest(userUrl, "GET", null);
-            log.info("【微信接口调试】获取用户列表:{}", result);
-            return result;
-        }
-        return "获取微信用户列表异常";
+    public String getUserList(String token, String  openid) {
+        String url =wechatApiUrl + "/cgi-bin/user/get?access_token="+ token + "&next_openid="+ openid;
+        String result = HttpUtil.httpRequest(url, "GET", null);
+        log.info("【微信接口调试】获取用户列表:{}", result);
+        return result;
     }
 
     /**
      * 公众号迁移后更新OpenId
      * 帐号迁移15天后,该转换接口将会失效、无法拉取到数据
+     * https://api.weixin.qq.com/cgi-bin/changeopenid?access_token=
      */
     @PostMapping("/wechat/debug/change/openid")
-    public String changeOpenId(@RequestBody String json) {
-        String tokenUrl = wechatApiUrl + "/cgi-bin/token?grant_type=client_credential&appid=" + caimeiAppid + "&secret=" + caimeiSecret;
-        String jsonResult = HttpUtil.httpRequest(tokenUrl, "GET", null);
-        log.info("【微信接口调试】获取access_token:{}", jsonResult);
-        if(StringUtils.hasLength(jsonResult)){
-            JSONObject jsonObject = JSON.parseObject(jsonResult);
-            String accessToken = jsonObject.getString("access_token");
-            String changeUrl = wechatApiUrl + "/cgi-bin/changeopenid?access_token=?access_token="+ accessToken;
-            String result = HttpUtil.httpRequest(changeUrl, "POST", json);
-            log.info("【微信接口调试】公众号迁移后更新OpenId:{}", result);
-            return result;
-        }
-        return "公众号迁移后更新OpenId异常";
+    public String changeOpenId(@RequestBody String json, String token) {
+        String url = wechatApiUrl + "/cgi-bin/changeopenid?access_token="+ token;
+        String result = HttpUtil.httpRequest(url, "POST", json);
+        log.info("【微信接口调试】公众号迁移后更新OpenId:{}", result);
+        return result;
     }
 }

+ 2 - 1
src/main/java/com/caimei365/wechat/controller/WechatServerApi.java

@@ -4,6 +4,7 @@ import com.caimei365.wechat.entity.WeChatConstant;
 import com.caimei365.wechat.service.WechatServerService;
 import com.caimei365.wechat.utils.MessageUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -96,7 +97,7 @@ public class WechatServerApi {
                     respXml = wechatServerService.handleTextMsg(requestMap);
                     break;
             }
-            if (respXml == null) { respXml = "success"; }
+            if (!StringUtils.hasLength(respXml)) { respXml = "success"; }
         } catch (Exception e) {
             log.info(">>>>>>>>>>微信公众号消息处理时出现异常:" + e);
         }

+ 0 - 1
src/main/java/com/caimei365/wechat/service/impl/WechatServerServiceImpl.java

@@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
-import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;

+ 0 - 1
src/main/java/com/caimei365/wechat/utils/HttpUtil.java

@@ -75,7 +75,6 @@ public class HttpUtil {
             inputStreamReader.close();
             // 释放资源
             inputStream.close();
-            inputStream = null;
             httpUrlConn.disconnect();
             return buffer.toString();
         } catch (Exception ignored) {}