huaWeiApi.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //TODO 未完成,需要查看Api文档
  2. import { Node, Vec2 } from "cc";
  3. import { MiniGameApiBase, getUserInfoParams, toastParams, loadParams, ModalParams, authorizeParams, loginParams, recordedGameScreenParams } from "./MiniGameApiBase";
  4. import { GameCfg } from "../Config/GameCfg";
  5. const huaWei: any = window['tt'];
  6. export default class HuaWeiApi extends MiniGameApiBase {
  7. showBannerAD(node: Node, successCB?: () => void, errorCB?: (error: any) => void, onCloseCB?: () => void, setBannerPos?: ({ screenWidth, screenHeight }: any) => Vec2): void {
  8. throw new Error("Method not implemented.");
  9. }
  10. checkScene(successCB?: Function, errorCB?: Function) {
  11. throw new Error("Method not implemented.");
  12. }
  13. navigateToScene(successCB?: Function, errorCB?: Function) {
  14. throw new Error("Method not implemented.");
  15. }
  16. getStorageSync(key?: string) {
  17. throw new Error("Method not implemented.");
  18. }
  19. removeStorage(key?: string) {
  20. throw new Error("Method not implemented.");
  21. }
  22. getDeviceInfo() {
  23. throw new Error("Method not implemented.");
  24. }
  25. setUnlockAllTime(time: number) {
  26. throw new Error("Method not implemented.");
  27. }
  28. getUnlockAllTime(call: Function) {
  29. throw new Error("Method not implemented.");
  30. }
  31. setUnitState(state?: string) {
  32. throw new Error("Method not implemented.");
  33. }
  34. reportAnalytics(eventName: string, data?: { [key: string]: string | number | boolean; }) {
  35. throw new Error("Method not implemented.");
  36. }
  37. setSoundInfo(sound?: boolean, music?: boolean) {
  38. throw new Error("Method not implemented.");
  39. }
  40. protected m_AppId: string = "";
  41. protected m_Secret: string = "";
  42. protected m_VideoAdIdIndex: number = 0;
  43. protected m_VideoAdIdList: string[] = [];
  44. protected m_BannerAdIdIndex: number = 0;
  45. protected m_BannerAdId: string[] = [];
  46. protected m_InsertAdIdIndex: number = 0;
  47. protected m_InsertAdId: string[] = [];
  48. protected m_videoAd: any = undefined;
  49. protected m_BannerAd: any = undefined;
  50. validAppId(): void {
  51. if (GameCfg.SYSTEM_LOG) console.log("huawei-api-validAppId");
  52. }
  53. ready(): void {
  54. if (GameCfg.SYSTEM_LOG) console.log("huaWei-api-ready");
  55. }
  56. getUserInfo(params: getUserInfoParams): void {
  57. huaWei.getUserInfo(params);
  58. }
  59. showToast(params: toastParams): void {
  60. huaWei.showToast(params);
  61. }
  62. showLoading(params: loadParams): void {
  63. huaWei.showLoading(params);
  64. }
  65. hideLoading(): void {
  66. huaWei.hideLoading();
  67. }
  68. showModal(params: ModalParams): void {
  69. huaWei.showModal(params);
  70. }
  71. authorize(params: authorizeParams): void {
  72. params.success && params.success();
  73. }
  74. login(params: loginParams): void {
  75. huaWei.login(params);
  76. }
  77. getVideoAdId(): string {
  78. const id: string = this.m_VideoAdIdList[this.m_VideoAdIdIndex];
  79. this.m_VideoAdIdIndex++;
  80. if (this.m_VideoAdIdIndex >= this.m_VideoAdIdList.length) {
  81. this.m_VideoAdIdIndex = 0;
  82. }
  83. return id;
  84. }
  85. loadAndShowVideoAd(successCB: () => void, errorCB: (error: any) => void, completeCB?: () => void, reportAnalyticsExplain?: string): void {
  86. this.showLoading({ title: '请求中', mask: true });
  87. const videoAdId = this.getVideoAdId();
  88. console.log('tt 视频广告', videoAdId, reportAnalyticsExplain);
  89. //创建 视频
  90. const videoAd: any = huaWei.createRewardedVideoAd({
  91. adUnitId: videoAdId,
  92. });
  93. //监听 视频关闭
  94. let onCloseVideoAd = (res) => {
  95. //
  96. videoAd.offClose(onCloseVideoAd);
  97. console.log('tt 视频广告关闭', res);
  98. if (res && res.isEnded) {
  99. // 给予奖励
  100. successCB();
  101. } else {
  102. errorCB('视频中途退出');
  103. }
  104. completeCB && completeCB();
  105. };
  106. //重试
  107. let onTryAgain = (e: any) => {
  108. videoAd.load().then(() => {
  109. videoAd.show().then(() => {
  110. this.hideLoading();
  111. videoAd.onClose(onCloseVideoAd);
  112. }).catch((e) => {//展示异常
  113. errorCB && errorCB('视频展示异常');
  114. completeCB && completeCB();
  115. });
  116. }).catch((e) => {//加载异常
  117. errorCB && errorCB('视频拉取异常02');
  118. completeCB && completeCB();
  119. });
  120. }
  121. console.log('展示一次');
  122. videoAd.show().then(() => {
  123. this.hideLoading();
  124. videoAd.onClose(onCloseVideoAd);
  125. //展示异常
  126. }).catch((e) => {
  127. console.log('重试一次');
  128. onTryAgain(e);
  129. })
  130. }
  131. getInsertAdId(): string {
  132. const id: string = this.m_InsertAdId[this.m_InsertAdIdIndex];
  133. this.m_InsertAdIdIndex++;
  134. if (this.m_InsertAdIdIndex >= this.m_InsertAdId.length) {
  135. this.m_InsertAdIdIndex = 0;
  136. }
  137. return id;
  138. }
  139. showInsertAd(onAdClose: () => void, onFailed: () => void): void {
  140. console.log("加抖音 插屏 广告");
  141. const adId = this.getInsertAdId();
  142. let insertAd = huaWei.createInterstitialAd({
  143. adUnitId: adId,
  144. });
  145. insertAd.load();
  146. insertAd.onLoad(() => {
  147. console.warn('插屏广告 加载完成');
  148. insertAd.show().catch((err) => {
  149. console.warn('插屏广告 显示失败 :' + JSON.stringify(err));
  150. insertAd.destroy();
  151. insertAd = null;
  152. if (onFailed) {
  153. onFailed();
  154. }
  155. });
  156. });
  157. insertAd.onError((err) => {
  158. console.warn('插屏广告 加载失败' + JSON.stringify(err));
  159. insertAd.destroy();
  160. insertAd = null;
  161. if (onFailed) {
  162. onFailed();
  163. }
  164. });
  165. insertAd.onClose(() => {
  166. console.warn('插屏广告 关闭');
  167. if (insertAd != null) {
  168. insertAd.destroy();
  169. insertAd = null;
  170. }
  171. if (onAdClose) {
  172. onAdClose();
  173. }
  174. });
  175. }
  176. getBannerAdId(): string {
  177. const id: string = this.m_BannerAdId[this.m_BannerAdIdIndex];
  178. this.m_BannerAdIdIndex++;
  179. if (this.m_BannerAdIdIndex >= this.m_BannerAdId.length) {
  180. this.m_BannerAdIdIndex = 0;
  181. }
  182. return id;
  183. }
  184. hideBanner() {
  185. // if (this.m_BannerAd != null) {
  186. // this.m_BannerAd.hide();
  187. // }
  188. }
  189. recordedGameScreenStart(params: recordedGameScreenParams, onStart?: (res: any) => void): void {
  190. console.log("huaWei-api-recordedGameScreenStart");
  191. onStart && onStart(null);
  192. }
  193. recordedGameScreenStop() {
  194. console.log("huaWei-api-recordedGameScreenStop");
  195. return true;
  196. }
  197. shareRecordedGameScreen(successCb?: () => void, failCb?: (e?: any) => void): void {
  198. console.log("huaWei-api-shareRecordedGameScreen");
  199. successCb && successCb();
  200. }
  201. isHaveRecordedGameScreen(): boolean {
  202. return true;
  203. }
  204. }