فهرست منبع

添加获取微信authorization接口

lighter 4 ماه پیش
والد
کامیت
5f56426d81

+ 6 - 0
src/main/java/thyyxxk/wxservice_server/controller/WxApiController.java

@@ -39,6 +39,12 @@ public class WxApiController {
         this.pushWxMessageService = pushWxMessageService;
     }
 
+    @PassToken
+    @GetMapping("/getAuthorization")
+    public JSONObject getAuthorization(@RequestParam("code") String code) {
+        return service.getAuthorization(code);
+    }
+
     @PassToken
     @GetMapping("/synchronizeAccessToken")
     public ResultVo<String> synchronizeAccessToken(@RequestParam("token") String token) {

+ 12 - 0
src/main/java/thyyxxk/wxservice_server/service/WxApiService.java

@@ -7,6 +7,7 @@ import com.wechat.pay.java.service.payments.model.Transaction;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
 import thyyxxk.wxservice_server.config.exception.ExceptionEnum;
 import thyyxxk.wxservice_server.constant.OrderType;
 import thyyxxk.wxservice_server.dao.WxApiDao;
@@ -40,6 +41,17 @@ public class WxApiService {
         this.savePayResultService = savePayResultService;
     }
 
+    public JSONObject getAuthorization(String code) {
+        RestTemplate template = new RestTemplate();
+        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +
+                PropertiesUtil.getLocalProperty("appId") + "&secret=" +
+                PropertiesUtil.getLocalProperty("appSecret") + "&code=" +
+                code + "&grant_type=authorization_code";
+        String str = template.getForObject(url, String.class);
+        log.info("微信获取authorization:{}", str);
+        return JSONObject.parseObject(str);
+    }
+
     public ResultVo<JsApiSHA1> getJsapiSHA1Sign(JsApiSHA1 data) throws Exception {
         data.setAppId(PropertiesUtil.getLocalProperty("appId"));
         data.setTicket(PropertiesUtil.getLocalProperty("ticket"));