Config.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\admin\controller\pay;
  3. use app\common\controller\Backend;
  4. use app\common\Enum\PayEnum;
  5. use think\exception\ValidateException;
  6. use think\exception\PDOException;
  7. use Exception;
  8. use think\Db;
  9. /**
  10. * 支付配置
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Config extends Backend
  15. {
  16. /**
  17. * Config模型对象
  18. * @var \app\admin\model\pay\Config
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\pay\Config;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. $this->assignconfig("statusList", json_encode($this->model->getStatusList()));
  27. $this->assignconfig("methodList", json_encode(PayEnum::getPayMethodList()));
  28. $this->view->assign("methodList", PayEnum::getPayMethodList());
  29. $this->view->assign("merchantTypeList", PayEnum::getMerchantTypeList());
  30. $this->assignconfig("merchantTypeList", json_encode(PayEnum::getMerchantTypeList()));
  31. //添加 默认选中的 支付方式
  32. $this->view->assign("defaultType", PayEnum::METHOD_DOUYIN);
  33. }
  34. /**
  35. * 添加
  36. *
  37. * @return string
  38. * @throws \think\Exception
  39. */
  40. public function add()
  41. {
  42. if (false === $this->request->isPost()) {
  43. return $this->view->fetch();
  44. }
  45. $params = $this->request->post('row/a');
  46. if (empty($params)) {
  47. $this->error(__('Parameter %s can not be empty', ''));
  48. }
  49. $params = $this->preExcludeFields($params);
  50. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  51. $params[$this->dataLimitField] = $this->auth->id;
  52. }
  53. $result = false;
  54. Db::startTrans();
  55. try {
  56. if(isset($params['params'])){
  57. $params['params'] = json_encode($params['params']);
  58. }
  59. //是否采用模型验证
  60. if ($this->modelValidate) {
  61. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  62. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  63. $this->model->validateFailException()->validate($validate);
  64. }
  65. $result = $this->model->allowField(true)->save($params);
  66. Db::commit();
  67. } catch (ValidateException|PDOException|Exception $e) {
  68. Db::rollback();
  69. $this->error($e->getMessage());
  70. }
  71. if ($result === false) {
  72. $this->error(__('No rows were inserted'));
  73. }
  74. $this->success();
  75. }
  76. /**
  77. * 编辑
  78. *
  79. * @param $ids
  80. * @return string
  81. * @throws DbException
  82. * @throws \think\Exception
  83. */
  84. public function edit($ids = null)
  85. {
  86. $row = $this->model->get($ids);
  87. if (!$row) {
  88. $this->error(__('No Results were found'));
  89. }
  90. $adminIds = $this->getDataLimitAdminIds();
  91. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  92. $this->error(__('You have no permission'));
  93. }
  94. if (false === $this->request->isPost()) {
  95. $this->view->assign('row', $row);
  96. return $this->view->fetch();
  97. }
  98. $params = $this->request->post('row/a');
  99. if (empty($params)) {
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. $params = $this->preExcludeFields($params);
  103. $result = false;
  104. Db::startTrans();
  105. try {
  106. if(isset($params['params'])){
  107. $params['params'] = json_encode($params['params']);
  108. }
  109. //是否采用模型验证
  110. if ($this->modelValidate) {
  111. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  112. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  113. $row->validateFailException()->validate($validate);
  114. }
  115. $result = $row->allowField(true)->save($params);
  116. Db::commit();
  117. } catch (ValidateException|PDOException|Exception $e) {
  118. Db::rollback();
  119. $this->error($e->getMessage());
  120. }
  121. if (false === $result) {
  122. $this->error(__('No rows were updated'));
  123. }
  124. $this->success();
  125. }
  126. /**
  127. * 过滤空数组值
  128. */
  129. private function filterEmptyArrayValues($data)
  130. {
  131. if (!is_array($data)) {
  132. return $data;
  133. }
  134. foreach ($data as $key => $value) {
  135. if (is_array($value)) {
  136. $data[$key] = $this->filterEmptyArrayValues($value);
  137. // 如果处理后的数组为空,则移除该键
  138. if (empty($data[$key])) {
  139. unset($data[$key]);
  140. }
  141. } else {
  142. // 如果值为空字符串,则移除该键
  143. if ($value === '' || $value === null) {
  144. unset($data[$key]);
  145. }
  146. }
  147. }
  148. return $data;
  149. }
  150. /**
  151. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  152. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  153. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  154. */
  155. }