User.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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', 'provincearea', 'cityarea', 'area', 'rank'])
  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. $v->getRelation('provincearea')->visible(['name']);
  46. $v->getRelation('cityarea')->visible(['name']);
  47. $v->getRelation('area')->visible(['name']);
  48. $v->getRelation('rank')->visible(['name']);
  49. }
  50. $result = array("total" => $list->total(), "rows" => $list->items());
  51. return json($result);
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 添加
  57. */
  58. public function add()
  59. {
  60. if ($this->request->isPost()) {
  61. $this->token();
  62. }
  63. return parent::add();
  64. }
  65. /**
  66. * 编辑
  67. */
  68. public function edit($ids = null)
  69. {
  70. /*if ($this->request->isPost()) {
  71. $this->token();
  72. }
  73. $row = $this->model->get($ids);
  74. $this->modelValidate = true;
  75. if (!$row) {
  76. $this->error(__('No Results were found'));
  77. }
  78. // $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  79. return parent::edit($ids);*/
  80. $row = $this->model->get($ids);
  81. if (!$row) {
  82. $this->error(__('No Results were found'));
  83. }
  84. $adminIds = $this->getDataLimitAdminIds();
  85. if (is_array($adminIds)) {
  86. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  87. $this->error(__('You have no permission'));
  88. }
  89. }
  90. if ($this->request->isPost()) {
  91. $params = $this->request->post("row/a");
  92. if ($params) {
  93. $params = $this->preExcludeFields($params);
  94. $result = false;
  95. $params['province_id'] = $params['province'];
  96. $params['city_id'] = $params['city'];
  97. $params['area_id'] = $params['area'];
  98. Db::startTrans();
  99. try {
  100. //是否采用模型验证
  101. if ($this->modelValidate) {
  102. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  103. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  104. $row->validateFailException(true)->validate($validate);
  105. }
  106. $result = $row->allowField(true)->save($params);
  107. Db::commit();
  108. } catch (ValidateException $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. } catch (PDOException $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. } catch (Exception $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. }
  118. if ($result !== false) {
  119. $this->success();
  120. } else {
  121. $this->error(__('No rows were updated'));
  122. }
  123. }
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. $row['province_name'] = Db::name('area')->where(['id' => $row['province_id']])->value('name');
  127. $row['city_name'] = Db::name('area')->where(['id' => $row['city_id']])->value('name');
  128. $row['area_name'] = Db::name('area')->where(['id' => $row['area_id']])->value('name');
  129. $this->view->assign("row", $row);
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * 删除
  134. */
  135. public function del($ids = "")
  136. {
  137. if (!$this->request->isPost()) {
  138. $this->error(__("Invalid parameters"));
  139. }
  140. $ids = $ids ? $ids : $this->request->post("ids");
  141. $row = $this->model->get($ids);
  142. $this->modelValidate = true;
  143. if (!$row) {
  144. $this->error(__('No Results were found'));
  145. }
  146. Auth::instance()->delete($row['id']);
  147. $this->success();
  148. }
  149. }