Guest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\UserGroup;
  4. use app\common\controller\Backend;
  5. use app\common\library\Auth;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 会员管理
  11. *
  12. * @icon fa fa-user
  13. */
  14. class Guest extends Backend
  15. {
  16. protected $relationSearch = true;
  17. protected $searchFields = 'id,username,nickname';
  18. protected $noNeedLogin=['levels'];
  19. /**
  20. * @var \app\admin\model\User
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->assign('levels',\app\common\model\User::$levels);
  27. $this->model = model('User');
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $list = $this->model
  43. ->with('group')
  44. ->where('user.level',\app\common\model\User::LEVEL_0)
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. foreach ($list as $k => $v) {
  49. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  50. $v->hidden(['password', 'salt']);
  51. $v->appendArea();
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $this->token();
  65. }
  66. return parent::add();
  67. }
  68. /**
  69. * 编辑
  70. */
  71. public function edit($ids = null)
  72. {
  73. if ($this->request->isPost()) {
  74. $this->token();
  75. }
  76. $row = $this->model->get($ids);
  77. $this->modelValidate = true;
  78. if (!$row) {
  79. $this->error(__('No Results were found'));
  80. }
  81. $this->view->assign('groupList', build_select('row[group_id]', UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  82. if ($this->request->isPost()) {
  83. $params = $this->request->post("row/a");
  84. if ($params) {
  85. $params = $this->preExcludeFields($params);
  86. if(!empty($params['level_close_at'])){
  87. $params['level_close_at']=strtotime($params['level_close_at']);
  88. }
  89. $result = false;
  90. Db::startTrans();
  91. try {
  92. //是否采用模型验证
  93. if ($this->modelValidate) {
  94. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  95. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  96. $row->validateFailException(true)->validate($validate);
  97. }
  98. $result = $row->allowField(true)->save($params);
  99. Db::commit();
  100. } catch (ValidateException $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. } catch (PDOException $e) {
  104. Db::rollback();
  105. $this->error($e->getMessage());
  106. } catch (\Exception $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. }
  110. if ($result !== false) {
  111. $this->success();
  112. } else {
  113. $this->error(__('No rows were updated'));
  114. }
  115. }
  116. $this->error(__('Parameter %s can not be empty', ''));
  117. }else{
  118. $this->view->assign("row", $row);
  119. return view();
  120. }
  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. public function levels(){
  140. return \app\common\model\User::$levels;
  141. }
  142. }