User.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $this->relationSearch = true;
  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(['noble','preuser','auth','age'])
  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. $v->getRelation('age')->visible(['name']);
  46. }
  47. $result = array("total" => $list->total(), "rows" => $list->items());
  48. return json($result);
  49. }
  50. return $this->view->fetch();
  51. }
  52. /**
  53. * 添加
  54. */
  55. public function add()
  56. {
  57. if ($this->request->isPost()) {
  58. $this->token();
  59. }
  60. return parent::add();
  61. }
  62. /**
  63. * 编辑
  64. */
  65. public function edit($ids = null)
  66. {
  67. if ($this->request->isPost()) {
  68. $this->token();
  69. }
  70. $row = $this->model->get($ids);
  71. $this->modelValidate = true;
  72. if (!$row) {
  73. $this->error(__('No Results were found'));
  74. }
  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. /**
  95. * 详情
  96. * @param null $ids
  97. * @return string
  98. * @throws \think\Exception
  99. * @throws \think\exception\DbException
  100. */
  101. public function detail($ids = null)
  102. {
  103. /* 判断数据是否存在*/
  104. $row = $this->model->get($ids);
  105. if (!$row) {
  106. $this->error(__('No Results were found'));
  107. }
  108. /* 判断是否有权限访问*/
  109. $adminIds = $this->getDataLimitAdminIds();
  110. if (is_array($adminIds)) {
  111. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  112. $this->error(__('You have no permission'));
  113. }
  114. }
  115. $this->view->assign("row", $row);
  116. return $this->view->fetch();
  117. }
  118. }