| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- /**
- * 支付配置控制器 - 字段单独存储版
- */
- namespace app\admin\controller\pay;
- use app\common\controller\Backend;
- use think\facade\Db;
- class Config extends Backend
- {
- protected array $noNeedLogin = ['*'];
- /**
- * 支付配置列表
- * @return void
- */
- public function index(): void
- {
- $where = [];
- // 配置名称搜索
- if (!empty($this->request->get('name'))) {
- $where[] = ['name', 'like', '%' . $this->request->get('name') . '%'];
- }
- // 支付类型筛选
- if (!empty($this->request->get('type'))) {
- $where['type'] = $this->request->get('type');
- }
- // 状态筛选
- if ($this->request->get('status') !== '' && $this->request->get('status') !== null) {
- $where['status'] = $this->request->get('status');
- }
- // 排序
- $order = ['id' => 'desc'];
- // 分页
- $page = $this->request->get('page', 1);
- $limit = $this->request->get('limit', 20);
- $configList = Db::name('pay_config')
- ->where($where)
- ->order($order)
- ->paginate([
- 'list_rows' => $limit,
- 'page' => $page
- ]);
- $list = $configList->items();
- foreach ($list as &$item) {
- // 添加类型文本
- $item['type_text'] = $this->getTypeText($item['type']);
- $item['status_text'] = $item['status'] ? '启用' : '禁用';
- }
- $this->success('查询成功', [
- 'list' => $list,
- 'total' => $configList->total()
- ]);
- }
- /**
- * 获取支付配置详情
- * @return void
- */
- public function detail(): void
- {
- $id = $this->request->get('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- $config = Db::name('pay_config')
- ->where('id', $id)
- ->find();
- if (!$config) {
- $this->error(__('配置不存在'));
- }
- // 添加类型文本
- $config['type_text'] = $this->getTypeText($config['type']);
- $this->success('查询成功', $config);
- }
- /**
- * 新增/编辑支付配置
- * @return void
- */
- public function edit(): void
- {
- $id = $this->request->post('id');
- $name = $this->request->post('name');
- $type = $this->request->post('type', 1);
- $status = $this->request->post('status', 0);
- // 通用字段
- $appId = $this->request->post('app_id', '');
- $mchId = $this->request->post('mch_id', '');
- $key = $this->request->post('key', '');
- $notifyUrl = $this->request->post('notify_url', '');
- $h5NotifyUrl = $this->request->post('h5_notify_url', '');
- // 支付宝专用字段
- $publicKeyPath = $this->request->post('public_key_path', '');
- $privateKeyPath = $this->request->post('private_key_path', '');
- if (empty($name)) {
- $this->error(__('配置名称不能为空'));
- }
- if (empty($type)) {
- $this->error(__('请选择支付类型'));
- }
- // 验证配置名称是否已存在(编辑时排除自己)
- $checkName = Db::name('pay_config')
- ->where('name', $name);
- if (!empty($id)) {
- $checkName->where('id', '<>', $id);
- }
- if ($checkName->find()) {
- $this->error(__('配置名称已存在'));
- }
- // 验证必填字段
- $validationResult = $this->validateFields($type, [
- 'app_id' => $appId,
- 'mch_id' => $mchId,
- 'key' => $key,
- 'notify_url' => $notifyUrl,
- 'h5_notify_url' => $h5NotifyUrl,
- 'public_key_path' => $publicKeyPath,
- 'private_key_path' => $privateKeyPath
- ]);
- if ($validationResult !== true) {
- $this->error($validationResult);
- }
- $data = [
- 'name' => $name,
- 'type' => $type,
- 'app_id' => $appId,
- 'mch_id' => $mchId,
- 'key' => $key,
- 'notify_url' => $notifyUrl,
- 'h5_notify_url' => $h5NotifyUrl,
- 'public_key_path' => $type == 2 ? $publicKeyPath : '',
- 'private_key_path' => $type == 2 ? $privateKeyPath : '',
- 'status' => $status,
- 'update_time' => date('Y-m-d H:i:s')
- ];
- try {
- if (!empty($id)) {
- // 编辑
- $configInfo = Db::name('pay_config')
- ->where('id', $id)
- ->find();
- if (!$configInfo) {
- $this->error(__('配置不存在'));
- }
- // 如果启用此配置,需要禁用同类型的其他配置
- if ($status == 1 && $configInfo['status'] == 0) {
- Db::name('pay_config')
- ->where('type', $type)
- ->where('id', '<>', $id)
- ->update(['status' => 0]);
- }
- $result = Db::name('pay_config')
- ->where('id', $id)
- ->update($data);
- $message = '编辑成功';
- } else {
- // 新增
- $data['create_time'] = date('Y-m-d H:i:s');
- // 如果启用此配置,需要禁用同类型的其他配置
- if ($status == 1) {
- Db::name('pay_config')
- ->where('type', $type)
- ->update(['status' => 0]);
- }
- $result = Db::name('pay_config')
- ->insert($data);
- $id = Db::name('pay_config')->getLastInsID();
- $message = '新增成功';
- }
- } catch (\Exception $e) {
- $this->error('操作失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success($message, ['id' => $id]);
- } else {
- $this->error(__('操作失败'));
- }
- }
- /**
- * 验证字段
- * @param int $type 支付类型
- * @param array $fields 字段值
- * @return bool|string
- */
- private function validateFields(int $type, array $fields)
- {
- // 通用验证
- if (empty($fields['app_id'])) {
- return '应用ID不能为空';
- }
- if (empty($fields['mch_id'])) {
- return '商户ID不能为空';
- }
- if (empty($fields['key'])) {
- return 'API密钥不能为空';
- }
- // 类型特定验证
- if ($type == 1) { // 微信支付
- if (strlen($fields['key']) < 32) {
- return '微信支付API密钥长度必须至少32位';
- }
- } elseif ($type == 2) { // 支付宝
- if (empty($fields['private_key_path'])) {
- return '支付宝私钥地址不能为空';
- }
- }
- // URL验证
- if (!empty($fields['notify_url']) && !filter_var($fields['notify_url'], FILTER_VALIDATE_URL)) {
- return '支付通知地址格式不正确';
- }
- if (!empty($fields['h5_notify_url']) && !filter_var($fields['h5_notify_url'], FILTER_VALIDATE_URL)) {
- return 'H5支付通知地址格式不正确';
- }
- return true;
- }
- /**
- * 删除支付配置
- * @return void
- */
- public function delete(): void
- {
- $id = $this->request->post('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- $configInfo = Db::name('pay_config')
- ->where('id', $id)
- ->find();
- if (!$configInfo) {
- $this->error(__('配置不存在'));
- }
- if ($configInfo['status'] == 1) {
- $this->error(__('当前配置已启用,请先禁用后再删除'));
- }
- $result = Db::name('pay_config')
- ->where('id', $id)
- ->delete();
- if ($result) {
- $this->success('删除成功');
- } else {
- $this->error(__('删除失败'));
- }
- }
- /**
- * 启用/禁用配置
- * @return void
- */
- public function toggleStatus(): void
- {
- $id = $this->request->post('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- $configInfo = Db::name('pay_config')
- ->where('id', $id)
- ->find();
- if (!$configInfo) {
- $this->error(__('配置不存在'));
- }
- $newStatus = $configInfo['status'] == 1 ? 0 : 1;
- $statusText = $newStatus ? '启用' : '禁用';
- try {
- // 如果启用此配置,需要禁用同类型的其他配置
- if ($newStatus == 1) {
- Db::name('pay_config')
- ->where('type', $configInfo['type'])
- ->where('id', '<>', $id)
- ->update(['status' => 0]);
- }
- $result = Db::name('pay_config')
- ->where('id', $id)
- ->update([
- 'status' => $newStatus,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- } catch (\Exception $e) {
- $this->error('操作失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success($statusText . '成功');
- } else {
- $this->error($statusText . '失败');
- }
- }
- /**
- * 获取支付类型文本
- * @param int $type
- * @return string
- */
- private function getTypeText(int $type): string
- {
- $map = [
- 1 => '微信支付',
- 2 => '支付宝支付'
- ];
- return $map[$type] ?? '未知类型';
- }
- /**
- * 获取支付类型选项
- * @return void
- */
- public function typeOptions(): void
- {
- $options = [
- ['value' => 1, 'label' => '微信支付'],
- ['value' => 2, 'label' => '支付宝支付']
- ];
- $this->success('获取成功', $options);
- }
- /**
- * 获取状态选项
- * @return void
- */
- public function statusOptions(): void
- {
- $options = [
- ['value' => 0, 'label' => '禁用'],
- ['value' => 1, 'label' => '启用']
- ];
- $this->success('获取成功', $options);
- }
- /**
- * 获取支付配置模板
- * @return void
- */
- public function getTemplate(): void
- {
- $type = $this->request->get('type', 1);
- if ($type == 1) { // 微信支付模板
- $template = [
- 'name' => '微信支付配置',
- 'type' => 1,
- 'app_id' => 'wx1234567890abcdef',
- 'mch_id' => '1234567890',
- 'key' => 'your_wechat_api_key_32_characters_long',
- 'notify_url' => 'https://your-domain.com/api/pay/wechat/notify',
- 'h5_notify_url' => 'https://your-domain.com/api/pay/wechat/h5-notify',
- 'public_key_path' => '',
- 'private_key_path' => '',
- 'status' => 0
- ];
- } elseif ($type == 2) { // 支付宝模板
- $template = [
- 'name' => '支付宝支付配置',
- 'type' => 2,
- 'app_id' => '2021000123456789',
- 'mch_id' => '2088101123456789',
- 'key' => 'your_alipay_api_key',
- 'notify_url' => 'https://your-domain.com/api/pay/alipay/notify',
- 'h5_notify_url' => 'https://your-domain.com/api/pay/alipay/h5-notify',
- 'public_key_path' => '/path/to/alipay/public_key.pem',
- 'private_key_path' => '/path/to/alipay/private_key.pem',
- 'status' => 0
- ];
- } else {
- $template = [
- 'name' => '',
- 'type' => $type,
- 'app_id' => '',
- 'mch_id' => '',
- 'key' => '',
- 'notify_url' => '',
- 'h5_notify_url' => '',
- 'public_key_path' => '',
- 'private_key_path' => '',
- 'status' => 0
- ];
- }
- $this->success('获取成功', $template);
- }
- /**
- * 获取当前启用的配置
- * @return void
- */
- public function getActiveConfig(): void
- {
- $type = $this->request->get('type', 1);
- $config = Db::name('pay_config')
- ->where('type', $type)
- ->where('status', 1)
- ->find();
- if (!$config) {
- $this->error('未找到启用的支付配置');
- }
- $this->success('获取成功', $config);
- }
- }
|