- import Singleton from "../core/Singleton";
- import GameConfig from "../GameConfig";
- import GameSetting from "../GameSetting";
- export class WXUserInfo {
- nickName: string;
- avatarUrl: string;
- gender: number;
- country: string;
- province: string;
- city: string;
- language: string;
- }
- export default class WXToolManager {
- public wxUserInfo: WXUserInfo = new WXUserInfo();
- private userButton: UserInfoButton;
- public static get instance(): WXToolManager {
- return Singleton.getInstance<WXToolManager>(WXToolManager);
- }
- public showBannerAD() {
- this.OpenBanner("adunit-549b2e8b53ad8e21");
- }
- public showVideoAd() {
- this.OpenAD("adunit-549b2e8b53ad8e21");
- }
- /**
- * 创建转发功能, 显示当前面页面的转发功能。
- */
- public showShareMenu: Function = ($title: string, $img: string): void => {
- let $self = this;
- $img = Laya.URL.basePath + $img;
- wx.showShareMenu({
- withShareTicket: true,
- success: () => {
- wx.onShareAppMessage((): any => {
- return {
- title: $title,
- imageUrl: $img
- }
- });
- },
- fail: (): void => {
- },
- complete: (): void => { }
- });
- }
- /**
- * 主动拉起转发,进入选择通讯录界面
- *
- * query: 查询字符串,从这条转发消息进入后,可通过 wx.getLaunchOptionsSync() 或 wx.onShow() 获取启动参数中的 query。
- * 必须是 key1=val1&key2=val2 的格式。
- */
- public showAppMessage: Function = ($title: string, $img: string, $query: string): void => {
- let $self = this;
- $img = Laya.URL.basePath + $img;
- wx.shareAppMessage({
- title: $title,
- imageUrl: $img,
- query: $query
- });
- }
- public buyItem(quantity: number, callBack: Function, goodsIndex: number) {
- wx.requestMidasPayment({
- mode: 'game',
- env: 1, // 1: 沙箱环境 0: 米大师正式环境
- offerId: '', // 在米大师申请的应用 id
- currencyType: 'CNY',
- platform: 'android',
- buyQuantity: quantity, // 购买数量。
- zoneId: 1,
- success: (res) => {
- callBack(true, goodsIndex);
- },
- fail: (res) => {
- callBack(false, goodsIndex);
- }
- });
- }
- public createAuthorizeButton(callBack: Function): UserInfoButton {
- if (!this.userButton) {
- this.userButton = wx.createUserInfoButton({ type: 'text', text: '', style: { width: 640, height: 1136, backgroundColr: '#ff0000', color: '#ff0000', textAlign: 'center', fontSize: 16, borderRadius: 4 } });
- this.userButton.onTap((res) => {
- this.wxAuthorization(this.userButton, callBack);
- });
- }
- return this.userButton;
- }
- public createUser(callBack: Function): void {
- this.wxAuthorization(this.userButton, callBack);
- }
- public setUserCloudStorage(data: Number): void {
- var kvDataList = [];
- var obj:any = {};
- obj.wxgame ={};
- obj.wxgame.value1 = data;
- obj.wxgame.update_time = Laya.Browser.now();
- kvDataList.push({"key":"tfmFriendsRank","value":JSON.stringify(obj)});
- wx.setUserCloudStorage({
- KVDataList:kvDataList,
- success:function(e):void{
- },
- fail:function(e):void{
- },
- complete:function(e):void{
- }
- });
- }
- private wxAuthorization: Function = ($button?: UserInfoButton, callBack?: Function): void => {
- wx.getSetting({
- success: (result: _getSettingSuccessObject): void => {
- if (result.authSetting['scope.userInfo']) {//已经授权了
- if ($button) {
- $button.hide();
- $button.offTap;
- }
- //获取微信玩家信息
- this.getUserInfo(callBack);
- } else {//授权失败
- // this.createUser(callBack);
- callBack(false);
- }
- },
- fail: (): void => {
- this.createUser(callBack);
- callBack(false);
- },
- complete: (): void => {
- }
- });
- }
- private getUserInfo(callBack?: Function): void {
- wx.getUserInfo({
- withCredentials: true,
- lang: "zh_CN",
- success: (result: _getUserInfoSuccessObject): void => {
- this.wxUserInfo.nickName = result.userInfo["nickName"];
- this.wxUserInfo.avatarUrl = result.userInfo["avatarUrl"];
- this.wxUserInfo.gender = result.userInfo["gender"];
- this.wxUserInfo.country = result.userInfo["country"];
- this.wxUserInfo.province = result.userInfo["province"];
- this.wxUserInfo.city = result.userInfo["city"];
- this.wxUserInfo.language = result.userInfo["language"];
- callBack(true);
- },
- fail: (): void => {
- console.warn(`微信获取玩家信息失败!(授权)`);
- callBack(false);
- },
- complete: (): void => {
- }
- });
- }
- //打开banner广告
- private OpenBanner(id) {
- //创建banner广告
- const bannerAd = wx.createBannerAd({
- adUnitId: id,//banner id
- style: {
- left: 10,
- top: 76,
- width: 320
- }
- });
- //加载banner
- bannerAd.onLoad(() => {
- console.log('banner 广告加载成功')
- bannerAd.style.width = 400;//随时设置banner宽
- bannerAd.show();//调用打开banner
- })
- bannerAd.onError(err => {
- console.log('banner 广告加载失败' + err)
- });
- }
- //打开激励广告
- private OpenAD(id) {
- //创建激励广告
- const video = wx.createRewardedVideoAd(
- { adUnitId: id }//传入广告id
- );
- video.load().then(() => {
- video.show();//load结束调用打开广告
- }).catch(err => console.log(err.errMsg))
- video.onError(err => {
- console.error("打开广告失败" + err);
- });
- video.onClose(res => {
- // 用户点击了【关闭广告】按钮
- // 小于 2.1.0 的基础库版本,res 是一个 undefined
- if (res && res.isEnded || res === undefined) {
- // 正常播放结束,可以下发游戏奖励
- console.error("正常关闭广告了");
- video.offClose();//注意取消这个视频的监听close
- } else {
- // 播放中途退出,不下发游戏奖励
- console.error("播放中途关闭广告了");
- video.offClose();//注意取消这个视频的监听close
- }
- });
- }
- public showRewardVideoAd(): Promise<boolean> {
- return new Promise<boolean>((reslove, reject) => {
- let ad = this.getRewardedVideoAd();
- ad.load().then(() => {
- ad.show();
- ad.onClose(res => {
- if (res && res.isEnded || res === undefined) {
- reslove(true);
- } else {
- reslove(false);
- }
- });
- }).catch(err => {
- console.log(err.errMsg);
- reject(err);
- })
- });
- }
- private getRewardedVideoAd(): any {
- return wx.createRewardedVideoAd(
- { adUnitId: "xxxxxx" }
- );
- }
- }
关于微信小游戏中微信接口的部分功能
最新推荐文章于 2024-07-28 23:58:13 发布