Setting.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 = $model::$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. return \resetOptions($model->defaultData(), $data);
  90. }
  91. /**
  92. * 获取设置项列表
  93. * @param int $storeId
  94. * @return \think\Collection
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. private function getList(int $storeId): \think\Collection
  100. {
  101. return $this->where('store_id', '=', $storeId)->select();
  102. }
  103. /**
  104. * 获取设置项信息
  105. * @param string $key
  106. * @return static|array|null
  107. */
  108. public static function detail(string $key)
  109. {
  110. return static::get(compact('key'));
  111. }
  112. /**
  113. * 默认配置
  114. * @return array
  115. */
  116. public function defaultData(): array
  117. {
  118. return [
  119. 'basic' => [
  120. 'key' => 'basic',
  121. 'describe' => '基础设置',
  122. 'values' => [
  123. // 是否启用H5端访问
  124. 'enabled' => true,
  125. // h5站点url [默认是当前访问的域名]
  126. 'baseUrl' => base_url(),
  127. ]
  128. ]
  129. ];
  130. }
  131. }