Setting.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. 'qrcode' => '分销海报',
  26. ];
  27. /**
  28. * 更新系统设置
  29. * @param $data
  30. * @return bool
  31. * @throws \think\exception\PDOException
  32. */
  33. public function edit($data)
  34. {
  35. $this->startTrans();
  36. try {
  37. foreach ($data as $key => $values)
  38. $this->saveValues($key, $values);
  39. $this->commit();
  40. // 删除系统设置缓存
  41. Cache::rm('dealer_setting_' . self::$wxapp_id);
  42. return true;
  43. } catch (\Exception $e) {
  44. $this->error = $e->getMessage();
  45. $this->rollback();
  46. return false;
  47. }
  48. }
  49. /**
  50. * 保存设置项
  51. * @param $key
  52. * @param $values
  53. * @return false|int
  54. * @throws BaseException
  55. * @throws \think\exception\DbException
  56. */
  57. private function saveValues($key, $values)
  58. {
  59. $model = self::detail($key) ?: new self;
  60. // 数据验证
  61. if (!$this->validValues($key, $values)) {
  62. throw new BaseException(['msg' => $this->error]);
  63. }
  64. return $model->save([
  65. 'key' => $key,
  66. 'describe' => $this->describe[$key],
  67. 'values' => $values,
  68. 'wxapp_id' => self::$wxapp_id,
  69. ]);
  70. }
  71. /**
  72. * 数据验证
  73. * @param $key
  74. * @param $values
  75. * @return bool
  76. */
  77. private function validValues($key, $values)
  78. {
  79. if ($key === 'settlement') {
  80. // 验证结算方式
  81. return $this->validSettlement($values);
  82. }
  83. // if ($key === 'condition') {
  84. // // 验证分销商条件
  85. // return $this->validCondition($values);
  86. // }
  87. return true;
  88. }
  89. /**
  90. * 验证结算方式
  91. * @param $values
  92. * @return bool
  93. */
  94. private function validSettlement($values)
  95. {
  96. if (!isset($values['pay_type']) || empty($values['pay_type'])) {
  97. $this->error = '请设置 结算-提现方式';
  98. return false;
  99. }
  100. return true;
  101. }
  102. }