Browse Source

:construction: 新注册用户为basic权限

visuddhinanda 2 years ago
parent
commit
4ae5117fd5
1 changed files with 48 additions and 40 deletions
  1. 48 40
      app/Http/Controllers/SignUpController.php

+ 48 - 40
app/Http/Controllers/SignUpController.php

@@ -6,8 +6,9 @@ use App\Models\UserInfo;
 use App\Models\Invite;
 use App\Models\Channel;
 use Illuminate\Support\Str;
-
+use Illuminate\Support\Facades\DB;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Log;
 
 class SignUpController extends Controller
 {
@@ -32,60 +33,67 @@ class SignUpController extends Controller
         //先查询invite核对uuid
         if(!Invite::where('id',$request->get('token'))
                   ->where('email',$request->get('email'))->exists()){
-            $this->error('error token',[],200);
+            $this->error('error token','',200);
         }
         if(UserInfo::where('username',$request->get('name'))->exists()){
-            $this->error('avoid user name',[],200);
+            $this->error('avoid user name','',200);
         }
-        $user = new UserInfo;
-        $user->userid = Str::Uuid();
-        $user->username = $request->get('username');
-        $user->nickname = $request->get('nickname');
-        $user->email = $request->get('email');
-        $user->password = md5($request->get('password'));
-        $user->create_time = time()*1000;
-        $user->modify_time = time()*1000;
-        $user->save();
 
-        //标记invite
-        Invite::where('id',$request->get('token'))
-                  ->where('email',$request->get('email'))
-                  ->update(['status'=>'sign-up']);
-        //建立channel
-        $channel = new Channel;
-        $channel->id = app('snowflake')->id();
-        $channel->name = $request->get('username');
-        $channel->owner_uid = $user->userid;
-        $channel->type = "translation";
-        $channel->lang = $request->get('lang');
-        $channel->editor_id = $user->id;
-        $channel->status = 30;
-        $channel->create_time = time()*1000;
-        $channel->modify_time = time()*1000;
-        $channel->save();
+        try {
+            DB::transaction(function() use($request){
+                $user = new UserInfo;
+                $user->userid = Str::Uuid();
+                $user->username = $request->get('username');
+                $user->nickname = $request->get('nickname');
+                $user->email = $request->get('email');
+                $user->password = md5($request->get('password'));
+                $user->role = json_encode(['basic']);
+                $user->create_time = time()*1000;
+                $user->modify_time = time()*1000;
+                $user->save();
+
+                //标记invite
+                Invite::where('id',$request->get('token'))
+                        ->where('email',$request->get('email'))
+                        ->update(['status'=>'sign-up']);
+                //建立channel
 
-        $channel_draft = new Channel;
-        $channel_draft->id = app('snowflake')->id();
-        $channel_draft->name = 'draft';
-        $channel_draft->owner_uid = $user->userid;
-        $channel_draft->type = "translation";
-        $channel_draft->lang = $request->get('lang');
-        $channel_draft->editor_id = $user->id;
-        $channel_draft->create_time = time()*1000;
-        $channel_draft->modify_time = time()*1000;
-        $channel_draft->save();
+                $channel_draft = new Channel;
+                $channel_draft->id = app('snowflake')->id();
+                $channel_draft->name = 'draft';
+                $channel_draft->owner_uid = $user->userid;
+                $channel_draft->type = "translation";
+                $channel_draft->lang = $request->get('lang');
+                $channel_draft->status = 5;
+                $channel_draft->editor_id = $user->id;
+                $channel_draft->create_time = time()*1000;
+                $channel_draft->modify_time = time()*1000;
+                $channel_draft->save();
+            });
+        }catch(\Exception $e) {
+            Log::error('user create fail',['data'=>$e]);
+            return $this->error('user create fail',500,500);
+        }
         return $this->ok('ok');
     }
 
     /**
      * Display the specified resource.
      *
-     * @param  \App\Models\UserInfo  $userInfo
+     * @param  string $username
      * @return \Illuminate\Http\Response
      */
-    public function show(UserInfo $userInfo)
+    public function show(Request $request,string $username)
     {
         //
+        $email = UserInfo::where('email',$request->get('email'))->exists();
+        $user = UserInfo::where('username',$username)->exists();
+        if($email && $user){
+            //send email
+            return $this->ok('ok');
+        }else{
+            return $this->error(['email'=>$email,'username'=>$user],[200],200);
+        }
     }
 
     /**