Setting.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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 bool
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public static function isEnableShipping(?int $storeId = null): bool
  79. {
  80. return static::getConfigBasic($storeId)['enableShipping'];
  81. }
  82. /**
  83. * 获取全部设置
  84. * @param int|null $storeId
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public static function getAll(?int $storeId = null): array
  91. {
  92. $model = new static;
  93. is_null($storeId) && $storeId = static::$storeId;
  94. if (!$data = Cache::get("wxapp_setting_{$storeId}")) {
  95. // 获取全部设置
  96. $data = $model->getList($storeId);
  97. // 写入缓存中
  98. Cache::tag('cache')->set("wxapp_setting_{$storeId}", $data);
  99. }
  100. return \resetOptions($model->defaultData(), $data);
  101. }
  102. /**
  103. * 获取设置项列表
  104. * @param int $storeId
  105. * @return array
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. private function getList(int $storeId): array
  111. {
  112. // 获取所有设置项
  113. $data = $this->where('store_id', '=', $storeId)->select();
  114. return $data->isEmpty() ? [] : helper::arrayColumn2Key($data->toArray(), 'key');
  115. }
  116. /**
  117. * 获取设置项信息
  118. * @param string $key
  119. * @return static|array|null
  120. */
  121. public static function detail(string $key)
  122. {
  123. return static::get(compact('key'));
  124. }
  125. /**
  126. * 默认配置
  127. * @return array
  128. */
  129. public function defaultData(): array
  130. {
  131. return [
  132. 'basic' => [
  133. 'key' => 'basic',
  134. 'describe' => '基础设置',
  135. 'values' => [
  136. // 是否开启微信小程序端访问
  137. 'enabled' => true,
  138. // 小程序AppID
  139. 'app_id' => '',
  140. // 小程序AppSecret
  141. 'app_secret' => '',
  142. // 是否开启发货信息管理
  143. 'enableShipping' => false,
  144. ]
  145. ]
  146. ];
  147. }
  148. }