// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\model\sharp; use cores\BaseModel; use think\facade\Cache; use app\common\library\helper; /** * 整点秒杀设置模型 * Class Setting * @package app\common\model\sharp */ class Setting extends BaseModel { // 定义表名 protected $name = 'sharp_setting'; protected $createTime = false; /** * 获取器: 转义数组格式 * @param $value * @return mixed */ public function getValuesAttr($value) { return helper::jsonDecode($value); } /** * 修改器: 转义成json格式 * @param $value * @return string */ public function setValuesAttr($value): string { return helper::jsonEncode($value); } /** * 获取是否开启分销 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getIsDealer() { return static::getItem('basic')['isDealer']; } /** * 获取指定项设置 * @param string $key * @param int|null $storeId * @return array|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getItem(string $key, int $storeId = null) { $data = static::getAll($storeId); return isset($data[$key]) ? $data[$key]['values'] : []; } /** * 获取全部设置 * @param int|null $storeId * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getAll(int $storeId = null): array { $model = new static; is_null($storeId) && $storeId = $model::$storeId; if (!$data = Cache::get("sharp_setting_{$storeId}")) { // 获取全部设置 $setting = $model->getList($storeId); $data = $setting->isEmpty() ? [] : helper::arrayColumn2Key($setting->toArray(), 'key'); // 写入缓存中 Cache::tag('cache')->set("sharp_setting_{$storeId}", $data); } // 重组setting缓存数据 (多维) return static::reorganize($model->defaultData(), $data, $type = 'cache'); } /** * 获取商城设置列表 * @param int $storeId * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function getList(int $storeId): \think\Collection { return $this->where('store_id', '=', $storeId)->select(); } /** * 获取设置项信息 * @param string $key * @return static|array|null */ public static function detail(string $key) { return static::get(compact('key')); } /** * 默认配置 * @return array */ public function defaultData(): array { return [ 'basic' => [ 'key' => 'basic', 'describe' => '基础设置', 'values' => [ // 是否开启分销 'isDealer' => false, 'order' => [ // 秒杀订单未支付n分钟后自动关闭 'orderClose' => 10, ] ] ] ]; } }