Setting.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\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 getAll(int $storeId = null): array
  67. {
  68. $model = new static;
  69. is_null($storeId) && $storeId = $model::$storeId;
  70. if (!$data = Cache::get("wxapp_setting_{$storeId}")) {
  71. // 获取全部设置
  72. $setting = $model->getList($storeId);
  73. $data = $setting->isEmpty() ? [] : helper::arrayColumn2Key($setting->toArray(), 'key');
  74. // 写入缓存中
  75. Cache::tag('cache')->set("wxapp_setting_{$storeId}", $data);
  76. }
  77. // 重组setting缓存数据 (多维)
  78. return static::reorganize($model->defaultData(), $data, $type = 'cache');
  79. }
  80. /**
  81. * 获取商城设置列表
  82. * @param int $storeId
  83. * @return \think\Collection
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. private function getList(int $storeId): \think\Collection
  89. {
  90. return $this->where('store_id', '=', $storeId)->select();
  91. }
  92. /**
  93. * 获取设置项信息
  94. * @param string $key
  95. * @return array|\think\Model|null
  96. */
  97. public static function detail(string $key)
  98. {
  99. return static::get(compact('key'));
  100. }
  101. /**
  102. * 默认配置
  103. * @return array
  104. */
  105. public function defaultData(): array
  106. {
  107. return [
  108. 'basic' => [
  109. 'key' => 'basic',
  110. 'describe' => '基础设置',
  111. 'values' => [
  112. // 小程序AppID
  113. 'app_id' => '',
  114. // 小程序AppSecret
  115. 'app_secret' => '',
  116. // 微信支付商户号ID
  117. 'mchid' => '',
  118. // 微信支付密钥
  119. 'apikey' => '',
  120. // 证书文件cert
  121. 'cert_pem' => '',
  122. // 证书文件key
  123. 'key_pem' => '',
  124. ]
  125. ]
  126. ];
  127. }
  128. }