Setting.php 2.5 KB

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