关于微信小游戏中微信接口的部分功能

  1. import Singleton from "../core/Singleton";
  2. import GameConfig from "../GameConfig";
  3. import GameSetting from "../GameSetting";
  4.  
  5. export class WXUserInfo {
  6.     nickName: string;
  7.     avatarUrl: string;
  8.     gender: number;
  9.     country: string;
  10.     province: string;
  11.     city: string;
  12.     language: string;
  13. }
  14.  

  15.  
  16. export default class WXToolManager {
  17.  
  18.     public wxUserInfo: WXUserInfo = new WXUserInfo();
  19.     private userButton: UserInfoButton;
  20.  
  21.     public static get instance(): WXToolManager {
  22.         return Singleton.getInstance<WXToolManager>(WXToolManager);
  23.     }
  24.  
  25.     public showBannerAD() {
  26.         this.OpenBanner("adunit-549b2e8b53ad8e21");
  27.     }
  28.  
  29.     public showVideoAd() {
  30.        this.OpenAD("adunit-549b2e8b53ad8e21");
  31.     }
  32.  
  33.     /**
  34.     * 创建转发功能, 显示当前面页面的转发功能。
  35.     */
  36.     public showShareMenu: Function = ($title: string, $img: string): void => {
  37.         let $self = this;
  38.         $img = Laya.URL.basePath + $img;
  39.         wx.showShareMenu({
  40.         withShareTicket: true,
  41.             success: () => {
  42.                 wx.onShareAppMessage((): any => {
  43.                 return {
  44.                         title: $title,
  45.                         imageUrl: $img
  46.                 }
  47.             });
  48.         },
  49.         fail: (): void => {
  50.         },
  51.         complete: (): void => { }
  52.         });
  53.     }
  54.  
  55.     /**
  56.     * 主动拉起转发,进入选择通讯录界面
  57.     *
  58.     * query: 查询字符串,从这条转发消息进入后,可通过 wx.getLaunchOptionsSync() 或 wx.onShow() 获取启动参数中的      query。
  59.     * 必须是 key1=val1&key2=val2 的格式。
  60.     */
  61.  
  62.     public showAppMessage: Function = ($title: string, $img: string, $query: string): void => {
  63.         let $self = this;
  64.         $img = Laya.URL.basePath + $img;
  65.         wx.shareAppMessage({
  66.                 title: $title,
  67.                 imageUrl: $img,
  68.                 query: $query
  69.         });
  70.     }
  71.  
  72.     public buyItem(quantity: number, callBack: Function, goodsIndex: number) {
  73.         wx.requestMidasPayment({
  74.                 mode: 'game',
  75.                 env: 1, // 1: 沙箱环境 0: 米大师正式环境
  76.                 offerId: '', // 在米大师申请的应用 id
  77.                 currencyType: 'CNY',
  78.                 platform: 'android',
  79.                 buyQuantity: quantity, // 购买数量。
  80.                 zoneId: 1,
  81.  
  82.                 success: (res) => {
  83.                         callBack(true, goodsIndex);
  84.                 },
  85.  
  86.                 fail: (res) => {
  87.                         callBack(false, goodsIndex);
  88.                 }
  89.             });
  90.         }
  91.  
  92.         public createAuthorizeButton(callBack: Function): UserInfoButton {
  93.                 if (!this.userButton) {
  94.                         this.userButton = wx.createUserInfoButton({ type: 'text', text: '', style: { width: 640, height: 1136,                                           backgroundColr: '#ff0000', color: '#ff0000', textAlign: 'center', fontSize: 16, borderRadius: 4 } });
  95.                         this.userButton.onTap((res) => {
  96.                                 this.wxAuthorization(this.userButton, callBack);
  97.                         });
  98.                 }
  99.                 return this.userButton;
  100.         }
  101.  
  102.         public createUser(callBack: Function): void {
  103.                 this.wxAuthorization(this.userButton, callBack);
  104.         }
  105.  
  106.         public setUserCloudStorage(data: Number): void {
  107.                 var kvDataList = [];
  108.                 var obj:any = {};
  109.                 obj.wxgame ={};
  110.                 obj.wxgame.value1 = data;
  111.                 obj.wxgame.update_time = Laya.Browser.now();
  112.                 kvDataList.push({"key":"tfmFriendsRank","value":JSON.stringify(obj)});
  113.                 wx.setUserCloudStorage({
  114.                         KVDataList:kvDataList,
  115.                         success:function(e):void{
  116.                         },
  117.                         fail:function(e):void{
  118.                         },
  119.                         complete:function(e):void{
  120.                         }
  121.                 });
  122.         }
  123.  
  124.         private wxAuthorization: Function = ($button?: UserInfoButton, callBack?: Function): void => {
  125.                 wx.getSetting({
  126.                 success: (result: _getSettingSuccessObject): void => {
  127.                         if (result.authSetting['scope.userInfo']) {//已经授权了
  128.                         if ($button) {
  129.                                 $button.hide();
  130.                                 $button.offTap;
  131.                           }
  132.                         //获取微信玩家信息
  133.                         this.getUserInfo(callBack);
  134.                         } else {//授权失败
  135.                                 // this.createUser(callBack);
  136.                                 callBack(false);
  137.                         }
  138.                 },
  139.                 fail: (): void => {
  140.                         this.createUser(callBack);
  141.                         callBack(false);
  142.                 },
  143.                 complete: (): void => {
  144.                 }
  145.         });
  146.     }
  147.  
  148.         private getUserInfo(callBack?: Function): void {
  149.                 wx.getUserInfo({
  150.                 withCredentials: true,
  151.                 lang: "zh_CN",
  152.                 success: (result: _getUserInfoSuccessObject): void => {
  153.                 this.wxUserInfo.nickName = result.userInfo["nickName"];
  154.                 this.wxUserInfo.avatarUrl = result.userInfo["avatarUrl"];
  155.                 this.wxUserInfo.gender = result.userInfo["gender"];
  156.                 this.wxUserInfo.country = result.userInfo["country"];
  157.                 this.wxUserInfo.province = result.userInfo["province"];
  158.                 this.wxUserInfo.city = result.userInfo["city"];
  159.                 this.wxUserInfo.language = result.userInfo["language"];
  160.  
  161.                 callBack(true);
  162.         },
  163.         fail: (): void => {
  164.                 console.warn(`微信获取玩家信息失败!(授权)`);
  165.                 callBack(false);
  166.         },
  167.         complete: (): void => {
  168.  
  169.         }
  170.         });
  171.     }
  172.  
  173.         //打开banner广告
  174.         private OpenBanner(id) {
  175.         //创建banner广告
  176.         const bannerAd = wx.createBannerAd({
  177.         adUnitId: id,//banner id
  178.         style: {
  179.                 left: 10,
  180.                 top: 76,
  181.                 width: 320
  182.         }
  183.         });
  184.         //加载banner
  185.         bannerAd.onLoad(() => {
  186.                 console.log('banner 广告加载成功')
  187.                 bannerAd.style.width = 400;//随时设置banner宽
  188.                 bannerAd.show();//调用打开banner
  189.         })
  190.         bannerAd.onError(err => {
  191.                 console.log('banner 广告加载失败' + err)
  192.         });
  193.         }
  194.         //打开激励广告
  195.         private OpenAD(id) {
  196.                 //创建激励广告
  197.                 const video = wx.createRewardedVideoAd(
  198.                 { adUnitId: id }//传入广告id
  199.         );
  200.  
  201.         video.load().then(() => {
  202.                 video.show();//load结束调用打开广告
  203.                 }).catch(err => console.log(err.errMsg))
  204.  
  205.                 video.onError(err => {
  206.                         console.error("打开广告失败" + err);
  207.                 });
  208.                 video.onClose(res => {
  209.                 // 用户点击了【关闭广告】按钮
  210.                 // 小于 2.1.0 的基础库版本,res 是一个 undefined
  211.                 if (res && res.isEnded || res === undefined) {
  212.                 // 正常播放结束,可以下发游戏奖励
  213.                         console.error("正常关闭广告了");
  214.                 video.offClose();//注意取消这个视频的监听close
  215.                 } else {
  216.                         // 播放中途退出,不下发游戏奖励
  217.                         console.error("播放中途关闭广告了");
  218.                         video.offClose();//注意取消这个视频的监听close
  219.                 }
  220.         });
  221.     }
  222.  
  223.         public showRewardVideoAd(): Promise<boolean> {
  224.                 return new Promise<boolean>((reslove, reject) => {
  225.                 let ad = this.getRewardedVideoAd();
  226.                 ad.load().then(() => {
  227.                         ad.show();
  228.                         ad.onClose(res => {
  229.                         if (res && res.isEnded || res === undefined) {
  230.                                 reslove(true);
  231.                         } else {
  232.                                 reslove(false);
  233.                         }
  234.                 });
  235.         }).catch(err => {
  236.                 console.log(err.errMsg);
  237.                 reject(err);
  238.         })
  239.         });
  240.         }
  241.  
  242.         private getRewardedVideoAd(): any {
  243.                 return wx.createRewardedVideoAd(
  244.                         { adUnitId: "xxxxxx" }
  245.                 );
  246.         }
  247.  
  248.   }

  249.  
发布了10 篇原创文章 · 获赞 6 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/youmangu/article/details/94016227