Setting.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\wxapp;
  13. use cores\BaseModel;
  14. use think\facade\Cache;
  15. use app\common\library\helper;
  16. /**
  17. * 微信小程序设置模型
  18. * Class Setting
  19. * @package app\common\model\wxapp
  20. */
  21. class Setting extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'wxapp_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. * 获取微信小程序基础配置
  60. * @param int|null $storeId
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function getConfigBasic(?int $storeId = null): array
  67. {
  68. return static::getItem('basic', $storeId);
  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("wxapp_setting_{$storeId}")) {
  83. // 获取全部设置
  84. $data = $model->getList($storeId);
  85. // 写入缓存中
  86. Cache::tag('cache')->set("wxapp_setting_{$storeId}", $data);
  87. }
  88. return \resetOptions($model->defaultData(), $data);
  89. }
  90. /**
  91. * 获取设置项列表
  92. * @param int $storeId
  93. * @return array
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. private function getList(int $storeId): array
  99. {
  100. // 获取所有设置项
  101. $data = $this->where('store_id', '=', $storeId)->select();
  102. return $data->isEmpty() ? [] : helper::arrayColumn2Key($data->toArray(), 'key');
  103. }
  104. /**
  105. * 获取设置项信息
  106. * @param string $key
  107. * @return static|array|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. // 是否启用微信小程序端访问
  125. 'enabled' => true,
  126. // 小程序AppID
  127. 'app_id' => '',
  128. // 小程序AppSecret
  129. 'app_secret' => ''
  130. ]
  131. ]
  132. ];
  133. }
  134. }