User.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. use app\common\library\Token;
  7. /**
  8. * 会员管理
  9. *
  10. * @icon fa fa-user
  11. */
  12. class User extends Backend
  13. {
  14. protected $searchFields = 'id,username,nickname';
  15. protected $selectpageFields = 'id,username,nickname,mobile';
  16. /**
  17. * User模型对象
  18. * @var \app\admin\model\User
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\User;
  25. $this->view->assign("realStatusList", $this->model->getRealStatusList());
  26. $this->view->assign("genderList", $this->model->getGenderList());
  27. $this->view->assign("idcardStatusList", $this->model->getIdcardStatusList());
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. $this->view->assign("isKefuList", $this->model->getIsKefuList());
  30. $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
  31. $this->view->assign("isAgentList", $this->model->getIsAgentList());
  32. $this->view->assign("openMatchVideoList", $this->model->getOpenMatchVideoList());
  33. $this->view->assign("openMatchAudioList", $this->model->getOpenMatchAudioList());
  34. $this->view->assign("openMatchTypingList", $this->model->getOpenMatchTypingList());
  35. $this->view->assign("freeVideoList", $this->model->getFreeVideoList());
  36. $this->view->assign("freeAudioList", $this->model->getFreeAudioList());
  37. $this->view->assign("freeTypingList", $this->model->getFreeTypingList());
  38. $typeList = [
  39. 'statusList' => $this->model->getStatusList(),
  40. ];
  41. $this->view->assign($typeList);
  42. $this->assignconfig($typeList);
  43. }
  44. public function import()
  45. {
  46. parent::import();
  47. }
  48. /**
  49. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  50. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  51. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  52. */
  53. /**
  54. * 查看
  55. */
  56. public function index()
  57. {
  58. //当前是否为关联查询
  59. $this->relationSearch = true;
  60. //设置过滤方法
  61. $this->request->filter(['strip_tags', 'trim']);
  62. if ($this->request->isAjax()) {
  63. //如果发送的来源是Selectpage,则转发到Selectpage
  64. if ($this->request->request('keyField')) {
  65. return $this->selectpage();
  66. }
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $list = $this->model
  69. // ->with(['gonghui'])
  70. ->with(['wallet'])
  71. ->where($where)
  72. ->order($sort, $order)
  73. ->paginate($limit);
  74. $day = 1; // 一周的第一天
  75. $wday = date('w'); // 星期几的数字表示 0(周日)到 6(周六)
  76. $wday_start = strtotime(date('Y-m-d', strtotime('-' . ($wday ? $wday - $day : 6) . ' day'))); // 本周开始日期
  77. $week_map = [
  78. 'log_type' => ['in', [21, 22, 23, 54, 60, 72, 82 ]]
  79. ];
  80. $mt_user = Db::name('user');
  81. $mt_user_money_log = Db::name('user_money_log');
  82. $mt_pay_order = Db::name('pay_order');
  83. $mt_user_wallet = Db::name('user_wallet');
  84. $time = time();
  85. foreach ($list as $row) {
  86. // $row->getRelation('gonghui')->visible(['id','name']);
  87. $row->getRelation('wallet')->visible(['pay_money', 'get_money','vip_endtime']);
  88. $week_map['user_id'] = $row['id'];
  89. //邀请人数
  90. // $row['intro_total_num'] = $mt_user->where(['intro_uid' => $row['id']])->count('id');
  91. //收益统计
  92. // $row['total_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
  93. //充值统计
  94. // $row['pay_sum'] = $mt_pay_order->where(['user_id' => $row['id'], 'status' => 1])->sum('money');
  95. //本周收益
  96. $week_map['createtime'] = ['egt', $wday_start];
  97. // $row['week_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
  98. //上周收益
  99. $week_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
  100. // $row['last_week_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
  101. //本周邀请人数
  102. // $row['week_count'] = $mt_user->where(['intro_uid' => $row['id'], 'invite_time' => ['egt', $wday_start]])->count('id');
  103. //上周邀请人数
  104. // $row['last_week_count'] = $mt_user->where(['intro_uid' => $row['id'], 'invite_time' => ['between', [$wday_start - 86400 * 7, $wday_start]]])->count('id');
  105. //是否是会员
  106. // $vip_endtime = $mt_user_wallet->where(['user_id' => $row['id']])->value('vip_endtime');
  107. // if ($vip_endtime > $time) {
  108. // $row['vip_endtime'] = date('Y-m-d H:i:s', $vip_endtime);
  109. // } else {
  110. // $row['vip_endtime'] = '';
  111. // }
  112. }
  113. $result = array("total" => $list->total(), "rows" => $list->items());
  114. return json($result);
  115. }
  116. return $this->view->fetch();
  117. }
  118. /**
  119. * 编辑
  120. */
  121. public function edit($ids = null)
  122. {
  123. $row = $this->model->get($ids);
  124. if (!$row) {
  125. $this->error(__('No Results were found'));
  126. }
  127. $adminIds = $this->getDataLimitAdminIds();
  128. if (is_array($adminIds)) {
  129. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  130. $this->error(__('You have no permission'));
  131. }
  132. }
  133. if ($this->request->isPost()) {
  134. $params = $this->request->post("row/a");
  135. if ($params) {
  136. $params = $this->preExcludeFields($params);
  137. $result = false;
  138. if ($params['mobile']) {
  139. $exists = db('user')->where('mobile', $params['mobile'])->where('id', '<>', $row['id'])->find();
  140. if ($exists) {
  141. $this->error('手机号已经被其他人使用');
  142. }
  143. }
  144. //推荐人查询
  145. if (isset($params['intro_uid']) && $params['intro_uid'] != 0) {
  146. if ($params['intro_uid'] == $row['id']) {
  147. $this->error('推荐人ID不能填写自己');
  148. }
  149. $intro_user = db('user')->field('id, intro_uid')->where('id', $params['intro_uid'])->find();
  150. if(!$intro_user){
  151. $this->error('不存在推荐人');
  152. }
  153. if ($intro_user['intro_uid'] == $row['id']) {
  154. $this->error('不能填写下级邀请码作为推荐人');
  155. }
  156. }
  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. if ($params['status'] != 1 || (isset($params['frozentime']) && strtotime($params['frozentime']) > time())) {
  167. //置空token
  168. Token::clear($row['id']);
  169. }
  170. Db::commit();
  171. } catch (ValidateException $e) {
  172. Db::rollback();
  173. $this->error($e->getMessage());
  174. } catch (PDOException $e) {
  175. Db::rollback();
  176. $this->error($e->getMessage());
  177. } catch (Exception $e) {
  178. Db::rollback();
  179. $this->error($e->getMessage());
  180. }
  181. if ($result !== false) {
  182. $this->success();
  183. } else {
  184. $this->error(__('No rows were updated'));
  185. }
  186. }
  187. $this->error(__('Parameter %s can not be empty', ''));
  188. }
  189. $this->view->assign("row", $row);
  190. return $this->view->fetch();
  191. }
  192. }