User.php 7.9 KB

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