liugc před 1 rokem
rodič
revize
e0dcaa72ad

+ 1 - 1
app/api/controller/LoginController.php

@@ -91,7 +91,7 @@ class LoginController extends BaseApiController
     public function getOpenIdByCode()
     {
         $code = $this->request->get('code');
-        $res = LoginLogic::getOaResByCode(['code'=>$code]);
+        $res = LoginLogic::getOpenIdByCode(['code'=>$code]);
         if (false === $res) {
             return $this->fail(LoginLogic::getError());
         }

+ 2 - 2
app/api/logic/LoginLogic.php

@@ -154,11 +154,11 @@ class LoginLogic extends BaseLogic
     /**
      * 获取微信用户openid信息
      */
-    public static function getOaResByCode($params)
+    public static function getOpenIdByCode($params)
     {
         try {
             //通过code获取微信 openid
-            $response = (new WeChatOaService())->getOaResByCode($params['code']);
+            $response = (new WeChatOaService())->getOpenIdByCode($params['code']);
             return $response;
         } catch (\Exception $e) {
             self::$error = $e->getMessage();

+ 17 - 1
app/common/service/wechat/WeChatOaService.php

@@ -20,6 +20,7 @@ use app\common\model\notice\NoticeSetting;
 use EasyWeChat\Kernel\Exceptions\Exception;
 use EasyWeChat\OfficialAccount\Application;
 use think\facade\Log;
+use WpOrg\Requests\Requests;
 
 
 /**
@@ -73,7 +74,22 @@ class WeChatOaService
         return $config;
     }
 
-
+    /**
+     * @notes 只获取微信用户openid信息
+     * @return array
+     * @throws Exception
+     * @author 段誉
+     * @date 2023/2/27 12:03
+     */
+    public function getOpenIdByCode(string $code)
+    {
+        $config = WeChatConfigService::getOaConfig();
+        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
+        $url .= '?appid=' . $config['app_id'] . '&secret=' . $config['secret'] . '&code=' . $code;
+        $url .= '&grant_type=authorization_code';
+        $requests = Requests::get($url);
+        return json_decode($requests->body, true);
+    }
     /**
      * @notes 公众号-根据code获取微信信息
      * @param string $code