Apply.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\controller\commission;
  3. use app\common\controller\Backend;
  4. use app\common\model\commission\Apply as ApplyModel;
  5. use app\common\model\commission\Identity as IdentityModel;
  6. use app\common\Service\Commission\AgentApply as AgentApplyService;
  7. use think\Db;
  8. use think\Exception;
  9. class Apply extends Backend
  10. {
  11. protected $model = null;
  12. protected $searchFields = 'id,user.nickname,real_name,company_name,status,agent_type,identity.name';
  13. protected $relationSearch = true;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new ApplyModel();
  18. }
  19. /**
  20. * 查看
  21. */
  22. public function index()
  23. {
  24. if (!$this->request->isAjax()) {
  25. return $this->view->fetch();
  26. }
  27. if ($this->request->request('keyField')) {
  28. return $this->selectpage();
  29. }
  30. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  31. $list = $this->model
  32. ->with(['user', 'identity', 'province', 'city', 'district', 'admin'])
  33. ->where($where)
  34. ->order($sort, $order)
  35. ->paginate($limit);
  36. $result = ['total' => $list->total(), 'rows' => $list->items()];
  37. return json($result);
  38. }
  39. /**
  40. * 详情
  41. */
  42. public function detail($ids = null)
  43. {
  44. $row = $this->model::with(['user', 'identity', 'province', 'city', 'district', 'admin'])
  45. ->where('id', $ids)
  46. ->find();
  47. if (!$row) {
  48. $this->error(__('No Results were found'));
  49. }
  50. $this->view->assign('row', $row);
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 审核通过
  55. */
  56. public function approve($ids = null)
  57. {
  58. if (!$this->request->isPost()) {
  59. $this->error(__('Invalid parameters'));
  60. }
  61. $row = $this->model->get($ids);
  62. if (!$row) {
  63. $this->error(__('No Results were found'));
  64. }
  65. if ($row->status != ApplyModel::STATUS_PENDING) {
  66. $this->error('该申请已经处理过了');
  67. }
  68. Db::transaction(function () use ($row) {
  69. $service = new AgentApplyService();
  70. $service->approveApply($row, $this->auth->id);
  71. });
  72. $this->success('审核通过成功');
  73. }
  74. /**
  75. * 审核拒绝
  76. */
  77. public function reject($ids = null)
  78. {
  79. if (!$this->request->isPost()) {
  80. $this->error(__('Invalid parameters'));
  81. }
  82. $row = $this->model->get($ids);
  83. if (!$row) {
  84. $this->error(__('No Results were found'));
  85. }
  86. if ($row->status != ApplyModel::STATUS_PENDING) {
  87. $this->error('该申请已经处理过了');
  88. }
  89. $reason = $this->request->post('reason', '');
  90. if (empty($reason)) {
  91. $this->error('请填写拒绝原因');
  92. }
  93. $service = new AgentApplyService();
  94. $service->rejectApply($row, $reason, $this->auth->id);
  95. $this->success('审核拒绝成功');
  96. }
  97. /**
  98. * 删除
  99. */
  100. public function del($ids = null)
  101. {
  102. if (!$this->request->isPost()) {
  103. $this->error(__('Invalid parameters'));
  104. }
  105. if (!$ids) {
  106. $this->error(__('Parameter %s can not be empty', 'ids'));
  107. }
  108. $ids = explode(',', $ids);
  109. $deleteCount = 0;
  110. Db::transaction(function () use ($ids, &$deleteCount) {
  111. foreach ($ids as $id) {
  112. $row = $this->model->get($id);
  113. if ($row) {
  114. // 只允许删除待审核和已拒绝的申请
  115. //if (in_array($row->status, [ApplyModel::STATUS_PENDING, ApplyModel::STATUS_REJECTED])) {
  116. $row->delete();
  117. $deleteCount++;
  118. //}
  119. }
  120. }
  121. });
  122. if ($deleteCount > 0) {
  123. $this->success("成功删除 {$deleteCount} 条申请记录");
  124. } else {
  125. $this->error('没有可删除的记录(只能删除待审核和已拒绝的申请)');
  126. }
  127. }
  128. /**
  129. * 批量审核通过
  130. */
  131. public function multi($ids = null)
  132. {
  133. if (!$this->request->isPost()) {
  134. $this->error(__('Invalid parameters'));
  135. }
  136. $action = $this->request->post('action');
  137. if ($action == 'approve') {
  138. // 批量审核通过
  139. if (!$ids) {
  140. $this->error(__('Parameter %s can not be empty', 'ids'));
  141. }
  142. $ids = explode(',', $ids);
  143. $service = new AgentApplyService();
  144. Db::transaction(function () use ($ids, $service) {
  145. foreach ($ids as $id) {
  146. $row = $this->model->get($id);
  147. if ($row && $row->status == ApplyModel::STATUS_PENDING) {
  148. $service->approveApply($row, $this->auth->id);
  149. }
  150. }
  151. });
  152. $this->success('批量审核成功');
  153. } elseif ($action == 'del') {
  154. // 批量删除
  155. return $this->del($ids);
  156. } else {
  157. return parent::multi($ids);
  158. }
  159. }
  160. }