// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\model\bargain; use think\facade\Cache; use app\common\library\helper; use cores\BaseModel; /** * 砍价活动设置模型 * Class Setting * @package app\common\model\bargain */ class Setting extends BaseModel { // 定义表名 protected $name = 'bargain_setting'; protected $createTime = false; /** * 获取器: 转义数组格式 * @param $value * @return array */ public function getValuesAttr($value): array { 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']; } /** * 获取每人每日帮砍次数限制 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getHelpCutLimitNum() { return static::getItem('basic')['helpCutLimitNum']; } /** * 获取指定项设置 * @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("bargain_setting_{$storeId}")) { // 获取全部设置 $setting = $model->getList($storeId); $data = $setting->isEmpty() ? [] : helper::arrayColumn2Key($setting->toArray(), 'key'); // 写入缓存中 Cache::tag('cache')->set("bargain_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, // 砍价规则 'rulesDesc' => "活动期间,用户可以在砍价活动页选择活动商品发起砍价,可通过微信分享砍价商品活动页面给好友,并通过好友助力砍价,将商品砍至一定金额。\n\n" . "每次砍价金额随机,可砍出最高商品日常价内的随机金额,参与好友越多越容易成功。\n\n" . "以最终砍价后的优惠价格购买该商品,且用户须在活动时间结束之前进行支付购买,否则砍价商品价格将过期失效。\n\n" . "商品库存有限,以前台展示的库存数量为准,先到先得。", // 每人每日帮砍次数限制 (0为不限制) 'helpCutLimitNum' => 999 ] ] ]; } }