diff --git a/README.md b/README.md index 1803d2b..d5a8b41 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Lumen-IM 是一个网页版在线即时聊天项目,前端使用 Element-ui + 1. 下载源码包 2. 安装框架依赖包执行 `compsoer install` 命令 [项目根目录下执行] 2. 拷贝项目根目录下 .env.example 文件为 .env 并正确配置相关参数(mysql、redis) -3. 执行数据库迁移文件命令生成相关数据表 `php bin/hyperf.php migrate` +3. 执行项目安装命令(安装数据库及测试数据) `php bin/hyperf.php system:install` 4. 启动运行项目 `php bin/hyperf.php start` 注 :[项目运行之前请确保 MySQL、Redis 及 Nginx 服务] diff --git a/app/Command/InstallCommand.php b/app/Command/InstallCommand.php new file mode 100644 index 0000000..d32b0fd --- /dev/null +++ b/app/Command/InstallCommand.php @@ -0,0 +1,43 @@ +container = $container; + + parent::__construct('system:install'); + } + + public function configure() + { + parent::configure(); + $this->setDescription('LumenIM 初始化数据表及生成测试数据!'); + } + + public function handle() + { + $this->line('LumenIM 正在创建数据库,请耐心等待!', 'info'); + $this->call('migrate'); + + if ($this->confirm('是否需要生成测试数据')) { + $this->call('db:seed'); + } + } +} diff --git a/migrations/2020_11_04_152602_create_users_table.php b/migrations/2020_11_04_152602_create_users_table.php index ec3e661..0571af0 100644 --- a/migrations/2020_11_04_152602_create_users_table.php +++ b/migrations/2020_11_04_152602_create_users_table.php @@ -14,7 +14,7 @@ class CreateUsersTable extends Migration { Schema::create('users', function (Blueprint $table) { $table->unsignedInteger('id', true)->comment('用户ID'); - $table->string('mobile', 11)->default('')->unique()->comment('手机号'); + $table->string('mobile', 11)->default('')->comment('手机号'); $table->string('nickname', 20)->default('')->comment('用户昵称'); $table->string('avatar', 255)->default('')->comment('用户头像地址'); $table->unsignedTinyInteger('gender')->default(0)->comment('用户性别[0:未知;1:男;2:女;]'); @@ -29,7 +29,7 @@ class CreateUsersTable extends Migration $table->collation = 'utf8_general_ci'; $table->engine = 'InnoDB'; - $table->unique(['mobile'], 'idx_mobile'); + $table->unique(['mobile'], 'uk_mobile'); }); $prefix = config('databases.default.prefix'); diff --git a/migrations/2020_11_04_153431_create_talk_records_table.php b/migrations/2020_11_04_153431_create_talk_records_table.php index 9a0ca7a..9777489 100644 --- a/migrations/2020_11_04_153431_create_talk_records_table.php +++ b/migrations/2020_11_04_153431_create_talk_records_table.php @@ -13,7 +13,7 @@ class CreateTalkRecordsTable extends Migration public function up(): void { Schema::create('talk_records', function (Blueprint $table) { - $table->unsignedBigInteger('id', true)->comment('聊天记录ID'); + $table->unsignedInteger('id', true)->comment('聊天记录ID'); $table->unsignedTinyInteger('talk_type')->unsigned()->default(1)->comment('对话类型[1:私信;2:群聊;]'); $table->unsignedTinyInteger('msg_type')->unsigned()->default(1)->comment('消息类型[1:文本消息;2:文件消息;3:会话消息;4:代码消息;5:投票消息;6:群公告;7:好友申请;8:登录通知;9:入群消息/退群消息;]'); $table->unsignedInteger('user_id')->default(0)->comment('发送者ID(0:代表系统消息 >0: 用户ID)'); @@ -21,7 +21,7 @@ class CreateTalkRecordsTable extends Migration $table->tinyInteger('is_revoke')->default(0)->comment('是否撤回消息[0:否;1:是]'); $table->tinyInteger('is_mark')->default(0)->comment('是否重要消息[0:否;1:是;]'); $table->tinyInteger('is_read')->default(0)->comment('是否已读[0:否;1:是;]'); - $table->unsignedBigInteger('quote_id')->default(0)->comment('引用消息ID'); + $table->unsignedInteger('quote_id')->default(0)->comment('引用消息ID'); $table->text('content')->nullable(true)->charset('utf8mb4')->comment('文本消息 {@nickname@}'); $table->string('warn_users', 200)->default('')->comment('@好友 、 多个用英文逗号 “,” 拼接 (0:代表所有人)'); $table->dateTime('created_at')->nullable(true)->comment('创建时间'); diff --git a/migrations/2020_11_04_153442_create_talk_records_file_table.php b/migrations/2020_11_04_153442_create_talk_records_file_table.php index 29badb2..4cae5b9 100644 --- a/migrations/2020_11_04_153442_create_talk_records_file_table.php +++ b/migrations/2020_11_04_153442_create_talk_records_file_table.php @@ -22,7 +22,7 @@ class CreateTalkRecordsFileTable extends Migration $table->string('original_name', 100)->default('')->comment('原文件名'); $table->string('file_suffix', 10)->default('')->comment('文件后缀名'); $table->unsignedBigInteger('file_size')->default(0)->comment('文件大小(单位字节)'); - $table->string('save_dir', 500)->default('')->comment('文件保存地址(相对地址/第三方网络地址)'); + $table->string('save_dir', 300)->default('')->comment('文件保存地址(相对地址/第三方网络地址)'); $table->tinyInteger('is_delete')->default(0)->unsigned()->comment('文件是否已删除[0:否;1:已删除]'); $table->dateTime('created_at')->nullable(true)->comment('创建时间'); @@ -30,7 +30,7 @@ class CreateTalkRecordsFileTable extends Migration $table->collation = 'utf8_general_ci'; $table->engine = 'InnoDB'; - $table->unique(['record_id'], 'idx_record_id'); + $table->unique(['record_id'], 'uk_record_id'); }); $prefix = config('databases.default.prefix'); diff --git a/migrations/2020_11_04_153516_create_talk_records_invite_table.php b/migrations/2020_11_04_153516_create_talk_records_invite_table.php index 6d06ecd..27e4f5c 100644 --- a/migrations/2020_11_04_153516_create_talk_records_invite_table.php +++ b/migrations/2020_11_04_153516_create_talk_records_invite_table.php @@ -17,13 +17,13 @@ class CreateTalkRecordsInviteTable extends Migration $table->unsignedInteger('record_id')->default(0)->comment('消息记录ID'); $table->tinyInteger('type')->default(1)->comment('通知类型[1:入群通知;2:自动退群;3:管理员踢群]'); $table->unsignedInteger('operate_user_id')->default(0)->comment('操作人的用户ID(邀请人)'); - $table->string('user_ids', 255)->default('')->comment("用户ID,多个用','分割"); + $table->string('user_ids', 255)->default('')->comment("用户ID,多个用 , 分割"); $table->charset = 'utf8'; $table->collation = 'utf8_general_ci'; $table->engine = 'InnoDB'; - $table->index(['record_id'], 'idx_recordid'); + $table->index(['record_id'], 'idx_record_id'); }); $prefix = config('databases.default.prefix'); diff --git a/migrations/2020_11_04_153541_create_users_emoticon_table.php b/migrations/2020_11_04_153541_create_users_emoticon_table.php index cd268db..bfbef38 100644 --- a/migrations/2020_11_04_153541_create_users_emoticon_table.php +++ b/migrations/2020_11_04_153541_create_users_emoticon_table.php @@ -13,13 +13,13 @@ class CreateUsersEmoticonTable extends Migration { Schema::create('users_emoticon', function (Blueprint $table) { $table->unsignedInteger('id', true)->comment('表情包收藏ID'); - $table->unsignedInteger('user_id')->default(0)->unique()->comment('用户ID'); + $table->unsignedInteger('user_id')->default(0)->comment('用户ID'); $table->string('emoticon_ids', 255)->default('')->comment('表情包ID'); $table->charset = 'utf8'; $table->collation = 'utf8_general_ci'; - $table->index(['user_id'], 'idx_user_id'); + $table->unique(['user_id'], 'uk_user_id'); }); $prefix = config('databases.default.prefix'); diff --git a/migrations/2021_08_01_183045_create_talk_records_vote_table.php b/migrations/2021_08_01_183045_create_talk_records_vote_table.php new file mode 100644 index 0000000..3b990f8 --- /dev/null +++ b/migrations/2021_08_01_183045_create_talk_records_vote_table.php @@ -0,0 +1,46 @@ +unsignedInteger('id', true)->comment('投票ID'); + $table->unsignedInteger('record_id')->default(0)->comment('消息记录ID'); + $table->unsignedInteger('user_id')->default(0)->comment('用户ID'); + $table->string('title', 50)->default('')->comment('投票标题'); + $table->unsignedInteger('answer_mode')->default(0)->comment('答题模式[0:单选;1:多选;]'); + $table->json('answer_option')->default(null)->comment('答题选项'); + $table->unsignedSmallInteger('answer_num')->default(0)->comment('应答人数'); + $table->unsignedSmallInteger('answered_num')->default(0)->comment('已答人数'); + $table->unsignedTinyInteger('status')->default(0)->comment('投票状态[0:投票中;1:已完成;]'); + $table->dateTime('created_at')->nullable(true)->comment('创建时间'); + $table->dateTime('updated_at')->nullable(true)->comment('更新时间'); + + $table->charset = 'utf8'; + $table->collation = 'utf8_general_ci'; + $table->engine = 'InnoDB'; + + $table->unique(['record_id'], 'uk_record_id'); + }); + + $prefix = config('databases.default.prefix'); + Db::statement("ALTER TABLE `{$prefix}talk_records_vote` comment '聊天对话记录(投票消息表)'"); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('talk_records_vote'); + } +} diff --git a/migrations/2021_08_01_183108_create_talk_records_vote_answer_table.php b/migrations/2021_08_01_183108_create_talk_records_vote_answer_table.php new file mode 100644 index 0000000..0c0c0cf --- /dev/null +++ b/migrations/2021_08_01_183108_create_talk_records_vote_answer_table.php @@ -0,0 +1,40 @@ +unsignedInteger('id', true)->comment('答题ID'); + $table->unsignedInteger('vote_id')->default(0)->comment('投票ID'); + $table->unsignedInteger('user_id')->default(0)->comment('用户ID'); + $table->char('option', 1)->default('')->comment('投票选项[A、B、C 、D、E、F]'); + $table->dateTime('created_at')->comment('答题时间'); + + $table->charset = 'utf8'; + $table->collation = 'utf8_general_ci'; + $table->engine = 'InnoDB'; + + $table->index(['vote_id', 'user_id'], 'idx_vote_id_user_id'); + }); + + $prefix = config('databases.default.prefix'); + Db::statement("ALTER TABLE `{$prefix}talk_records_vote_answer` comment '聊天对话记录(投票消息统计表)'"); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('talk_records_vote_answer'); + } +} diff --git a/migrations/2021_08_01_183126_create_talk_records_login_table.php b/migrations/2021_08_01_183126_create_talk_records_login_table.php new file mode 100644 index 0000000..79a9845 --- /dev/null +++ b/migrations/2021_08_01_183126_create_talk_records_login_table.php @@ -0,0 +1,44 @@ +unsignedInteger('id', true)->comment('登录ID'); + $table->unsignedInteger('record_id')->default(0)->comment('消息记录ID'); + $table->unsignedInteger('user_id')->default(0)->comment('用户ID'); + $table->string('platform', 20)->default('')->comment('登录平台[h5,ios,windows,mac,web]'); + $table->string('ip', 20)->default('')->comment('IP地址'); + $table->string('agent', 300)->default('')->comment('设备信息'); + $table->string('address', 100)->default('')->comment('IP所在地'); + $table->string('reason', 100)->default('')->comment('登录异常提示'); + $table->dateTime('created_at')->nullable(true)->comment('创建时间'); + + $table->charset = 'utf8'; + $table->collation = 'utf8_general_ci'; + $table->engine = 'InnoDB'; + + $table->unique(['record_id'], 'uk_record_id'); + }); + + $prefix = config('databases.default.prefix'); + Db::statement("ALTER TABLE `{$prefix}talk_records_login` comment '聊天对话记录(登录日志)'"); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('talk_records_login'); + } +} diff --git a/seeders/initialize.php b/seeders/initialize.php new file mode 100644 index 0000000..df056a5 --- /dev/null +++ b/seeders/initialize.php @@ -0,0 +1,74 @@ + 0) { + echo "数据库已存在数据,不能执行初始化数据脚本...\n"; + return; + } + + $users = []; + for ($i = 0; $i < 9; $i++) { + $users[] = [ + 'mobile' => '1879827205' . $i, + 'password' => Hash::make('admin123'), + 'nickname' => 'test' . $i, + 'created_at' => date('Y-m-d H:i:s'), + 'updated_at' => date('Y-m-d H:i:s') + ]; + } + + User::insert($users); + + $defaultArticleClass = []; + $usersFriends = []; + foreach (User::all() as $user) { + $defaultArticleClass[] = [ + 'user_id' => $user->id, + 'class_name' => '我的笔记', + 'sort' => 1, + 'is_default' => 1, + 'created_at' => time(), + ]; + } + + ArticleClass::insert($defaultArticleClass); + + $list = Db::select('SELECT u1.id as user_id,u2.id as friend_id FROM im_users as u1,im_users as u2 where u1.id != u2.id'); + + $friends = []; + + foreach ($list as $item) { + $friends[] = [ + 'user_id' => $item->user_id, + 'friend_id' => $item->friend_id, + 'status' => 1, + 'created_at' => date('Y-m-d H:i:s'), + 'updated_at' => date('Y-m-d H:i:s') + ]; + } + + UsersFriend::insert($friends); + + $service = new \App\Service\TalkListService(); + foreach ($list as $item) { + $service->create($item->user_id, $item->friend_id, \App\Constants\TalkModeConstant::PRIVATE_CHAT); + } + } +}