Setting.php 2.8 KB

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