Setting.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\store\model;
  13. use think\facade\Cache;
  14. use app\common\model\store\Setting as SettingModel;
  15. use app\common\enum\Setting as SettingEnum;
  16. /**
  17. * 系统设置模型
  18. * Class Setting
  19. * @package app\store\model
  20. */
  21. class Setting extends SettingModel
  22. {
  23. /**
  24. * 更新系统设置
  25. * @param string $key
  26. * @param array $values
  27. * @return bool
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function edit(string $key, array $values)
  33. {
  34. // 设置项详情记录
  35. $model = self::detail($key) ?: $this;
  36. // 数据验证
  37. if (!$this->validValues($key, $values)) {
  38. return false;
  39. }
  40. // 删除系统设置缓存
  41. Cache::delete('setting_' . self::$storeId);
  42. // 更新记录
  43. return $model->save([
  44. 'key' => $key,
  45. 'describe' => SettingEnum::data()[$key]['describe'],
  46. 'values' => $values,
  47. 'update_time' => time(),
  48. 'store_id' => self::$storeId,
  49. ]) !== false;
  50. }
  51. /**
  52. * 数据验证
  53. * @param string $key
  54. * @param array $values
  55. * @return bool
  56. */
  57. private function validValues(string $key, array $values)
  58. {
  59. $callback = [
  60. 'store' => function ($values) {
  61. return $this->validStore($values);
  62. }
  63. ];
  64. // 验证商城设置
  65. return isset($callback[$key]) ? $callback[$key]($values) : true;
  66. }
  67. /**
  68. * 验证商城设置
  69. * @param array $values
  70. * @return bool
  71. */
  72. private function validStore(array $values)
  73. {
  74. if (!isset($values['delivery_type']) || empty($values['delivery_type'])) {
  75. $this->error = '配送方式至少选择一个';
  76. return false;
  77. }
  78. return true;
  79. }
  80. }