PayConfig.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use think\Db;
  4. use app\admin\model\shopro\PayConfig as PayConfigModel;
  5. /**
  6. * 支付配置
  7. */
  8. class PayConfig extends Common
  9. {
  10. protected $noNeedRight = ['select'];
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = new PayConfigModel;
  15. }
  16. /**
  17. * 支付配置列表
  18. */
  19. public function index()
  20. {
  21. if (!$this->request->isAjax()) {
  22. return $this->view->fetch();
  23. }
  24. $payConfigs = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
  25. $this->success('获取成功', null, $payConfigs);
  26. }
  27. /**
  28. * 添加支付配置
  29. */
  30. public function add()
  31. {
  32. if (!$this->request->isAjax()) {
  33. return $this->view->fetch();
  34. }
  35. $params = $this->request->only([
  36. 'name', 'type', 'params', 'status'
  37. ]);
  38. $this->svalidate($params, ".add");
  39. $this->svalidate($params['params'], '.' . $params['type']); // 验证对应的支付参数是否设置完整
  40. $this->model->save($params);
  41. $this->success('保存成功');
  42. }
  43. /**
  44. * 支付配置详情
  45. *
  46. * @param $id
  47. */
  48. public function detail($id)
  49. {
  50. $payConfig = $this->model->where('id', $id)->find();
  51. if (!$payConfig) {
  52. $this->error(__('No Results were found'));
  53. }
  54. $payConfig->append(['params']);
  55. $this->success('获取成功', null, pay_config_show($payConfig));
  56. }
  57. /**
  58. * 修改支付配置
  59. */
  60. public function edit($id = null)
  61. {
  62. if (!$this->request->isAjax()) {
  63. return $this->view->fetch('add');
  64. }
  65. $params = $this->request->only([
  66. 'name', 'params', 'status'
  67. ]);
  68. $this->svalidate($params);
  69. $payConfig = $this->model->where('id', $id)->find();
  70. if (!$payConfig) {
  71. $this->error(__('No Results were found'));
  72. }
  73. if (isset($params['params'])) {
  74. $this->svalidate($params['params'], '.' . $payConfig['type']); // 验证对应的支付参数是否设置完整
  75. }
  76. $payConfig->save($params);
  77. $this->success('更新成功');
  78. }
  79. /**
  80. * 删除支付配置
  81. *
  82. * @param string $id 要删除的商品分类列表
  83. */
  84. public function delete($id)
  85. {
  86. if (empty($id)) {
  87. $this->error(__('Parameter %s can not be empty', 'id'));
  88. }
  89. $list = $this->model->where('id', 'in', $id)->select();
  90. $result = Db::transaction(function () use ($list) {
  91. $count = 0;
  92. foreach ($list as $item) {
  93. $count += $item->delete();
  94. }
  95. return $count;
  96. });
  97. if ($result) {
  98. $this->success('删除成功', null, $result);
  99. } else {
  100. $this->error(__('No rows were deleted'));
  101. }
  102. }
  103. /**
  104. * 获取所有支付配置
  105. */
  106. public function select()
  107. {
  108. $payConfigs = $this->model->sheepFilter()->normal()
  109. ->field('id, name, type,status')
  110. ->select();
  111. $this->success('获取成功', null, $payConfigs);
  112. }
  113. public function recyclebin()
  114. {
  115. if (!$this->request->isAjax()) {
  116. return $this->view->fetch();
  117. }
  118. $goods = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));
  119. $this->success('获取成功', null, $goods);
  120. }
  121. /**
  122. * 还原(支持批量)
  123. *
  124. * @param $id
  125. * @return \think\Response
  126. */
  127. public function restore($id = null)
  128. {
  129. if (empty($id)) {
  130. $this->error(__('Parameter %s can not be empty', 'id'));
  131. }
  132. $items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
  133. $result = Db::transaction(function () use ($items) {
  134. $count = 0;
  135. foreach ($items as $item) {
  136. $count += $item->restore();
  137. }
  138. return $count;
  139. });
  140. if ($result) {
  141. $this->success('还原成功', null, $result);
  142. } else {
  143. $this->error(__('No rows were updated'));
  144. }
  145. }
  146. /**
  147. * 销毁(支持批量)
  148. *
  149. * @param $id
  150. * @return \think\Response
  151. */
  152. public function destroy($id = null)
  153. {
  154. if (empty($id)) {
  155. $this->error(__('Parameter %s can not be empty', 'id'));
  156. }
  157. if ($id !== 'all') {
  158. $items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
  159. } else {
  160. $items = $this->model->onlyTrashed()->select();
  161. }
  162. $result = Db::transaction(function () use ($items) {
  163. $count = 0;
  164. foreach ($items as $config) {
  165. // 删除商品
  166. $count += $config->delete(true);
  167. }
  168. return $count;
  169. });
  170. if ($result) {
  171. $this->success('销毁成功', null, $result);
  172. }
  173. $this->error('销毁失败');
  174. }
  175. }