oppoAdMange.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. class oppoAdMange {
  2. public static readonly _instance: oppoAdMange = new oppoAdMange()
  3. /**激励视频广告对象 */
  4. private rewardVideoAd: any
  5. /**banner广告对象 */
  6. private bannerAd: any
  7. /**原生广告对象 */
  8. private nativeAd: any
  9. /**原生插屏广告对象 */
  10. private insertAd: any
  11. /**激励视频广告ID */
  12. private _rewardVideoAdUnitId = '388920';
  13. /**banner广告ID */
  14. private _banneradUnitId = '388926';
  15. /**原生广告ID */
  16. private nativeadUnitId = '388925';
  17. /**原生插屏广告ID */
  18. private insertAdUnitId = '';
  19. public set bannerAdUnitId(adUnitId: string) {
  20. this._banneradUnitId = adUnitId;
  21. }
  22. public set rewardedVideoAdUnitId(adUnitId: string) {
  23. this._rewardVideoAdUnitId = adUnitId;
  24. }
  25. //获取系统信息的同步版本
  26. get systemInfo() {
  27. return qg.getSystemInfoSync();
  28. }
  29. /**登陆接口 */
  30. login() {
  31. qg.login({
  32. success: res => {
  33. // 可全局储存 token,方便其他能力获取,注意 token 过期时间
  34. console.log(`登录成功: ${JSON.stringify(res.data)}`)
  35. },
  36. fail: res => {
  37. console.log(`登录失败: ${JSON.stringify(res)}`)
  38. }
  39. })
  40. }
  41. /**获取授权信息 */
  42. public getProfile(_token) {
  43. return new Promise((resolve, reject) => {
  44. qg.getProfile({
  45. token: _token,
  46. success: function (data) {
  47. resolve(data);
  48. },
  49. fail: function (data, code) {
  50. reject(data);
  51. }
  52. })
  53. });
  54. }
  55. /** 退出游戏,同步方法*/
  56. exitApplication() {
  57. qg.exitApplication();
  58. }
  59. /**使手机发生较短时间的振动(20 ms) */
  60. vibrateShort() {
  61. qg.vibrateShort({
  62. success: function (res) { },
  63. fail: function (res) { },
  64. complete: function (res) { }
  65. })
  66. }
  67. /**使手机发生较长时间的振动(400 ms) */
  68. vibrateLong() {
  69. qg.vibrateLong({
  70. success: function (res) { },
  71. fail: function (res) { },
  72. complete: function (res) { }
  73. })
  74. }
  75. //=============================广告
  76. /**展示激励视频 */
  77. showRewardVideoAd(suc: Function, fail: Function) {
  78. console.log("展示激励视频");
  79. // if(this.systemInfo.minPlatformVersion<1051)return
  80. // return new Promise<void>((resolve, reject) => {
  81. this.rewardVideoAd = qg.createRewardedVideoAd({
  82. adUnitId: this._rewardVideoAdUnitId,
  83. })
  84. console.log('加载广告')
  85. this.rewardVideoAd
  86. .load()
  87. .then(() => {
  88. console.log('promise 回调:加载成功')
  89. console.log('展示广告')
  90. // 调用 show 方法请求展示 banner,成功的时候回调 onShow,出错的时候回调 onError
  91. this.rewardVideoAd
  92. .show()
  93. .then(() => {
  94. console.log('promise 回调:展示成功')
  95. })
  96. .catch(err => {
  97. console.log(`promise 回调:展示失败 ${JSON.stringify(err)}`);
  98. })
  99. })
  100. .catch(err => {
  101. console.log(`promise 回调:加载失败 ${JSON.stringify(err)}`)
  102. })
  103. this.rewardVideoAd.onClose((res: any) => {
  104. if (res.isEnded) {
  105. console.log('激励视频广告完成,发放奖励')
  106. suc()
  107. this.rewardVideoAd.offClose()
  108. // resolve();
  109. } else {
  110. console.log('激励视频广告取消关闭,不发放奖励')
  111. fail();
  112. // reject();
  113. this.rewardVideoAd.offClose()
  114. }
  115. })
  116. // })
  117. }
  118. /**展示Banner广告 */
  119. showBannerAd() {
  120. console.log("展示banner视频");
  121. if (this.systemInfo.minPlatformVersion < 1051) return
  122. if (this.bannerAd) {
  123. return;
  124. }
  125. this.bannerAd = qg.createBannerAd({
  126. adUnitId: this._banneradUnitId,
  127. })
  128. console.log("创建成功");
  129. this.bannerAd.onLoad(() => {
  130. console.log('banner 广告加载成功')
  131. })
  132. this.bannerAd.show(() => {
  133. console.log('banner 广告显示')
  134. })
  135. this.bannerAd.onError(function (err) {
  136. console.error("banner 广告出错")
  137. console.error(JSON.stringify(err))
  138. this.bannerAd.destroy()
  139. this.bannerAd = null;
  140. })
  141. }
  142. /**隐藏Banner广告 */
  143. hideBannerAd() {
  144. if (this.bannerAd) {
  145. this.bannerAd.hide();
  146. this.bannerAd.destroy();
  147. this.bannerAd = null;
  148. }
  149. }
  150. /**展示原生插屏广告 */
  151. showInterstitialAd() {
  152. }
  153. /**原生插屏 */
  154. creatorInterstitialAd() {
  155. }
  156. /**创建原生广告 */
  157. createNativeAd(next: Function, fail: Function) {
  158. if (this.systemInfo.platformVersionCode < 1051) return
  159. // if (this.nativeAd) {
  160. //     this.nativeAd.destroy();
  161. //     this.nativeAd = null;
  162. // }
  163. this.nativeAd = qg.createNativeAd({
  164. posId: this.nativeadUnitId
  165. })
  166. this.nativeAd.onLoad((res) => {
  167. if (res && res.adList) {
  168. console.log("原生广告", res);
  169. this.reportAdShow(res.adList[0].adId);
  170. if (next) {
  171. next(res.adList[0]);
  172. }
  173. }
  174. })
  175. this.nativeAd.onError((res) => {
  176. console.log('原生广告加载失败', res)
  177. if (fail) {
  178. fail(res);
  179. }
  180. })
  181. let adLoad = this.nativeAd.load()
  182. adLoad && adLoad.then(() => {
  183. console.log("原生广告加载成功");
  184. }).catch((err) => {
  185. console.log('原生广告加载失败', JSON.stringify(err));
  186. if (fail) {
  187. fail();
  188. }
  189. });
  190. }
  191. /**上报原生点击 */
  192. clickNativeAd(adId) {
  193. if (!this.nativeAd) return;
  194. this.nativeAd.reportAdClick({
  195. adId: adId,
  196. });
  197. }
  198. /**上报广告曝光 */
  199. reportAdShow(adId) {
  200. if (!this.nativeAd) return;
  201. this.nativeAd.reportAdShow({
  202. adId: adId
  203. })
  204. }
  205. // 检查登录信息 5分钟后token 失效
  206. public getSetting(cbOK, cbFail = null) {
  207. }
  208. /**判断是否已经创建桌面图标 */
  209. hasShortcutInstalled() {
  210. this.login()
  211. qg.hasShortcutInstalled({
  212. success: function (res) {
  213. // 判断图标未存在时,创建图标
  214. if (res == false) {
  215. qg.installShortcut({
  216. success: function () {
  217. // 执行用户创建图标奖励
  218. },
  219. fail: function (err) { },
  220. complete: function () { }
  221. })
  222. }
  223. },
  224. fail: function (err) { },
  225. complete: function () { }
  226. })
  227. }
  228. /**设置游戏加载进度页面(该功能必须接入,否则会因为未接入,导致游戏加载页进度条缺失而被测试打回) */
  229. setLoadingProgress() {
  230. qg.setLoadingProgress({
  231. progress: 50
  232. });
  233. }
  234. /**隐藏游戏加载进度页面 */
  235. loadingComplete() {
  236. qg.loadingComplete({
  237. complete: function (res) { }
  238. });
  239. }
  240. /**跳转小游戏 */
  241. navigateToMiniGame() {
  242. qg.navigateToMiniGame({
  243. pkgName: 'com.sjmsj.nearme.gamecenter',
  244. path: '?page=pageB',
  245. extraData: {
  246. from: 'pageA'
  247. },
  248. success: function () {
  249. },
  250. fail: function (res) {
  251. console.log(JSON.stringify(res))
  252. }
  253. })
  254. }
  255. }
  256. export const OPPO = oppoAdMange._instance