Ad.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\controller;
  4. use app\api\service\User as UserService;
  5. use app\common\model\Ad as AdModel;
  6. use app\common\model\BrowseRecords;
  7. /**
  8. * 广告管理
  9. * @package app\api\controller
  10. */
  11. class Ad extends Controller
  12. {
  13. /**
  14. * Notes:
  15. * Author: zhangs
  16. * DateTime: 2021/9/24 11:16
  17. * @param int $id
  18. * @return array
  19. */
  20. public function detail($id)
  21. {
  22. $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();
  23. $time = time();
  24. foreach ($list as $key => &$arr) {
  25. if (!empty($arr['start_time']) && (strtotime($arr['start_time']) - $time) > 0) { //有开始时间
  26. unset($list[$key]);
  27. }
  28. if (!empty($arr['end_time']) && (strtotime($arr['end_time']) - $time) < 0) { //有结束时间
  29. unset($list[$key]);
  30. }
  31. $arr['image'] = $arr['image']['preview_url'] ?? '';
  32. }
  33. return $this->renderSuccess(array_slice(array_values($list), 0, 5));
  34. }
  35. public function list($ad_types){
  36. $idarray = explode(',', $ad_types);
  37. $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();
  38. $time = time();
  39. $list = [];
  40. foreach ($oldlist as $key => &$arr) {
  41. if (!empty($arr['start_time']) && (strtotime($arr['start_time']) - $time) > 0) { //有开始时间
  42. unset($oldlist[$key]);
  43. continue;
  44. }
  45. if (!empty($arr['end_time']) && (strtotime($arr['end_time']) - $time) < 0) { //有结束时间
  46. unset($oldlist[$key]);
  47. continue;
  48. }
  49. if ($arr['jump_type'] == 13) { // 13跳转推荐官申请页 过滤不同角色的用户
  50. $user = UserService::getCurrentLoginUser();
  51. $role = $user['role'];
  52. if ($user && ($role == 2 || $role == 3 ||$role == 4 ||$role == 5 )) {
  53. continue;
  54. }
  55. }
  56. $arr['image'] = $arr['image']['preview_url'] ?? '';
  57. $list[$arr['ad_type']][] = $arr;
  58. }
  59. return $this->renderSuccess($list);
  60. }
  61. //点击广告统计次数
  62. public function clickAd()
  63. {
  64. $id = $this->request->param('id');
  65. $mac_id = $this->request->param('mac_id');
  66. $detail = AdModel::find($id);
  67. $detail->click_nums += 1; //点击数+1
  68. $user = UserService::getCurrentLoginUser();
  69. if ($user) {
  70. $browseRecord = BrowseRecords::where(['user_id' => $user->user_id, 'source_type' => 2, 'source_id' => $id])->find();
  71. if (!$browseRecord) {
  72. BrowseRecords::create([
  73. 'user_id' => $user->user_id,
  74. 'source_type' => 2,
  75. 'source_id' => $id,
  76. ]);
  77. $detail->click_user_nums += 1; //用户+1
  78. } else {
  79. $browseRecord->update_time = time();
  80. $browseRecord->save();
  81. }
  82. } else {
  83. $browseRecord = BrowseRecords::where(['mac_id' => $mac_id, 'source_type' => 2, 'source_id' => $id])->find();
  84. if (!$browseRecord) {
  85. BrowseRecords::create([
  86. 'mac_id' => $mac_id,
  87. 'source_type' => 2,
  88. 'source_id' => $id,
  89. ]);
  90. $detail->click_user_nums += 1; //用户+1
  91. } else {
  92. $browseRecord->update_time = time();
  93. $browseRecord->save();
  94. }
  95. }
  96. $detail->save();
  97. return $this->renderSuccess();
  98. }
  99. }