User.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\library\Auth;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-user
  9. */
  10. class User extends Backend
  11. {
  12. protected $relationSearch = true;
  13. protected $searchFields = 'u_id,username,nickname';
  14. /**
  15. * @var \app\admin\model\User
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('User');
  22. $typeList = [
  23. 'isCoolList' => $this->model->getIsCoolList(),
  24. 'isManagerList' => $this->model->getIsManagerList(),
  25. ];
  26. $this->view->assign($typeList);
  27. $this->assignconfig($typeList);
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. $this->relationSearch = true;
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $list = $this->model
  44. ->with(['noble','preuser','auth','age'])
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. foreach ($list as $k => $v) {
  49. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  50. $v->hidden(['password', 'salt']);
  51. $v->getRelation('age')->visible(['name']);
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $this->token();
  65. }
  66. return parent::add();
  67. }
  68. /**
  69. * 编辑
  70. */
  71. public function edit($ids = null)
  72. {
  73. if ($this->request->isPost()) {
  74. $this->token();
  75. }
  76. $row = $this->model->get($ids);
  77. $this->modelValidate = true;
  78. if (!$row) {
  79. $this->error(__('No Results were found'));
  80. }
  81. return parent::edit($ids);
  82. }
  83. /**
  84. * 删除
  85. */
  86. public function del($ids = "")
  87. {
  88. if (!$this->request->isPost()) {
  89. $this->error(__("Invalid parameters"));
  90. }
  91. $ids = $ids ? $ids : $this->request->post("ids");
  92. $row = $this->model->get($ids);
  93. $this->modelValidate = true;
  94. if (!$row) {
  95. $this->error(__('No Results were found'));
  96. }
  97. Auth::instance()->delete($row['id']);
  98. $this->success();
  99. }
  100. /**
  101. * 详情
  102. * @param null $ids
  103. * @return string
  104. * @throws \think\Exception
  105. * @throws \think\exception\DbException
  106. */
  107. public function detail($ids = null)
  108. {
  109. /* 判断数据是否存在*/
  110. $row = $this->model->get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. /* 判断是否有权限访问*/
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds)) {
  117. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  118. $this->error(__('You have no permission'));
  119. }
  120. }
  121. $this->view->assign("row", $row);
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 编辑
  126. */
  127. public function infoCheck($ids = null)
  128. {
  129. $row = $this->model->get($ids);
  130. if (!$row) {
  131. $this->error(__('No Results were found'));
  132. }
  133. $adminIds = $this->getDataLimitAdminIds();
  134. if (is_array($adminIds)) {
  135. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  136. $this->error(__('You have no permission'));
  137. }
  138. }
  139. if ($this->request->isPost()) {
  140. $params = $this->request->post("row/a");
  141. if (!$params) {
  142. $this->error(__('Parameter %s can not be empty', ''));
  143. }
  144. $params = $this->preExcludeFields($params);
  145. $result = false;
  146. try {
  147. //是否采用模型验证
  148. if ($this->modelValidate) {
  149. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  150. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  151. $row->validateFailException(true)->validate($validate);
  152. }
  153. $result = $row->allowField(true)->save($params);
  154. } catch (ValidateException|PDOException|Exception $e) {
  155. $this->error($e->getMessage());
  156. }
  157. if ($result == false) {
  158. $this->error(__('No rows were updated'));
  159. }
  160. $this->success();
  161. }
  162. $this->view->assign("row", $row);
  163. return $this->view->fetch();
  164. }
  165. }