wechat.ts 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. //import AD from "../../../AD/AD";
  2. import { view } from "cc";
  3. const global = {
  4. /**临时处理加的 2020-12-29 */
  5. latticeAdunit: null,
  6. /**临时处理加的 2020-12-29 */
  7. iconCount: null,
  8. }
  9. class wechat {
  10. ////////////////////////////
  11. // 类成员
  12. ///////////////////////////
  13. public static readonly _instance: wechat = new wechat();
  14. /** 系统信息 */
  15. private systemInfo: wx.types.SystemInfo = null;
  16. /** 登录按钮对象 */
  17. private userInfoButton: UserInfoButton = null;
  18. /** banner广告对象 */
  19. private bannerAd: BannerAd = null;
  20. /** 奖励视频广告对象 */
  21. private rewardedVideoAd: RewardedVideoAd = null;
  22. /** 插屏广告对象 */
  23. private interstitialAd = null;
  24. /**推荐位广告对象 */
  25. private RiconAD: iconAd = null;
  26. private LiconAD: iconAd = null;
  27. /**格子广告对象 */
  28. private gridAd: gridAd = null;
  29. /**格子广告数量 */
  30. private gridAdCunt: number = null;
  31. /** 游戏画面录制对象 */
  32. private GameRecorder: GameRecorder = null;
  33. /** 录屏开始事件 */
  34. private startListener: Function = null;
  35. /** 监听录屏继续事件 */
  36. private resumeListener: Function = null;
  37. /** 录屏暂停事件 */
  38. private pauseListener: Function = null;
  39. /** 录屏结束事件 */
  40. private stopListener: Function = null;
  41. /** 录屏错误事件 */
  42. private errorListener: Function = null;
  43. /** 录屏取消事件 */
  44. private abortListener: Function = null;
  45. /** 录制时间更新事件。在录制过程中触发该事件 */
  46. private timeUpdateListener: Function = null;
  47. /** 游戏圈对象 */
  48. private gameClub: GameClubButton = null;
  49. /** 意见反馈按钮 */
  50. private feedbackButton: FeedbackButton = null;
  51. /** 切换到前台事件 */
  52. private showListener: Function = null;
  53. /** 切换到后台事件 */
  54. private hideListener: Function = null;
  55. /** banner广告id */
  56. private _bannerAdUnitId = ['adunit-941c3f0eb62ccad2', 'adunit-ce3ffa210e8f5f05', 'adunit-fddeec0c74c7429e', 'adunit-68af5dcafd3c0fe1', 'adunit-528b9e05536b7f4f'];
  57. private _bannerAdObjList: BannerAd[] = [];
  58. private _bannerShowIndex = 0;
  59. private _bannerTimer = -1;
  60. private _bannerAdRefreshTimeOutId: number = 0;
  61. /** 奖励视频广告id */
  62. private _rewardedVideoAdUnitId = ['adunit-9eac140dd1f2f777', 'adunit-db982a9d278eb210', 'adunit-d479ce4dfccdf7eb'];
  63. /** 插屏广告id */
  64. private _interstitialAdUnitId = ['adunit-eb023b20448e38d9'];
  65. /**格子广告ID */
  66. private _latticeAdUnitId = ''
  67. /**推荐位广告,左右 */
  68. private _RIconAdunit = ''
  69. private _LIconAdunit = ''
  70. /** 登录按钮信息 */
  71. private mainBannerAD = null;
  72. private _userInfoButtonData = {
  73. width: null,
  74. height: null,
  75. x: null,
  76. y: null,
  77. url: null
  78. };
  79. /** 反馈页面的按钮信息 */
  80. private _feedbackButtonData = {
  81. width: null,
  82. height: null,
  83. x: null,
  84. y: null,
  85. url: null
  86. };
  87. /** banner广告的像素大小 */
  88. private _bannerSize = {
  89. width: 600,
  90. height: 172
  91. };
  92. private _gameClubSize = {
  93. icon: '',
  94. x: 0,
  95. y: 0,
  96. width: 0,
  97. height: 0
  98. };
  99. private _closeRewardVideoListener: Function = null;
  100. ////////////////////////////
  101. // get、set访问器
  102. ///////////////////////////
  103. public set RIconAduni(adUnitId: string) {
  104. this._RIconAdunit = adUnitId;
  105. }
  106. public set LIconAduni(adUnitId: string) {
  107. this._LIconAdunit = adUnitId;
  108. }
  109. public set bannerAdUnitId(adUnitId: string) {
  110. // this._bannerAdUnitId = adUnitId;
  111. }
  112. public set rewardedVideoAdUnitId(adUnitId: string) {
  113. // this._rewardedVideoAdUnitId = adUnitId;
  114. }
  115. public set interstitialAdUnitId(adUnitId: string) {
  116. // this._interstitialAdUnitId = adUnitId;
  117. }
  118. public set bannerSize(size: any) {
  119. this._bannerSize.width = size.width;
  120. this._bannerSize.height = size.height;
  121. }
  122. public set latticeAdUnitId(adUnitId: string) {
  123. this._latticeAdUnitId = adUnitId;
  124. }
  125. public set gameClubSize(size: any) {
  126. this._gameClubSize.x = size.x;
  127. this._gameClubSize.y = size.y;
  128. this._gameClubSize.width = size.width;
  129. this._gameClubSize.height = size.height;
  130. this._gameClubSize.icon = size.icon;
  131. }
  132. public set userInfoButtonData(data: any) {
  133. this._userInfoButtonData.width = data.width;
  134. this._userInfoButtonData.height = data.height;
  135. this._userInfoButtonData.x = data.x;
  136. this._userInfoButtonData.y = data.y;
  137. this._userInfoButtonData.url = data.url;
  138. }
  139. public set closeRewardVideoListener(cb: Function) {
  140. this._closeRewardVideoListener = cb;
  141. }
  142. ////////////////////////////
  143. // get、set访问器
  144. ///////////////////////////
  145. public constructor() {
  146. if (window.wx) {
  147. // this.registerRecordScreenEvent();
  148. this.getSystemInfoSync();
  149. }
  150. }
  151. //预加载所有广告
  152. public preLoadAd() {
  153. this.createRewardedVideoAd();
  154. this.preLoadBanner();
  155. this.createInterstitialAd();
  156. }
  157. //预加载所有banner广告
  158. public preLoadBanner() {
  159. for (let i = 0; i < this._bannerAdUnitId.length; i++) {
  160. let id = this._bannerAdUnitId[i];
  161. let bannerAd = this.createBanner(id);
  162. this._bannerAdObjList.push(bannerAd);
  163. }
  164. }
  165. private createBanner(id) {
  166. let bannerSize = {
  167. // width: this._bannerSize.width / this.systemInfo.pixelRatio,
  168. // height: this._bannerSize.height / this.systemInfo.pixelRatio
  169. width: this._bannerSize.width * 0.65,
  170. height: this._bannerSize.height * 0.35
  171. };
  172. // let id = this._bannerAdUnitId[Math.floor(Math.random() * this._bannerAdUnitId.length)];
  173. let bannerObj = wx.createBannerAd({
  174. // 广告单元 id
  175. adUnitId: id,
  176. // 广告自动刷新的间隔时间,单位为秒,参数值必须大于等于30(该参数不传入时 Banner 广告不会自动刷新)
  177. adIntervals: 30,
  178. style: {
  179. left: this.systemInfo.windowWidth / 2 - bannerSize.width / 2,
  180. top: this.systemInfo.windowHeight - bannerSize.height + this.systemInfo.windowHeight * 0.1,
  181. height: bannerSize.height,
  182. width: bannerSize.width
  183. }
  184. });
  185. // console.log('bannerSize: ', bannerSize, ', systemInfo: ', this.systemInfo, ',this.bannerAd.style: ', this.bannerAd.style);
  186. bannerObj.onLoad(() => {
  187. console.log('=====> @framework, banner广告加载成功' + id);
  188. // 移除setTimeout
  189. // if (this._bannerAdRefreshTimeOutId != 0) {
  190. // clearTimeout(this._bannerAdRefreshTimeOutId);
  191. // this._bannerAdRefreshTimeOutId = 0;
  192. // }
  193. // if (isShow) this.bannerAd.show();
  194. // 延时刷新banner
  195. // if (refreshTime > 0) {
  196. // this._bannerAdRefreshTimeOutId = setTimeout(() => {
  197. // if (this.bannerAd)
  198. // this.showBannerAd(refreshTime);
  199. // }, refreshTime * 1000);
  200. // }
  201. // next && next(true);
  202. });
  203. bannerObj.onError((res) => {
  204. console.log('=====> @framework, banner广告加载失败:', id);
  205. // next && next(false);
  206. });
  207. bannerObj.onResize((res: { width: number; height: number }) => {
  208. bannerObj.style.left = this.systemInfo.windowWidth / 2 - res.width / 2 - 0.1;
  209. bannerObj.style.top = this.systemInfo.windowHeight - res.height + 0.1;
  210. //AD.setErrNodeY(-((this.bannerAd.style.top * winSize.height / this.systemInfo.windowHeight) - winSize.height / 2));
  211. });
  212. return bannerObj;
  213. }
  214. ////////////////////////////
  215. // 登录模块
  216. ///////////////////////////
  217. /**
  218. * 登陆接口
  219. * @param cbOK
  220. * @param cbFail
  221. */
  222. public wetchatLogin(cbOK, cbFail): void {
  223. let data = {
  224. wccode: null,
  225. wcencrypted: null,
  226. wciv: null,
  227. // userInfo: null
  228. };
  229. // 获取玩家数据
  230. let getLoginData = () => {
  231. this.login((code: any) => {
  232. data.wccode = code;
  233. if (cbOK)
  234. cbOK(code);
  235. }, (res: any) => {
  236. data.wcencrypted = res.encryptedData;
  237. data.wciv = res.iv;
  238. if (cbFail)
  239. cbFail(data);
  240. });
  241. };
  242. getLoginData();
  243. }
  244. /**
  245. * @description 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限
  246. *
  247. * @param cbDone 登陆成功后回调
  248. * @param cbFail 登陆失败后回调
  249. */
  250. public getSetting(cbDone, cbFail): void {
  251. if (wechat.isSupportedAPI(wx.getSetting)) {
  252. wx.getSetting({
  253. success: (res: { authSetting: wx.types.AuthSetting }) => {
  254. // if (res.authSetting['scope.userInfo']) {
  255. // if (cbDone)
  256. // cbDone(res.authSetting['scope.userInfo']);
  257. // } else
  258. // if (cbFail) cbFail("开关无");
  259. cbDone(res.authSetting['scope.userInfo']);
  260. },
  261. fail: (res) => {
  262. if (cbFail) cbFail(res);
  263. }
  264. });
  265. } else {
  266. console.error("----> 微信版本不支持getSetting API");
  267. if (cbFail) cbFail("----> 微信版本不支持getSetting API");
  268. }
  269. }
  270. /**
  271. * @description 创建用户登录按钮
  272. */
  273. public createUserInfoButton(x = 0, y = 0, width = view.getVisibleSize().width, height = view.getVisibleSize().height): void {
  274. if (wechat.isSupportedAPI(wx.createUserInfoButton)) {
  275. this.userInfoButton = wx.createUserInfoButton({
  276. type: 'image',
  277. // image: this._userInfoButtonData.url,
  278. style: {
  279. // left: this.systemInfo.screenWidth * this._userInfoButtonData.x - this._userInfoButtonData.width / 2,
  280. // top: this.systemInfo.screenHeight * this._userInfoButtonData.y - this._userInfoButtonData.height / 2,
  281. // width: this._userInfoButtonData.width,
  282. // height: this._userInfoButtonData.height
  283. left: x,
  284. top: y,
  285. width: view.getVisibleSize().width,
  286. height: view.getVisibleSize().height
  287. }
  288. });
  289. } else {
  290. console.log('=====> @framework,当前微信版本过低,无法使用授权按钮,请升级到最新微信版本后重试');
  291. }
  292. }
  293. // /**
  294. // * 根据node的属性,创建一个授权按钮
  295. // * @param btnNode
  296. // * @param cbOk 成功后回调
  297. // * @param cbFail 失败后回调
  298. // */
  299. // public createAuthorizeBtnByNode(btnNode: Node, cbOk?: Function, cbFail?: Function): void {
  300. // if (!wechat.isSupportedAPI(wx.createUserInfoButton)) {
  301. // console.log('=====> @framework,当前微信版本过低,无法使用授权按钮,请升级到最新微信版本后重试');
  302. // return;
  303. // }
  304. // this.destroyAuthorizeBtn()
  305. // let btnSize = size(btnNode.width + 10, btnNode.height + 10);
  306. // let btnPosI = btnNode.convertToWorldSpaceAR(v2(0, 0));
  307. // let frameSize = view.getFrameSize();
  308. // let winSize = director.getWinSize();
  309. // //适配不同机型来创建微信授权按钮
  310. // let left = (btnPosI.x - btnSize.width * 0.5) / winSize.width * frameSize.width;
  311. // let top = (winSize.height - btnPosI.y - btnSize.height * 0.5) / winSize.height * frameSize.height;
  312. // let width = btnSize.width / winSize.width * frameSize.width;
  313. // let height = btnSize.height / winSize.height * frameSize.height;
  314. // this.userInfoButton = wx.createUserInfoButton({
  315. // type: 'text',
  316. // text: '',
  317. // // type: 'image',
  318. // // image: this._userInfoButtonData.url,
  319. // style: {
  320. // left: left,
  321. // top: top,
  322. // width: width,
  323. // height: height,
  324. // // lineHeight: 0,
  325. // // backgroundColor: '',
  326. // // color: '#ffffff',
  327. // // textAlign: 'center',
  328. // // fontSize: 16,
  329. // // borderRadius: 4
  330. // }
  331. // })
  332. // this.onTapUserInfoButton(cbOk, cbFail)
  333. // }
  334. public destroyAuthorizeBtn(): void {
  335. if (this.userInfoButton) {
  336. this.userInfoButton.destroy();
  337. this.userInfoButton = null;
  338. }
  339. };
  340. /**
  341. * @description 监听用户点击登录按钮的事件
  342. */
  343. private onTapUserInfoButton(cbOK, cbFail): void {
  344. if (this.userInfoButton) {
  345. this.userInfoButton.onTap((res: any) => {
  346. // 点击登录按钮,用户授权成功
  347. console.log('onTap', res);
  348. if (res.userInfo) {
  349. this.destroyAuthorizeBtn();
  350. if (cbOK)
  351. cbOK(res);
  352. } else {
  353. cbFail(res);
  354. }
  355. });
  356. } else {
  357. cbFail(null);
  358. }
  359. }
  360. /**
  361. * @description 登录游戏服务
  362. */
  363. public login(cbOk, cbFail): void {
  364. wx.login({
  365. success: (res: { code: string }) => {
  366. cbOk(res.code);
  367. },
  368. fail: () => {
  369. cbFail();
  370. }
  371. });
  372. }
  373. /**
  374. * @description 获取用户信息
  375. */
  376. public getUserInfo(resolve?, reject?): void {
  377. this.getSetting((res: any) => {
  378. wx.getUserInfo({
  379. success: (res: any) => {
  380. if (resolve)
  381. resolve(res);
  382. },
  383. fail: (err) => {
  384. if (reject)
  385. reject(err);
  386. }
  387. });
  388. }, (res) => {
  389. if (reject) {
  390. console.log(res);
  391. reject("getsetting 失败");
  392. }
  393. })
  394. }
  395. ////////////////////////////
  396. // 分享
  397. ///////////////////////////
  398. /**
  399. * @description 分享,主动拉起转发,进入选择通讯录界面
  400. * @param _title
  401. * @param _imageUrl
  402. * @param _query
  403. */
  404. public shareAppMessage(_title: string, _imageUrl: string, _query: string): void {
  405. wx.shareAppMessage({
  406. title: _title,
  407. imageUrl: _imageUrl,
  408. query: _query
  409. });
  410. }
  411. ////////////////////////////
  412. // banner广告
  413. ///////////////////////////
  414. /**
  415. * @description 显示banner广告
  416. * @param refreshTime 自动刷新时间,默认30秒,必须>=30秒;
  417. */
  418. public showBannerAd(next?, refreshTime: number = 30) {
  419. if (!wechat.isSupportedAPI(wx.createBannerAd)) return;
  420. if (!this.bannerAd) {
  421. this.createBannerAd(refreshTime, next);
  422. this.bannerAd.show();
  423. return;
  424. }
  425. else {
  426. this.bannerAd.show();
  427. next && next(true);
  428. }
  429. }
  430. //轮流显示banner
  431. public showBannerTurns(times) {
  432. if (!wechat.isSupportedAPI(wx.createBannerAd)) return;
  433. clearInterval(this._bannerTimer);
  434. this.showBannerNext();
  435. this._bannerTimer = setInterval(() => {
  436. this.showBannerNext();
  437. }, times)
  438. }
  439. private showBannerNext() {
  440. if (this._bannerAdObjList.length <= 0) {
  441. console.error('zzzzzzz无banner广告');
  442. return;
  443. }
  444. this._bannerAdObjList.forEach(ad => {
  445. ad.hide();
  446. })
  447. this._bannerAdObjList[this._bannerShowIndex].hide();
  448. this._bannerShowIndex++;
  449. if (this._bannerShowIndex >= this._bannerAdObjList.length) {
  450. this._bannerShowIndex = 0;
  451. }
  452. let bannerAd = this._bannerAdObjList[this._bannerShowIndex];
  453. if (bannerAd) {
  454. bannerAd.show().catch(() => {
  455. let id = this._bannerAdUnitId[this._bannerShowIndex];
  456. bannerAd = this.createBanner(id);
  457. if (bannerAd) {
  458. bannerAd.show();
  459. }
  460. });
  461. } else {
  462. let id = this._bannerAdUnitId[this._bannerShowIndex];
  463. bannerAd = this.createBanner(id);
  464. if (bannerAd) {
  465. bannerAd.show();
  466. }
  467. }
  468. }
  469. /**
  470. * @description 显示banner广告
  471. * @param refreshTime 自动刷新时间,默认30秒,必须>=30秒;
  472. */
  473. // public showMainBanner(next?, refreshTime: number = 30) {
  474. // if (!wechat.isSupportedAPI(wx.createBannerAd)) return;
  475. // if (!this.mainBannerAD) {
  476. // this.mainCreateBannerAd(refreshTime, next);
  477. // return;
  478. // } else {
  479. // this.mainBannerAD.destroy();
  480. // this.mainBannerAD = null;
  481. // this.mainCreateBannerAd(refreshTime, next);
  482. // return;
  483. // }
  484. // }
  485. /**
  486. * @description 创建banner广告
  487. */
  488. // public mainCreateBannerAd(refreshTime, next?) {
  489. // console.log("banner width= ", this._bannerSize.width, this._bannerAdUnitId)
  490. // let bannerSize = {
  491. // // width: this._bannerSize.width / this.systemInfo.pixelRatio,
  492. // // height: this._bannerSize.height / this.systemInfo.pixelRatio
  493. // width: this._bannerSize.width * 0.65,
  494. // height: this._bannerSize.height * 0.35
  495. // };
  496. // this.mainBannerAD = wx.createBannerAd({
  497. // // 广告单元 id
  498. // adUnitId: this._bannerAdUnitId,
  499. // // 广告自动刷新的间隔时间,单位为秒,参数值必须大于等于30(该参数不传入时 Banner 广告不会自动刷新)
  500. // adIntervals: refreshTime,
  501. // style: {
  502. // left: this.systemInfo.windowWidth / 2 - bannerSize.width / 2,
  503. // top: this.systemInfo.windowHeight - bannerSize.height + this.systemInfo.windowHeight * 0.1,
  504. // height: bannerSize.height,
  505. // width: bannerSize.width
  506. // }
  507. // });
  508. // console.log('mainBanner')
  509. // this.mainBannerAD.onLoad(() => {
  510. // console.log('=====> @framework, banner广告加载成功');
  511. // // 移除setTimeout
  512. // if (this._bannerAdRefreshTimeOutId != 0) {
  513. // clearTimeout(this._bannerAdRefreshTimeOutId);
  514. // this._bannerAdRefreshTimeOutId = 0;
  515. // }
  516. // this.mainBannerAD.hide();
  517. // // 延时刷新banner
  518. // // if (refreshTime > 0) {
  519. // // this._bannerAdRefreshTimeOutId = setTimeout(() => {
  520. // // if (this.bannerAd)
  521. // // this.showBannerAd(refreshTime);
  522. // // }, refreshTime * 1000);
  523. // // }
  524. // next && next(true);
  525. // });
  526. // this.mainBannerAD.onError((res) => {
  527. // console.log('=====> @framework, banner广告加载失败:', res);
  528. // next && next(false);
  529. // });
  530. // // this.mainBannerAD.onResize((res: { width: number; height: number }) => {
  531. // // this.mainBannerAD.style.left = this.systemInfo.windowWidth / 2 - res.width / 2 - 0.1;
  532. // // this.mainBannerAD.style.top = this.systemInfo.windowHeight - res.height + 0.1;
  533. // // AD.setErrNodeY(-((this.bannerAd.style.top * winSize.height / this.systemInfo.windowHeight) - winSize.height / 2));
  534. // // });
  535. // }
  536. /**
  537. * @description 隐藏banner广告
  538. */
  539. public hideBannerAd() {
  540. if (this.bannerAd) {
  541. this.bannerAd.hide();
  542. }
  543. if (this._bannerAdRefreshTimeOutId != 0) {
  544. clearTimeout(this._bannerAdRefreshTimeOutId);
  545. this._bannerAdRefreshTimeOutId = 0;
  546. }
  547. clearInterval(this._bannerTimer);
  548. this._bannerAdObjList.forEach(ad => {
  549. ad.hide();
  550. })
  551. }
  552. /**
  553. * @description 销毁banner广告
  554. */
  555. public destroyBannerAd() {
  556. if (this.bannerAd) {
  557. this.bannerAd.destroy();
  558. this.bannerAd = null;
  559. }
  560. if (this._bannerAdRefreshTimeOutId != 0) {
  561. clearTimeout(this._bannerAdRefreshTimeOutId);
  562. this._bannerAdRefreshTimeOutId = 0;
  563. }
  564. }
  565. /**
  566. * @description 创建banner广告
  567. */
  568. public createBannerAd(refreshTime, next?, isShow = true) {
  569. console.log("banner width= ", this._bannerSize.width, this._bannerAdUnitId)
  570. let bannerSize = {
  571. // width: this._bannerSize.width / this.systemInfo.pixelRatio,
  572. // height: this._bannerSize.height / this.systemInfo.pixelRatio
  573. width: this._bannerSize.width * 0.65,
  574. height: this._bannerSize.height * 0.35
  575. };
  576. let id = this._bannerAdUnitId[Math.floor(Math.random() * this._bannerAdUnitId.length)];
  577. this.bannerAd = wx.createBannerAd({
  578. // 广告单元 id
  579. adUnitId: id,
  580. // 广告自动刷新的间隔时间,单位为秒,参数值必须大于等于30(该参数不传入时 Banner 广告不会自动刷新)
  581. adIntervals: 30,
  582. style: {
  583. left: this.systemInfo.windowWidth / 2 - bannerSize.width / 2,
  584. top: this.systemInfo.windowHeight - bannerSize.height + this.systemInfo.windowHeight * 0.1,
  585. height: bannerSize.height,
  586. width: bannerSize.width
  587. }
  588. });
  589. console.log('bannerSize: ', bannerSize, ', systemInfo: ', this.systemInfo, ',this.bannerAd.style: ', this.bannerAd.style);
  590. this.bannerAd.onLoad(() => {
  591. console.log('=====> @framework, banner广告加载成功');
  592. // 移除setTimeout
  593. if (this._bannerAdRefreshTimeOutId != 0) {
  594. clearTimeout(this._bannerAdRefreshTimeOutId);
  595. this._bannerAdRefreshTimeOutId = 0;
  596. }
  597. if (isShow) this.bannerAd.show();
  598. // 延时刷新banner
  599. // if (refreshTime > 0) {
  600. // this._bannerAdRefreshTimeOutId = setTimeout(() => {
  601. // if (this.bannerAd)
  602. // this.showBannerAd(refreshTime);
  603. // }, refreshTime * 1000);
  604. // }
  605. next && next(true);
  606. });
  607. this.bannerAd.onError((res) => {
  608. console.log('=====> @framework, banner广告加载失败:', res);
  609. next && next(false);
  610. });
  611. this.bannerAd.onResize((res: { width: number; height: number }) => {
  612. this.bannerAd.style.left = this.systemInfo.windowWidth / 2 - res.width / 2 - 0.1;
  613. this.bannerAd.style.top = this.systemInfo.windowHeight - res.height + 0.1;
  614. //AD.setErrNodeY(-((this.bannerAd.style.top * winSize.height / this.systemInfo.windowHeight) - winSize.height / 2));
  615. });
  616. }
  617. ////////////////////////////
  618. // 奖励广告
  619. ///////////////////////////
  620. public showRewardVideoAd() {
  621. return new Promise((resolve, reject) => {
  622. this.createRewardedVideoAd();
  623. if (!this.rewardedVideoAd) {
  624. console.log('=====> @framework, 奖励视频对象为不存在');
  625. reject();
  626. return null;
  627. }
  628. this.rewardedVideoAd.load().then(() => {
  629. this.rewardedVideoAd.show();
  630. });
  631. let closeListener = (res: { isEnded: boolean }) => {
  632. let isComplete: boolean;
  633. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  634. if (res && res.isEnded || res === undefined) {
  635. isComplete = true;
  636. resolve(isComplete);
  637. } else {
  638. isComplete = false;
  639. resolve(isComplete);
  640. }
  641. this.closeRewardVideo();
  642. this.rewardedVideoAd.offClose(closeListener);
  643. this.rewardedVideoAd = null;
  644. this.createRewardedVideoAd();
  645. };
  646. let errorListener = (res: { errMsg: string; errCode: number }) => {
  647. console.log('=====> @framework, 奖励视频发生错误:', res);
  648. // 再拉一次视频
  649. this.rewardedVideoAd.load()
  650. .then(() => {
  651. return this.rewardedVideoAd.show();
  652. })
  653. .then(() => {
  654. this.rewardedVideoAd.offError(errorListener);
  655. })
  656. .catch(() => {
  657. reject();
  658. this.rewardedVideoAd.offError(errorListener);
  659. });
  660. };
  661. this.rewardedVideoAd.onClose(closeListener);
  662. this.rewardedVideoAd.onError(errorListener);
  663. });
  664. }
  665. /**
  666. * @description 创建奖励视频单例(小游戏端是全局单例)
  667. */
  668. private createRewardedVideoAd(): void {
  669. if (!wx.createRewardedVideoAd) {
  670. console.log('=====> @framework,当前微信版本过低,无法使用奖励视频功能,请升级到最新微信版本后重试');
  671. return;
  672. }
  673. if (this.rewardedVideoAd) {
  674. return;
  675. }
  676. let id = Math.floor(Math.random() * this._rewardedVideoAdUnitId.length);
  677. this.rewardedVideoAd = wx.createRewardedVideoAd({
  678. adUnitId: this._rewardedVideoAdUnitId[id]
  679. });
  680. this.rewardedVideoAd.onLoad(() => {
  681. console.log('=====> @framework, 加载视频广告成功');
  682. });
  683. this.rewardedVideoAd.onError(() => {
  684. console.log('=====> @framework, 加载视频广告失败');
  685. })
  686. }
  687. private closeRewardVideo(): void {
  688. if (this._closeRewardVideoListener) {
  689. this._closeRewardVideoListener();
  690. }
  691. }
  692. ////////////////////////////
  693. // 插屏广告
  694. ///////////////////////////
  695. public showInterstitialAd(closeCb?): void {
  696. if (!wx.createInterstitialAd) {
  697. console.log('=====> @framework,当前微信版本过低,无法使用插屏广告功能,请升级到最新微信版本后重试');
  698. return;
  699. }
  700. this.createInterstitialAd();
  701. console.log("----> 显示插屏广告");
  702. this.interstitialAd.show();
  703. this.interstitialAd.onError((res) => {
  704. console.log('=====> @framework,插屏创建失败: ', res);
  705. closeCb && closeCb();
  706. });
  707. this.interstitialAd.onClose(res => {
  708. this.interstitialAd = null;
  709. console.log('----> 插屏 广告关闭')
  710. closeCb && closeCb();
  711. this.interstitialAd = null;
  712. this.createInterstitialAd();
  713. })
  714. }
  715. private createInterstitialAd() {
  716. if (!this.interstitialAd) {
  717. let id = this._interstitialAdUnitId[Math.floor(Math.random() * this._interstitialAdUnitId.length)];
  718. this.interstitialAd = wx.createInterstitialAd({
  719. adUnitId: id
  720. });
  721. this.interstitialAd.onLoad(() => {
  722. console.log('=====> @framework,插屏创建成功');
  723. });
  724. // this.interstitialAd.onError(() => {
  725. // console.log('=====> @framework,插屏创建失败');
  726. // });
  727. }
  728. }
  729. ////////////////////////////
  730. // 游戏圈
  731. ///////////////////////////
  732. /**
  733. * @description 创建微信游戏圈
  734. */
  735. public createGameClubButton(): void {
  736. // 微信圈是用户必要的功能,不需求弹出提示
  737. if (!wx.createGameClubButton) {
  738. console.log('=====> @framework,当前微信版本过低,无法使用游戏圈功能,请升级到最新微信版本后重试');
  739. return;
  740. }
  741. if (this.gameClub) {
  742. return;
  743. }
  744. this.gameClub = wx.createGameClubButton({
  745. icon: this._gameClubSize.icon,
  746. style: {
  747. left: this._gameClubSize.x / this.systemInfo.pixelRatio,
  748. top: this._gameClubSize.y / this.systemInfo.pixelRatio,
  749. width: this._gameClubSize.width,
  750. height: this._gameClubSize.height
  751. }
  752. });
  753. this.gameClub.hide();
  754. console.log('gameClub', this.gameClub);
  755. }
  756. /**
  757. * @description 显示微信游戏圈
  758. */
  759. public showGameClub(): void {
  760. console.log('显示游戏圈:', this.gameClub);
  761. this.createGameClubButton();
  762. if (this.gameClub) {
  763. this.gameClub.show();
  764. console.log('显示游戏圈成功');
  765. }
  766. }
  767. /**
  768. * @description 隐藏微信游戏圈
  769. */
  770. public hideGameClub(): void {
  771. if (this.gameClub) {
  772. this.gameClub.hide();
  773. }
  774. }
  775. ////////////////////////////
  776. // 开放数据域
  777. ///////////////////////////
  778. /**
  779. * @description 向开放数据域发送消息
  780. * @param msg 要发送的消息,message 中及嵌套对象中 key 的 value 只能是 primitive value。即 number、string、boolean、null、undefined
  781. */
  782. public postMessage(msg: any, info?: any): void {
  783. let data = {
  784. message: '',
  785. value: info
  786. }
  787. data.message = msg;
  788. data.value = info;
  789. console.log('发送信息: ', data);
  790. wx.getOpenDataContext().postMessage(data);
  791. }
  792. ////////////////////////////
  793. // 意见反馈
  794. ///////////////////////////
  795. /**
  796. * @description 创建打开意见反馈页面的按钮, 默认使用图片按钮的形式创建
  797. */
  798. public createFeedbackButton(cbOk, cbFail): void {
  799. if (wechat.isSupportedAPI(wx.createFeedbackButton)) {
  800. this.feedbackButton = wx.createFeedbackButton({
  801. type: 'image', // 按钮的类型.支持text、image
  802. image: this._feedbackButtonData.url,
  803. style: {
  804. left: this._feedbackButtonData.x, // 左上角横坐标
  805. top: this._feedbackButtonData.y, // 左上角纵坐标
  806. width: this._feedbackButtonData.width, // 宽度
  807. height: this._feedbackButtonData.height // 高度
  808. }
  809. });
  810. cbOk(this.feedbackButton);
  811. } else {
  812. console.log('=====> @framework,当前微信版本过低,无法使用反馈按钮,请升级到最新微信版本后重试');
  813. cbFail();
  814. }
  815. }
  816. /**
  817. * @description 监听意见反馈按钮的点击事件(在创建意见反馈按钮后,立即执行该函数,注册点击事件),只能使用一次
  818. */
  819. public onTapFeedbackButton(cbOk, cbFail): void {
  820. if (this.feedbackButton) {
  821. this.feedbackButton.onTap(() => {
  822. cbOk();
  823. });
  824. } else {
  825. cbFail();
  826. }
  827. }
  828. /**
  829. * @description 显示意见反馈按钮
  830. */
  831. public showFeedbackButton(): void {
  832. if (this.feedbackButton) {
  833. this.feedbackButton.show();
  834. }
  835. }
  836. /**
  837. * @description 隐藏意见反馈按钮
  838. */
  839. public hideFeedbackButton(): void {
  840. if (this.feedbackButton) {
  841. this.feedbackButton.hide();
  842. }
  843. }
  844. ////////////////////////////
  845. // 录屏
  846. ///////////////////////////
  847. /**
  848. * @description 获取全局唯一的游戏画面录制对象
  849. */
  850. public getGameRecorder(cbOk, cbFail): void {
  851. if (wechat.isSupportedAPI(wx.getGameRecorder)) {
  852. this.GameRecorder = wx.getGameRecorder();
  853. cbOk();
  854. } else {
  855. cbFail();
  856. }
  857. }
  858. public registerRecordScreenEvent(): void {
  859. if (!wechat.isSupportedAPI(wx.getGameRecorder)) {
  860. console.log('=====> @framework,当前客户端版过低,无法使用录制功能,请升级到最新客户端版本后重试');
  861. return;
  862. }
  863. this.GameRecorder = wx.getGameRecorder();
  864. // 监听录屏继续事件
  865. this.GameRecorder.on('start', (res: { currentTime?: number; error?: { code: number; message: string }; duration?: number }) => {
  866. if (this.resumeListener) {
  867. this.resumeListener();
  868. }
  869. });
  870. // 监听录屏暂停事件
  871. this.GameRecorder.on('pause', (res: { currentTime?: number; error?: { code: number; message: string }; duration?: number }) => {
  872. if (this.pauseListener) {
  873. this.pauseListener();
  874. }
  875. });
  876. // 监听录屏结束事件。可以通过 onStop 接口监听录屏结束事件,获得录屏地址
  877. this.GameRecorder.on('stop', (res: { currentTime?: number; error?: { code: number; message: string }; duration?: number }) => {
  878. if (this.stopListener) {
  879. console.log('视频的时长:', res.duration);
  880. this.stopListener(res.duration);
  881. }
  882. });
  883. // 监听录屏错误事件
  884. this.GameRecorder.on('error', (res: { currentTime?: number; error?: { code: number; message: string }; duration?: number }) => {
  885. console.log('=====> @framework,录屏错误:', res.error.message, ',code: ', res.error.code);
  886. if (this.errorListener) {
  887. this.errorListener();
  888. }
  889. });
  890. // 录制时间更新事件。在录制过程中触发该事件
  891. this.GameRecorder.on('timeUpdate', (res: { currentTime?: number; error?: { code: number; message: string }; duration?: number }) => {
  892. console.log('=====> @framework,录屏到第几秒:', res.currentTime);
  893. if (this.timeUpdateListener) {
  894. this.timeUpdateListener();
  895. }
  896. });
  897. // 录制取消事件
  898. this.GameRecorder.on('abort', (res: { currentTime?: number; error?: { code: number; message: string }; duration?: number }) => {
  899. if (this.abortListener) {
  900. this.abortListener();
  901. }
  902. });
  903. }
  904. /**
  905. * @description 开始录屏
  906. * @param callback
  907. */
  908. public startRecordScreen(callback?: Function): void {
  909. return
  910. this.startListener = callback;
  911. this.GameRecorder.start({
  912. fps: 24,
  913. duration: 7200,
  914. bitrate: 1000,
  915. gop: 12
  916. });
  917. }
  918. /**
  919. * @description 暂停录屏
  920. */
  921. public pauseRecordScreen(callback?: Function): void {
  922. this.pauseListener = callback;
  923. this.GameRecorder.pause();
  924. }
  925. /**
  926. * @description 恢复录制游戏画面
  927. * @param callback
  928. */
  929. public resumeRecordScreen(callback?: Function): void {
  930. this.resumeListener = callback;
  931. this.GameRecorder.resume();
  932. }
  933. /**
  934. * @description 结束录制游戏画面。结束录制后可以发起分享
  935. * @param callback
  936. */
  937. public stopRecordScreen(callback?: Function): void {
  938. this.stopListener = callback;
  939. this.GameRecorder.stop();
  940. }
  941. /**
  942. * @description 放弃录制游戏画面。此时已经录制的内容会被丢弃
  943. * @param callback
  944. */
  945. public abortRecordScreen(callback?: Function): void {
  946. this.abortListener = callback;
  947. this.GameRecorder.abort();
  948. }
  949. public createGameRecorderShareButton(): void {
  950. }
  951. ////////////////////////////
  952. // 其他能力
  953. ///////////////////////////
  954. /**
  955. * 监听小游戏回到前台的事件
  956. * @param callbak
  957. */
  958. public onShow(callbak): void {
  959. this.showListener = callbak;
  960. wx.onShow(this.onShowListener.bind(this));
  961. }
  962. /**
  963. * @description 取消监听小游戏回到前台的事件
  964. */
  965. public offShow(): void {
  966. wx.offShow(this.onShowListener);
  967. }
  968. /**
  969. * @description 监听小游戏隐藏到后台事件。锁屏、按 HOME 键退到桌面、显示在聊天顶部等操作会触发此事件
  970. * @param callbak
  971. */
  972. public onHide(callbak): void {
  973. this.hideListener = callbak;
  974. wx.onHide(this.onHideListener.bind(this));
  975. }
  976. /**
  977. * @description 取消监听小游戏隐藏到后台事件
  978. */
  979. public offHide(): void {
  980. this.hideListener = null;
  981. wx.offHide(this.onHideListener);
  982. }
  983. public onShowListener(res): void {
  984. if (this.showListener) {
  985. this.showListener(res);
  986. }
  987. }
  988. public onHideListener(): void {
  989. if (this.hideListener) {
  990. this.hideListener();
  991. }
  992. }
  993. /**
  994. * @description 打开另一个小程序
  995. * @param _appId
  996. * @param _path
  997. */
  998. public navigateToMiniProgram(_appId: string, _path: string, cbOk, cbFail): void {
  999. if (wechat.isSupportedAPI(wx.navigateToMiniProgram)) {
  1000. wx.navigateToMiniProgram({
  1001. appId: _appId,
  1002. path: _path,
  1003. envVersion: 'release',
  1004. success: () => {
  1005. cbOk();
  1006. },
  1007. fail: () => {
  1008. cbFail();
  1009. },
  1010. complete: () => {
  1011. }
  1012. });
  1013. } else {
  1014. cbFail();
  1015. }
  1016. }
  1017. ////////////////////////////
  1018. // 震动
  1019. ///////////////////////////
  1020. /**
  1021. * 使手机发生较长时间的振动。
  1022. */
  1023. /**
  1024. * 使手机发生较长时间的振动。
  1025. * @param cb 回调
  1026. */
  1027. public vibrateLong(cb?: Function): void {
  1028. if (cb)
  1029. console.warn("----> vibrateLong 微信可能没有cb");
  1030. wx.vibrateLong();
  1031. };
  1032. /**
  1033. * 使手机发生较短时间的振动
  1034. * @param cb 回调
  1035. */
  1036. public vibrateShort(cb?: Function): void {
  1037. console.warn("----> vibrateShort 微信可能没有cb");
  1038. wx.vibrateShort();
  1039. };
  1040. ////////////////////////////
  1041. // 通用
  1042. ///////////////////////////
  1043. /**
  1044. * @description 获取系统信息
  1045. */
  1046. public getSystemInfoSync(): void {
  1047. this.systemInfo = wx.getSystemInfoSync();
  1048. }
  1049. /**
  1050. * @description 是否支持改API
  1051. * @param api
  1052. */
  1053. private static isSupportedAPI(api: any): boolean {
  1054. if (api) {
  1055. return true;
  1056. } else {
  1057. wx.showModal({
  1058. title: '提示',
  1059. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  1060. });
  1061. return false;
  1062. }
  1063. }
  1064. /**
  1065. * 退出小程序
  1066. * @param cb
  1067. */
  1068. public exitMiniProgram(cb: any): void {
  1069. wx.exitMiniProgram(cb);
  1070. };
  1071. /**
  1072. * 显示灰色背景的消息提示框
  1073. * @param title 内容
  1074. * @param duration 提示框停留时间,单位ms
  1075. * @param callback 成功后回调
  1076. */
  1077. public showToast(title: string, duration: number = 3000, callback?: Function): void {
  1078. wx.showToast({
  1079. title: title,
  1080. duration: duration,
  1081. success: () => {
  1082. if (callback) callback();
  1083. }
  1084. });
  1085. };
  1086. /**
  1087. * 获取系统信息
  1088. * @param callback 成功后回调
  1089. */
  1090. public getSystemInfo(callback?: Function): void {
  1091. wx.getSystemInfo({
  1092. success: (res) => {
  1093. if (callback) callback(res);
  1094. }
  1095. });
  1096. };
  1097. /**
  1098. * 阿拉丁事件
  1099. * @param str
  1100. * @param objOrStr {key:string,value:string}
  1101. */
  1102. public aldSendEvent(str, objOrStr?): void {
  1103. console.log('send msg: ', str);
  1104. if (wx.aldSendEvent) {
  1105. if (objOrStr)
  1106. wx.aldSendEvent(str);
  1107. else
  1108. wx.aldSendEvent(str, objOrStr);
  1109. } else
  1110. console.error("----> 阿拉丁接入失败,请检查game.js");
  1111. }
  1112. /**
  1113. * 创建供量主广告实例
  1114. */
  1115. public createIconAd() {
  1116. let styleItemArray = [...Array(global.iconCount + 1).map((_, i) => ({
  1117. appNameHidden: false,
  1118. color: 'white',
  1119. size: 100,
  1120. borderWidth: 100,
  1121. borderColor: 'white',
  1122. left: 100 * i,
  1123. top: 100,
  1124. }))]
  1125. // 创建推荐位实例,提前初始化
  1126. this.LiconAD = wx.createGameIcon({
  1127. adUnitId: this._LIconAdunit,
  1128. count: global.iconCount,
  1129. style: styleItemArray
  1130. })
  1131. // this.RiconAD = wx.createGameIcon({
  1132. // adUnitId:this._RIconAdunit,
  1133. // count:global.iconCount,
  1134. // style:styleItemArray
  1135. // })
  1136. this.LiconAD.onError((res) => {
  1137. console.log('=====> @framework, 小游戏推荐左边icon加载失败:', res);
  1138. //next && next(false);
  1139. });
  1140. this.LiconAD.onLoad(() => {
  1141. console.log('=====> @framework, 小游戏推荐左边icon加载成功');
  1142. })
  1143. // this.RiconAD.onError((res)=>{
  1144. // console.log('=====> @framework, 小游戏推荐右边icon加载失败:',res);
  1145. // //next && next(false);
  1146. // });
  1147. // this.RiconAD.onLoad(()=>{
  1148. // console.log('=====> @framework, 小游戏推荐右边icon加载成功');
  1149. // })
  1150. this.LiconAD.onResize(size => {
  1151. //偶数 自适应排位置
  1152. var starTop = 0;
  1153. if (global.iconCount % 2 === 0) {
  1154. starTop = this.systemInfo.windowHeight / 2 - global.iconCount / 2 * size[0].height;
  1155. } else {
  1156. starTop = this.systemInfo.windowHeight / 2 - global.iconCount / 2 * size[0].height + size[0].height / 2;
  1157. }
  1158. for (let i = 0; i < size.length; i++) {
  1159. size[i].left = 10
  1160. size[i].top = starTop + (72 * i);
  1161. }
  1162. })
  1163. //同一时间只能展示一个,放弃右边的
  1164. // this.RiconAD.onResize(size =>{
  1165. // //偶数 自适应排位置
  1166. // var starTop = 0;
  1167. // if(global.iconCount%2 === 0){
  1168. // starTop = this.systemInfo.windowHeight/2 - global.iconCount/2*size[0].height;
  1169. // }else{
  1170. // starTop = this.systemInfo.windowHeight/2 - global.iconCount/2*size[0].height+size[0].height/2;
  1171. // }
  1172. // for (let i = 0; i < size.length; i++) {
  1173. // size[i].left = this.systemInfo.windowWidth-10
  1174. // size[i].top = starTop + (72*i);
  1175. // }
  1176. // })
  1177. }
  1178. /**
  1179. * 显示小游戏推荐广告
  1180. */
  1181. public showIconAd(dir: string) {
  1182. if (dir == 'R') {
  1183. if (this.RiconAD) {
  1184. this.RiconAD.load().then(() => {
  1185. this.RiconAD.show()
  1186. }).catch((err) => {
  1187. console.error(err)
  1188. })
  1189. } else {
  1190. setTimeout(this.createIconAd, 1000)
  1191. }
  1192. } else {
  1193. if (this.LiconAD) {
  1194. this.LiconAD.load().then(() => {
  1195. this.LiconAD.show()
  1196. }).catch((err) => {
  1197. console.error(err)
  1198. })
  1199. } else {
  1200. setTimeout(this.createIconAd, 1000)
  1201. }
  1202. }
  1203. }
  1204. /**创建格子广告 */
  1205. public createGridAd(count: number) {
  1206. this.gridAd = wx.createGridAd({
  1207. adUnitId: global.latticeAdunit,
  1208. adTheme: 'white',
  1209. gridCount: count,
  1210. adIntervals: 30,
  1211. style: {
  1212. left: 0,
  1213. top: this.systemInfo.windowHeight / 2 - 350,
  1214. width: 500,
  1215. opacity: 0.8
  1216. }
  1217. })
  1218. this.gridAd.onLoad(() => {
  1219. console.log('=====> @framework, 格子广告加载成功');
  1220. this.gridAdCunt = count;
  1221. });
  1222. this.gridAd.onError((res) => {
  1223. console.log('=====> @framework, 格子广告加载失败:', res);
  1224. });
  1225. }
  1226. /**显示格子广告 */
  1227. public showGridAd(count: number) {
  1228. //数量不一样的时候重新创建格子
  1229. if (count != this.gridAdCunt) {
  1230. if (this.gridAd) {
  1231. this.gridAd.destroy();
  1232. }
  1233. this.createGridAd(count);
  1234. }
  1235. this.gridAd.show();
  1236. }
  1237. /**隐藏格子广告 */
  1238. public hideGridAd() {
  1239. this.gridAd.hide();
  1240. }
  1241. /**
  1242. * 显示转发和分享到朋友圈
  1243. * @param bool 是否使用带 shareTicket 的转发详情
  1244. */
  1245. public showShareMenu(bool: boolean = false) {
  1246. if (window.wx) {
  1247. wx.showShareMenu({
  1248. withShareTicket: bool,
  1249. menus: ['shareAppMessage', 'shareTimeline']
  1250. })
  1251. }
  1252. }
  1253. }
  1254. export const Wechat = wechat._instance;