User.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. 'isCostList' => $this->model->getIsCostList(),
  40. 'statusList' => $this->model->getStatusList(),
  41. ];
  42. $this->view->assign($typeList);
  43. $this->assignconfig($typeList);
  44. }
  45. public function import()
  46. {
  47. parent::import();
  48. }
  49. /**
  50. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  51. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  52. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  53. */
  54. /**
  55. * 查看
  56. */
  57. public function index()
  58. {
  59. //当前是否为关联查询
  60. $this->relationSearch = true;
  61. //设置过滤方法
  62. $this->request->filter(['strip_tags', 'trim']);
  63. if ($this->request->isAjax()) {
  64. //如果发送的来源是Selectpage,则转发到Selectpage
  65. if ($this->request->request('keyField')) {
  66. return $this->selectpage();
  67. }
  68. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  69. $list = $this->model
  70. // ->with(['gonghui'])
  71. ->with(['wallet'])
  72. ->where($where)
  73. ->order($sort, $order)
  74. ->paginate($limit);
  75. $day = 1; // 一周的第一天
  76. $wday = date('w'); // 星期几的数字表示 0(周日)到 6(周六)
  77. $wday_start = strtotime(date('Y-m-d', strtotime('-' . ($wday ? $wday - $day : 6) . ' day'))); // 本周开始日期
  78. $week_map = [
  79. 'log_type' => ['in', [21, 22, 23, 54, 58, 60, 82 ]]
  80. ];
  81. $mt_user = Db::name('user');
  82. $mt_user_money_log = Db::name('user_money_log');
  83. $mt_pay_order = Db::name('pay_order');
  84. $mt_user_wallet = Db::name('user_wallet');
  85. $time = time();
  86. foreach ($list as $row) {
  87. // $row->getRelation('gonghui')->visible(['id','name']);
  88. $row->getRelation('wallet')->visible(['pay_money', 'get_money','vip_endtime']);
  89. $week_map['user_id'] = $row['id'];
  90. //邀请人数
  91. // $row['intro_total_num'] = $mt_user->where(['intro_uid' => $row['id']])->count('id');
  92. //收益统计
  93. // $row['total_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
  94. //充值统计
  95. // $row['pay_sum'] = $mt_pay_order->where(['user_id' => $row['id'], 'status' => 1])->sum('money');
  96. //本周收益
  97. $week_map['createtime'] = ['egt', $wday_start];
  98. // $row['week_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
  99. //上周收益
  100. $week_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
  101. // $row['last_week_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
  102. //本周邀请人数
  103. // $row['week_count'] = $mt_user->where(['intro_uid' => $row['id'], 'invite_time' => ['egt', $wday_start]])->count('id');
  104. //上周邀请人数
  105. // $row['last_week_count'] = $mt_user->where(['intro_uid' => $row['id'], 'invite_time' => ['between', [$wday_start - 86400 * 7, $wday_start]]])->count('id');
  106. //是否是会员
  107. // $vip_endtime = $mt_user_wallet->where(['user_id' => $row['id']])->value('vip_endtime');
  108. // if ($vip_endtime > $time) {
  109. // $row['vip_endtime'] = date('Y-m-d H:i:s', $vip_endtime);
  110. // } else {
  111. // $row['vip_endtime'] = '';
  112. // }
  113. }
  114. $result = array("total" => $list->total(), "rows" => $list->items());
  115. return json($result);
  116. }
  117. return $this->view->fetch();
  118. }
  119. /**
  120. * 编辑
  121. */
  122. public function edit($ids = null)
  123. {
  124. $row = $this->model->get($ids);
  125. if (!$row) {
  126. $this->error(__('No Results were found'));
  127. }
  128. $adminIds = $this->getDataLimitAdminIds();
  129. if (is_array($adminIds)) {
  130. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  131. $this->error(__('You have no permission'));
  132. }
  133. }
  134. if ($this->request->isPost()) {
  135. $params = $this->request->post("row/a");
  136. if ($params) {
  137. $params = $this->preExcludeFields($params);
  138. $result = false;
  139. if ($params['mobile']) {
  140. $exists = db('user')->where('mobile', $params['mobile'])->where('id', '<>', $row['id'])->find();
  141. if ($exists) {
  142. $this->error('手机号已经被其他人使用');
  143. }
  144. }
  145. //推荐人查询
  146. if (isset($params['intro_uid']) && $params['intro_uid'] != 0) {
  147. if ($params['intro_uid'] == $row['id']) {
  148. $this->error('推荐人ID不能填写自己');
  149. }
  150. $intro_user = db('user')->field('id, intro_uid')->where('id', $params['intro_uid'])->find();
  151. if(!$intro_user){
  152. $this->error('不存在推荐人');
  153. }
  154. if ($intro_user['intro_uid'] == $row['id']) {
  155. $this->error('不能填写下级邀请码作为推荐人');
  156. }
  157. }
  158. Db::startTrans();
  159. if (isset($params['vip_endtime'])) { //会员到期时间
  160. $rs = Db::name('user_wallet')->where(['user_id' => $ids])->setField('vip_endtime', strtotime($params['vip_endtime']));
  161. if ($rs === false) {
  162. Db::rollback();
  163. $this->error('会员到期时间有错误');
  164. }
  165. unset($params['vip_endtime']);
  166. }
  167. try {
  168. //是否采用模型验证
  169. if ($this->modelValidate) {
  170. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  171. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  172. $row->validateFailException(true)->validate($validate);
  173. }
  174. $result = $row->allowField(true)->save($params);
  175. if ($params['status'] != 1 || (isset($params['frozentime']) && strtotime($params['frozentime']) > time())) {
  176. //置空token
  177. Token::clear($row['id']);
  178. }
  179. Db::commit();
  180. } catch (ValidateException $e) {
  181. Db::rollback();
  182. $this->error($e->getMessage());
  183. } catch (PDOException $e) {
  184. Db::rollback();
  185. $this->error($e->getMessage());
  186. } catch (Exception $e) {
  187. Db::rollback();
  188. $this->error($e->getMessage());
  189. }
  190. if ($result !== false) {
  191. $this->success();
  192. } else {
  193. $this->error(__('No rows were updated'));
  194. }
  195. }
  196. $this->error(__('Parameter %s can not be empty', ''));
  197. }
  198. $row['vip_endtime'] = Db::name('user_wallet')->where(['user_id' => $ids])->value('vip_endtime');
  199. $this->view->assign("row", $row);
  200. return $this->view->fetch();
  201. }
  202. }