Setting.php 2.3 KB

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