0, 'popularity_sign_end_at' => 0, 'algorithm' => null ); /** * 获取 * @param int $uid * @param string $key * @return mixed * @throws AlertException */ public static function get(int $uid, string $key) { if (!in_array($key, self::$keys)) { throw new AlertException("错误的配置"); } try { $model = self::where([['uid', $uid], ['key', $key]])->firstOrFail(); return $model->value; } catch (ModelNotFoundException $exception) { return self::$keys[$key]; } } /** * 设置 * @param int $uid * @param string $key * @param string $value * @return bool * @throws AlertException */ public static function set(int $uid, string $key, $value) { if (!in_array($key, self::$keys)) { throw new AlertException("错误的配置"); } self::updateOrCreate(['uid' => $uid, 'key' => $key], [ 'uid' => $uid, 'key' => $key, 'value' => (string)$value ]); return true; } /** * 增加 * @param int $uid * @param string $key * @param string $value * @param bool $now 是否以现在作为基准 * @return bool * @throws AlertException */ public static function inc(int $uid, string $key, $value, bool $now = false) { if (!in_array($key, self::$keys)) { throw new AlertException("错误的配置"); } $state = self::where([['uid', $uid], ['key', $key]])->first(); if (collect($state)->isEmpty()) { if ($now) { $v = time() + $value; } else { $v = self::$keys[$key] + $value; } self::create(['uid' => $uid, 'key' => $key, 'value' => $v]); } else { $v = intval($state->value) + intval($value); if ($now && intval($state->value) < time()) { $v = time() + $value; } $state->value = $v; $state->save(); } return true; } }