Vivo.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. interface tempVideoAd {
  2. adObj: any
  3. isInVideo: boolean
  4. callBack: Function
  5. }
  6. class vivo {
  7. public static readonly _instance: vivo = new vivo();
  8. private videoAd!: tempVideoAd
  9. private bannerAd: any
  10. private interstitialAd: any
  11. private nativeAd: any
  12. private videoPosId = 'c468a33abd4a401c945cdf2b1b132df0';
  13. private bannerPosId = '8c1c8b7057d545a1bc9b25702c949172';
  14. private interstitialPosId = 'b4a8c8c389e74d60854582616ea09fc1';
  15. private nativePosId = '6611630f4c6246c19a7f9e970aea7921';
  16. constructor() {
  17. }
  18. //获取系统信息的同步版本
  19. get systemInfo() {
  20. return qg.getSystemInfoSync();
  21. }
  22. //设置屏幕是否常量
  23. public setKeepScreenOn(bol: any) {
  24. qg.setKeepScreenOn({
  25. keepSrceenOn: bol,
  26. success: () => { console.log('设置屏幕常亮成功') },
  27. fail: () => { },
  28. complete: () => { },
  29. })
  30. }
  31. //登录授权
  32. public authorize_code() {
  33. return new Promise((resolve, reject) => {
  34. qg.authorize({
  35. type: "code",
  36. success: function (data: any) {
  37. resolve(data);
  38. },
  39. fail: function (data: any, code: any) {
  40. reject(data);
  41. }
  42. })
  43. });
  44. }
  45. // 旧版本获取token
  46. public authorize_token() {
  47. return new Promise((resolve, reject) => {
  48. qg.authorize({
  49. type: "token",
  50. success: function (obj: any) {
  51. resolve(obj);
  52. },
  53. fail: function (data: any, code: any) {
  54. reject(data);
  55. }
  56. })
  57. })
  58. }
  59. // 获取授权信息
  60. public getProfile(_token: any) {
  61. return new Promise((resolve, reject) => {
  62. qg.getProfile({
  63. token: _token,
  64. success: function (data: any) {
  65. resolve(data);
  66. },
  67. fail: function (data: any, code: any) {
  68. reject(data);
  69. }
  70. })
  71. });
  72. }
  73. /*
  74. * qg.exitApplication()
  75. * 退出游戏,同步方法
  76. */
  77. exitApplication(cb: any) {
  78. qg.exitApplication();
  79. }
  80. /**
  81. * 使手机发生较长时间的振动。
  82. * @param callback 回调 (isSuccess, res)
  83. */
  84. public vibrateLong(callback?: Function): void {
  85. qg.vibrateLong();
  86. if (callback) {
  87. callback()
  88. }
  89. };
  90. /**
  91. * 使手机发生较短时间的振动
  92. *
  93. */
  94. public vibrateShort(cb?: Function): void {
  95. qg.vibrateShort();
  96. if (cb) {
  97. cb()
  98. }
  99. };
  100. //=============================广告
  101. //播放激励视频
  102. showRewardVideoAd(next: any) {
  103. if (this.systemInfo.platformVersionCode < 1041) return next && next(false);
  104. if (!qg.createRewardedVideoAd) return next && next(false);
  105. if (this.videoAd) {
  106. } else {
  107. this.videoAd = {
  108. adObj: null,
  109. isInVideo: false,
  110. callBack: null!
  111. }
  112. this.videoAd.adObj = qg.createRewardedVideoAd({
  113. posId: this.videoPosId
  114. });
  115. this.videoAd.adObj.onError((err: any) => {
  116. this.videoAd.isInVideo = false;
  117. this.videoAd.callBack && this.videoAd.callBack(false);
  118. // Emitter.getInstance().emit("showADTip", '频繁观看广告,请稍后再试!');
  119. console.log("视频广告创建失败")
  120. switch (err.errCode) {
  121. case -3:
  122. console.log("激励广告加载失败---调用太频繁", JSON.stringify(err));
  123. break;
  124. case -4:
  125. console.log("激励广告加载失败--- 一分钟内不能重复加载", JSON.stringify(err));
  126. break;
  127. case 30008:
  128. // 当前启动来源不支持激励视频广告,请选择其他激励策略
  129. break;
  130. default:
  131. console.log("激励广告展示失败", err)
  132. break;
  133. }
  134. })
  135. this.videoAd.adObj.onLoad(() => {
  136. let adshow = this.videoAd.adObj.show();
  137. adshow && adshow.then(() => {// 捕捉show失败的错误
  138. console.log("激励视频广告显示成功");
  139. // if (AudioMgr.isOpen) {
  140. // AudioMgr.pause();
  141. // }
  142. this.videoAd.isInVideo = true;
  143. }).catch((err: any) => {
  144. console.log("激励视频广告显示失败", err);
  145. this.videoAd.isInVideo = false;
  146. this.videoAd.callBack && this.videoAd.callBack(false);
  147. // Emitter.getInstance().emit("showADTip", '频繁观看广告,请稍后再试!');
  148. });
  149. })
  150. this.videoAd.adObj.onClose((res: any) => {
  151. this.videoAd.isInVideo = false;
  152. console.log('onClose!', res)
  153. if (res && res["isEnded"]) {
  154. this.videoAd.callBack && this.videoAd.callBack(true);
  155. } else {
  156. // Emitter.getInstance().emit("showADTip", '提前关闭视频不能得到奖励哦!');
  157. this.videoAd.callBack && this.videoAd.callBack(false);
  158. }
  159. // if (AudioMgr.isOpen) {
  160. // AudioMgr.resume();
  161. // }
  162. this.videoAd.callBack = null!;
  163. // this.rewardedVideoAd.offClose();
  164. });
  165. }
  166. this.videoAd.isInVideo = true;
  167. this.videoAd.callBack = next;
  168. let adLoad = this.videoAd.adObj.load();//第一次调用 可能会报-3 广告能正常展示就可以忽略
  169. // 捕捉load失败的错误
  170. adLoad && adLoad.then(() => {
  171. console.log("激励视频广告加载成功");
  172. this.videoAd.isInVideo = true;
  173. }).catch((err: any) => {
  174. this.videoAd.isInVideo = false;
  175. console.log("激励视频广告加载失败", err);
  176. // Emitter.getInstance().emit("showADTip", '频繁观看广告,请稍后再试!');
  177. this.videoAd.callBack && this.videoAd.callBack(false);
  178. });
  179. }
  180. //------
  181. showBannerAd() {
  182. if (this.systemInfo.platformVersionCode < 1041) return;
  183. if (this.bannerAd) return;
  184. console.log('显示banner广告3')
  185. let doBannerShow = () => {
  186. let adshow = this.bannerAd.show();
  187. adshow && adshow.then(() => {
  188. console.log("banner广告展示成功");
  189. }).catch((err: any) => {
  190. switch (err.code) {
  191. case 30003:
  192. console.log("新用户7天内不能曝光Banner,请将手机时间调整为7天后,退出游戏重新进入")
  193. break;
  194. case 30009:
  195. console.log("10秒内调用广告次数超过1次,10秒后再调用")
  196. break;
  197. case 30002:
  198. console.log("加载广告失败,重新加载广告")
  199. break;
  200. default:
  201. console.log("banner广告展示失败")
  202. console.log(JSON.stringify(err))
  203. break;
  204. }
  205. });
  206. }
  207. this.bannerAd = qg.createBannerAd({
  208. posId: this.bannerPosId,
  209. style: {
  210. }
  211. });
  212. console.log('创建banner广告第一步', this.bannerAd)
  213. this.bannerAd.onSize((res: any) => {
  214. console.log('banner onResize', res);
  215. // gl.bannerSize = {
  216. // width: res.width,
  217. // height: (res.width / 16 * 9),
  218. // winHeight: this.systemInfo.screenHeight
  219. // };
  220. // this.bannerAd.style.left = (this.systemInfo.screenWidth - res.width) / 2;
  221. // this.bannerAd.style.top = this.systemInfo.screenHeight - (res.width / 16 * 9);
  222. })
  223. this.bannerAd.onLoad(() => {
  224. this.bannerAd.offLoad && this.bannerAd.offLoad();
  225. console.log("banner 广告加载成功 ...");
  226. doBannerShow();
  227. });
  228. this.bannerAd.onError((err: any) => {
  229. this.bannerAd.offError && this.bannerAd.offError();
  230. console.log("banner 广告创建失败 ...", err);
  231. });
  232. this.bannerAd.onClose((err: any) => {
  233. this.bannerAd.offClose();
  234. console.log("onClose 广告创建 ...", err);
  235. });
  236. }
  237. hideBannerAd() {
  238. if (!this.bannerAd) {
  239. return;
  240. }
  241. var addestroy = this.bannerAd.destroy();
  242. // 调用then和catch之前需要对show的结果做下判空处理,防止出错(如果没有判空,在平台版本为1052以及以下的手机上将会出现错误)
  243. addestroy && addestroy.then(() => {
  244. console.log("banner广告销毁成功");
  245. this.bannerAd.offLoad();
  246. this.bannerAd.offError();
  247. this.bannerAd.offSize();
  248. }).catch((err: any) => {
  249. console.log("banner广告销毁失败", err);
  250. });
  251. }
  252. //-------
  253. //显示插屏广告
  254. showInterstitialAd(next?: any) {
  255. if (this.systemInfo.platformVersionCode < 1031) return next && next(false);
  256. if (this.interstitialAd) return;
  257. console.log("显示插屏广告");
  258. this.interstitialAd = qg.createInterstitialAd({
  259. posId: this.interstitialPosId,
  260. });
  261. var doShow = () => {
  262. let adShow = this.interstitialAd.show();
  263. // 调用then和catch之前需要对show的结果做下判空处理,防止出错(如果没有判空,在平台版本为1052以及以下的手机上将会出现错误)
  264. adShow && adShow.then(() => {
  265. console.log("插屏广告展示成功");
  266. next && next();
  267. }).catch((err: any) => {
  268. this.interstitialAd = null;
  269. switch (err.code) {
  270. case 30003:
  271. console.log("新用户7天内不能曝光Banner,请将手机时间调整为7天后,退出游戏重新进入")
  272. break;
  273. case 30009:
  274. console.log("10秒内调用广告次数超过1次,10秒后再调用")
  275. break;
  276. case 30002:
  277. console.log("load广告失败,重新加载广告")
  278. break;
  279. case 30000:
  280. break;
  281. default:
  282. console.log("插屏广告展示失败")
  283. console.log(err)
  284. break;
  285. }
  286. });
  287. }
  288. this.interstitialAd.onLoad(() => {
  289. console.log('加载插屏广告成功')
  290. doShow();
  291. }, () => {
  292. console.log('加载插屏广告失败')
  293. this.interstitialAd = null;
  294. })
  295. this.interstitialAd.onError((err: any) => {
  296. console.log("插屏广告创建失败 ...", err)
  297. this.interstitialAd = null;
  298. })
  299. this.interstitialAd.onClose(() => {
  300. console.log("插屏 关闭 ...")
  301. this.interstitialAd && this.interstitialAd.offClose();
  302. this.interstitialAd = null;
  303. })
  304. }
  305. //----------
  306. //创建原生广告
  307. createNativeAd(next?: Function, resS?: Function) {
  308. if (this.systemInfo.platformVersionCode < 1053) return next && next(false);
  309. this.nativeAd = qg.createNativeAd({
  310. posId: this.nativePosId
  311. })
  312. console.log("原生广告原生广告", this.nativeAd)
  313. this.nativeAd.onLoad((res: any) => {
  314. if (res && res.adList) {
  315. console.log("原生广告", res);
  316. console.log(res.adList)
  317. console.log(res.adList[0])
  318. next && next(res.adList[0]);
  319. }
  320. else {
  321. next && next(null);
  322. }
  323. this.reportAdShow(res.adList[0].adId);
  324. })
  325. this.nativeAd.onError((res: any) => {
  326. console.log('原生广告加载失败', res)
  327. // next && next()
  328. if (resS) {
  329. resS()
  330. }
  331. })
  332. let adLoad = this.nativeAd.load()
  333. adLoad && adLoad.then(() => {
  334. console.log("原生广告加载成功");
  335. }).catch((err: any) => {
  336. console.log('原生广告加载失败', JSON.stringify(err));
  337. // next && next()
  338. if (resS) {
  339. resS()
  340. }
  341. });
  342. }
  343. /**上报原生点击 */
  344. clickNativeAd(adId: any) {
  345. if (!this.nativeAd) return;
  346. this.nativeAd.reportAdClick({
  347. adId: adId,
  348. });
  349. }
  350. /**上报广告曝光 */
  351. reportAdShow(adId: any) {
  352. if (!this.nativeAd) return;
  353. this.nativeAd.reportAdShow({
  354. adId: adId
  355. })
  356. }
  357. /**销毁广告组件,释放资源 */
  358. desNativeAd() {
  359. console.log("销毁广告组件,释放资源", this.nativeAd);
  360. this.nativeAd.destroy();
  361. }
  362. // 检查登录信息 5分钟后token 失效
  363. public getSetting(cbOK: any, cbFail = null) {
  364. }
  365. }
  366. export const Vivo = vivo._instance;