优化代码

main
gzydong 2021-08-01 19:57:41 +08:00
parent 8d8c4d90e9
commit 1fe2bd0e9c
11 changed files with 258 additions and 11 deletions

View File

@ -37,7 +37,7 @@ Lumen-IM 是一个网页版在线即时聊天项目,前端使用 Element-ui +
1. 下载源码包 1. 下载源码包
2. 安装框架依赖包执行 `compsoer install` 命令 [项目根目录下执行] 2. 安装框架依赖包执行 `compsoer install` 命令 [项目根目录下执行]
2. 拷贝项目根目录下 .env.example 文件为 .env 并正确配置相关参数mysql、redis 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` 4. 启动运行项目 `php bin/hyperf.php start`
[项目运行之前请确保 MySQL、Redis 及 Nginx 服务] [项目运行之前请确保 MySQL、Redis 及 Nginx 服务]

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
/**
* @Command
*/
class InstallCommand extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->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');
}
}
}

View File

@ -14,7 +14,7 @@ class CreateUsersTable extends Migration
{ {
Schema::create('users', function (Blueprint $table) { Schema::create('users', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('用户ID'); $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('nickname', 20)->default('')->comment('用户昵称');
$table->string('avatar', 255)->default('')->comment('用户头像地址'); $table->string('avatar', 255)->default('')->comment('用户头像地址');
$table->unsignedTinyInteger('gender')->default(0)->comment('用户性别[0:未知;1:男;2:女;]'); $table->unsignedTinyInteger('gender')->default(0)->comment('用户性别[0:未知;1:男;2:女;]');
@ -29,7 +29,7 @@ class CreateUsersTable extends Migration
$table->collation = 'utf8_general_ci'; $table->collation = 'utf8_general_ci';
$table->engine = 'InnoDB'; $table->engine = 'InnoDB';
$table->unique(['mobile'], 'idx_mobile'); $table->unique(['mobile'], 'uk_mobile');
}); });
$prefix = config('databases.default.prefix'); $prefix = config('databases.default.prefix');

View File

@ -13,7 +13,7 @@ class CreateTalkRecordsTable extends Migration
public function up(): void public function up(): void
{ {
Schema::create('talk_records', function (Blueprint $table) { 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('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->unsignedTinyInteger('msg_type')->unsigned()->default(1)->comment('消息类型[1:文本消息;2:文件消息;3:会话消息;4:代码消息;5:投票消息;6:群公告;7:好友申请;8:登录通知;9:入群消息/退群消息;]');
$table->unsignedInteger('user_id')->default(0)->comment('发送者ID0:代表系统消息 >0: 用户ID'); $table->unsignedInteger('user_id')->default(0)->comment('发送者ID0:代表系统消息 >0: 用户ID');
@ -21,7 +21,7 @@ class CreateTalkRecordsTable extends Migration
$table->tinyInteger('is_revoke')->default(0)->comment('是否撤回消息[0:否;1:是]'); $table->tinyInteger('is_revoke')->default(0)->comment('是否撤回消息[0:否;1:是]');
$table->tinyInteger('is_mark')->default(0)->comment('是否重要消息[0:否;1:是;]'); $table->tinyInteger('is_mark')->default(0)->comment('是否重要消息[0:否;1:是;]');
$table->tinyInteger('is_read')->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->text('content')->nullable(true)->charset('utf8mb4')->comment('文本消息 {@nickname@}');
$table->string('warn_users', 200)->default('')->comment('@好友 、 多个用英文逗号 “,” 拼接 (0:代表所有人)'); $table->string('warn_users', 200)->default('')->comment('@好友 、 多个用英文逗号 “,” 拼接 (0:代表所有人)');
$table->dateTime('created_at')->nullable(true)->comment('创建时间'); $table->dateTime('created_at')->nullable(true)->comment('创建时间');

View File

@ -22,7 +22,7 @@ class CreateTalkRecordsFileTable extends Migration
$table->string('original_name', 100)->default('')->comment('原文件名'); $table->string('original_name', 100)->default('')->comment('原文件名');
$table->string('file_suffix', 10)->default('')->comment('文件后缀名'); $table->string('file_suffix', 10)->default('')->comment('文件后缀名');
$table->unsignedBigInteger('file_size')->default(0)->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->tinyInteger('is_delete')->default(0)->unsigned()->comment('文件是否已删除[0:否;1:已删除]');
$table->dateTime('created_at')->nullable(true)->comment('创建时间'); $table->dateTime('created_at')->nullable(true)->comment('创建时间');
@ -30,7 +30,7 @@ class CreateTalkRecordsFileTable extends Migration
$table->collation = 'utf8_general_ci'; $table->collation = 'utf8_general_ci';
$table->engine = 'InnoDB'; $table->engine = 'InnoDB';
$table->unique(['record_id'], 'idx_record_id'); $table->unique(['record_id'], 'uk_record_id');
}); });
$prefix = config('databases.default.prefix'); $prefix = config('databases.default.prefix');

View File

@ -17,13 +17,13 @@ class CreateTalkRecordsInviteTable extends Migration
$table->unsignedInteger('record_id')->default(0)->comment('消息记录ID'); $table->unsignedInteger('record_id')->default(0)->comment('消息记录ID');
$table->tinyInteger('type')->default(1)->comment('通知类型[1:入群通知;2:自动退群;3:管理员踢群]'); $table->tinyInteger('type')->default(1)->comment('通知类型[1:入群通知;2:自动退群;3:管理员踢群]');
$table->unsignedInteger('operate_user_id')->default(0)->comment('操作人的用户ID(邀请人)'); $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->charset = 'utf8';
$table->collation = 'utf8_general_ci'; $table->collation = 'utf8_general_ci';
$table->engine = 'InnoDB'; $table->engine = 'InnoDB';
$table->index(['record_id'], 'idx_recordid'); $table->index(['record_id'], 'idx_record_id');
}); });
$prefix = config('databases.default.prefix'); $prefix = config('databases.default.prefix');

View File

@ -13,13 +13,13 @@ class CreateUsersEmoticonTable extends Migration
{ {
Schema::create('users_emoticon', function (Blueprint $table) { Schema::create('users_emoticon', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('表情包收藏ID'); $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->string('emoticon_ids', 255)->default('')->comment('表情包ID');
$table->charset = 'utf8'; $table->charset = 'utf8';
$table->collation = 'utf8_general_ci'; $table->collation = 'utf8_general_ci';
$table->index(['user_id'], 'idx_user_id'); $table->unique(['user_id'], 'uk_user_id');
}); });
$prefix = config('databases.default.prefix'); $prefix = config('databases.default.prefix');

View File

@ -0,0 +1,46 @@
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateTalkRecordsVoteTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('talk_records_vote', function (Blueprint $table) {
$table->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');
}
}

View File

@ -0,0 +1,40 @@
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateTalkRecordsVoteAnswerTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('talk_records_vote_answer', function (Blueprint $table) {
$table->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');
}
}

View File

@ -0,0 +1,44 @@
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateTalkRecordsLoginTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('talk_records_login', function (Blueprint $table) {
$table->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');
}
}

74
seeders/initialize.php Normal file
View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
use App\Helper\Hash;
use Hyperf\Database\Seeders\Seeder;
use App\Model\User;
use App\Model\Article\ArticleClass;
use Hyperf\DbConnection\Db;
use App\Model\UsersFriend;
class Initialize extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
if (User::count() > 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);
}
}
}