User.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-user
  9. */
  10. class User extends Backend
  11. {
  12. /**
  13. * User模型对象
  14. * @var \app\admin\model\user\User
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\user\User;
  21. $this->view->assign("genderList", $this->model->getGenderList());
  22. $this->view->assign("isAuthList", $this->model->getIsAuthList());
  23. $this->view->assign("isGoddessList", $this->model->getIsGoddessList());
  24. $this->view->assign("rechargeAuthList", $this->model->getRechargeAuthList());
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //当前是否为关联查询
  42. $this->relationSearch = false;
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if ($this->request->isAjax()) {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('keyField')) {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $list = $this->model
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->visible(['id','nickname','mobile','avatar','gender','birthday','age','province_name','city_name','district_name','money','frozen','logintime','createtime','status','constellation','profession','wechat','declaration','is_auth','is_goddess','vip_duetime','income','recharge_auth','invite_no','pre_user_id']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $params = $this->request->post("row/a");
  70. if ($params) {
  71. $params = $this->preExcludeFields($params);
  72. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  73. $params[$this->dataLimitField] = $this->auth->id;
  74. }
  75. $result = false;
  76. Db::startTrans();
  77. try {
  78. //是否采用模型验证
  79. if ($this->modelValidate) {
  80. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  81. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  82. $this->model->validateFailException(true)->validate($validate);
  83. }
  84. $result = $this->model->allowField(true)->save($params);
  85. delUserInfo($params['id']);
  86. Db::commit();
  87. } catch (ValidateException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (PDOException $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. } catch (Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result !== false) {
  98. $this->success();
  99. } else {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. return $this->view->fetch();
  106. }
  107. /**
  108. * 编辑
  109. */
  110. public function edit($ids = null)
  111. {
  112. $row = $this->model->get($ids);
  113. if (!$row) {
  114. $this->error(__('No Results were found'));
  115. }
  116. $adminIds = $this->getDataLimitAdminIds();
  117. if (is_array($adminIds)) {
  118. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  119. $this->error(__('You have no permission'));
  120. }
  121. }
  122. if ($this->request->isPost()) {
  123. $params = $this->request->post("row/a");
  124. if ($params) {
  125. $params = $this->preExcludeFields($params);
  126. $result = false;
  127. Db::startTrans();
  128. try {
  129. //是否采用模型验证
  130. if ($this->modelValidate) {
  131. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  132. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  133. $row->validateFailException(true)->validate($validate);
  134. }
  135. $result = $row->allowField(true)->save($params);
  136. delUserInfo($row['id']);
  137. Db::commit();
  138. } catch (ValidateException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (PDOException $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. } catch (Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. if ($result !== false) {
  149. $this->success();
  150. } else {
  151. $this->error(__('No rows were updated'));
  152. }
  153. }
  154. $this->error(__('Parameter %s can not be empty', ''));
  155. }
  156. $this->view->assign("row", $row);
  157. return $this->view->fetch();
  158. }
  159. }