From adf9225180a52e9fad00935e0282b01dbb870cf5 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 23 Jun 2022 19:36:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E6=8B=BC=E9=9F=B3=E6=90=9C=E7=B4=A2=E8=81=94=E7=B3=BB=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/UsersController.php | 9 ++-- app/Models/User.php | 20 +++------ app/Module/Base.php | 17 +++++++ ...2022_06_23_191554_add_web_users_pinyin.php | 45 +++++++++++++++++++ .../assets/js/pages/manage/messenger.vue | 2 +- 5 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 database/migrations/2022_06_23_191554_add_web_users_pinyin.php diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 270d87a37..84f30ebdf 100755 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -271,8 +271,10 @@ class UsersController extends AbstractController return Base::retError('昵称不可以少于2个字'); } elseif (mb_strlen($nickname) > 20) { return Base::retError('昵称最多只能设置20个字'); - } else { + } elseif ($nickname != $user->nickname) { $user->nickname = $nickname; + $user->az = Base::getFirstCharter($nickname); + $user->pinyin = Base::cn2pinyin($nickname); } } // 职位/职称 @@ -289,7 +291,6 @@ class UsersController extends AbstractController // $user->save(); User::token($user); - User::AZUpdate($user->userid); // if (empty($user->userimg)) { $user->userimg = $user->getUserimgAttribute(null); @@ -367,7 +368,7 @@ class UsersController extends AbstractController */ public function search() { - $builder = User::select(['userid', 'email', 'nickname', 'profession', 'userimg', 'az', 'line_at', 'disable_at']); + $builder = User::select(['userid', 'email', 'nickname', 'profession', 'userimg', 'az', 'pinyin', 'line_at', 'disable_at']); // $keys = Request::input('keys'); $sorts = Request::input('sorts'); @@ -640,6 +641,8 @@ class UsersController extends AbstractController return Base::retError('昵称最多只能设置20个字'); } else { $upArray['nickname'] = $nickname; + $upArray['az'] = Base::getFirstCharter($nickname); + $upArray['pinyin'] = Base::cn2pinyin($nickname); } } // 职位/职称 diff --git a/app/Models/User.php b/app/Models/User.php index 90b926c44..cc80e0edd 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -14,6 +14,7 @@ use Carbon\Carbon; * @property int $userid * @property array $identity 身份 * @property string|null $az A-Z + * @property string|null $pinyin 拼音(主要用于搜索) * @property string|null $email 邮箱 * @property string $nickname 昵称 * @property string|null $profession 职位/职称 @@ -52,6 +53,7 @@ use Carbon\Carbon; * @method static \Illuminate\Database\Eloquent\Builder|User whereLoginNum($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereNickname($value) * @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value) + * @method static \Illuminate\Database\Eloquent\Builder|User wherePinyin($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereProfession($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereTaskDialogId($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value) @@ -226,8 +228,9 @@ class User extends AbstractModel $inArray = array_merge($inArray, $other); } $user = User::createInstance($inArray); + $user->az = Base::getFirstCharter($user->nickname); + $user->pinyin = Base::cn2pinyin($user->nickname); $user->save(); - User::AZUpdate($user->userid); return $user->find($user->userid); } @@ -427,7 +430,7 @@ class User extends AbstractModel if (isset($_A["__static_userid2basic_" . $userid])) { return $_A["__static_userid2basic_" . $userid]; } - $fields = ['userid', 'email', 'nickname', 'profession', 'userimg', 'az', 'line_at', 'disable_at']; + $fields = ['userid', 'email', 'nickname', 'profession', 'userimg', 'az', 'pinyin', 'line_at', 'disable_at']; $userInfo = self::whereUserid($userid)->select($fields)->first(); if ($userInfo) { $userInfo->online = $userInfo->getOnlineStatus(); @@ -447,19 +450,6 @@ class User extends AbstractModel return $basic ? $basic->nickname : ''; } - /** - * 更新首字母 - * @param $userid - */ - public static function AZUpdate($userid) - { - $row = self::whereUserid($userid)->first(); - if ($row) { - $row->az = Base::getFirstCharter($row->nickname); - $row->save(); - } - } - /** * 是否需要验证码 * @param $email diff --git a/app/Module/Base.php b/app/Module/Base.php index 71630c81d..3dc964b93 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -2849,6 +2849,23 @@ class Base return $first ? strtoupper($first) : '#'; } + /** + * 中文转拼音 + * @param $str + * @return string + */ + public static function cn2pinyin($str) + { + if (empty($str)) { + return ''; + } + if (!preg_match("/^[a-zA-Z0-9_.]+$/", $str)) { + $pinyin = new Pinyin(); + $str = $pinyin->permalink($str, ''); + } + return $str; + } + /** * 缓存数据 * @param $name diff --git a/database/migrations/2022_06_23_191554_add_web_users_pinyin.php b/database/migrations/2022_06_23_191554_add_web_users_pinyin.php new file mode 100644 index 000000000..6f0b608d0 --- /dev/null +++ b/database/migrations/2022_06_23_191554_add_web_users_pinyin.php @@ -0,0 +1,45 @@ +string('pinyin', 255)->nullable()->default('')->after('az')->comment('拼音(主要用于搜索)'); + } + }); + if ($isAdd) { + \App\Models\User::chunkById(100, function ($lists) { + /** @var \App\Models\User $item */ + foreach ($lists as $item) { + $item->pinyin = \App\Module\Base::cn2pinyin($item->nickname); + $item->save(); + } + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn("pinyin"); + }); + } +} diff --git a/resources/assets/js/pages/manage/messenger.vue b/resources/assets/js/pages/manage/messenger.vue index 09764b069..4a3a30d79 100644 --- a/resources/assets/js/pages/manage/messenger.vue +++ b/resources/assets/js/pages/manage/messenger.vue @@ -286,7 +286,7 @@ export default { return []; } if (contactsKey) { - return contactsData.filter(item => $A.strExists(`${item.email}||${item.nickname}`, contactsKey)) + return contactsData.filter(item => $A.strExists(`${item.email}||${item.nickname}||${item.profession}||${item.pinyin}`, contactsKey)) } return contactsData; },