牟新芬 3 years ago
parent
commit
03cdf12200
2 changed files with 35 additions and 1 deletions
  1. 32 0
      app/api/controller/v1/Weixin.php
  2. 3 1
      app/api/route/user.php

+ 32 - 0
app/api/controller/v1/Weixin.php

@@ -12,6 +12,7 @@ use app\Request;
 use EasyWeChat\Factory;
 use library\lib\weixina;
 use library\services\UtilService;
+use library\services\MiniProgramService;
 use think\db\exception\DbException;
 use think\db\exception\PDOException;
 use think\Exception;
@@ -32,6 +33,9 @@ class Weixin extends BaseController
         return app('json')->success($jssdk);
     }
 
+    /**
+     * 公众号获取微信信息
+     */
     public function getInfo(Request $request)
     {
         $code = trim($request->get('code'));
@@ -55,6 +59,34 @@ class Weixin extends BaseController
         }
     }
 
+    /**
+     * 小程序授权登录
+     * @param Request $request
+     * @return mixed
+     * @throws \Psr\SimpleCache\InvalidArgumentException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function mp_auth(Request $request){
+        $code = trim($request->post('code'));
+        if (!$code)
+            return app('json')->fail('授权失败,参数有误');
+        try {
+            $appid = "wxb4c2433f398a6a48";
+            $secret = "3fb376e8197f3fb9670644adbcabae59";
+            $c= file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code");
+            dump(json_encode($c));//对JSON格式的字符串进行编码
+
+//            $userInfoCong = MiniProgramService::getUserInfo($code);
+//            $session_key = $userInfoCong['session_key'];
+//            $openid = $userInfoCong['openid'];
+//            $unionid = $userInfoCong['unionid'];
+        } catch (\Exception $e) {
+            return app('json')->fail('获取session_key失败,请检查您的配置!', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
+        }
+    }
+
     /**
      * @param Request $request
      */

+ 3 - 1
app/api/route/user.php

@@ -21,8 +21,10 @@ Route::group('user',function () {
     Route::post('address/del', 'v1.user/address_del')->name('addressDel');//删除地址
     //基本信息
     Route::rule('init','v1.user/init');
-    //获取信息
+    //获取微信信息
     Route::rule('getInfo', 'v1.weixin/getInfo');
+    //获取小程序信息
+    Route::rule('mp_auth', 'v1.weixin/mp_auth');
     //会员数据
     Route::rule('index', 'v1.user/index');
     //平台数据