User.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = '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. $this->view->assign("typesList", $this->model->getTypesList());
  23. }
  24. /**
  25. * 查看
  26. */
  27. public function index()
  28. {
  29. //设置过滤方法
  30. $this->request->filter(['strip_tags', 'trim']);
  31. if ($this->request->isAjax()) {
  32. //如果发送的来源是Selectpage,则转发到Selectpage
  33. if ($this->request->request('keyField')) {
  34. return $this->selectpage();
  35. }
  36. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  37. // $filter = $this->request->get("filter", '');
  38. // $filter = (array)json_decode($filter, true);
  39. // $filter = $filter ? $filter : [];
  40. // $type = $filter['type'];
  41. // if ()
  42. $list = $this->model
  43. ->with('group')
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->paginate($limit);
  47. foreach ($list as $k => $v) {
  48. $type_arr = [];
  49. // 添加对应用户身份
  50. if ($v['is_author'] == 'correct') {
  51. $type_arr[] = '作者';
  52. }
  53. if ($v['is_review'] == 'correct') {
  54. $type_arr[] = '审稿人';
  55. }
  56. if ($v['is_editor'] == 'correct') {
  57. $type_arr[] = '编辑';
  58. }
  59. $v->type = implode(',',$type_arr);
  60. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  61. $v->hidden(['password', 'salt']);
  62. }
  63. $result = array("total" => $list->total(), "rows" => $list->items());
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 添加
  70. */
  71. public function add()
  72. {
  73. if ($this->request->isPost()) {
  74. $this->token();
  75. }
  76. return parent::add();
  77. }
  78. /**
  79. * 编辑
  80. */
  81. public function edit($ids = null)
  82. {
  83. if ($this->request->isPost()) {
  84. $this->token();
  85. }
  86. $row = $this->model->get($ids);
  87. $this->modelValidate = true;
  88. if (!$row) {
  89. $this->error(__('No Results were found'));
  90. }
  91. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  92. return parent::edit($ids);
  93. }
  94. /**
  95. * 删除
  96. */
  97. public function del($ids = "")
  98. {
  99. if (!$this->request->isPost()) {
  100. $this->error(__("Invalid parameters"));
  101. }
  102. $ids = $ids ? $ids : $this->request->post("ids");
  103. $row = $this->model->get($ids);
  104. $this->modelValidate = true;
  105. if (!$row) {
  106. $this->error(__('No Results were found'));
  107. }
  108. Auth::instance()->delete($row['id']);
  109. $this->success();
  110. }
  111. /**
  112. * 获取作者列表
  113. *
  114. * @return string|\think\response\Json
  115. * @throws \think\Exception
  116. * @throws \think\exception\DbException
  117. */
  118. public function getAuthorList()
  119. {
  120. //设置过滤方法
  121. $this->request->filter(['strip_tags', 'trim']);
  122. if ($this->request->isAjax()) {
  123. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  124. $list = $this->model
  125. ->with('group')
  126. ->where(['user.is_author' => 'correct'])
  127. ->where($where)
  128. ->order($sort, $order)
  129. ->paginate($limit);
  130. foreach ($list as $k => $v) {
  131. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  132. $v->hidden(['password', 'salt']);
  133. }
  134. $result = array("total" => $list->total(), "rows" => $list->items());
  135. return json($result);
  136. }
  137. return $this->view->fetch();
  138. }
  139. }