User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  23. /**
  24. * 查看
  25. */
  26. public function index()
  27. {
  28. //设置过滤方法
  29. $this->request->filter(['strip_tags', 'trim']);
  30. if ($this->request->isAjax()) {
  31. //如果发送的来源是Selectpage,则转发到Selectpage
  32. if ($this->request->request('keyField')) {
  33. return $this->selectpage();
  34. }
  35. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  36. $list = $this->model
  37. ->with(['noble','preuser','auth'])
  38. ->where($where)
  39. ->order($sort, $order)
  40. ->paginate($limit);
  41. foreach ($list as $k => $v) {
  42. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  43. $v->hidden(['password', 'salt']);
  44. }
  45. $result = array("total" => $list->total(), "rows" => $list->items());
  46. return json($result);
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 添加
  52. */
  53. public function add()
  54. {
  55. if ($this->request->isPost()) {
  56. $this->token();
  57. }
  58. return parent::add();
  59. }
  60. /**
  61. * 编辑
  62. */
  63. public function edit($ids = null)
  64. {
  65. if ($this->request->isPost()) {
  66. $this->token();
  67. }
  68. $row = $this->model->get($ids);
  69. $this->modelValidate = true;
  70. if (!$row) {
  71. $this->error(__('No Results were found'));
  72. }
  73. return parent::edit($ids);
  74. }
  75. /**
  76. * 删除
  77. */
  78. public function del($ids = "")
  79. {
  80. if (!$this->request->isPost()) {
  81. $this->error(__("Invalid parameters"));
  82. }
  83. $ids = $ids ? $ids : $this->request->post("ids");
  84. $row = $this->model->get($ids);
  85. $this->modelValidate = true;
  86. if (!$row) {
  87. $this->error(__('No Results were found'));
  88. }
  89. Auth::instance()->delete($row['id']);
  90. $this->success();
  91. }
  92. /**
  93. * 详情
  94. * @param null $ids
  95. * @return string
  96. * @throws \think\Exception
  97. * @throws \think\exception\DbException
  98. */
  99. public function detail($ids = null)
  100. {
  101. /* 判断数据是否存在*/
  102. $row = $this->model->get($ids);
  103. if (!$row) {
  104. $this->error(__('No Results were found'));
  105. }
  106. /* 判断是否有权限访问*/
  107. $adminIds = $this->getDataLimitAdminIds();
  108. if (is_array($adminIds)) {
  109. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  110. $this->error(__('You have no permission'));
  111. }
  112. }
  113. $this->view->assign("row", $row);
  114. return $this->view->fetch();
  115. }
  116. }