User.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. $user_info = Db::name('user_info');
  59. $list2 = collection($list->items())->toArray();
  60. foreach ($list2 as $k => &$row) {
  61. $asktime_info = $user_info->where(['user_id' => $row['id']])->find();
  62. $asktime = explode(',', $asktime_info['asktime']);
  63. if ($asktime_info) {
  64. if ($asktime && time() - $asktime[count($asktime) - 1] <= 3600) {
  65. $online = 1; //0离线 1在线
  66. } else {
  67. $online = 0; //0离线 1在线
  68. }
  69. if (count($asktime) >= 20 && $asktime[count($asktime) - 1] - $asktime[0] <= 86400) {
  70. $active = 1; //活跃
  71. } else {
  72. $active = 0; //不活跃
  73. }
  74. } else {
  75. $online = 0;
  76. $active = 0;
  77. }
  78. $list2[$k]['online'] = $online;
  79. $list2[$k]['active'] = $active;
  80. }
  81. $result = array("total" => $list->total(), "rows" => $list2);
  82. return json($result);
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 添加
  88. */
  89. public function add()
  90. {
  91. if ($this->request->isPost()) {
  92. $params = $this->request->post("row/a");
  93. if ($params) {
  94. $params = $this->preExcludeFields($params);
  95. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  96. $params[$this->dataLimitField] = $this->auth->id;
  97. }
  98. $result = false;
  99. Db::startTrans();
  100. try {
  101. //是否采用模型验证
  102. if ($this->modelValidate) {
  103. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  104. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  105. $this->model->validateFailException(true)->validate($validate);
  106. }
  107. $result = $this->model->allowField(true)->save($params);
  108. delUserInfo($params['id']);
  109. Db::commit();
  110. } catch (ValidateException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (PDOException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if ($result !== false) {
  121. $this->success();
  122. } else {
  123. $this->error(__('No rows were inserted'));
  124. }
  125. }
  126. $this->error(__('Parameter %s can not be empty', ''));
  127. }
  128. return $this->view->fetch();
  129. }
  130. /**
  131. * 编辑
  132. */
  133. public function edit($ids = null)
  134. {
  135. $row = $this->model->get($ids);
  136. if (!$row) {
  137. $this->error(__('No Results were found'));
  138. }
  139. $adminIds = $this->getDataLimitAdminIds();
  140. if (is_array($adminIds)) {
  141. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  142. $this->error(__('You have no permission'));
  143. }
  144. }
  145. if ($this->request->isPost()) {
  146. $params = $this->request->post("row/a");
  147. if ($params) {
  148. $params = $this->preExcludeFields($params);
  149. $result = false;
  150. Db::startTrans();
  151. try {
  152. //是否采用模型验证
  153. if ($this->modelValidate) {
  154. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  155. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  156. $row->validateFailException(true)->validate($validate);
  157. }
  158. $result = $row->allowField(true)->save($params);
  159. delUserInfo($row['id']);
  160. Db::commit();
  161. } catch (ValidateException $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. } catch (PDOException $e) {
  165. Db::rollback();
  166. $this->error($e->getMessage());
  167. } catch (Exception $e) {
  168. Db::rollback();
  169. $this->error($e->getMessage());
  170. }
  171. if ($result !== false) {
  172. $this->success();
  173. } else {
  174. $this->error(__('No rows were updated'));
  175. }
  176. }
  177. $this->error(__('Parameter %s can not be empty', ''));
  178. }
  179. $this->view->assign("row", $row);
  180. return $this->view->fetch();
  181. }
  182. }