diff --git a/app/Helpers/Str.php b/app/Helpers/Str.php new file mode 100644 index 00000000..1e8906e8 --- /dev/null +++ b/app/Helpers/Str.php @@ -0,0 +1,27 @@ + self::STATUS_ACTIVE, + ]; + + /** + * @var array + */ + protected $fillable = [ + 'username', + 'password', + 'phone', + 'phone_verified_at', + 'email', + 'email_verified_at', + 'last_login_ip', + 'last_login_at', + 'register_ip', + 'status', + 'status_remark', + ]; + + /** + * @var array + */ + protected $casts = [ + 'phone_verified_at' => 'datetime', + 'email_verified_at' => 'datetime', + 'last_login_at' => 'datetime', + 'status' => 'int', + ]; +} diff --git a/app/Models/UserInfo.php b/app/Models/UserInfo.php new file mode 100644 index 00000000..0117bdf6 --- /dev/null +++ b/app/Models/UserInfo.php @@ -0,0 +1,60 @@ + self::GENDER_UNKNOWN, + ]; + + /** + * @var array + */ + protected $fillable = [ + 'user_id', + 'inviter_id', + 'nickname', + 'avatar', + 'gender', + 'birthday', + ]; + + /** + * @var array + */ + protected $casts = [ + 'birthday' => 'date', + ]; + + /** + * {@inheritdoc} + */ + protected static function booted() + { + parent::saving(function ($userInfo) { + // 如果没有邀请码,则自动分配邀请码 + if ($userInfo->code === null) { + do { + $userInfo->code = Str::randomAlpha(6); + } while (static::where('code', $userInfo->code)->exists()); + } + + // 邀请码统一小写 + $userInfo->code = strtolower($userInfo->code); + }); + } +} diff --git a/database/migrations/2021_11_19_135844_create_users_table.php b/database/migrations/2021_11_19_135844_create_users_table.php new file mode 100644 index 00000000..84a2505d --- /dev/null +++ b/database/migrations/2021_11_19_135844_create_users_table.php @@ -0,0 +1,43 @@ +id(); + $table->string('username')->unique()->nullable()->comment('用户名'); + $table->string('password')->nullable()->comment('密码'); + $table->string('phone')->unique()->nullable()->comment('手机号'); + $table->timestamp('phone_verified_at')->nullable()->comment('手机号认证时间'); + $table->string('email')->unique()->nullable()->comment('邮箱'); + $table->timestamp('email_verified_at')->nullable()->comment('邮箱认证时间'); + $table->ipAddress('last_login_ip')->nullable()->comment('最近登录IP'); + $table->timestamp('last_login_at')->nullable()->comment('最近登录时间'); + $table->ipAddress('register_ip')->nullable()->comment('注册IP'); + $table->tinyInteger('status')->default(1)->comment('状态: -1 冻结,0 未激活,1 已激活'); + $table->string('status_remark')->nullable()->comment('状态备注'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('users'); + } +} diff --git a/database/migrations/2021_11_19_142529_create_user_infos_table.php b/database/migrations/2021_11_19_142529_create_user_infos_table.php new file mode 100644 index 00000000..4a47630b --- /dev/null +++ b/database/migrations/2021_11_19_142529_create_user_infos_table.php @@ -0,0 +1,38 @@ +id(); + $table->unsignedBigInteger('user_id')->unique()->comment('用户ID'); + $table->unsignedBigInteger('inviter_id')->nullable()->comment('邀请人ID'); + $table->string('nickname', 100)->nullable()->comment('昵称'); + $table->string('avatar')->nullable()->comment('头像'); + $table->enum('gender', ['male', 'female', 'unknown'])->default('unknown')->comment('性别:male 男性,female 女性,unknown 未知'); + $table->date('birthday')->nullable()->comment('生日'); + $table->string('code', 10)->unique()->comment('邀请码(全小写)'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_infos'); + } +}