Setting.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 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\model\Wxapp as WxappModel;
  16. use app\common\library\helper;
  17. /**
  18. * 微信小程序设置模型
  19. * Class Setting
  20. * @package app\common\model\wxapp
  21. */
  22. class Setting extends BaseModel
  23. {
  24. // 定义表名
  25. protected $name = 'wxapp_setting';
  26. protected $createTime = false;
  27. /**
  28. * 获取器: 转义数组格式
  29. * @param $value
  30. * @return array
  31. */
  32. public function getValuesAttr($value): array
  33. {
  34. return helper::jsonDecode($value);
  35. }
  36. /**
  37. * 修改器: 转义成json格式
  38. * @param $value
  39. * @return string
  40. */
  41. public function setValuesAttr($value): string
  42. {
  43. return helper::jsonEncode($value);
  44. }
  45. /**
  46. * 获取指定项设置
  47. * @param string $key
  48. * @param int|null $storeId
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public static function getItem(string $key, int $storeId = null): array
  55. {
  56. $data = static::getAll($storeId);
  57. return isset($data[$key]) ? $data[$key]['values'] : [];
  58. }
  59. /**
  60. * 获取微信小程序基础配置
  61. * @param int|null $storeId
  62. * @return array
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public static function getWxappConfig(?int $storeId = null): array
  68. {
  69. return static::getItem('basic', $storeId);
  70. }
  71. /**
  72. * 获取全部设置
  73. * @param int|null $storeId
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public static function getAll(?int $storeId = null): array
  80. {
  81. $model = new static;
  82. is_null($storeId) && $storeId = static::$storeId;
  83. if (!$data = Cache::get("wxapp_setting_{$storeId}")) {
  84. // 获取全部设置
  85. $data = $model->getList($storeId);
  86. // 写入缓存中
  87. Cache::tag('cache')->set("wxapp_setting_{$storeId}", $data);
  88. }
  89. // 合并默认设置
  90. return array_merge_multiple($model->defaultData(), $data);
  91. }
  92. /**
  93. * 获取商城设置列表
  94. * @param int $storeId
  95. * @return array
  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): array
  101. {
  102. // 获取所有设置项
  103. $data = $this->where('store_id', '=', $storeId)->select();
  104. return $data->isEmpty() ? [] : helper::arrayColumn2Key($data->toArray(), 'key');
  105. }
  106. /**
  107. * 获取设置项信息
  108. * @param string $key
  109. * @return array|\think\Model|null
  110. */
  111. public static function detail(string $key)
  112. {
  113. return static::get(compact('key'));
  114. }
  115. /**
  116. * 默认配置
  117. * @return array
  118. */
  119. public function defaultData(): array
  120. {
  121. return [
  122. 'basic' => [
  123. 'key' => 'basic',
  124. 'describe' => '基础设置',
  125. 'values' => [
  126. // 小程序AppID
  127. 'app_id' => '',
  128. // 小程序AppSecret
  129. 'app_secret' => '',
  130. // 微信支付商户号ID
  131. 'mchid' => '',
  132. // 微信支付密钥
  133. 'apikey' => '',
  134. // 证书文件cert
  135. 'cert_pem' => '',
  136. // 证书文件key
  137. 'key_pem' => '',
  138. ]
  139. ]
  140. ];
  141. }
  142. }