PlatformWX.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import { Adapter } from "../adapter/Adapter";
  2. import { Constants } from "../data/Constants";
  3. const WX_CONFIG = {
  4. AD_ID: {
  5. /** banner广告 */
  6. banner: 'adunit-fa7b08ac6d8cf200',
  7. /** 视频广告 */
  8. video: 'adunit-29b5f0955ee77f59',
  9. /** 插屏广告 */
  10. interstitial: 'adunit-a3615ebe31eba64a',
  11. /** 原生模板多个格子广告 */
  12. multiGridCustom: 'adunit-xxx',
  13. /** 原生模版单格格子广告 */
  14. sigleGridCustom: 'adunit-d4f43001efd879de',
  15. }
  16. }
  17. const delayTime: number = 1900;
  18. class PlatformWX {
  19. private _onHideTime: number = Infinity;
  20. private _rewardVideo: wx.RewardedVideoAd;
  21. private _bannerAd: wx.BannerAd;
  22. private _interstitialAd: any;
  23. private _customAd: any;
  24. private _sigleCustomAd: any;
  25. private _shareSuccessHandler: Function = null;
  26. private _videoSuccessHandler: Function = null;
  27. private _customAdCloseHandler: Function = null;
  28. private _showInterstitialAdTime: number = null;
  29. private _shareModalIsActive: boolean = false;
  30. private _shareStr: string = "东北F4来到美国西部拓荒时代,保卫城镇,完成自我救赎。";
  31. public initialize(): void {
  32. console.log("初始化微信框架");
  33. wx.showShareMenu({
  34. withShareTicket: true,
  35. menus: ['shareAppMessage', 'shareTimeline'],
  36. success: () => { },
  37. fail: () => { },
  38. complete: () => { }
  39. });
  40. //发送给朋友
  41. wx.onShareAppMessage(function(){
  42. wx.shareAppMessage({
  43. imageUrl: "share.jpg",
  44. title: this._shareStr,
  45. query: 'shareMsg='+'a=1',
  46. });
  47. }.bind(this));
  48. wx.onShow((res: {
  49. scene: string;
  50. query: any;
  51. shareTicket: string;
  52. referrerInfo: wx.ReferrerInfo;
  53. }) => {
  54. if (this._shareSuccessHandler == null) return;
  55. if (this._shareModalIsActive) return;
  56. if (Date.now() - this._onHideTime >= delayTime) {
  57. if (this._shareSuccessHandler) this._shareSuccessHandler(true);
  58. } else {
  59. wx.showModal({
  60. title: '分享提示',
  61. content: '成功分享到群即可获得奖励',
  62. success: (res) => {
  63. if (res.confirm) {
  64. this.shareShowReward(this._shareSuccessHandler(false));
  65. } else if (res.cancel) {
  66. this._shareSuccessHandler = null;
  67. }
  68. this._shareModalIsActive = false;
  69. }
  70. });
  71. this._shareModalIsActive = true;
  72. }
  73. this._onHideTime = Infinity;
  74. });
  75. //this.__createBannerAd();
  76. this.__createVideoAd();
  77. //this.__createCustomAd();
  78. //this.__createSigleCustomAd();
  79. //this.__createInterstitialAd();
  80. //this.__customAdExist();
  81. //this.__customSigleAdExist();
  82. }
  83. public showToast(param: { title: string, icon?: 'success' | 'loading' | 'none', image?: string, duration?: number, mask?: boolean, success?: (res?: any) => void, fail?: (res?: any) => void, complete?: (res?: any) => void }): void {
  84. let finalParam = {
  85. title: '',
  86. icon: 'none',
  87. duration: 2000,
  88. } as any;
  89. Object.assign(finalParam, param);
  90. wx.showToast(finalParam);
  91. }
  92. public shareAppMessage(param: { title?: string, imageUrl?: string, query?: string, imageUrlId?: string } = {}): void {
  93. wx.shareAppMessage(param);
  94. }
  95. public vibrateShort(param: any = {}): void {
  96. wx.vibrateShort(param);
  97. }
  98. public vibrateLong(param: any = {}): void {
  99. wx.vibrateLong(param);
  100. }
  101. /**
  102. * 是否支持添加桌面
  103. * @returns
  104. */
  105. public isSupportDesk(){
  106. //@ts-ignore
  107. if (wx.canAddToFavorites) {
  108. return true;
  109. }else{
  110. return false;
  111. }
  112. }
  113. //添加到桌面
  114. public addDesk(){
  115. //@ts-ignore
  116. if (wx.canAddToFavorites) {
  117. //@ts-ignore
  118. wx.canAddToFavorites({
  119. success(res) {
  120. if (res.canAdd) {
  121. //可以添加到桌面
  122. //@ts-ignore
  123. wx.addToFavorites({
  124. // 小程序的相关信息
  125. title: Constants.gameName,
  126. query: '', // 可以携带参数
  127. success() {
  128. this.showToast({title: '添加到桌面成功'});
  129. },
  130. fail(err) {
  131. this.showToast({title: '添加到桌面失败', err});
  132. }
  133. });
  134. } else {
  135. // 不支持添加到桌面
  136. this.showToast({title: '当前环境不支持添加到桌面'});
  137. }
  138. },
  139. fail(err) {
  140. this.showToast({title: '检查添加到桌面权限失败', err});
  141. }
  142. });
  143. } else {
  144. this.showToast({title: "当前环境不支持添加到桌面"});
  145. }
  146. }
  147. //分享
  148. public shareShowReward(handler: Function = null): void {
  149. if(handler){
  150. this._shareSuccessHandler = handler;
  151. }
  152. this._onHideTime = Date.now();
  153. this.shareAppMessage({
  154. imageUrl: "share.jpg",
  155. title: this._shareStr,
  156. query: 'shareMsg='+'a=1',
  157. });
  158. }
  159. public showRewardVideo(handler: Function = null): void {
  160. //console.log('当日视频次数: ', ArchiveSystem.localData.videoRewardTimesToday);
  161. /*if (ArchiveSystem.localData.videoRewardTimesToday % 2 === 1) {
  162. cc.log(ArchiveSystem.localData.videoRewardTimesToday, '转为分享');
  163. this.__turnVideoToShare(handler);
  164. return;
  165. }*/
  166. if (!this._rewardVideo) {
  167. this.__turnVideoToShare(handler);
  168. return;
  169. }
  170. this._videoSuccessHandler = handler;
  171. this._rewardVideo
  172. .show()
  173. .then(() => {
  174. console.log("视频播放成功");
  175. })
  176. .catch(err => {
  177. this._rewardVideo.load()
  178. .then(() => {
  179. this._rewardVideo
  180. .show()
  181. .then(() => {
  182. console.log("视频重新加载,播放成功");
  183. });
  184. })
  185. .catch(err2 => {
  186. this._videoSuccessHandler?.(false);
  187. this.shareShowReward(handler);
  188. this._videoSuccessHandler = null;
  189. });
  190. });
  191. }
  192. public showBannerAd(): void {
  193. if (!this._bannerAd) return;
  194. const { screenWidth, screenHeight } = wx.getSystemInfoSync();
  195. this._bannerAd.show()
  196. .then(() => {
  197. this._bannerAd.style.left = (screenWidth - this._bannerAd.style.realWidth) / 2;
  198. this._bannerAd.style.top = screenHeight - this._bannerAd.style.realHeight;
  199. }).catch((e: any) => {
  200. console.log('banner广告显示出错', e);
  201. });
  202. }
  203. public hideBannerAd(recreate: boolean = false): void {
  204. if (!this._bannerAd) return;
  205. this._bannerAd.hide();
  206. if (recreate) {
  207. this.refreshBannerAd();
  208. }
  209. }
  210. public refreshBannerAd(): void {
  211. if (!this._bannerAd) return;
  212. this._bannerAd.destroy();
  213. this.__createBannerAd();
  214. }
  215. public showInterstitialAd(force: boolean = false): void {
  216. if(force){
  217. if (!this._interstitialAd) return;
  218. this._interstitialAd.show();
  219. }else{
  220. if (!this._interstitialAd) return;
  221. let timeNow = Date.now();
  222. if (this._showInterstitialAdTime == null) {
  223. this._showInterstitialAdTime = timeNow;
  224. return;
  225. }
  226. if (timeNow - this._showInterstitialAdTime > 120 * 1000) {
  227. this._interstitialAd.show();
  228. this._showInterstitialAdTime = timeNow;
  229. }
  230. }
  231. }
  232. public showCustomAd(close: Function): void {
  233. if (!this._customAd) return;
  234. this._customAdCloseHandler = close;
  235. this._customAd
  236. .show()
  237. .catch((e: any) => {
  238. console.log('原生广告显示出错', e);
  239. });
  240. }
  241. public hideCustomAd(): void {
  242. if (!this._customAd) return;
  243. this._customAd
  244. .hide()
  245. .catch((e: any) => {
  246. console.log('原生广告隐藏出错', e);
  247. });
  248. }
  249. public showSigleCustomAd(): void {
  250. if (!this._sigleCustomAd
  251. || false) return;
  252. this._sigleCustomAd
  253. .show()
  254. .catch((e: any) => {
  255. console.log('原生当个格子广告显示出错', e);
  256. });
  257. }
  258. public hideSigleCustomAd(): void {
  259. if (!this._sigleCustomAd
  260. || false) return;
  261. this._sigleCustomAd
  262. .hide()
  263. .catch((e: any) => {
  264. console.log('隐藏单个格子广告出错', e);
  265. });
  266. }
  267. /**单个格子广告*/
  268. public __customSigleAdExist(): void {
  269. if(!this._sigleCustomAd){
  270. this.__createSigleCustomAd();
  271. }
  272. this._sigleCustomAd.show()
  273. .then(() => {
  274. if(this._sigleCustomAd.isShow()){
  275. this.hideSigleCustomAd();
  276. }
  277. })
  278. .catch((res) => {
  279. if(this._sigleCustomAd.isShow()){
  280. this.hideSigleCustomAd();
  281. }
  282. });
  283. }
  284. /**多个格子广告*/
  285. public __customAdExist(): void {
  286. if(!this._customAd){
  287. this.__createCustomAd();
  288. }
  289. this._customAd.show()
  290. .then(() => {
  291. if(this._customAd.isShow()){
  292. this.hideCustomAd();
  293. }
  294. })
  295. .catch((res) => {
  296. if(this._customAd.isShow()){
  297. this.hideCustomAd();
  298. }
  299. });
  300. }
  301. public exitGame(): void {
  302. wx.exitMiniProgram({});
  303. }
  304. private __createVideoAd(): void {
  305. if (!WX_CONFIG.AD_ID.video) return;
  306. this._rewardVideo = wx.createRewardedVideoAd({ adUnitId: WX_CONFIG.AD_ID.video });
  307. // 注册视频的onClose事件
  308. this._rewardVideo.onClose(res => {
  309. if (res && res.isEnded || res === undefined) {
  310. this._videoSuccessHandler?.(true);
  311. } else {
  312. this.showToast({
  313. title: "完整观看视频才有奖励哦"
  314. });
  315. this._videoSuccessHandler?.(false);
  316. }
  317. this._videoSuccessHandler = null;
  318. });
  319. // 注册视频的onError事件
  320. this._rewardVideo.onError(res => {
  321. this._videoSuccessHandler?.(false);
  322. this.showToast({
  323. title: "暂时没有可观看的视频"
  324. });
  325. this.shareShowReward(this._videoSuccessHandler);
  326. this._videoSuccessHandler = null;
  327. });
  328. }
  329. private __createBannerAd(): void {
  330. if (!WX_CONFIG.AD_ID.banner) return;
  331. let info = wx.getSystemInfoSync();
  332. let f = Adapter.isNormalScreen();
  333. let w = f ? info.screenWidth * 0.85 : info.screenWidth * 0.85;
  334. this._bannerAd = wx.createBannerAd({
  335. adUnitId: WX_CONFIG.AD_ID.banner,
  336. adIntervals: 48,
  337. style: {
  338. left: 0,
  339. top: info.screenHeight,
  340. width: w,
  341. height: 70 //w * (f ? 0.48 : 0.5) //原始0
  342. }
  343. });
  344. this._bannerAd.onError(err => {
  345. switch (err.errCode) {
  346. case 1001: {
  347. break;
  348. }
  349. case 1004: {
  350. break;
  351. }
  352. }
  353. });
  354. this._bannerAd.onResize(() => {});
  355. }
  356. private __createInterstitialAd(): void {
  357. if (!WX_CONFIG.AD_ID.interstitial) return;
  358. this._interstitialAd = wx.createInterstitialAd({ adUnitId: WX_CONFIG.AD_ID.interstitial });
  359. }
  360. private __createCustomAd(): void {
  361. if (!WX_CONFIG.AD_ID.multiGridCustom) return;
  362. let info = wx.getSystemInfoSync();
  363. this._customAd = wx.createCustomAd({
  364. adUnitId: WX_CONFIG.AD_ID.multiGridCustom,
  365. adIntervals: 60,
  366. style: {
  367. left: 0,
  368. top: info.screenHeight * 0.16,
  369. with: info.screenWidth * 0.9
  370. }
  371. });
  372. if(this._customAd){
  373. this._customAd.onHide(res => {
  374. if(this._customAdCloseHandler){this._customAdCloseHandler()};
  375. });
  376. this._customAd.onError(err => {
  377. switch (err.errCode) {
  378. case 1001: {
  379. break;
  380. }
  381. case 1004: {
  382. break;
  383. }
  384. }
  385. });
  386. }
  387. }
  388. private __createSigleCustomAd(): void {
  389. if (!WX_CONFIG.AD_ID.sigleGridCustom) return;
  390. let info = wx.getSystemInfoSync();
  391. let f = Adapter.isNormalScreen();
  392. this._sigleCustomAd = wx.createCustomAd({
  393. adUnitId: WX_CONFIG.AD_ID.sigleGridCustom,
  394. adIntervals: 60,
  395. style: {
  396. left: info.screenWidth * 0.49,
  397. top: info.screenHeight * 0.038 + (f ? 20 : 35)
  398. }
  399. });
  400. this._sigleCustomAd.onError(err => {
  401. console.log('展示格子广告失败1');
  402. });
  403. }
  404. private __turnVideoToShare(handler: Function): void {
  405. this.shareShowReward(handler);
  406. this._videoSuccessHandler = null;
  407. }
  408. }
  409. export default new PlatformWX();