Payment.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\data\controller\base;
  3. use app\data\model\BaseUserPayment;
  4. use app\data\service\PaymentService;
  5. use app\data\service\UserAdminService;
  6. use think\admin\Controller;
  7. use think\admin\extend\CodeExtend;
  8. use think\admin\helper\QueryHelper;
  9. /**
  10. * 支付通道管理
  11. * Class Payment
  12. * @package app\data\controller\base
  13. */
  14. class Payment extends Controller
  15. {
  16. /**
  17. * 支付通道类型
  18. * @var array
  19. */
  20. protected $types = PaymentService::TYPES;
  21. /**
  22. * 支付通道管理
  23. * @auth true
  24. * @menu true
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function index()
  30. {
  31. $this->type = input('get.type', 'index');
  32. BaseUserPayment::mQuery()->layTable(function () {
  33. $this->title = '支付通道管理';
  34. }, function (QueryHelper $query) {
  35. $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0]);
  36. $query->like('name,code')->equal('status,type#ptype')->dateBetween('create_at');
  37. });
  38. }
  39. /**
  40. * 获取支付名称
  41. * @param array $data
  42. * @return void
  43. */
  44. protected function _page_filter(array &$data)
  45. {
  46. foreach ($data as &$vo) {
  47. $vo['ntype'] = $this->types[$vo['type']]['name'] ?? $vo['type'];
  48. }
  49. }
  50. /**
  51. * 添加支付通道
  52. * @auth true
  53. */
  54. public function add()
  55. {
  56. $this->title = '添加支付通道';
  57. BaseUserPayment::mForm('form');
  58. }
  59. /**
  60. * 编辑支付通道
  61. * @auth true
  62. */
  63. public function edit()
  64. {
  65. $this->title = '编辑支付通道';
  66. BaseUserPayment::mForm('form');
  67. }
  68. /**
  69. * 数据表单处理
  70. * @param array $data
  71. */
  72. protected function _form_filter(array &$data)
  73. {
  74. if (empty($data['code'])) {
  75. $data['code'] = CodeExtend::uniqidNumber(20, 'M');
  76. }
  77. if ($this->request->isGet()) {
  78. $this->payments = [];
  79. foreach ($this->types as $k => $vo) {
  80. $allow = [];
  81. foreach ($vo['bind'] as $api) if (isset(UserAdminService::TYPES[$api])) {
  82. $allow[$api] = UserAdminService::TYPES[$api]['name'];
  83. }
  84. if (empty($allow)) continue;
  85. $this->payments[$k] = array_merge($vo, ['allow' => join('、', $allow)]);
  86. }
  87. $data['content'] = json_decode($data['content'] ?? '[]', true) ?: [];
  88. } else {
  89. if (empty($data['type'])) $this->error('请选择支付通道并配置参数!');
  90. if (empty($data['cover'])) $this->error('请上传支付方式图标!');
  91. $data['content'] = json_encode($this->request->post() ?: [], JSON_UNESCAPED_UNICODE);
  92. }
  93. }
  94. /**
  95. * 表单结果处理
  96. * @param boolean $state
  97. */
  98. protected function _form_result(bool $state)
  99. {
  100. if ($state) {
  101. $this->success('支付通道保存成功!', 'javascript:history.back()');
  102. }
  103. }
  104. /**
  105. * 修改通道状态
  106. * @auth true
  107. */
  108. public function state()
  109. {
  110. BaseUserPayment::mSave($this->_vali([
  111. 'status.in:0,1' => '状态值范围异常!',
  112. 'status.require' => '状态值不能为空!',
  113. ]));
  114. }
  115. /**
  116. * 删除支付通道
  117. * @auth true
  118. */
  119. public function remove()
  120. {
  121. BaseUserPayment::mDelete();
  122. }
  123. }