Config.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\admin\controller\withdraw;
  3. use app\common\controller\Backend;
  4. use app\common\Service\ShopConfigService;
  5. use app\common\Enum\WithdrawEnum;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 提现配置管理
  10. *
  11. * @icon fa fa-credit-card
  12. */
  13. class Config extends Backend
  14. {
  15. /**
  16. * Config模型对象
  17. *
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. }
  24. /**
  25. * 查看提现配置
  26. */
  27. public function index()
  28. {
  29. // 获取提现配置
  30. $withdrawConfig = ShopConfigService::getConfigs('shop.withdraw');
  31. // 设置默认值
  32. $defaultConfig = [
  33. 'min_amount' => '100.00', // 单次最小提现金额
  34. 'max_amount' => '1000.00', // 单次最大提现金额
  35. 'charge_rate' => '1', // 手续费率(%)
  36. 'max_num' => '10', // 最多提现次数
  37. 'num_unit' => 'month', // 提现次数单位(day/month)
  38. 'auto_arrival' => 0, // 支付宝自动到账开关 (0=关闭, 1=开启)
  39. 'methods' => ['wechat', 'alipay', 'bank'] // 支持的提现方式
  40. ];
  41. $withdrawConfig = array_merge($defaultConfig, $withdrawConfig ?: []);
  42. // 确保数组类型的配置项是数组
  43. if (!is_array($withdrawConfig['methods'])) {
  44. $withdrawConfig['methods'] = ['wechat', 'alipay', 'bank'];
  45. }
  46. // 确保布尔值类型正确
  47. $withdrawConfig['auto_arrival'] = (int)$withdrawConfig['auto_arrival'];
  48. // 获取提现方式和状态列表用于前端显示
  49. $this->view->assign([
  50. 'withdrawConfig' => $withdrawConfig,
  51. 'withdrawTypeList' => WithdrawEnum::getTypeList(),
  52. 'statusList' => WithdrawEnum::getStatusList()
  53. ]);
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 编辑配置
  58. */
  59. public function edit($ids = null)
  60. {
  61. if ($this->request->isPost()) {
  62. $this->token();
  63. $params = $this->request->post("row/a", [], 'trim');
  64. if ($params) {
  65. // try {
  66. // 处理布尔类型的自动到账开关
  67. $params['auto_arrival'] = isset($params['auto_arrival']) ? (int)$params['auto_arrival'] : 0;
  68. // 处理数组类型的提现方式配置
  69. if (isset($params['methods']) && is_array($params['methods'])) {
  70. $params['methods'] = $params['methods'];
  71. } else {
  72. $params['methods'] = [];
  73. }
  74. // 验证数据
  75. $this->validateConfig($params);
  76. // 更新配置
  77. ShopConfigService::setConfigs('shop.withdraw', $params);
  78. $this->success('配置保存成功');
  79. // } catch (\Exception $e) {
  80. // $this->error($e->getMessage());
  81. // }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. }
  86. /**
  87. * 验证配置参数
  88. * @param array $params
  89. * @throws ValidateException
  90. */
  91. private function validateConfig($params)
  92. {
  93. // 验证最小金额
  94. if (isset($params['min_amount']) && !is_numeric($params['min_amount'])) {
  95. $this->error('最小提现金额必须为数字');
  96. }
  97. // 验证最大金额
  98. if (isset($params['max_amount']) && !is_numeric($params['max_amount'])) {
  99. $this->error('最大提现金额必须为数字');
  100. }
  101. // 验证最小金额不能大于最大金额
  102. if (isset($params['min_amount']) && isset($params['max_amount'])) {
  103. $minAmount = $params['min_amount'];
  104. $maxAmount = $params['max_amount'];
  105. // 确保都是有效的数字
  106. if (is_numeric($minAmount) && is_numeric($maxAmount) && $minAmount > 0 && $maxAmount > 0) {
  107. if (bccomp((string)$minAmount, (string)$maxAmount, 2) > 0) {
  108. throw new ValidateException('最小提现金额不能大于最大提现金额');
  109. }
  110. }
  111. }
  112. // 验证手续费率
  113. if (isset($params['charge_rate'])) {
  114. $chargeRate = (float)$params['charge_rate'];
  115. if (!is_numeric($params['charge_rate']) || $chargeRate < 0 || $chargeRate > 100) {
  116. $this->error('手续费率必须为0-100之间的数字');
  117. }
  118. }
  119. // 验证提现次数
  120. if (isset($params['max_num'])) {
  121. $maxNum = (int)$params['max_num'];
  122. if (!is_numeric($params['max_num']) || $maxNum < 0) {
  123. $this->error('提现次数必须为正整数');
  124. }
  125. }
  126. // 验证提现次数单位
  127. if (isset($params['num_unit']) && !in_array($params['num_unit'], ['day', 'month'])) {
  128. $this->error('提现次数单位只能为day或month');
  129. }
  130. // 验证提现方式
  131. if (isset($params['methods']) && is_array($params['methods'])) {
  132. $validMethods = array_keys(WithdrawEnum::getTypeList());
  133. foreach ($params['methods'] as $method) {
  134. if (!in_array($method, $validMethods)) {
  135. $this->error('请至少选择一种提现方式');
  136. }
  137. }
  138. }
  139. // 验证自动到账开关
  140. if (isset($params['auto_arrival'])) {
  141. $autoArrival = (int)$params['auto_arrival'];
  142. if (!in_array($autoArrival, [0, 1])) {
  143. $this->error('自动到账设置不合法');
  144. }
  145. }
  146. }
  147. }