浏览代码

增加匿名注册

vali 6 年之前
父节点
当前提交
8307f19c5f
共有 1 个文件被更改,包括 53 次插入11 次删除
  1. 53 11
      application/index/controller/Register.php

+ 53 - 11
application/index/controller/Register.php

@@ -4,6 +4,7 @@
  * Date: 2017/10/24 10:46
  * Email: 1902822973@qq.com
  */
+
 namespace app\index\controller;
 
 use think\Controller;
@@ -23,7 +24,7 @@ class Register extends Controller
     // 处理注册
     public function doRegister()
     {
-        if(request()->isPost()){
+        if (request()->isPost()) {
 
             $userName = input("param.user_name/s");
             $userEmail = input("param.user_email/s");
@@ -31,37 +32,37 @@ class Register extends Controller
             $phone = input("param.phone/s");
             $emailCode = input("param.code/s");
 
-            if(empty($userName)){
+            if (empty($userName)) {
                 return json(['code' => -1, 'data' => '', 'msg' => '用户名不能为空']);
             }
 
-            if(empty($userEmail)){
+            if (empty($userEmail)) {
                 return json(['code' => -2, 'data' => '', 'msg' => '邮箱不能为空']);
             }
 
-            if(empty($password)){
+            if (empty($password)) {
                 return json(['code' => -3, 'data' => '', 'msg' => '密码不能为空']);
             }
 
-            if(empty($phone)){
+            if (empty($phone)) {
                 return json(['code' => -4, 'data' => '', 'msg' => '密码不能为空']);
             }
 
-            if(empty($emailCode)){
+            if (empty($emailCode)) {
                 return json(['code' => -5, 'data' => '', 'msg' => '验证码不能为空']);
             }
 
-            if($emailCode != session('code')){
+            if ($emailCode != session('code')) {
                 return json(['code' => -6, 'data' => '', 'msg' => '验证码不正确']);
             }
 
             $name = db('accounts')->where('account_name', $userName)->find();
-            if(!empty($name)){
+            if (!empty($name)) {
                 return json(['code' => -7, 'data' => '', 'msg' => '用户名已存在']);
             }
 
             $email = db('accounts')->where('account_email', $userEmail)->find();
-            if(!empty($email)){
+            if (!empty($email)) {
                 return json(['code' => -8, 'data' => '', 'msg' => '邮箱已存在']);
             }
 
@@ -79,8 +80,8 @@ class Register extends Controller
             $user_id = db('accounts')->insertGetId($userInfo);
 
             // 注册成功 生成token
-            $module = mt_rand(100000,999999);
-            $token = base64_encode($module.'#$@%!^*/'.time().'/'.$userInfo['id']);
+            $module = mt_rand(100000, 999999);
+            $token = base64_encode($module . '#$@%!^*/' . time() . '/' . $userInfo['id']);
             // 更新用户状态
             $param = [
                 'token' => $token,
@@ -92,4 +93,45 @@ class Register extends Controller
         }
     }
 
+
+    //自动注册
+    public function autoReg()
+    {
+        $appid = input("post.appid/s");
+        $appuid = input("post.appuid/s");
+        $token = trim(input("post.token/s"));
+        $nowuid = (empty($appuid) || empty($appuid)) ? uniqid('anon_') : $appid . '_' . $appuid;
+        $avatar = "http://wx2.sinaimg.cn/mw690/5db11ff4gy1flxmew7edlj203d03wt8n.jpg";  //默认头像
+
+        if ($token) {
+            $old = db('accounts')->where(['token' => $token])->find();
+            if ($old) {
+                if ($old['status'] != 1) {
+                    return json(['code' => 0, 'data' => [], 'msg' => '禁止登陆']);
+                }
+                return json(['code' => 1, 'data' => ['id' => $old['id'], 'name' => $old['account_name'], 'token' => $old['token'], 'avatar' => $avatar], 'msg' => '注册成功']);
+            }
+        }
+
+
+        $now = time();
+        $token = md5(uniqid() . rand(10000, 50000));
+        $newdata = [
+            'account_name' => $nowuid,
+            'password' => md5(time() . rand(1, 5000)),
+            'status' => 1,
+            'add_time' => $now,
+            'last_login_time' => $now,
+            'token' => $token,
+            'expire_time' => $now,
+        ];
+        $retid = Db::name('accounts')->insertGetId($newdata);
+        if ($retid) {
+            return json(['code' => 1, 'data' => ['id' => $retid, 'name' => $nowuid, 'token' => $token, 'avatar' => $avatar], 'msg' => '注册成功']);
+        } else {
+            return json(['code' => 0, 'data' => [], 'msg' => '注册用户失败']);
+        }
+
+    }
+
 }