瀏覽代碼

电视盒子用户自动登录+注册

lizhen_gitee 9 月之前
父節點
當前提交
f5b6f7006c
共有 3 個文件被更改,包括 90 次插入4 次删除
  1. 6 2
      application/api/controller/Demo.php
  2. 2 2
      application/common/controller/Apitv.php
  3. 82 0
      application/common/library/Auth.php

+ 6 - 2
application/api/controller/Demo.php

@@ -87,8 +87,12 @@ class Demo extends Api
      */
     public function test3()
     {
-        $a = 346846154;
-        dump(format_bytes($a));
+        $tv_userid = input('tv_userid');
+
+        $salt = 'be7bcf1499b0fec801406f6aafbd04c4';
+        $get_sign = md5(md5($tv_userid) . $salt);
+
+        dump($get_sign);
     }
 
     public function test4(){

+ 2 - 2
application/common/controller/Apitv.php

@@ -14,7 +14,7 @@ use think\Response;
 use think\Route;
 use think\Validate;
 use Redis;
-
+use think\Db;
 /**
  * API控制器基类
  */
@@ -103,7 +103,7 @@ class Apitv
         }
 
         //验签
-        $salt = '123456';
+        $salt = 'be7bcf1499b0fec801406f6aafbd04c4';
         $get_sign = md5(md5($tv_userid) . $salt);
         if($tv_sign != $get_sign){
             $this->error('验签失败');

+ 82 - 0
application/common/library/Auth.php

@@ -239,6 +239,88 @@ class Auth
         }
         return true;
     }
+    public function tv_register($tv_userid = '')
+    {
+        if(empty($tv_userid)){
+            $this->setError('盒子用户id必填');
+            return false;
+        }
+
+        if ($tv_userid && User::getByTvUserid($tv_userid)) {
+            $this->setError('tv user already exist');
+            return false;
+        }
+
+        $ip = request()->ip();
+        $time = time();
+
+        $data = [
+            'tv_userid' => $tv_userid,
+            'nickname'  => '盒子用户',
+            'comefrom'  => 2,
+            'avatar'    => config('site.user_default_avatar'),
+        ];
+        $params = array_merge($data, [
+            'jointime'  => $time,
+            'joinip'    => $ip,
+            'logintime' => $time,
+            'loginip'   => $ip,
+            'prevtime'  => $time,
+            'status'    => 1
+        ]);
+
+        //账号注册时需要开启事务,避免出现垃圾数据
+        Db::startTrans();
+        try {
+            $user = User::create($params, true);
+
+            $this->_user = User::get($user->id);
+            $this->_user->username = 'u' . (10000 + $user->id);
+            $this->_user->save();
+
+            //设置Token
+            $this->_token = Random::uuid();
+            Token::set($this->_token, $user->id, $this->keeptime);
+
+            //设置登录状态
+            $this->_logined = true;
+
+            //注册钱包
+            $wallet_id = Db::name('user_wallet')->insertGetId(['user_id'=>$user->id]);
+            if(!$wallet_id){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }
+
+            //[环信]注册用户。忽略失败
+            /*$easemob = new Easemob();
+            $rs = $easemob->user_create('user'.$user->id);
+            if($rs === false){
+                $this->setError('注册用户失败');
+                Db::rollback();
+                return false;
+            }*/
+
+            //腾讯im注册用户
+            $tenim = new Tenim();
+            $rs = $tenim->register('user'.$user->id,$params['nickname'],'');
+            if($rs !== true){
+                $this->setError($rs);
+                Db::rollback();
+                return false;
+            }
+
+            //注册成功的事件
+            Hook::listen("user_register_successed", $this->_user, $data);
+            Db::commit();
+        } catch (Exception $e) {
+            $this->setError($e->getMessage());
+            Db::rollback();
+            return false;
+        }
+        return true;
+    }
 
     /**
      * 用户登录