model = model('User'); $this->assign('levels',$this->model::getLevels()); } /** * 查看 */ public function index() { //设置过滤方法 $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(['group','admin']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $k => $v) { $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname); $v->hidden(['password', 'salt']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } public function show($ids){ $user=$this->model->find($ids); $programmes=Programme::where('user_id',$user['id'])->order('id','desc')->select(); $this->assign('user',$user); $this->assign('programmes',$programmes); $buyNum=$user->orderInfo()->sum('num'); $buyAmount=$user->orders()->payed()->sum('amount_pay'); $this->assign('buyNum',$buyNum); $this->assign('buyAmount',$buyAmount); #产品统计 $buyGoods=$user->orderInfo()->payed()->group('goods_id')->column('sum(amount_pay) as amount,sum(num) as num,goods_name'); $this->assign('buyGoods',$buyGoods); #分类统计 $bugCategory=$user->orderInfo()->payed()->group('category_id')->column('sum(amount_pay) as amount,sum(num) as num,category_name'); $this->assign('bugCategory',$bugCategory); #金额排名前10 $buyAmountRank=$user->orders()->payed()->order('amount_pay','desc')->limit(10)->select(); $this->assign('buyAmountRank',$buyAmountRank); #登录次数 $ranges=UserLoginRange::getRange(); $userRanges=$user->loginRange()->column('times','range'); foreach ($ranges as $idx=>&$range){ $range['num']=$userRanges[$idx]??0; } $this->assign('loginRange',$ranges); return $this->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $this->token(); } if($this->request->isGet()){ return $this->fetch(); } $data=input('row/a'); $this->validate($data,[ 'username'=>['require'], 'mobile|手机号'=>['require','mobile'], ]); if($this->model->where('username',$data['username'])->find()){ $this->error('用户名已存在'); } if($this->model->where('mobile',$data['mobile'])->find()){ $this->error('手机号已存在'); } if(!empty($data['email']) && $this->model->where('email',$data['email'])->find()){ $this->error('邮箱已存在'); } $this->model->allowField(true)->create($data); $this->success(); } /** * 编辑 */ public function edit($ids = null) { if ($this->request->isPost()) { $this->token(); } $row = $this->model->get($ids); $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker'])); return parent::edit($ids); } /** * 删除 */ public function del($ids = "") { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); $row = $this->model->get($ids); $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } Auth::instance()->delete($row['id']); $this->success(); } public function level_discount(){ if ($this->request->isAjax()) { $levels=$this->model::getLevels();; $list=[]; $config=DiscountService::getById(); foreach ($levels as $id=>$level){ $list[]=[ 'id'=>$id, 'name'=>$level, 'discount'=>$config[$id]??'', ]; } $result = array("total" => count($list), "rows" => $list); return json($result); } return $this->view->fetch(); } public function level_discount_edit($ids){ if($this->request->isGet()){ $this->assign('row',DiscountService::getById($ids)); $this->assign('id',$ids); return $this->fetch(); }else{ $data=input('row/a'); $this->validate($data,[ 'discount|折扣'=>['require','gt:0','lt:10'], ]); DiscountService::saveDiscount($ids,$data['discount']); $this->success(); } } #赠送优惠券 public function send_coupon($ids){ if($this->request->isGet()) { $coupon = Coupon::column('name','id'); $this->assign('coupon',$coupon); return $this->fetch(); }else{ $data=input('row/a'); $this->validate($data,[ 'coupon|券ID'=>['require','integer'], 'num|数量'=>['require','integer','gt:0'], ]); $coupon=Coupon::where('id',$data['coupon'])->find(); if(!$coupon){ $this->error('优惠券不存在'); } $coupon->sendToUser([$ids],$data['num']); $this->success(); } } }