admin
2021-12-04 5b1540fa5c18bc8b2739f4ab06d2a7b7953c65bf
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_unionad/flutter_unionad.dart';
import 'package:locations/model/common/adinfo_model.dart';
import 'package:locations/utils/config_util.dart';
import 'package:locations/utils/string_util.dart';
import 'package:flutter_tencentad/flutter_tencentad.dart';
 
//紧急联系人输入框确定事件
typedef OnAdCallback = void Function(bool success, String msg);
 
class AdUtil {
  static Future init() async {
    await CSJAdUtil.init();
    await GDTAdUtil.init();
  }
 
  static void showSplashAd(
      double width, double height, OnAdCallback adCallback) async {
    //加载广告
  }
 
  static Widget? loadSplash(AdinfoModel? adInfo, double width, double height,
      OnAdCallback adCallback) {
    if (adInfo == null) {
      adCallback(false, "广告信息为空");
      return null;
    }
 
    if (StringUtil.isNullOrEmpty(adInfo.type)) {
      adCallback(false, "广告类型为空");
      return null;
    }
 
    if (adInfo.type == "csj") {
      return CSJAdUtil.loadSplash(adInfo.pid!, width, height, adCallback);
    } else {
      return GDTAdUtil.loadSplash(adInfo.pid!, width, height, adCallback);
    }
  }
 
  static Widget? loadBanner(
      AdinfoModel? adInfo, double width, double height, OnAdCallback callback) {
    if (adInfo == null) {
      callback(false, "广告信息为空");
      return null;
    }
 
    if (StringUtil.isNullOrEmpty(adInfo.type)) {
      callback(false, "广告类型为空");
      return null;
    }
 
    if (adInfo.type == "csj") {
      return CSJAdUtil.loadBanner(adInfo.pid!, width, height, callback);
    } else {
      return GDTAdUtil.loadBanner(adInfo.pid!, width, height, callback);
    }
  }
 
  static Widget? loadExpress(
      AdinfoModel? adInfo, double width, double height, OnAdCallback callback) {
    if (adInfo == null) {
      callback(false, "广告信息为空");
      return null;
    }
 
    if (StringUtil.isNullOrEmpty(adInfo.type)) {
      callback(false, "广告类型为空");
      return null;
    }
 
    if (adInfo.type == "csj") {
      return CSJAdUtil.loadExpress(adInfo.pid!, width, height, callback);
    } else {
      return GDTAdUtil.loadExpress(adInfo.pid!, width, height, callback);
    }
  }
 
  static loadReward(AdinfoModel? adInfo, OnAdCallback callback) async {
    if (adInfo == null) {
      callback(false, "广告信息为空");
      return null;
    }
 
    if (StringUtil.isNullOrEmpty(adInfo.type)) {
      callback(false, "广告类型为空");
      return null;
    }
 
    if (adInfo.type == "csj") {
      return CSJAdUtil.loadReward(adInfo.pid!, callback);
    } else {
      return GDTAdUtil.loadReward(adInfo.pid!, callback);
    }
  }
 
  static loadInterstitial(AdinfoModel? adInfo, OnAdCallback callback) async {
    if (adInfo == null) {
      return null;
    }
 
    if (StringUtil.isNullOrEmpty(adInfo.type)) {
      return null;
    }
 
    if (adInfo.type == "csj") {
      CSJAdUtil.loadInterstitial(adInfo.pid!, callback);
    } else {
      GDTAdUtil.loadInterstitial(adInfo.pid!, callback);
    }
  }
 
  static Future<AdinfoModel?> getAdInfo(String position) async {
    var result = await ConfigUtil.getConfig(position);
    if (result == null) {
      return null;
    }
    AdinfoModel model = AdinfoModel.fromJson(jsonDecode(result));
    if (StringUtil.isNullOrEmpty(model.type)) {
      return null;
    }
    return model;
  }
}
 
class CSJAdUtil {
  static Future init() async {
    var csjAppId = "5240078";
    //初始化穿山甲
    if (!StringUtil.isNullOrEmpty(csjAppId)) {
      // if (Platform.isAndroid) {
      //   await FlutterUnionad.andridPrivacy(
      //     isCanUseLocation: false,
      //     //是否允许SDK主动使用地理位置信息 true可以获取,false禁止获取。默认为true
      //     lat: 1.0,
      //     //当isCanUseLocation=false时,可传入地理位置信息,穿山甲sdk使用您传入的地理位置信息lat
      //     lon: 1.0,
      //     //当isCanUseLocation=false时,可传入地理位置信息,穿山甲sdk使用您传入的地理位置信息lon
      //     isCanUsePhoneState: false,
      //     //是否允许SDK主动使用手机硬件参数,如:imei
      //     imei: "123",
      //     //当isCanUsePhoneState=false时,可传入imei信息,穿山甲sdk使用您传入的imei信息
      //     isCanUseWifiState: false,
      //     //是否允许SDK主动使用ACCESS_WIFI_STATE权限
      //     isCanUseWriteExternal: false,
      //     //是否允许SDK主动使用WRITE_EXTERNAL_STORAGE权限
      //     oaid: "111", //开发者可以传入oaid
      //   );
      // }
 
      await FlutterUnionad.register(
          androidAppId: csjAppId,
          //穿山甲广告 Android appid 必填
          iosAppId: "",
          //穿山甲广告 ios appid 必填
          useTextureView: false,
          //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景,可以使用TextureView 选填
          appName: "定位追踪轨迹",
          //appname 必填
          allowShowNotify: true,
          //是否允许sdk展示通知栏提示 选填
          allowShowPageWhenScreenLock: true,
          //是否在锁屏场景支持展示广告落地页 选填
          debug: true,
          //是否显示debug日志
          supportMultiProcess: true,
          //是否支持多进程,true支持 选填
          directDownloadNetworkType: [
            FlutterUnionadNetCode.NETWORK_STATE_4G,
            FlutterUnionadNetCode.NETWORK_STATE_WIFI
          ]); //允许直接下载的网络状态集合 选填
    }
  }
 
  static Widget? loadSplash(
      String? pid, double width, double height, OnAdCallback adCallback) {
    if (pid == null) {
      adCallback(false, "pid为空");
      return null;
    }
    return FlutterUnionad.splashAdView(
      //是否使用个性化模版  设定widget宽高
      mIsExpress: true,
      //android 开屏广告广告id 必填
      androidCodeId: pid,
      //ios 开屏广告广告id 必填
      iosCodeId: "",
      //是否支持 DeepLink 选填
      supportDeepLink: true,
      // 期望view 宽度 dp 选填 mIsExpress=true必填
      expressViewWidth: width,
      //期望view高度 dp 选填 mIsExpress=true必填
      expressViewHeight: height,
      callBack: FlutterUnionadSplashCallBack(
        onShow: () {
          print("开屏广告显示");
        },
        onClick: () {
          print("开屏广告点击");
        },
        onFail: (error) {
          adCallback(false, error);
        },
        onFinish: () {
          print("开屏广告倒计时结束");
          adCallback(true, "");
        },
        onSkip: () {
          print("开屏广告跳过");
          adCallback(true, "");
        },
        onTimeOut: () {
          print("开屏广告超时");
          adCallback(false, "开屏广告超时");
        },
      ),
    );
  }
 
  static Widget loadBanner(
      String pid, double width, double height, OnAdCallback callback) {
    return FlutterUnionad.bannerAdView(
      //andrrid banner广告id 必填
      androidCodeId: pid,
      //ios banner广告id 必填
      iosCodeId: "",
      //是否使用个性化模版
      mIsExpress: true,
      //是否支持 DeepLink 选填
      supportDeepLink: true,
      //一次请求广告数量 大于1小于3 必填
      expressAdNum: 3,
      //轮播间隔事件 30-120秒  选填
      expressTime: 30,
      // 期望view 宽度 dp 必填
      expressViewWidth: width,
      //期望view高度 dp 必填
      expressViewHeight: height,
      //广告事件回调 选填
      callBack: FlutterUnionadBannerCallBack(onShow: () {
        print("banner广告加载完成");
      }, onDislike: (message) {
        print("banner不感兴趣 $message");
        callback(false, "banner不感兴趣 $message");
      }, onFail: (error) {
        print("banner广告加载失败 $error");
      }, onClick: () {
        print("banner广告点击");
      }),
    );
  }
 
  static Widget loadExpress(
      String pid, double width, double height, OnAdCallback callback) {
    return FlutterUnionad.nativeAdView(
      androidCodeId: pid,
      //android 信息流广告id 必填
      iosCodeId: "",
      //ios banner广告id 必填
      supportDeepLink: true,
      //是否支持 DeepLink 选填
      expressViewWidth: width,
      // 期望view 宽度 dp 必填
      expressViewHeight: height,
      //期望view高度 dp 必填
      expressNum: 1,
      mIsExpress: true,
      //一次请求广告数量 大于1小于3 必填
      callBack: FlutterUnionadNativeCallBack(
        onShow: () {
          print("信息流广告显示");
        },
        onFail: (error) {
          print("信息流广告失败 $error");
        },
        onDislike: (message) {
          print("信息流广告不感兴趣 $message");
          callback(false, "信息流广告不感兴趣 $message");
        },
        onClick: () {
          print("信息流广告点击");
        },
      ),
    );
  }
 
  static loadReward(String pid, OnAdCallback adCallback) async {
    FlutterUnionad.loadRewardVideoAd(
      mIsExpress: true,
      //是否个性化 选填
      androidCodeId: pid,
      //Android 激励视频广告id  必填
      iosCodeId: "",
      //ios 激励视频广告id  必填
      supportDeepLink: true,
      //是否支持 DeepLink 选填
      rewardName: "vip",
      //奖励名称 选填
      rewardAmount: 1,
      //奖励数量 选填
      userID: "",
      //  用户id 选填
      orientation: FlutterUnionadOrientation.VERTICAL,
      //视屏方向 选填
      mediaExtra: null, //扩展参数 选填
    );
 
    FlutterUnionadStream.initAdStream(
      //激励广告
      flutterUnionadRewardAdCallBack: FlutterUnionadRewardAdCallBack(
        onShow: () {
          print("激励广告显示");
        },
        onClick: () {
          print("激励广告点击");
        },
        onFail: (error) {
          print("激励广告失败 $error");
          adCallback(false, "激励广告失败 $error");
        },
        onClose: () {
          print("激励广告关闭");
        },
        onSkip: () {
          print("激励广告跳过");
        },
        onVerify: (bool isVerify, int rewardAmount, String rewardName,
            int errorCode, String message) {
          adCallback(true, "获取激励成功");
        },
        onReady: () async {
          print("激励广告预加载准备就绪");
          //显示激励广告
          await FlutterUnionad.showRewardVideoAd();
        },
        onUnReady: () {
          print("激励广告预加载未准备就绪");
        },
      ),
    );
  }
 
  static loadInterstitial(String pid, OnAdCallback adCallback) async {
    FlutterUnionad.loadFullScreenVideoAdInteraction(
      androidCodeId: pid, //android 全屏广告id 必填
      iosCodeId: "", //ios 全屏广告id 必填
      supportDeepLink: true, //是否支持 DeepLink 选填
      orientation: FlutterUnionadOrientation.VERTICAL, //视屏方向 选填
    );
 
    FlutterUnionadStream.initAdStream(
      // 新模板渲染插屏广告回调
      flutterUnionadNewInteractionCallBack:
          FlutterUnionadNewInteractionCallBack(
        onShow: () {
          print("新模板渲染插屏广告显示");
        },
        onSkip: () {
          print("新模板渲染插屏广告跳过");
        },
        onClick: () {
          print("新模板渲染插屏广告点击");
        },
        onFinish: () {
          print("新模板渲染插屏广告结束");
        },
        onFail: (error) {
          print("新模板渲染插屏广告错误 $error");
          adCallback(false, "新模板渲染插屏广告错误 $error");
        },
        onClose: () {
          print("新模板渲染插屏广告关闭");
          adCallback(true, "新模板渲染插屏广告关闭");
        },
        onReady: () async {
          print("新模板渲染插屏广告预加载准备就绪");
          //显示新模板渲染插屏
          await FlutterUnionad.showFullScreenVideoAdInteraction();
        },
        onUnReady: () {
          print("新模板渲染插屏广告预加载未准备就绪");
        },
      ),
    );
  }
}
 
class GDTAdUtil {
  static Future init() async {
    var gdtAppId = "1200343363";
    //初始化广点通
    if (!StringUtil.isNullOrEmpty(gdtAppId)) {
      await FlutterTencentad.register(
        androidId: gdtAppId, //androidId
        iosId: "", //iosId
        debug: true, //是否显示日志log
      );
    }
  }
 
  static Widget? loadSplash(
      String? pid, double width, double height, OnAdCallback adCallback) {
    return FlutterTencentad.splashAdView(
      //android广告id
      androidId: pid!,
      //ios广告id
      iosId: "",
      ////设置开屏广告从请求到展示所花的最大时长(并不是指广告曝光时长),取值范围为[1500, 5000]ms
      fetchDelay: 3500,
      //广告回调
      callBack: FlutterTencentadSplashCallBack(
        onShow: () {
          print("开屏广告显示");
        },
        onADTick: (time) {
          print("开屏广告倒计时剩余时间 $time");
        },
        onClick: () {
          print("开屏广告点击");
        },
        onClose: () {
          print("开屏广告关闭");
          adCallback(true, "开屏广告关闭");
        },
        onExpose: () {
          print("开屏广告曝光");
        },
        onFail: (code, message) {
          adCallback(false, message);
        },
      ),
    );
  }
 
  static Widget loadBanner(
      String pid, double width, double height, OnAdCallback callback) {
    return FlutterTencentad.bannerAdView(
      //android广告id
      androidId: pid,
      //ios广告id
      iosId: "",
      //广告宽 单位dp
      viewWidth: width,
      //广告高  单位dp   宽高比应该为6.4:1
      viewHeight: height,
      // 广告回调
      callBack: FlutterTencentadBannerCallBack(
        onShow: () {
          print("Banner广告显示");
        },
        onFail: (code, message) {
          print("Banner广告错误 $code $message");
          callback(false, "Banner广告错误  $code $message");
        },
        onClose: () {
          print("Banner广告关闭");
          callback(true, "Banner广告关闭");
        },
        onExpose: () {
          print("Banner广告曝光");
        },
        onClick: () {
          print("Banner广告点击");
        },
      ),
    );
  }
 
  static Widget loadExpress(
      String pid, double width, double height, OnAdCallback callback) {
    return FlutterTencentad.expressAdView(
        //android广告id
        androidId: pid,
        //ios广告id
        iosId: "",
        //广告宽 单位dp
        viewWidth: width.toInt(),
        //广告高  单位dp
        viewHeight: height.toInt(),
        //回调事件
        callBack: FlutterTencentadExpressCallBack(
          onShow: () {
            print("动态信息流广告显示");
          },
          onFail: (code, message) {
            print("动态信息流广告错误 $code $message");
          },
          onClose: () {
            print("动态信息流广告关闭");
          },
          onExpose: () {
            print("动态信息流广告曝光");
          },
          onClick: () {
            print("动态信息流广告点击");
          },
        ));
  }
 
  static loadReward(String pid, OnAdCallback adCallback) async {
    await FlutterTencentad.loadRewardVideoAd(
        //android广告id
        androidId: pid,
        //ios广告id
        iosId: "",
        //用户id
        userID: "",
        //奖励
        rewardName: "VIP",
        //奖励数
        rewardAmount: 1,
        //扩展参数 服务器回调使用
        customData: "");
 
    FlutterTencentAdStream.initAdStream(
      //激励广告
      flutterTencentadRewardCallBack:
          FlutterTencentadRewardCallBack(onShow: () {
        print("激励广告显示");
      }, onClick: () {
        print("激励广告点击");
      }, onFail: (code, message) {
        print("激励广告失败 $code $message");
        adCallback(false, "激励广告失败");
      }, onClose: () {
        print("激励广告关闭");
      }, onReady: () async {
        print("激励广告预加载准备就绪");
        await FlutterTencentad.showRewardVideoAd();
      }, onUnReady: () {
        print("激励广告预加载未准备就绪");
      }, onVerify: (transId, rewardName, rewardAmount) {
        print("激励广告奖励  $transId   $rewardName   $rewardAmount");
        adCallback(true, "激励广告奖励");
      }, onFinish: () {
        print("激励广告完成");
      }),
    );
  }
 
  static loadInterstitial(String pid, OnAdCallback adCallback) async {
    await FlutterTencentad.loadUnifiedInterstitialAD(
      //android广告id
      androidId: pid,
      //ios广告id
      iosId: "",
      //是否全屏
      isFullScreen: false,
    );
 
    FlutterTencentAdStream.initAdStream(
      flutterTencentadInteractionCallBack: FlutterTencentadInteractionCallBack(
        onShow: () {
          print("插屏广告显示");
        },
        onClick: () {
          print("插屏广告点击");
        },
        onFail: (code, message) {
          print("插屏广告失败 $code $message");
          adCallback(false, "插屏广告失败 $code $message");
        },
        onClose: () {
          print("插屏广告关闭");
          adCallback(true, "插屏广告关闭");
        },
        onReady: () async {
          print("插屏广告预加载准备就绪");
          await FlutterTencentad.showUnifiedInterstitialAD();
        },
        onUnReady: () {
          print("插屏广告预加载未准备就绪");
        },
      ),
    );
  }
}
 
class AdPosition {
  //开屏
  static String splash = "splash";
 
  //首页插屏
  static String homeInterstitial = "homeInterstitial";
 
  //我的页面原生
  static String mineExpress = "mineExpress";
 
  //搜索页面原生
  static String searchExpress = "searchExpress";
 
  //搜索结果banner
  static String searchResultBanner = "searchResultBanner";
 
  //轨迹分享页插屏
  static String travelShareInterstitial = "travelShareInterstitial";
 
  //会员激励视频
  static String vipReward = "vipReward";
}