123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace app\admin\controller\withdraw;
- use app\common\controller\Backend;
- use app\common\Service\ShopConfigService;
- use app\common\Enum\WithdrawEnum;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 提现配置管理
- *
- * @icon fa fa-credit-card
- */
- class Config extends Backend
- {
- /**
- * Config模型对象
- *
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 查看提现配置
- */
- public function index()
- {
- // 获取提现配置
- $withdrawConfig = ShopConfigService::getConfigs('shop.withdraw');
-
- // 设置默认值
- $defaultConfig = [
- 'min_amount' => '100.00', // 单次最小提现金额
- 'max_amount' => '1000.00', // 单次最大提现金额
- 'charge_rate' => '1', // 手续费率(%)
- 'max_num' => '10', // 最多提现次数
- 'num_unit' => 'month', // 提现次数单位(day/month)
- 'auto_arrival' => 0, // 支付宝自动到账开关 (0=关闭, 1=开启)
- 'methods' => ['wechat', 'alipay', 'bank'] // 支持的提现方式
- ];
-
- $withdrawConfig = array_merge($defaultConfig, $withdrawConfig ?: []);
-
- // 确保数组类型的配置项是数组
- if (!is_array($withdrawConfig['methods'])) {
- $withdrawConfig['methods'] = ['wechat', 'alipay', 'bank'];
- }
-
- // 确保布尔值类型正确
- $withdrawConfig['auto_arrival'] = (int)$withdrawConfig['auto_arrival'];
-
- // 获取提现方式和状态列表用于前端显示
- $this->view->assign([
- 'withdrawConfig' => $withdrawConfig,
- 'withdrawTypeList' => WithdrawEnum::getTypeList(),
- 'statusList' => WithdrawEnum::getStatusList()
- ]);
-
- return $this->view->fetch();
- }
-
- /**
- * 编辑配置
- */
- public function edit($ids = null)
- {
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post("row/a", [], 'trim');
-
- if ($params) {
- // try {
- // 处理布尔类型的自动到账开关
- $params['auto_arrival'] = isset($params['auto_arrival']) ? (int)$params['auto_arrival'] : 0;
-
- // 处理数组类型的提现方式配置
- if (isset($params['methods']) && is_array($params['methods'])) {
- $params['methods'] = $params['methods'];
- } else {
- $params['methods'] = [];
- }
- // 验证数据
- $this->validateConfig($params);
- // 更新配置
- ShopConfigService::setConfigs('shop.withdraw', $params);
- $this->success('配置保存成功');
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- }
-
- /**
- * 验证配置参数
- * @param array $params
- * @throws ValidateException
- */
- private function validateConfig($params)
- {
- // 验证最小金额
- if (isset($params['min_amount']) && !is_numeric($params['min_amount'])) {
- $this->error('最小提现金额必须为数字');
- }
-
- // 验证最大金额
- if (isset($params['max_amount']) && !is_numeric($params['max_amount'])) {
- $this->error('最大提现金额必须为数字');
- }
-
- // 验证最小金额不能大于最大金额
- if (isset($params['min_amount']) && isset($params['max_amount'])) {
- $minAmount = $params['min_amount'];
- $maxAmount = $params['max_amount'];
-
- // 确保都是有效的数字
- if (is_numeric($minAmount) && is_numeric($maxAmount) && $minAmount > 0 && $maxAmount > 0) {
- if (bccomp((string)$minAmount, (string)$maxAmount, 2) > 0) {
- throw new ValidateException('最小提现金额不能大于最大提现金额');
- }
- }
- }
-
- // 验证手续费率
- if (isset($params['charge_rate'])) {
- $chargeRate = (float)$params['charge_rate'];
- if (!is_numeric($params['charge_rate']) || $chargeRate < 0 || $chargeRate > 100) {
- $this->error('手续费率必须为0-100之间的数字');
- }
- }
-
- // 验证提现次数
- if (isset($params['max_num'])) {
- $maxNum = (int)$params['max_num'];
- if (!is_numeric($params['max_num']) || $maxNum < 0) {
- $this->error('提现次数必须为正整数');
- }
- }
-
- // 验证提现次数单位
- if (isset($params['num_unit']) && !in_array($params['num_unit'], ['day', 'month'])) {
- $this->error('提现次数单位只能为day或month');
- }
-
- // 验证提现方式
- if (isset($params['methods']) && is_array($params['methods'])) {
- $validMethods = array_keys(WithdrawEnum::getTypeList());
- foreach ($params['methods'] as $method) {
- if (!in_array($method, $validMethods)) {
- $this->error('请至少选择一种提现方式');
- }
- }
- }
-
- // 验证自动到账开关
- if (isset($params['auto_arrival'])) {
- $autoArrival = (int)$params['auto_arrival'];
- if (!in_array($autoArrival, [0, 1])) {
- $this->error('自动到账设置不合法');
- }
- }
- }
- }
|