From 52c389edd855387242a9b64a1e16183a534e732a Mon Sep 17 00:00:00 2001 From: kuaifan Date: Mon, 21 Apr 2025 11:35:52 +0800 Subject: [PATCH] no message --- app/Http/Controllers/Api/UsersController.php | 27 +++++++------- app/Models/UmengAlias.php | 4 +++ app/Models/UserDevice.php | 21 +++++------ ...ash_and_user_lang_to_umeng_alias_table.php | 35 +++++++++++++++++++ docker-compose.yml | 4 +-- 5 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 database/migrations/2025_04_21_113033_add_device_hash_and_user_lang_to_umeng_alias_table.php diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 723c2e465..039ab1875 100755 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -1202,30 +1202,27 @@ class UsersController extends AbstractController } // $user = User::auth(); + $version = $data['appVersion'] ? ($data['appVersionName'] . " ({$data['appVersion']})") : ''; + $isNotified = trim($data['isNotified']) === 'true' || $data['isNotified'] === true ? 1 : intval($data['isNotified']); $inArray = [ 'userid' => $user->userid, 'alias' => $data['alias'], 'platform' => Base::platform(), ]; - $version = $data['appVersion'] ? ($data['appVersionName'] . " ({$data['appVersion']})") : ''; - $isNotified = trim($data['isNotified']) === 'true' || $data['isNotified'] === true ? 1 : intval($data['isNotified']); - $row = UmengAlias::where($inArray); - if ($row->exists()) { - $row->update([ - 'ua' => $data['userAgent'], - 'device' => $data['deviceModel'], - 'version' => $version, - 'is_notified' => $isNotified, - 'updated_at' => Carbon::now() - ]); - return Base::retSuccess('别名已存在'); - } - $row = UmengAlias::createInstance(array_merge($inArray, [ + $upArray = [ 'ua' => $data['userAgent'], 'device' => $data['deviceModel'], + 'device_hash' => UserDevice::check(), 'version' => $version, 'is_notified' => $isNotified, - ])); + ]; + $row = UmengAlias::where($inArray); + if ($row->exists()) { + $upArray['updated_at'] = Carbon::now(); + $row->update($upArray); + return Base::retSuccess('别名已存在'); + } + $row = UmengAlias::createInstance(array_merge($inArray, $upArray)); if ($row->save()) { return Base::retSuccess('添加成功'); } else { diff --git a/app/Models/UmengAlias.php b/app/Models/UmengAlias.php index d10073127..1417d1880 100644 --- a/app/Models/UmengAlias.php +++ b/app/Models/UmengAlias.php @@ -15,6 +15,8 @@ use Hedeqiang\UMeng\IOS; * @property string|null $alias 别名 * @property string|null $platform 平台类型 * @property string|null $device 设备类型 + * @property string|null $device_hash 设备哈希值,用于关联UserDevice表 + * @property string|null $user_lang 用户语言 * @property string|null $version 应用版本号 * @property string|null $ua userAgent * @property int|null $is_notified 通知权限 @@ -32,11 +34,13 @@ use Hedeqiang\UMeng\IOS; * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereAlias($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereCreatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereDevice($value) + * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereDeviceHash($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereIsNotified($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias wherePlatform($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereUa($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereUpdatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereUserLang($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereUserid($value) * @method static \Illuminate\Database\Eloquent\Builder|UmengAlias whereVersion($value) * @mixin \Eloquent diff --git a/app/Models/UserDevice.php b/app/Models/UserDevice.php index f802b1fae..1bccfe119 100644 --- a/app/Models/UserDevice.php +++ b/app/Models/UserDevice.php @@ -21,6 +21,7 @@ use Request; * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $deleted_at + * @property-read int $is_current * @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend() * @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden() * @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array) @@ -173,36 +174,35 @@ class UserDevice extends AbstractModel /** * 检查用户是否存在 - * @return bool + * @return string|null */ - public static function check(): bool + public static function check(): ?string { $token = Doo::userToken(); $userid = Doo::userId(); $hash = md5($token); if (Cache::has(self::ck($hash))) { - return true; + return $hash; } $row = self::whereHash($hash)->first(); if ($row) { // 判断是否过期 if (Carbon::parse($row->expired_at)->isPast()) { - Cache::forget(self::ck($hash)); - $row->delete(); - return false; + self::forget($row); + return null; } // 更新缓存 self::record(); - return true; + return $hash; } // 没有记录,尝试创建一个(防止升级后所有登录都失效,保证留一个可以保持登录) // todo 后期删除 - return AbstractModel::transaction(function () use ($userid) { + return AbstractModel::transaction(function () use ($hash, $userid) { if (self::whereUserid($userid)->withoutTrashed()->lockForUpdate()->exists()) { - return false; + return null; } - return (bool)self::record(); + return self::record() ? $hash : null; }); } @@ -285,6 +285,7 @@ class UserDevice extends AbstractModel } if (isset($hash)) { Cache::forget(self::ck($hash)); + UmengAlias::whereDeviceHash($hash)->delete(); } } } diff --git a/database/migrations/2025_04_21_113033_add_device_hash_and_user_lang_to_umeng_alias_table.php b/database/migrations/2025_04_21_113033_add_device_hash_and_user_lang_to_umeng_alias_table.php new file mode 100644 index 000000000..e09c8d260 --- /dev/null +++ b/database/migrations/2025_04_21_113033_add_device_hash_and_user_lang_to_umeng_alias_table.php @@ -0,0 +1,35 @@ +string('device_hash')->index()->nullable()->after('device')->comment('设备哈希值,用于关联UserDevice表'); + $table->string('user_lang', 10)->nullable()->after('device_hash')->comment('用户语言'); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('umeng_alias', function (Blueprint $table) { + $table->dropColumn(['device_hash', 'user_lang']); + }); + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 14a22c13f..ed21f1f15 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -231,8 +231,8 @@ services: deploy: resources: limits: - cpus: '1' - memory: 1G + cpus: '2' + memory: 4G networks: extnetwork: ipv4_address: "${APP_IPPR}.15"