123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace app\admin\controller\user;
- use app\common\controller\Backend;
- use app\common\library\Auth;
- use Think\Db;
- use app\common\library\Token;
- /**
- * 会员管理
- *
- * @icon fa fa-user
- */
- class User extends Backend
- {
-
- protected $searchFields = 'id,username,nickname';
- protected $selectpageFields = 'id,username,nickname,mobile';
- /**
- * User模型对象
- * @var \app\admin\model\User
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\User;
- $this->view->assign("realStatusList", $this->model->getRealStatusList());
- $this->view->assign("genderList", $this->model->getGenderList());
- $this->view->assign("idcardStatusList", $this->model->getIdcardStatusList());
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("isKefuList", $this->model->getIsKefuList());
- $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
- $this->view->assign("isAgentList", $this->model->getIsAgentList());
- $this->view->assign("openMatchVideoList", $this->model->getOpenMatchVideoList());
- $this->view->assign("openMatchAudioList", $this->model->getOpenMatchAudioList());
- $this->view->assign("openMatchTypingList", $this->model->getOpenMatchTypingList());
- $this->view->assign("freeVideoList", $this->model->getFreeVideoList());
- $this->view->assign("freeAudioList", $this->model->getFreeAudioList());
- $this->view->assign("freeTypingList", $this->model->getFreeTypingList());
- $typeList = [
- 'isCostList' => $this->model->getIsCostList(),
- 'statusList' => $this->model->getStatusList(),
- ];
- $this->view->assign($typeList);
- $this->assignconfig($typeList);
- }
- public function import()
- {
- parent::import();
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- // ->with(['gonghui'])
- ->with(['wallet'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- $day = 1; // 一周的第一天
- $wday = date('w'); // 星期几的数字表示 0(周日)到 6(周六)
- $wday_start = strtotime(date('Y-m-d', strtotime('-' . ($wday ? $wday - $day : 6) . ' day'))); // 本周开始日期
- $week_map = [
- 'log_type' => ['in', [21, 22, 23, 54, 58, 60, 82 ]]
- ];
- $mt_user = Db::name('user');
- $mt_user_money_log = Db::name('user_money_log');
- $mt_pay_order = Db::name('pay_order');
- $mt_user_wallet = Db::name('user_wallet');
- $time = time();
- foreach ($list as $row) {
- // $row->getRelation('gonghui')->visible(['id','name']);
- $row->getRelation('wallet')->visible(['pay_money', 'get_money','vip_endtime']);
- $week_map['user_id'] = $row['id'];
- //邀请人数
- // $row['intro_total_num'] = $mt_user->where(['intro_uid' => $row['id']])->count('id');
- //收益统计
- // $row['total_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
- //充值统计
- // $row['pay_sum'] = $mt_pay_order->where(['user_id' => $row['id'], 'status' => 1])->sum('money');
- //本周收益
- $week_map['createtime'] = ['egt', $wday_start];
- // $row['week_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
- //上周收益
- $week_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
- // $row['last_week_profit'] = $mt_user_money_log->where($week_map)->sum('change_value');
- //本周邀请人数
- // $row['week_count'] = $mt_user->where(['intro_uid' => $row['id'], 'invite_time' => ['egt', $wday_start]])->count('id');
- //上周邀请人数
- // $row['last_week_count'] = $mt_user->where(['intro_uid' => $row['id'], 'invite_time' => ['between', [$wday_start - 86400 * 7, $wday_start]]])->count('id');
- //是否是会员
- // $vip_endtime = $mt_user_wallet->where(['user_id' => $row['id']])->value('vip_endtime');
- // if ($vip_endtime > $time) {
- // $row['vip_endtime'] = date('Y-m-d H:i:s', $vip_endtime);
- // } else {
- // $row['vip_endtime'] = '';
- // }
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- $result = false;
- if ($params['mobile']) {
- $exists = db('user')->where('mobile', $params['mobile'])->where('id', '<>', $row['id'])->find();
- if ($exists) {
- $this->error('手机号已经被其他人使用');
- }
- }
- //推荐人查询
- if (isset($params['intro_uid']) && $params['intro_uid'] != 0) {
- if ($params['intro_uid'] == $row['id']) {
- $this->error('推荐人ID不能填写自己');
- }
- $intro_user = db('user')->field('id, intro_uid')->where('id', $params['intro_uid'])->find();
- if(!$intro_user){
- $this->error('不存在推荐人');
- }
- if ($intro_user['intro_uid'] == $row['id']) {
- $this->error('不能填写下级邀请码作为推荐人');
- }
- }
- Db::startTrans();
- if (isset($params['vip_endtime'])) { //会员到期时间
- $rs = Db::name('user_wallet')->where(['user_id' => $ids])->setField('vip_endtime', strtotime($params['vip_endtime']));
- if ($rs === false) {
- Db::rollback();
- $this->error('会员到期时间有错误');
- }
- unset($params['vip_endtime']);
- }
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- if ($params['status'] != 1 || (isset($params['frozentime']) && strtotime($params['frozentime']) > time())) {
- //置空token
- Token::clear($row['id']);
- }
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $row['vip_endtime'] = Db::name('user_wallet')->where(['user_id' => $ids])->value('vip_endtime');
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- }
|