Setting.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\store\model\dealer;
  3. use think\Cache;
  4. use app\common\exception\BaseException;
  5. use app\common\model\dealer\Setting as SettingModel;
  6. /**
  7. * 分销商设置模型
  8. * Class Setting
  9. * @package app\store\model\dealer
  10. */
  11. class Setting extends SettingModel
  12. {
  13. /**
  14. * 设置项描述
  15. * @var array
  16. */
  17. private $describe = [
  18. 'basic' => '基础设置',
  19. 'condition' => '分销商条件',
  20. 'commission' => '佣金设置',
  21. 'settlement' => '结算',
  22. 'words' => '自定义文字',
  23. 'license' => '申请协议',
  24. 'background' => '页面背景图',
  25. 'template_msg' => '模板消息',
  26. 'qrcode' => '分销海报',
  27. ];
  28. /**
  29. * 更新系统设置
  30. * @param $data
  31. * @return bool
  32. * @throws \think\exception\PDOException
  33. */
  34. public function edit($data)
  35. {
  36. $this->startTrans();
  37. try {
  38. foreach ($data as $key => $values)
  39. $this->saveValues($key, $values);
  40. $this->commit();
  41. // 删除系统设置缓存
  42. Cache::rm('dealer_setting_' . self::$wxapp_id);
  43. return true;
  44. } catch (\Exception $e) {
  45. $this->error = $e->getMessage();
  46. $this->rollback();
  47. return false;
  48. }
  49. }
  50. /**
  51. * 保存设置项
  52. * @param $key
  53. * @param $values
  54. * @return false|int
  55. * @throws BaseException
  56. * @throws \think\exception\DbException
  57. */
  58. private function saveValues($key, $values)
  59. {
  60. $model = self::detail($key) ?: new self;
  61. // 数据验证
  62. if (!$this->validValues($key, $values)) {
  63. throw new BaseException(['msg' => $this->error]);
  64. }
  65. return $model->save([
  66. 'key' => $key,
  67. 'describe' => $this->describe[$key],
  68. 'values' => $values,
  69. 'wxapp_id' => self::$wxapp_id,
  70. ]);
  71. }
  72. /**
  73. * 数据验证
  74. * @param $key
  75. * @param $values
  76. * @return bool
  77. */
  78. private function validValues($key, $values)
  79. {
  80. if ($key === 'settlement') {
  81. // 验证结算方式
  82. return $this->validSettlement($values);
  83. }
  84. // if ($key === 'condition') {
  85. // // 验证分销商条件
  86. // return $this->validCondition($values);
  87. // }
  88. return true;
  89. }
  90. /**
  91. * 验证结算方式
  92. * @param $values
  93. * @return bool
  94. */
  95. private function validSettlement($values)
  96. {
  97. if (!isset($values['pay_type']) || empty($values['pay_type'])) {
  98. $this->error = '请设置 结算-提现方式';
  99. return false;
  100. }
  101. return true;
  102. }
  103. }