User.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\library\Auth;
  5. use think\response\Json;
  6. /**
  7. * 会员管理
  8. *
  9. * @icon fa fa-user
  10. */
  11. class User extends Backend
  12. {
  13. protected $relationSearch = true;
  14. protected $searchFields = 'id,username,nickname';
  15. /**
  16. * @var \app\admin\model\User
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('User');
  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. $data = $this->selectpage()->getData();
  35. $data['list'] = array_merge([['id'=>0,'nickname'=>'无','pid'=>0]],$data['list']);
  36. $data['total'] ++;
  37. return $data;
  38. }
  39. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  40. /**
  41. * 获取企业信息
  42. */
  43. $where1 = is_sys_admin();
  44. $list = $this->model
  45. ->with('group')
  46. ->where($where)->where($where1)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. foreach ($list as $k => $v) {
  50. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  51. $v->hidden(['password', 'salt']);
  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. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  82. return parent::edit($ids);
  83. }
  84. /**
  85. * 删除
  86. */
  87. public function del($ids = "")
  88. {
  89. if (!$this->request->isPost()) {
  90. $this->error(__("Invalid parameters"));
  91. }
  92. $ids = $ids ? $ids : $this->request->post("ids");
  93. $row = $this->model->get($ids);
  94. $this->modelValidate = true;
  95. if (!$row) {
  96. $this->error(__('No Results were found'));
  97. }
  98. Auth::instance()->delete($row['id']);
  99. $this->success();
  100. }
  101. }