6
0
Fork 0

用户和用户信息模型

release
李静 2021-11-19 17:14:43 +08:00
parent e0084c5d77
commit 171c2e29d2
5 changed files with 223 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<?php
namespace App\Helpers;
class Str
{
/**
* 生成给定长度的随机英文字符串
*
* @param int $length
* @return string
*/
public static function randomAlpha(int $length = 6): string
{
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$max = strlen($characters) - 1;
$str = '';
for ($i=0; $i < $length; $i++) {
$str .= $characters[mt_rand(0, $max)];
}
return $str;
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\Authorizable;
class User extends Model implements AuthorizableContract, AuthenticatableContract
{
use Authenticatable;
use Authorizable;
use HasFactory;
public const STATUS_FROZEN = -1; // 冻结
public const STATUS_INACTIVATED = 0; // 未激活
public const STATUS_ACTIVE = 1; // 正常
/**
* @var array
*/
protected $attributes = [
'status' => 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',
];
}

View File

@ -0,0 +1,60 @@
<?php
namespace App\Models;
use App\Helpers\Str;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class UserInfo extends Model
{
use HasFactory;
public const GENDER_UNKNOWN = 'unknown'; // 未知
public const GENDER_MALE = 'male'; // 男性
public const GENDER_FEMALE = 'female'; // 女性
/**
* @var array
*/
protected $attributes = [
'gender' => 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);
});
}
}

View File

@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->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');
}
}

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserInfosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_infos', function (Blueprint $table) {
$table->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');
}
}