Anchor.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 主播申请管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Anchor extends Backend
  14. {
  15. protected $searchFields = 'user.u_id,user.nickname';
  16. /**
  17. * Anchor模型对象
  18. * @var \app\admin\model\user\Anchor
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\user\Anchor;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //当前是否为关联查询
  42. $this->relationSearch = true;
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if ($this->request->isAjax()) {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('keyField')) {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $list = $this->model
  52. ->with(['user', 'useranchortype','partytype'])
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->getRelation('user')->visible(['u_id', 'nickname']);
  58. $row->getRelation('useranchortype')->visible(['name']);
  59. $row->getRelation('partytype')->visible(['name']);
  60. }
  61. $result = array("total" => $list->total(), "rows" => $list->items());
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 编辑
  68. */
  69. public function edit($ids = null)
  70. {
  71. $row = $this->model->get($ids);
  72. if (!$row) {
  73. $this->error(__('No Results were found'));
  74. }
  75. $adminIds = $this->getDataLimitAdminIds();
  76. if (is_array($adminIds)) {
  77. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  78. $this->error(__('You have no permission'));
  79. }
  80. }
  81. if ($this->request->isPost()) {
  82. $params = $this->request->post("row/a");
  83. if ($params) {
  84. $params = $this->preExcludeFields($params);
  85. $result = false;
  86. Db::startTrans();
  87. try {
  88. $data = [];
  89. switch ($params["status"]) {
  90. case 0:
  91. $data["is_anchor"] = 1;
  92. break;
  93. case 1:
  94. $data["is_anchor"] = 2;
  95. break;
  96. case 2:
  97. $data["is_anchor"] = -1;
  98. break;
  99. default:
  100. $data["is_anchor"] = 1;
  101. break;
  102. }
  103. \app\common\model\User::update($data, ["id" => $row["user_id"]]);
  104. // +message
  105. $params["status"] == 1 && \app\common\model\Message::addMessage($row["user_id"], "主播申请通知", "恭喜,您的主播申请审核已通过,快去开通直播间吧!");
  106. $params["status"] == 2 && \app\common\model\Message::addMessage($row["user_id"], "主播申请通知", "您的主播申请已被审核拒绝。");
  107. //是否采用模型验证
  108. if ($this->modelValidate) {
  109. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  110. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  111. $row->validateFailException(true)->validate($validate);
  112. }
  113. $result = $row->allowField(true)->save($params);
  114. Db::commit();
  115. } catch (ValidateException $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. } catch (PDOException $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. } catch (Exception $e) {
  122. Db::rollback();
  123. $this->error($e->getMessage());
  124. }
  125. if ($result !== false) {
  126. $this->success();
  127. } else {
  128. $this->error(__('No rows were updated'));
  129. }
  130. }
  131. $this->error(__('Parameter %s can not be empty', ''));
  132. }
  133. $this->view->assign("row", $row);
  134. return $this->view->fetch();
  135. }
  136. /**
  137. * 批量审核同意
  138. */
  139. public function batchApproval()
  140. {
  141. if ($this->request->isPost()) {
  142. $ids = $this->request->request('ids');
  143. $checkids = explode(',', $ids);
  144. if (count($checkids) > 0) {
  145. Db::startTrans();
  146. try {
  147. foreach ($checkids as $checkid) {
  148. $row = $this->model->get($checkid);
  149. $row->status = 1;
  150. $row->save();
  151. \app\common\model\User::update(['is_anchor' => 2], ["id" => $row["user_id"]]);
  152. \app\common\model\Message::addMessage($row["user_id"], "主播申请通知", "恭喜,您的主播申请审核已通过,快去开通直播间吧!");
  153. }
  154. Db::commit();
  155. $this->success();
  156. } catch (ValidateException $e) {
  157. Db::rollback();
  158. $this->error($e->getMessage());
  159. } catch (PDOException $e) {
  160. Db::rollback();
  161. $this->error($e->getMessage());
  162. } catch (Exception $e) {
  163. Db::rollback();
  164. $this->error($e->getMessage());
  165. }
  166. }
  167. $this->error(__('请选择需要批量操作的项', ''));
  168. }
  169. }
  170. /**
  171. * 批量审核拒绝
  172. */
  173. public function batchReject()
  174. {
  175. if ($this->request->isPost()) {
  176. $ids = $this->request->request('ids');
  177. $checkids = explode(',', $ids);
  178. if (count($checkids) > 0) {
  179. Db::startTrans();
  180. try {
  181. foreach ($checkids as $checkid) {
  182. $row = $this->model->get($checkid);
  183. $row->status = 2;
  184. $row->save();
  185. \app\common\model\User::update(['is_anchor' => -1], ["id" => $row["user_id"]]);
  186. }
  187. Db::commit();
  188. $this->success();
  189. } catch (ValidateException $e) {
  190. Db::rollback();
  191. $this->error($e->getMessage());
  192. } catch (PDOException $e) {
  193. Db::rollback();
  194. $this->error($e->getMessage());
  195. } catch (Exception $e) {
  196. Db::rollback();
  197. $this->error($e->getMessage());
  198. }
  199. }
  200. $this->error(__('请选择需要批量操作的项', ''));
  201. }
  202. }
  203. }