123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- declare (strict_types = 1);
- namespace app\api\controller;
- use app\api\service\User as UserService;
- use app\common\model\Ad as AdModel;
- use app\common\model\BrowseRecords;
- /**
- * 广告管理
- * @package app\api\controller
- */
- class Ad extends Controller
- {
- /**
- * Notes:
- * Author: zhangs
- * DateTime: 2021/9/24 11:16
- * @param int $id
- * @return array
- */
- public function detail($id)
- {
- $list = 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, '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();
- }
- }
|