with(['image'])->where(['is_up' => 1, 'ad_type' => $id])->order(['sort' => 'asc', 'id' => 'asc'])->select()->toArray(); $time = time(); foreach ($list as $key => &$arr) { if (!empty($arr['start_time']) && (strtotime($arr['start_time']) - $time) > 0) { //有开始时间 unset($list[$key]); } if (!empty($arr['end_time']) && (strtotime($arr['end_time']) - $time) < 0) { //有结束时间 unset($list[$key]); } $arr['image'] = $arr['image']['preview_url'] ?? ''; } return $this->renderSuccess(array_slice(array_values($list), 0, 5)); } public function list($ad_types){ $idarray = explode(',', $ad_types); $oldlist = AdModel::field('id,image_id,url,url_h5,title,start_time,end_time,ad_type,jump_type,jump_id')->with(['image'])->where(['is_up' => 1])->where('ad_type','in',$idarray)->order(['sort' => 'asc', 'id' => 'asc'])->select()->toArray(); $time = time(); $list = []; foreach ($oldlist as $key => &$arr) { if (!empty($arr['start_time']) && (strtotime($arr['start_time']) - $time) > 0) { //有开始时间 unset($oldlist[$key]); continue; } if (!empty($arr['end_time']) && (strtotime($arr['end_time']) - $time) < 0) { //有结束时间 unset($oldlist[$key]); continue; } if ($arr['jump_type'] == 13) { // 13跳转推荐官申请页 过滤不同角色的用户 $user = UserService::getCurrentLoginUser(); $role = $user['role']; if ($user && ($role == 2 || $role == 3 ||$role == 4 ||$role == 5 )) { continue; } } $arr['image'] = $arr['image']['preview_url'] ?? ''; $list[$arr['ad_type']][] = $arr; } return $this->renderSuccess($list); } //点击广告统计次数 public function clickAd() { $id = $this->request->param('id'); $mac_id = $this->request->param('mac_id'); $detail = AdModel::find($id); $detail->click_nums += 1; //点击数+1 $user = UserService::getCurrentLoginUser(); if ($user) { $browseRecord = BrowseRecords::where(['user_id' => $user->user_id, 'source_type' => 2, 'source_id' => $id])->find(); if (!$browseRecord) { BrowseRecords::create([ 'user_id' => $user->user_id, 'source_type' => 2, 'source_id' => $id, ]); $detail->click_user_nums += 1; //用户+1 } else { $browseRecord->update_time = time(); $browseRecord->save(); } } else { $browseRecord = BrowseRecords::where(['mac_id' => $mac_id, 'source_type' => 2, 'source_id' => $id])->find(); if (!$browseRecord) { BrowseRecords::create([ 'mac_id' => $mac_id, 'source_type' => 2, 'source_id' => $id, ]); $detail->click_user_nums += 1; //用户+1 } else { $browseRecord->update_time = time(); $browseRecord->save(); } } $detail->save(); return $this->renderSuccess(); } }