Setting.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\common\model\wow;
  3. use think\Cache;
  4. use app\common\model\BaseModel;
  5. /**
  6. * 好物圈设置模型
  7. * Class Setting
  8. * @package app\common\model\wow
  9. */
  10. class Setting extends BaseModel
  11. {
  12. protected $name = 'wow_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 = "wow_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_shopping' => '0',
  82. // 是否同步订单
  83. 'is_order' => '0',
  84. ]
  85. ]
  86. ];
  87. }
  88. }