getAttr('uid'); } catch (\Throwable $e) { $uid = null; } if ($uid === null || $uid === '') { $uid = self::generateUid(); $model->setAttr('uid', $uid); } try { $name = $model->getAttr('name'); } catch (\Throwable $e) { $name = null; } if ($name === null || $name === '') { $model->setAttr('name', $uid); } // 彩金池权重默认取 type=0 的奖池配置 self::setDefaultWeightsFromLotteryConfig($model); } /** * 从 DiceLotteryConfig type=0 取 t1_weight~t5_weight 作为玩家未设置时的默认值 */ protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void { $config = DiceLotteryConfig::where('type', 0)->find(); if (!$config) { return; } $fields = ['t1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight']; foreach ($fields as $field) { try { $val = $model->getAttr($field); } catch (\Throwable $e) { $val = null; } if ($val === null || $val === '') { try { $model->setAttr($field, $config->getAttr($field)); } catch (\Throwable $e) { // 忽略字段不存在 } } } } /** * 生成唯一标识 uid(12 位十六进制) */ public static function generateUid(): string { return strtoupper(substr(bin2hex(random_bytes(6)), 0, 12)); } /** * 用户名 搜索 */ public function searchUsernameAttr($query, $value) { $query->where('username', 'like', '%'.$value.'%'); } /** * 昵称 搜索 */ public function searchNameAttr($query, $value) { $query->where('name', 'like', '%'.$value.'%'); } /** * 手机号 模糊搜索 */ public function searchPhoneAttr($query, $value) { if ($value !== '' && $value !== null) { $query->where('phone', 'like', '%' . $value . '%'); } } /** * 状态 搜索 */ public function searchStatusAttr($query, $value) { if ($value !== '' && $value !== null) { $query->where('status', '=', $value); } } /** * 平台币 搜索 */ public function searchCoinAttr($query, $value) { if ($value !== '' && $value !== null) { $query->where('coin', '=', $value); } } /** * 彩金池配置ID 搜索 */ public function searchLottery_config_idAttr($query, $value) { if ($value !== '' && $value !== null) { $query->where('lottery_config_id', '=', $value); } } /** * 关联彩金池配置 */ public function diceLotteryConfig() { return $this->belongsTo(DiceLotteryConfig::class, 'lottery_config_id', 'id'); } }