Anchor.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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'])
  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. }
  60. $result = array("total" => $list->total(), "rows" => $list->items());
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 编辑
  67. */
  68. public function edit($ids = null)
  69. {
  70. $row = $this->model->get($ids);
  71. if (!$row) {
  72. $this->error(__('No Results were found'));
  73. }
  74. $adminIds = $this->getDataLimitAdminIds();
  75. if (is_array($adminIds)) {
  76. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  77. $this->error(__('You have no permission'));
  78. }
  79. }
  80. if ($this->request->isPost()) {
  81. $params = $this->request->post("row/a");
  82. if ($params) {
  83. $params = $this->preExcludeFields($params);
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. $data = [];
  88. switch ($params["status"]) {
  89. case 0:
  90. $data["is_anchor"] = 1;
  91. break;
  92. case 1:
  93. $data["is_anchor"] = 2;
  94. break;
  95. case 2:
  96. $data["is_anchor"] = -1;
  97. break;
  98. default:
  99. $data["is_anchor"] = 1;
  100. break;
  101. }
  102. \app\common\model\User::update($data, ["id" => $row["user_id"]]);
  103. // +message
  104. $params["status"] == 1 && \app\common\model\Message::addMessage($row["user_id"], "主播申请通知", "恭喜,您的主播申请审核已通过,快去开通直播间吧!");
  105. $params["status"] == 2 && \app\common\model\Message::addMessage($row["user_id"], "主播申请通知", "您的主播申请已被审核拒绝。");
  106. //是否采用模型验证
  107. if ($this->modelValidate) {
  108. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  109. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  110. $row->validateFailException(true)->validate($validate);
  111. }
  112. $result = $row->allowField(true)->save($params);
  113. Db::commit();
  114. } catch (ValidateException $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. } catch (PDOException $e) {
  118. Db::rollback();
  119. $this->error($e->getMessage());
  120. } catch (Exception $e) {
  121. Db::rollback();
  122. $this->error($e->getMessage());
  123. }
  124. if ($result !== false) {
  125. $this->success();
  126. } else {
  127. $this->error(__('No rows were updated'));
  128. }
  129. }
  130. $this->error(__('Parameter %s can not be empty', ''));
  131. }
  132. $this->view->assign("row", $row);
  133. return $this->view->fetch();
  134. }
  135. /**
  136. * 批量审核同意
  137. */
  138. public function batchApproval()
  139. {
  140. if ($this->request->isPost()) {
  141. $ids = $this->request->request('ids');
  142. $checkids = explode(',', $ids);
  143. if (count($checkids) > 0) {
  144. Db::startTrans();
  145. try {
  146. foreach ($checkids as $checkid) {
  147. $row = $this->model->get($checkid);
  148. $row->status = 1;
  149. $row->save();
  150. \app\common\model\User::update(['is_anchor' => 2], ["id" => $row["user_id"]]);
  151. \app\common\model\Message::addMessage($row["user_id"], "主播申请通知", "恭喜,您的主播申请审核已通过,快去开通直播间吧!");
  152. }
  153. Db::commit();
  154. $this->success();
  155. } catch (ValidateException $e) {
  156. Db::rollback();
  157. $this->error($e->getMessage());
  158. } catch (PDOException $e) {
  159. Db::rollback();
  160. $this->error($e->getMessage());
  161. } catch (Exception $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. }
  165. }
  166. $this->error(__('请选择需要批量操作的项', ''));
  167. }
  168. }
  169. /**
  170. * 批量审核拒绝
  171. */
  172. public function batchReject()
  173. {
  174. if ($this->request->isPost()) {
  175. $ids = $this->request->request('ids');
  176. $checkids = explode(',', $ids);
  177. if (count($checkids) > 0) {
  178. Db::startTrans();
  179. try {
  180. foreach ($checkids as $checkid) {
  181. $row = $this->model->get($checkid);
  182. $row->status = 2;
  183. $row->save();
  184. \app\common\model\User::update(['is_anchor' => -1], ["id" => $row["user_id"]]);
  185. }
  186. Db::commit();
  187. $this->success();
  188. } catch (ValidateException $e) {
  189. Db::rollback();
  190. $this->error($e->getMessage());
  191. } catch (PDOException $e) {
  192. Db::rollback();
  193. $this->error($e->getMessage());
  194. } catch (Exception $e) {
  195. Db::rollback();
  196. $this->error($e->getMessage());
  197. }
  198. }
  199. $this->error(__('请选择需要批量操作的项', ''));
  200. }
  201. }
  202. }