Setting.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\model;
  10. use think\Cache;
  11. use app\common\model\Setting as SettingModel;
  12. use app\common\enum\Setting as SettingEnum;
  13. /**
  14. * 系统设置模型
  15. * Class Wxapp
  16. * @package app\store\model
  17. */
  18. class Setting extends SettingModel
  19. {
  20. /**
  21. * 更新系统设置
  22. * @param $key
  23. * @param $values
  24. * @return bool
  25. * @throws \think\exception\DbException
  26. */
  27. public function edit($key, $values)
  28. {
  29. $model = self::detail($key) ?: $this;
  30. // 数据验证
  31. if (!$this->validValues($key, $values)) {
  32. return false;
  33. }
  34. // 删除系统设置缓存
  35. Cache::rm('setting_' . self::$wxapp_id);
  36. return $model->save([
  37. 'key' => $key,
  38. 'describe' => SettingEnum::data()[$key]['describe'],
  39. 'values' => $values,
  40. 'wxapp_id' => self::$wxapp_id,
  41. ]) !== false;
  42. }
  43. /**
  44. * 数据验证
  45. * @param $key
  46. * @param $values
  47. * @return bool
  48. */
  49. private function validValues($key, $values)
  50. {
  51. $callback = [
  52. 'store' => function ($values) {
  53. return $this->validStore($values);
  54. },
  55. 'printer' => function ($values) {
  56. return $this->validPrinter($values);
  57. },
  58. ];
  59. // 验证商城设置
  60. return isset($callback[$key]) ? $callback[$key]($values) : true;
  61. }
  62. /**
  63. * 验证商城设置
  64. * @param $values
  65. * @return bool
  66. */
  67. private function validStore($values)
  68. {
  69. if (!isset($values['delivery_type']) || empty($values['delivery_type'])) {
  70. $this->error = '配送方式至少选择一个';
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * 验证小票打印机设置
  77. * @param $values
  78. * @return bool
  79. */
  80. private function validPrinter($values)
  81. {
  82. if ($values['is_open'] == false) {
  83. return true;
  84. }
  85. if (!$values['printer_id']) {
  86. $this->error = '请选择订单打印机';
  87. return false;
  88. }
  89. if (empty($values['order_status'])) {
  90. $this->error = '请选择订单打印方式';
  91. return false;
  92. }
  93. return true;
  94. }
  95. }