Browse Source

feat():授权

geek 4 years ago
parent
commit
4d18082179

+ 18 - 5
application/index/controller/Index.php

@@ -9,6 +9,9 @@ namespace app\index\controller;
 
 use app\common\service\CommonService;
 use app\index\exception\ApiException;
+use app\index\model\UserModel;
+use think\console\command\make\Model;
+use think\facade\Cache;
 use think\facade\Session;
 
 class Index {
@@ -21,15 +24,25 @@ class Index {
             throw new ApiException('渠道id不为空');
         }
         $data = CommonService::getSetData($channelId);
-        if (empty(Session::get('wxId'))){
-            Session::set('visitorId','');
+        $info = (new UserModel())::where(['unionid' => Session::get('wxId'), 'channel_id' => $channelId])->find();
+        if (empty(Session::get('wxId')) || empty($info)){
+            Cache::set(Session::get('wxId'), '');
             header("Location:".CommonService::getAuthUrl($channelId));
             die();
         }
-        $visitorId =  Session::get($visitorId);
-        if(empty($visitorId)){
-            Session::set('visitorId', $visitorId);
+        if (empty(Cache::get(Session::get('wxId')))) {
+            Cache::set(Session::get('wxId'), $visitorId);
         }
+        $visitorId = Cache::get(Session::get('wxId'));
+        //        $params = [
+//            'id'     => $info['channel_id'],
+//            'userid' => $info['unionid'],
+//            'name'   => $info['nickName'],
+//            'avatar' => $info['avatar'],
+//            'key'    => $info['key']
+//        ];
+//        header('Location:http://mudu.tv/activity.php?a=userAssign&'.http_build_query($params));
+//        die();
         //增加客户自己的验证逻辑,例如登录,关注,付费,填手机号等
         $key = md5($visitorId.$data['channel_auth_code']);
         if(strpos($notifyUrl,'?') !== false){//url参数处理,将key加到url参数中

+ 9 - 0
application/index/controller/User.php

@@ -10,6 +10,8 @@ namespace app\index\controller;
 use app\common\service\CommonService;
 use app\index\BaseController;
 use app\index\exception\ApiException;
+use app\index\model\UserModel;
+use think\facade\Cache;
 use think\facade\Session;
 
 class User extends BaseController {
@@ -27,6 +29,13 @@ class User extends BaseController {
             'key'    => md5(input('unionid').CommonService::getSetData($channelId)['channel_auth_code'])
         ];
         Session::set('wxId',input('unionid'));
+        (new UserModel())->insertGetId([
+            'channel_id' => $channelId,
+            'unionid'    => input('unionid'),
+            'name'       => input('nickName'),
+            'avatar'     => input('avatar'),
+            'key'        => $params['key'],
+        ]);
         header('Location:http://mudu.tv/activity.php?a=userAssign&'.http_build_query($params));
     }
 

+ 15 - 0
application/index/model/UserModel.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * Author: luzheng.liu
+ * Time: 2020/12/5 19:52
+ */
+
+namespace app\index\model;
+
+
+use think\Model;
+
+class UserModel  extends Model {
+
+    protected $table = 'user';
+}

+ 4 - 1
config/cache.php

@@ -15,11 +15,14 @@
 
 return [
     // 驱动方式
-    'type'   => 'File',
+    'type'   => 'reids',
     // 缓存保存目录
     'path'   => '',
     // 缓存前缀
     'prefix' => '',
     // 缓存有效期 0表示永久缓存
     'expire' => 0,
+    'host'     => '127.0.0.1',
+//    'port'     => '',
+//    'password' => ''
 ];