Setting.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\common\model\sharp;
  3. use think\Cache;
  4. use app\common\model\BaseModel;
  5. /**
  6. * 整点秒杀设置模型
  7. * Class Setting
  8. * @package app\common\model\sharp
  9. */
  10. class Setting extends BaseModel
  11. {
  12. protected $name = 'sharp_setting';
  13. protected $createTime = false;
  14. /**
  15. * 获取器: 转义数组格式
  16. * @param $value
  17. * @return mixed
  18. */
  19. public function getValuesAttr($value)
  20. {
  21. return json_decode($value, true);
  22. }
  23. /**
  24. * 修改器: 转义成json格式
  25. * @param $value
  26. * @return string
  27. */
  28. public function setValuesAttr($value)
  29. {
  30. return json_encode($value);
  31. }
  32. /**
  33. * 获取指定项设置
  34. * @param $key
  35. * @param $wxapp_id
  36. * @return array
  37. */
  38. public static function getItem($key, $wxapp_id = null)
  39. {
  40. $data = static::getAll($wxapp_id);
  41. return isset($data[$key]) ? $data[$key]['values'] : [];
  42. }
  43. /**
  44. * 获取全部设置
  45. * @param null $wxapp_id
  46. * @return array|mixed
  47. */
  48. public static function getAll($wxapp_id = null)
  49. {
  50. $self = new static;
  51. is_null($wxapp_id) && $wxapp_id = $self::$wxapp_id;
  52. $cacheKey = "sharp_setting_{$wxapp_id}";
  53. if (!$data = Cache::get($cacheKey)) {
  54. $data = array_column(collection($self::all())->toArray(), null, 'key');
  55. Cache::tag('cache')->set($cacheKey, $data);
  56. }
  57. return array_merge_multiple($self->defaultData(), $data);
  58. }
  59. /**
  60. * 获取设置项信息
  61. * @param $key
  62. * @return null|static
  63. * @throws \think\exception\DbException
  64. */
  65. public static function detail($key)
  66. {
  67. return static::get(compact('key'));
  68. }
  69. /**
  70. * 默认配置
  71. * @return array
  72. */
  73. public function defaultData()
  74. {
  75. return [
  76. 'basic' => [
  77. 'key' => 'basic',
  78. 'describe' => '基础设置',
  79. 'values' => [
  80. // 是否开启分销
  81. 'is_dealer' => '0',
  82. 'order' => [
  83. // 秒杀订单未支付n分钟后自动关闭
  84. 'order_close' => '10',
  85. ]
  86. ]
  87. ]
  88. ];
  89. }
  90. }