Setting.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\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. $setting = $data->isEmpty() ? [] : helper::arrayColumn2Key($data->toArray(), 'key');
  105. // 兼容旧数据
  106. return static::compatibleOld($setting, $storeId);
  107. }
  108. /**
  109. * 兼容老版本数据 (v2.0.4之前)
  110. * @param array $newSetting
  111. * @param int|null $storeId
  112. * @return array
  113. */
  114. private static function compatibleOld(array $newSetting, ?int $storeId = null): array
  115. {
  116. if (empty($newSetting) || !isset($newSetting['basic'])) {
  117. $basic = WxappModel::getOldData($storeId);
  118. $newSetting['basic']['values'] = $basic;
  119. (new static)->save([
  120. 'key' => 'basic',
  121. 'describe' => '基础设置',
  122. 'values' => $basic,
  123. 'store_id' => $storeId,
  124. 'update_time' => time(),
  125. ]);
  126. }
  127. return $newSetting;
  128. }
  129. /**
  130. * 获取设置项信息
  131. * @param string $key
  132. * @return array|\think\Model|null
  133. */
  134. public static function detail(string $key)
  135. {
  136. return static::get(compact('key'));
  137. }
  138. /**
  139. * 默认配置
  140. * @return array
  141. */
  142. public function defaultData(): array
  143. {
  144. return [
  145. 'basic' => [
  146. 'key' => 'basic',
  147. 'describe' => '基础设置',
  148. 'values' => [
  149. // 小程序AppID
  150. 'app_id' => '',
  151. // 小程序AppSecret
  152. 'app_secret' => '',
  153. // 微信支付商户号ID
  154. 'mchid' => '',
  155. // 微信支付密钥
  156. 'apikey' => '',
  157. // 证书文件cert
  158. 'cert_pem' => '',
  159. // 证书文件key
  160. 'key_pem' => '',
  161. ]
  162. ]
  163. ];
  164. }
  165. }