User.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\library\Auth;
  5. use think\Db;
  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. 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 $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. $row = $this->model->get($ids);
  77. if (!$row) {
  78. $this->error(__('No Results were found'));
  79. }
  80. $adminIds = $this->getDataLimitAdminIds();
  81. if (is_array($adminIds)) {
  82. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  83. $this->error(__('You have no permission'));
  84. }
  85. }
  86. if ($this->request->isPost()) {
  87. $params = $this->request->post("row/a");
  88. if ($params) {
  89. $params = $this->preExcludeFields($params);
  90. $result = false;
  91. Db::startTrans();
  92. try {
  93. //是否采用模型验证
  94. if ($this->modelValidate) {
  95. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  96. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  97. $row->validateFailException(true)->validate($validate);
  98. }
  99. $result = $row->allowField(true)->save($params);
  100. Db::commit();
  101. } catch (ValidateException $e) {
  102. Db::rollback();
  103. $this->error($e->getMessage());
  104. } catch (PDOException $e) {
  105. Db::rollback();
  106. $this->error($e->getMessage());
  107. } catch (Exception $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. }
  111. if ($result !== false) {
  112. $this->success();
  113. } else {
  114. $this->error(__('No rows were updated'));
  115. }
  116. }
  117. $this->error(__('Parameter %s can not be empty', ''));
  118. }
  119. $this->view->assign("row", $row);
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 删除
  124. */
  125. public function del($ids = "")
  126. {
  127. if (!$this->request->isPost()) {
  128. $this->error(__("Invalid parameters"));
  129. }
  130. $ids = $ids ? $ids : $this->request->post("ids");
  131. $row = $this->model->get($ids);
  132. $this->modelValidate = true;
  133. if (!$row) {
  134. $this->error(__('No Results were found'));
  135. }
  136. Auth::instance()->delete($row['id']);
  137. $this->success();
  138. }
  139. }