Setting.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\store\model;
  3. use think\Cache;
  4. use app\common\model\settings\Setting as SettingModel;
  5. use app\common\enum\settings\SettingEnum;
  6. /**
  7. * 系统设置模型
  8. */
  9. class Setting extends SettingModel
  10. {
  11. /**
  12. * 更新系统设置
  13. */
  14. public function edit($key, $values)
  15. {
  16. $model = self::detail($key) ?: $this;
  17. // 数据验证
  18. if (!$this->validValues($key, $values)) {
  19. return false;
  20. }
  21. // 删除系统设置缓存
  22. Cache::delete('setting_' . self::$app_id);
  23. return $model->save([
  24. 'key' => $key,
  25. 'describe' => SettingEnum::data()[$key]['describe'],
  26. 'values' => $values,
  27. 'app_id' => self::$app_id,
  28. ]) !== false;
  29. }
  30. /**
  31. * 数据验证
  32. */
  33. private function validValues($key, $values)
  34. {
  35. $callback = [
  36. 'store' => function ($values) {
  37. return $this->validStore($values);
  38. },
  39. 'printer' => function ($values) {
  40. return $this->validPrinter($values);
  41. },
  42. ];
  43. // 验证商城设置
  44. return isset($callback[$key]) ? $callback[$key]($values) : true;
  45. }
  46. /**
  47. * 验证商城设置
  48. */
  49. private function validStore($values)
  50. {
  51. if (!isset($values['delivery_type']) || empty($values['delivery_type'])) {
  52. $this->error = '配送方式至少选择一个';
  53. return false;
  54. }
  55. return true;
  56. }
  57. /**
  58. * 验证小票打印机设置
  59. */
  60. private function validPrinter($values)
  61. {
  62. if ($values['is_open'] == false) {
  63. return true;
  64. }
  65. if (!$values['printer_id']) {
  66. $this->error = '请选择订单打印机';
  67. return false;
  68. }
  69. if (empty($values['order_status'])) {
  70. $this->error = '请选择订单打印方式';
  71. return false;
  72. }
  73. return true;
  74. }
  75. }