Setting.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model\h5;
  13. use cores\BaseModel;
  14. use think\facade\Cache;
  15. use app\common\library\helper;
  16. /**
  17. * H5设置模型
  18. * Class Setting
  19. * @package app\common\model\h5
  20. */
  21. class Setting extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'h5_setting';
  25. protected $createTime = false;
  26. /**
  27. * 获取器: 转义数组格式
  28. * @param $value
  29. * @return array
  30. */
  31. public function getValuesAttr($value): array
  32. {
  33. return helper::jsonDecode($value);
  34. }
  35. /**
  36. * 修改器: 转义成json格式
  37. * @param $value
  38. * @return string
  39. */
  40. public function setValuesAttr($value): string
  41. {
  42. return helper::jsonEncode($value);
  43. }
  44. /**
  45. * 获取指定项设置
  46. * @param string $key
  47. * @param int|null $storeId
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public static function getItem(string $key, ?int $storeId = null): array
  54. {
  55. $data = static::getAll($storeId);
  56. return isset($data[$key]) ? $data[$key]['values'] : [];
  57. }
  58. /**
  59. * 获取H5访问url
  60. * @param int|null $storeId
  61. * @return mixed
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function getH5Url(?int $storeId = null)
  67. {
  68. return static::getItem('basic', $storeId)['baseUrl'];
  69. }
  70. /**
  71. * 获取全部设置
  72. * @param int|null $storeId
  73. * @return array
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public static function getAll(int $storeId = null): array
  79. {
  80. $model = new static;
  81. is_null($storeId) && $storeId = static::$storeId;
  82. if (!$data = Cache::get("h5_setting_{$storeId}")) {
  83. // 获取全部设置
  84. $setting = $model->getList($storeId);
  85. $data = $setting->isEmpty() ? [] : helper::arrayColumn2Key($setting->toArray(), 'key');
  86. // 写入缓存中
  87. Cache::tag('cache')->set("h5_setting_{$storeId}", $data);
  88. }
  89. // 合并默认设置
  90. return array_merge_multiple($model->defaultData(), $data);
  91. }
  92. /**
  93. * 获取商城设置列表
  94. * @param int $storeId
  95. * @return \think\Collection
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. private function getList(int $storeId): \think\Collection
  101. {
  102. return $this->where('store_id', '=', $storeId)->select();
  103. }
  104. /**
  105. * 获取设置项信息
  106. * @param string $key
  107. * @return array|\think\Model|null
  108. */
  109. public static function detail(string $key)
  110. {
  111. return static::get(compact('key'));
  112. }
  113. /**
  114. * 默认配置
  115. * @return array
  116. */
  117. public function defaultData(): array
  118. {
  119. return [
  120. 'basic' => [
  121. 'key' => 'basic',
  122. 'describe' => '基础设置',
  123. 'values' => [
  124. // 是否启用h5端访问 0=>关闭 1=>开启
  125. 'enabled' => 1,
  126. // h5站点url [默认是当前访问的域名]
  127. 'baseUrl' => base_url(),
  128. ]
  129. ]
  130. ];
  131. }
  132. }