User.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. protected $selectpageFields = 'id,username,nickname,mobile';
  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. return $this->selectpage();
  35. }
  36. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  37. $list = $this->model
  38. ->with('group')
  39. ->where($where)
  40. ->order($sort, $order)
  41. ->paginate($limit);
  42. foreach ($list as $k => $v) {
  43. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  44. $v->hidden(['password', 'salt']);
  45. }
  46. $result = array("total" => $list->total(), "rows" => $list->items());
  47. return json($result);
  48. }
  49. return $this->view->fetch();
  50. }
  51. /**
  52. * 添加
  53. */
  54. public function add()
  55. {
  56. if ($this->request->isPost()) {
  57. $this->token();
  58. }
  59. return parent::add();
  60. }
  61. /**
  62. * 编辑
  63. */
  64. public function edit($ids = null)
  65. {
  66. if ($this->request->isPost()) {
  67. $this->token();
  68. }
  69. $row = $this->model->get($ids);
  70. $this->modelValidate = true;
  71. if (!$row) {
  72. $this->error(__('No Results were found'));
  73. }
  74. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  75. return parent::edit($ids);
  76. }
  77. /**
  78. * 删除
  79. */
  80. public function del($ids = "")
  81. {
  82. if (!$this->request->isPost()) {
  83. $this->error(__("Invalid parameters"));
  84. }
  85. $ids = $ids ? $ids : $this->request->post("ids");
  86. $row = $this->model->get($ids);
  87. $this->modelValidate = true;
  88. if (!$row) {
  89. $this->error(__('No Results were found'));
  90. }
  91. Auth::instance()->delete($row['id']);
  92. $this->success();
  93. }
  94. }