admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import 'dart:convert';
 
import 'package:flutter/services.dart';
 
import '../../utils/share_preference.dart';
import '../../utils/string_util.dart';
 
MethodChannel adMethodChannel = const MethodChannel('com.yeshi.video/ad');
 
enum AdType { csj, gdt }
 
enum AdPosition {
  //推荐
  other,
  //搜索
  videoSearch,
  //全屏视频
  videoDetailFullVideo,
  videoPlayPre
}
 
class CSJADConstant {
  //推荐首页大图
  static const String PID_RECOMMEND_BIG_PICTURE = "948067685";
  //搜索页广告
  static const String PID_VIDEO_SEARCH = "948119448";
  //全屏视频广告
  static const String PID_VIDEO_DETAIL_FULLSCREEN = "948119553";
}
 
class GDTADConstant {
  //推荐首页大图
  static const String PID_RECOMMEND_BIG_PICTURE = "5033406516973331";
 
  //搜索页广告
  static const String PID_VIDEO_SEARCH = "4033805974201049";
 
  //全屏视频广告
  static const String PID_VIDEO_DETAIL_FULLSCREEN = "6053800914701167";
}
 
class AdUtil {
  static void loadFullScreenAd(AdType adType, String pid) {
    if (adType == AdType.csj) {
      adMethodChannel.invokeMethod("loadCSJFullScreenAd", {"pid": pid});
    } else {
      adMethodChannel.invokeMethod("loadGDTFullScreenAd", {"pid": pid});
    }
  }
 
  //获取广告配置信息
  static Future<Map<String, dynamic>?> _getAdConfig() async {
    String result = await dataMethodChannel.invokeMethod("getAdConfig");
    print("广告配置:$result");
    if (StringUtil.isNullOrEmpty(result)) {
      return null;
    }
    return jsonDecode(result);
  }
 
  static Future<AdType?> getAdType(AdPosition position) async {
    String key = "";
    if (position == AdPosition.other) {
      key = "other";
    } else if (position == AdPosition.videoSearch) {
      key = "videoSearch";
    } else if (position == AdPosition.videoDetailFullVideo) {
      key = "videoDetailFullVideo";
    } else if (position == AdPosition.videoPlayPre) {
      key = "videoPlayPre";
    }
 
    Map<String, dynamic>? config = await _getAdConfig();
    if (config == null) {
      return null;
    }
 
    print("广告key:$key  类型${config[key]}");
    if (config[key] == null) {
      return null;
    }
    if (config[key]["type"] == "csj") {
      return AdType.csj;
    }
 
    if (config[key]["type"] == "gdt") {
      return AdType.gdt;
    }
 
    return null;
  }
}