微信小程序和抖音小程序的分享和广告接入代码

开发完成小程序或者小游戏之后,我们为什么要接入分享和广告视频功能,主要原因有以下几个方面。

微信小程序和抖音小程序接入分享和广告功能主要基于以下几个原因:

  1. 用户获取与增长:分享功能可以帮助用户将小程序内容传播给更多人,从而扩大用户基础。通过社交网络的分享,能够吸引更多潜在用户使用小程序。

  2. 增强用户互动:分享功能可以提升用户之间的互动,增加用户粘性。用户在分享内容时,往往会带来新的使用场景和体验,这样可以促进用户的积极参与。

  3. 商业变现:广告功能为小程序提供了一种商业变现的方式。通过广告,开发者可以获得收入,从而支持小程序的持续运营与发展。

目前大部分小程序和小游戏都是靠广告变现,所以接入广告的代码是所有开发者必不可少的环节了,下面给大家讲解一下如何加入分享和广告接入的代码。

文章最后整合了一个工具类,有需要的朋友可以直接复制使用.

微信小程序

分享功能
  1. 在页面逻辑中设置分享:

  • 在小程序的页面代码中,使用 onShareAppMessage 函数设置分享内容:

  • 
    Page({  
      onShareAppMessage: function () {  
        return {  
          title: '分享标题',  
          path: '/pages/example/example?id=123',  
          imageUrl: '/images/share.png' // 可选  
        };  
      }  
    });

    2. 用户触发分享:

  • 可以在页面中添加一个分享按钮,使用 wx.showShareMenu 显示分享菜单:

  • 
    wx.showShareMenu({  
      withShareTicket: true // 可选,是否使用带分享票据的分享  
    });
    广告接入
  • 1,接入广告组件:
    • 在小程序的页面中引入广告组件,例如 banner 广告:

    • 
      <ad unit-id="你的广告位ID" />

      由于广告目前是各大平台的主要收入来源,所以微信和抖音都是花了很大的力气让开发者尽可能地简单接入广告代码。就这样一句代码,加到你的小程序里面,他就会源源不断地输送广告。

    • 输送的广告完全是依靠大数据,你喜欢看什么,就给你推什么广告。

        

2,初始化广告:

  • 在对应的 JavaScript 文件中,您可以监控广告加载事件:

  • 
    const adInstance = wx.createBannerAd({  
      adUnitId: '你的广告位ID',  
      style: {  
        left: 0,  
        top: 0,  
        width: 300  
      }  
    });  
    
    adInstance.onLoad(() => {  
      console.log('广告加载成功');  
    });  
    
    adInstance.onError((err) => {  
      console.error('广告加载失败', err);  
    });

    抖音小程序

    分享功能
  • 在页面中设置分享行为:

    • 在 页面 的 JS 文件中,使用 onShareAppMessage

    • 
      Page({  
        onShareAppMessage: function () {  
          return {  
            title: '抖音小程序分享',   
            path: '/pages/example/example?id=123',  
          };  
        }  
      });

      2.主动分享:

    • 通过按钮或其他触发事件调用 my.share 方法:

    • 
      my.share({  
        title: '分享标题',  
        url: '分享链接',  
        success: (result) => {  
          console.log(result);  
        },  
        fail: (error) => {  
          console.error(error);  
        }  
      });
      广告接入
    • 引入广告组件:

      • 在页面的 HTML 代码中添加广告组件:

      • 
        <ad unit-id="你的广告位ID" />

        广告初始化和监控:

        通过 JavaScript 控制广告的加载和监控

      • const ad = my.createBannerAd({  
          adUnitId: '你的广告位ID',  
          style: {  
            top: 0,  
            left: 0,  
            width: '100%',  
          },  
        });  
        
        ad.onLoad(() => {  
          console.log('广告加载成功');  
        });  
        
        ad.onError((err) => {  
          console.error('广告加载失败', err);  
        });

虽然看起来非常简单,但是实际上开发起来还是不方便,所以我写了一套完整的小游戏开发的工具类,满足了各大平台的要求.包括登录,分享,插屏广告,激励广告,banner广告,抖音侧边栏功能.

/**
 * 小游戏平台SDK工具封装,目前只支持微信和抖音平台
 */
export namespace MiniGameSdk {

    interface ISize {
        width: number;
        height: number;
    }

    export interface IPosition {
        top: number;
        left: number;
    }

    export function isWechat(): boolean {
        //@ts-ignore
        return window.wx !== null && window.wx !== undefined;
    }

    export function isBytedance(): boolean {
        //@ts-ignore
        return window.tt !== null && window.tt !== undefined;
    }

    // 支付宝平台
    export function isAli(): boolean {
        //@ts-ignore
        return window.my !== null && window.my !== undefined;
    }

    function getSysWinSize(): ISize {
        let sys: any;
        if (isWechat()) {
            // @ts-ignore
            sys = wx.getSystemInfoSync();
        } else if (isBytedance()) {
            // @ts-ignore
            sys = tt.getSystemInfoSync();
        }

        let size: ISize = { width: 0, height: 0 };
        if (sys) {
            size.width = sys.windowWidth;
            size.height = sys.windowHeight;
        }

        return size;
    }

    /**
     * 插屏广告。微信抖音都支持!
     */
    class ADInterstitial {
        private _adUid: string;
        private _interstitial: any;

        get aduid() {
            return this._adUid;
        }

        constructor(adUid: string) {
            this._adUid = adUid;
        }

        show() {
            // @ts-ignore
            if (isWechat() && !wx.createInterstitialAd) {
                console.warn('wechat unsupport interstitial AD!');
                this._interstitial = null;
                return;
            }

            // @ts-ignore
            if (isBytedance() && !tt.createInterstitialAd) {
                console.warn('bytedance unsupport interstitial AD!');
                this._interstitial = null;
                return;
            }


            if (this._interstitial) {
                this._interstitial.load();
            } else {
                if (isWechat()) {
                    // @ts-ignore
                    this._interstitial = wx.createInterstitialAd({ adUnitId: this._adUid });
                } else if (isBytedance()) {
                    // @ts-ignore
                    this._interstitial = tt.createInterstitialAd({ adUnitId: this._adUid });
                } else {
                    this._interstitial = null;
                }

                this._interstitial?.onLoad(() => {
                    console.log('load interstitial ad success');
                    this._interstitial.show().catch((err: any) => {
                        console.log('catch interstitial ad error:', err);
                    });
                });

                this._interstitial?.onError((err: any) => {
                    console.log('interstitial ad on error:', err);
                });
            }
        }
        destory() {
            this._interstitial?.destroy();
        }
    }

    class ADBanner {
        private _adUid: string;
        private _banner: any;

        get aduid() {
            return this._adUid;
        }

        /**
         * 抖音和微信都支持
         * 横幅广告。预估宽度默认为300,预估高度为140。如果你不确定就按默认值来。
         * @param adUid 广告UID,后端配置
         * @param isTop 是否在屏幕顶部展示。内部会自动居中计算位置。
         * @param bannerWidth 横幅广告的预估宽度。默认300
         * @param autoShow 广告加载完成后是否立刻显示,默认为不显示
         */
        constructor(adUid: string, param: boolean | IPosition, bannerWidth: number = 300, autoShow: boolean = false) {
            this._adUid = adUid;
            this.create(autoShow, bannerWidth, param); // 默认300比较合适
        }

        private create(autoShow: boolean, bannerWidth: number, param: boolean | IPosition) {
            if (!isWechat() && !isBytedance()) {
                this._banner = null;
                return;
            }

            this.destroy();

            let winSize = getSysWinSize();

            let height = bannerWidth * 0.4;
            let top = 0, left = 0;

            if (typeof param === "boolean") {
                left = (winSize.width - bannerWidth) / 2
                top = param ? 5 : (winSize.height - height);
            } else {
                left = param.left;
                top = param.top;
            }

            let params = {
                adUnitId: this._adUid,
                adIntervals: 30,// 自动刷新频率不能小于30秒
                style: { left: left, top: top, width: bannerWidth }
            }

            if (isWechat()) {
                // @ts-ignore
                this._banner = wx.createBannerAd(params);
            } else if (isBytedance()) {
                // @ts-ignore
                this._banner = tt.createBannerAd(params);
            } else {
                this._banner = null;
            }

            this._banner?.onError((err: any) => {
                console.log('ad banner error:', err);
            });

            this._banner?.onLoad(() => {
                autoShow && this._banner.show();
            });
        }

        show() {
            this._banner?.show();
        }

        hide() {
            this._banner?.hide();
        }

        destroy() {
            this._banner?.destroy();
        }
    }

    class ADCustom {
        private _adUid: string;
        private _adCustom: any;

        get aduid() {
            return this._adUid;
        }
        /**
         * 由于原生模板广告在微信服务后端可以定制宽度大小,个数,缩放比例等,所以位置调整要根据设置的宽度来定。抖音不支持!
         * @param adUid 广告UID,后端配置
         * @param top 从左上角开始,距离屏幕顶部的距离。注意:这个数据为设备屏幕宽度width。如果需要获取屏幕的像素,需要乘以设备像素比Pixel-Ratio,例如iPhone 13 Pro的Pixel-Ratio为3,像素为Width*3。
         * @param left 从左上角开始,距离屏幕最左边的距离。注意:这个数据为设备屏幕宽度width。如果需要获取屏幕的像素,需要乘以设备像素比Pixel-Ratio,例如iPhone 13 Pro的Pixel-Ratio为3,像素为Width*3。
         * @param scale 原生模板广告的尺寸,默认为1,即100%。此值在微信服务后端广告中获得,默认为100%,目前有100%,90%,80%三种,一般情况不用修改。若有修改,记得传入值,例如90%就传入0.9。
         */
        constructor(adUid: string, top: number = 0, left: number = 0, scale: number = 1.0) {
            this._adUid = adUid;
            this.createCustomAd(top, left, scale);
        }

        private createCustomAd(top: number, left: number, scale: number) {
            if (!isWechat()) { // only wechat support custom ad
                this._adCustom = null;
                console.log('Only wechat support Custom Ad');
                return;
            }

            this.destroy();
            // 原生模板5个应用宽度为375,若设置了缩放比例,则宽度也需要设置
            // let width = 375 * this._scale;
            // let newLeft = (sys.windowWidth - width) / 2;
            // let newTop = sys.windowHeight / 2; // 120是预估高度

            // @ts-ignore
            this._adCustom = wx.createCustomAd({
                adUnitId: this._adUid,
                style: { left: left, top: top, fixed: true }
            });

            this._adCustom?.onError((err: any) => {
                console.log('ad custom error:', err);
            });
        }
        show() {
            this._adCustom?.show();
        }

        hide() {
            this._adCustom?.hide();
        }

        destroy() {
            this._adCustom?.destroy();
        }
    }


    /**
     * 视频广告用户点击行为结果
     */
    export enum EAdVideoResult {
        /**
         * 用户看完了广告,游戏可发放奖励。
         */
        ACCEPT,

        /**
         * 用户中途关闭了广告,即未看完状态。不可发放奖励。
         */
        REJECT,

        /**
         * 广告组件内部发生了错误。不可发放奖励。
         */
        ERROR,
    }

    class ADVideo {
        private _adUid: string;
        private _adVideo: any = null;

        get aduid() {
            return this._adUid;
        }

        constructor(adUid: string) {
            this._adUid = adUid;
        }

        /**
         * 由于微信和抖音视频广告机制不同,微信可以看的视频广告个数只有0和1个,抖音平台则可以看0~maxVideoCount
         * @param onResult 两个参数:第一个res是EAdVideoResult定义,第二count是用户看了多少个视频广告。
         * @param target onResult的拥有者
         * @param maxVideoCount 可以连续看最大视频个数,可最大化商业效率。默认为3个。
         * @returns 
         */
        show(onResult: (res: EAdVideoResult, count: number) => void, target?: any, maxVideoCount: number = 3): void {
            let callback = (state: EAdVideoResult, count: number) => {
                onResult?.call(target, state, count);
            }

            if (!isWechat() && !isBytedance()) {
                callback(EAdVideoResult.ACCEPT, 1);
                this._adVideo = null;
                return;
            }

            let onAdVideoClosed = (res: any) => {
                this._adVideo?.offClose(onAdVideoClosed);
                if (isWechat()) {
                    if (res && res.isEnded || res === undefined) {
                        callback(EAdVideoResult.ACCEPT, 1);
                    } else {
                        callback(EAdVideoResult.REJECT, 0);
                    }
                } else if (isBytedance()) {
                    let resConverted = res as { isEnded: boolean, count: number };
                    if (resConverted && resConverted.count > 0) {
                        callback(EAdVideoResult.ACCEPT, resConverted.count);
                    } else {
                        callback(EAdVideoResult.REJECT, 0);
                    }
                }
            }

            this._adVideo?.offClose(onAdVideoClosed);

            if (isWechat()) {
                // @ts-ignore
                this._adVideo = wx.createRewardedVideoAd({
                    adUnitId: this._adUid
                });
            } else if (isBytedance()) {
                // @ts-ignore
                this._adVideo = tt.createRewardedVideoAd({
                    adUnitId: this._adUid,
                    multiton: true,
                    multitonRewardMsg: ['多1次奖励', '再多一次奖励', '再多一次奖励'],
                    multitonRewardTimes: maxVideoCount,
                });
            } else {
                this._adVideo = null;
            }



            this._adVideo?.onLoad(() => {
                console.log('Ad load success');
            });

            this._adVideo?.onError((err: { errMsg: string, errCode: number }) => {
                console.log('Ad video error:', err);
                callback(EAdVideoResult.ERROR, 0);
            });

            this._adVideo?.onClose(onAdVideoClosed);

            console.info("广告2");
            this._adVideo?.show().catch(() => {
                this._adVideo?.load().then(() =>
                    this._adVideo?.show()).catch((err: { errMsg: string, errCode: number }) => {
                        console.log('Catch video ad error:', err);
                        callback(EAdVideoResult.ERROR, 0);
                    });
            });
        }

        destory() {
            this._adVideo?.destory();
        }
    }

    export enum EAdBannerLocation {
        /**
         * 屏幕顶部
         */
        TOP,

        /**
         * 屏幕底部
         */
        BOTTOM,
    }

    export class AdvertManager {

        private static _instance: AdvertManager;

        static get instance(): AdvertManager {
            if (!AdvertManager._instance) {
                AdvertManager._instance = new AdvertManager();
            }
            return AdvertManager._instance;
        }

        private _video: ADVideo;
        private _interstitial: ADInterstitial;
        private _banner: ADBanner;
        private _customs: Record<string, ADCustom> = {};

        private constructor() {

        }

        /**
         * 预加载横幅广告,不会显示。只有你在调用showBanner时才会显示。
         * 可重复调用,但是会销毁上一次的实例。一般情况,全局有一个就行了,太多占用内存,而且没必要。
         * @param adUid 广告UID
         * @param location 位置有两种情况:1、可以传入枚举值,默认上方; 2、可以自定义位置传入IPosition,注意IPosition中的top和left跟平台的top,left是一致(没有乘以设备像素比ratio),需要开发者自己调试位置
         * @param scale 默认为跟屏幕一样的宽度,可以通过设置缩放比例来调整大小。当然,平台有规定最大或最小宽度,函数内部会自动计算。
         */
        public loadBanner(adUid: string, location: EAdBannerLocation | IPosition = EAdBannerLocation.TOP, scale: number = 1.0) {
            this._banner?.destroy();
            let size: ISize = getSysWinSize();
            // 当 style.width 小于 300 时,会取作 300。 当 style.width 大于屏幕宽度时,会取作屏幕宽度。
            let width = size.width * scale;
            width = width < 300 ? 300 : width; // 最小值矫正
            width = width > size.width ? size.width : width; //最大值矫正
            this._banner = typeof location === 'number' ? new ADBanner(adUid, location === EAdBannerLocation.TOP, width, false) : new ADBanner(adUid, location, width, false);
        }

        /**
         * 显示横幅广告
         */
        public showBanner() {
            if (this._banner) {
                this._banner.show();
            } else {
                console.warn('MiniGameSDK: banner is null, you must call loadBanner(...) first!');
            }
        }

        /**
         * 隐藏横幅广告
         */
        public hideBanner() {
            this._banner?.hide();
        }

        /**
         * 弹出插屏广告
         * @param adUid 广告单元id
         */
        public showInterstitial(adUid: string) {
            if (this._interstitial && this._interstitial.aduid === adUid) {
                this._interstitial.show();
            } else {
                this._interstitial?.destory();
                this._interstitial = new ADInterstitial(adUid);
                this._interstitial.show();
            }
        }

        /**
         * 加载原生模板广告,不会显示。只有你在调用showCustom时才会显示。
         * 由于原生模板广告在微信服务后端可以定制宽度大小,个数,缩放比例等,所以位置调整要根据设置的宽度来定。抖音不支持本函数,会调用无效!
         * @param adUid 广告ID
         * @param location 位置有两种情况:1、可以传入枚举值,默认上方; 2、可以自定义位置传入IPosition,注意IPosition中的top和left跟平台的top,left是一致(没有乘以设备像素比ratio),需要开发者自己调试位置
         * @param scale 缩放比例,默认是1,即不缩放。这个缩放并不是自己填,而是根据微信MP后台你配置的原生模板广告的缩放比例填,目前有100%,90%,80%三种,一般情况不用修改。若有后台修改,记得传入值,例如90%就传入0.9。
         */
        public loadCustom(adUid: string, location: IPosition = { top: 0, left: 0 }, scale: number = 1) {
            // this._custom?.destroy();
            // this._custom = new ADCustom(adUid, location.top, location.left, scale);
            if (this._customs[adUid]) {
                console.log(`${adUid} has been loaded.`);
                return;
            }

            this._customs[adUid] = new ADCustom(adUid, location.top, location.left, scale);
        }

        /**
         * 显示自定义广告。
         * @param adUid 广告的唯一标识符。使用此标识符来查找和显示特定的自定义广告。
         * 
         * 此方法尝试根据提供的adUid显示一个自定义广告。如果给定的adUid对应的自定义广告已加载,
         * 则调用该广告的显示方法。如果广告未加载,则在控制台输出警告信息。
         */
        public showCustom(adUid: string) {
            if (this._customs[adUid]) {
                this._customs[adUid].show();
            } else {
                console.warn(`You have not load ${adUid} of Custom AD, can not show!`);
            }
        }

        /**
         * 隐藏指定的自定义广告单元
         * 
         * 此方法用于隐藏通过广告单元标识符(adUid)指定的自定义广告。如果指定的广告单元已加载并显示,
         * 则将其隐藏;如果广告单元未加载,则在控制台输出警告信息。
         * 
         * @param adUid 广告单元标识符,用于唯一标识一个自定义广告单元。
         */
        public hideCustom(adUid: string) {
            if (this._customs[adUid]) {
                this._customs[adUid].hide();
            } else {
                console.warn(`You have not load ${adUid} of Custom AD, can not hide!`);
            }
        }

        /**
         * 由于微信和抖音视频广告机制不同,微信可以看的视频广告个数只有0和1个,抖音平台则可以看0~maxVideoCount
         * @param adUid 广告ID。如果与上一次UID不同,则内部会重新创建实例。开发者完全不用关心这个细节。
         * @param onVideoResult 两个参数:第一个res是EAdVideoResult定义,第二count是用户看了多少个视频广告。 
         * @param target onVideoResult的拥有者
         * @param maxVideoCount 最大视频个数。默认是3,仅对抖音平台生效。微信平台看完视频count的结果永远是1或0
         */
        public showVideo(adUid: string, onVideoResult: (res: EAdVideoResult, count: number) => void, target?: any, maxVideoCount: number = 3) {
            
             
            

            if (this._video && this._video.aduid === adUid) {
                console.info('show','进来了');
                this._video.show(onVideoResult, target, maxVideoCount);
            } else {
                this._video?.destory();
                this._video = new ADVideo(adUid);
                this._video.show(onVideoResult, target, maxVideoCount);
            }
        }

        /**
         * 销毁内部所有实例,清空内存
         */
        public destroyAll() {
            this._banner?.destroy();
            this._banner = null;

            this._interstitial?.destory();
            this._interstitial = null;

            this._video?.destory();
            this._video = null;

            if (this._customs) {
                for (let val in this._customs) {
                    this._customs[val]?.destroy();
                }
                this._customs = {};
            }
        }
    }

    export enum EGameClubIcon {
        /** 绿色图标 */
        GREEN = 'green',

        /** 红色图标 */
        WHITE = 'white',

        /** 有黑色圆角背景的白色图标 */
        DARK = 'dark',

        /** 有白色圆角背景的绿色图标 */
        LIGHT = 'light'
    }

    export class GameClub {
        private static _instance: GameClub;

        static get instance(): GameClub {
            if (!this._instance) {
                this._instance = new GameClub();
            }
            return this._instance;
        }

        private _club: any;

        private constructor() {

        }

        /**
         * 创建游戏圈按钮
         * @param icon 
         * @param position 
         * @param size 
         * @param openLink 
         */
        create(icon: EGameClubIcon = EGameClubIcon.GREEN, position: IPosition = { top: 0, left: 0 }, size: ISize = { width: 40, height: 40 }, openLink?: string) {
            if (isWechat()) {
                // @ts-ignore
                this._club = wx.createGameClubButton({
                    icon: icon,
                    style: {
                        left: position.left,
                        top: position.top,
                        width: size.width,
                        height: size.height
                    },
                    openlink: openLink
                });
            }
        }

        show() {
            this._club?.show();
        }

        hide() {
            this._club?.hide();
        }

        destory() {
            this._club?.destroy();
        }
    }


    /**
     * 振动类型
     */
    export enum EVirbrateType {
        /**
         * 短振动
         */
        SHORT,

        /**
         * 长振动
         */
        LONG
    }

    /**
     * 平台常用API合集
     */
    export class API {
        private static _loginCode: string = null;
        private static _loginAnonymousCode: string = null;
        private static _hasInitWechatCloudFunction: boolean = false;


        /**
         * 分享app给朋友,微信小游戏分享是没有onSuccess回调的。
         * @param title 标题
         * @param description 细节描述信息 
         * @param imageUrl 图片地址
         * @param query 查询信息
         * @param onSuccess 抖音会回调,微信不会回调
         */
        static shareAppToFriends(title: string, description: string = '', imageUrl?: string, query?: string, onSuccess?: () => void) {
            if (isWechat()) {
                try {
                    //@ts-ignore
                    wx.shareAppMessage({
                        title: title,
                        imageUrl: imageUrl,
                        query: query,
                    });
                } catch (err) {
                    console.log(`share faild: ${err}`);
                }
            }

            if (isBytedance()) {
                //@ts-ignore
                console.info('字节分享方法');
                tt.shareAppMessage({
                    title: title,
                    desc: description,
                    imageUrl: imageUrl ?? '',
                    query: query ?? '',
                    success(res: any) {
                        console.log('share success:', res);
                        onSuccess?.();
                    },
                    fail(res: any) {
                        console.log('share fail:', res);
                    }
                });
            }
        }

        /**
         * 显示提示信息
         * @param title 标题
         * @param duration 时长(单位:秒)
         * @returns 
         */
        static showToast(title: string, duration: number = 2) {
            if (isWechat()) {
                // @ts-ignore
                wx.showToast({
                    title: title,
                    icon: 'success',
                    duration: duration * 1000
                });
            }

            if (isBytedance()) {
                //@ts-ignore
                tt.showToast({
                    title: title,
                    duration: duration * 1000,
                    success(res: any) {
                        console.log(`${res}`);
                    },
                    fail(res: any) {
                        console.log(`showToast调用失败`);
                    },
                });
            }
        }

        /**
         * 设备震动效果,默认为短震动。注意:可能一些机型不会生效,具体看平台方的说明
         * @param type MiniGameSdk.API.EVirbrateType
         */
        static vibrate(type: EVirbrateType = EVirbrateType.SHORT) {
            if (isWechat()) {
                switch (type) {
                    case EVirbrateType.SHORT:
                        //@ts-ignore
                        wx.vibrateShort({
                            success(res: any) {
                                console.log('vibrate success:', res);
                            },
                            fail(res: any) {
                                console.log('vibrateShort failed', res);
                            },
                        });
                        break;
                    case EVirbrateType.LONG:
                        //@ts-ignore
                        wx.vibrateLong({
                            success(res: any) {
                                console.log('vibrate success', res);
                            },
                            fail(res: any) {
                                console.log(`vibrateLong failed`, res);
                            },
                        });
                        break;
                    default:
                        break;
                }
            }

            if (isBytedance()) {
                switch (type) {
                    case EVirbrateType.SHORT:
                        //@ts-ignore
                        tt.vibrateShort({
                            success(res: any) {
                                console.log('vibrate success:', res);
                            },
                            fail(res: any) {
                                console.log('vibrateShort failed', res);
                            },
                        });
                        break;
                    case EVirbrateType.LONG:
                        //@ts-ignore
                        tt.vibrateLong({
                            success(res: any) {
                                console.log('vibrate success', res);
                            },
                            fail(res: any) {
                                console.log(`vibrateLong failed`, res);
                            },
                        });
                        break;
                    default:
                        break;
                }
            }
        }

        /**
         * 重启小游戏
         */
        static reboot() {
            if (isWechat()) {
                //@ts-ignore
                wx.restartMiniProgram({
                    success: () => {
                        console.log('restart success');
                    },

                    fail: () => {
                        console.log('restart failed');
                    }
                })
            }

            if (isBytedance()) {
                try {
                    // @ts-ignore
                    tt.restartMiniProgramSync();
                } catch (error) {
                    console.log(`restartMiniProgramSync`, error);
                }
            }
        }

        /**
         * 退出小游戏
         */
        static exit() {
            if (isWechat()) {
                //@ts-ignore
                wx.exitMiniProgram({
                    success: () => {
                        console.log('exit success');
                    },
                    fail: () => {
                        console.log('exit failed');
                    }
                });
            }

            if (isBytedance()) {
                // @ts-ignore
                tt.exitMiniProgram({
                    success(res: any) {
                        console.log("exit success:", res?.data);
                    },
                    fail(res: any) {
                        console.log("exit fail:", res?.errMsg);
                    },
                });
            }
        }

        /**
         * 显示转发按钮。通常在刚进入游戏的时候调用。
         * 主要是打开平台“...”这个按钮里面的分享菜单,一般默认是关闭的,需要调用这个函数打开。可以让用户分享你的游戏入口。
         */
        static showShareMenu() {
            if (isWechat()) {
                //@ts-ignore
                wx.showShareMenu({
                    withShareTicket: true,
                    menus: ['shareAppMessage', 'shareTimeline'],
                    success: () => { },
                    fail: () => { },
                    complete: () => { }
                });
            }

            if (isBytedance()) {
                //@ts-ignore
                tt.showShareMenu({
                    success(res: any) {
                        console.log("show menu is showing");
                    },
                    fail(err: any) {
                        console.log("showShareMenu:", err.errMsg);
                    },
                    complete(res: any) {
                        console.log("showShareMenu complete");
                    },
                });
            }
        }

        /**
         * 微信小游戏:跳转到另外一款小游戏
         * 抖音小游戏:跳转到指定的视频界面
         * @param targetId 微信小游戏appid或者视频界面
         */
        static navigateTo(targetId: string, onSuccess?: () => void) {
            if (isWechat()) {
                // @ts-ignore
                wx.navigateToMiniProgram({
                    appId: targetId,
                    extraData: {
                        foo: 'bar'
                    },
                    envVersion: 'develop',
                    success(res: any) {
                        onSuccess?.();
                    }
                });
            }

            if (isBytedance()) {
                // @ts-ignore
                tt.navigateToVideoView({
                    videoId: targetId,
                    success: (res: any) => {
                        onSuccess?.();
                    },
                    fail: (err: any) => {
                        console.log("bytedance navigateToVideoView fail", err);
                    },
                });
            }
        }


        /**
         * 小游戏平台登录功能。微信返回code,抖音返回code和anonymousCode。用于登录的凭证,需要把这个code传回你的服务器程序中去调用code2Session
         * @param callback (code, anonymousCode) 第一个参数为code,微信和抖音都支持;第二个参数为匿名设备ID,仅抖音支持,失败都返回null
         */
        static login(callback: (code: string, anonymousCode: string) => void) {
            let loginPlatform = () => {
                if (isWechat()) {
                    //@ts-ignore
                    wx.login({
                        success: (res: { code: any; errMsg: any; }) => {
                            if (res.code) {
                                API._loginCode = res.code;
                                API._loginAnonymousCode = null;
                                callback?.(API._loginCode, API._loginAnonymousCode);
                            } else {
                                console.log('login error:', res.errMsg)
                            }
                        },

                        fail: () => {
                            API._loginCode = null;
                            API._loginAnonymousCode = null;
                            callback?.(API._loginCode, API._loginAnonymousCode);
                            console.log('login fail')
                        }
                    });
                } else if (isBytedance()) {
                    //@ts-ignore
                    tt.login({
                        force: true,
                        success(res: any) {
                            console.log(`login ${res.code} ${res.anonymousCode}`);
                            if (res.code) {
                                API._loginCode = res.code?.toString();
                                API._loginAnonymousCode = res.anonymousCode?.toString();
                                callback?.(API._loginCode, API._loginAnonymousCode);
                            } else {
                                console.log('login error:', res.errMsg)
                            }
                        },
                        fail(res: any) {
                            API._loginCode = null;
                            API._loginAnonymousCode = null;
                            callback?.(API._loginCode, API._loginAnonymousCode);
                            console.log(`login fail`, res);
                        },
                    });
                } else {
                    API._loginCode = null;
                    API._loginAnonymousCode = null;
                    callback?.(API._loginCode, API._loginAnonymousCode);
                    console.log('not mini game platform, login codes are all null');
                }
            }



            if (!API._loginCode) {
                loginPlatform();
            } else {
                if (isWechat()) {
                    //@ts-ignore
                    wx.checkSession({
                        success() {
                            console.log(`session is valid, use current code:`, API._loginCode);
                            callback?.(API._loginCode, API._loginAnonymousCode);
                        },
                        fail() {
                            console.log(`session expired`);
                            loginPlatform();
                        }
                    });
                } else if (isBytedance()) {
                    //@ts-ignore
                    tt.checkSession({
                        success() {
                            console.log(`session is valid, user current code: ${API._loginCode}, ${API._loginAnonymousCode}`);
                            callback?.(API._loginCode, API._loginAnonymousCode);
                        },
                        fail() {
                            console.log(`session expired`);
                            loginPlatform();
                        },
                    });

                } else {
                    console.log('not mini game platform, login null');
                    callback?.(null, null);
                }
            }
        }

        /**
         * 调用微信云函数。由于参数需要自定义,所以为any,需要自行解释。函数只完成通道和处理一场的作用
         * @param callback 返回云函数调用结果。需要检查返回参数是否为空,失败的时候为空
         * @param name 云函数的名字
         * @param data 云函数的内容
         */
        static callWechatCloudFunction(callback: (res: any) => void, name: string, data: {}) {
            if (!isWechat()) {
                console.log('Not wechat platform, not support callWechatCloudFunction');
                return;
            }

            this.login((code: string, anonymousCode: string) => {
                if (!API._hasInitWechatCloudFunction) {
                    //@ts-ignore
                    wx.cloud.init();
                    API._hasInitWechatCloudFunction = true;
                }
                //@ts-ignore
                wx.cloud.callFunction({
                    name: name,
                    data: data,
                    success: (res: any) => callback?.(res),
                    fail: (err: any) => {
                        console.log('wechat cloud function error:', err);
                        callback?.(null);
                    }
                });
            });
        }

        /**
         * 存储用户信息,数据量不能大。可以考虑用于分数排行榜。用户之间可共享排行数据。
         * @param key 
         * @param value 
         */
        static setUserCloudStorage(key: string, value: string) {
            if (isWechat()) {
                // @ts-ignore
                wx.setUserCloudStorage({
                    KVDataList: [{ key: key, value: value }],
                    success: () => console.log(`set cloud storage success:${key}, value:${value}`),
                    fail: (err: any) => console.log('set cloud storage error:', err)
                });
            }

            if (isBytedance()) {
                // @ts-ignore
                tt.setUserCloudStorage({
                    KVDataList: [{ key: key, value: value, }],
                    success: () => console.log(`set cloud storage success:${key}, value:${value}`),
                    fail: (err: any) => console.log('set cloud storage error:', err)
                });
            }
        }
    }


    /**
     * 抖音侧边栏专属接口
     */
    export class BytedanceSidebar {
        /**
         * 本游戏在抖音环境下启动监控,需要放在全局环境中,保证能第一时间启动。因为可能监听抖音失败(抖音小游戏官方的说明)!
         * @param onResult 包含一个boolean参数的函数
         * @param target 上述函数的拥有者,如果是类的成员函数,需要传入this。普通或匿名函数忽略即可。
         */
        static listenFromSidebar(onResult: (success: boolean) => void, target?: any) {
            if (!isBytedance()) {
                onResult?.call(target, false);
                return;
            }
            // @ts-ignore
            tt.onShow((res: any) => {
                console.log('onShow launch res:', res);
                if (res.scene === '021036') {
                    onResult?.call(target, true);
                    console.log('launch from sidebar');
                } else {
                    onResult?.call(target, false);
                    console.log('NOT launch from douyin sidebar!');
                }
            });

            // @ts-ignore
            let options = tt.getLaunchOptionsSync();
            if (options && options.scene === '021036') {
                onResult?.call(target, true);
            }
        }

        /**
         * 检测抖音侧边栏是否存在
         * @param onResult 包含一个boolean参数的函数
         * @param target 上述函数的拥有者,如果是类的成员函数,需要传入this。普通或匿名函数忽略即可。
         * @returns 
         */
        static checkSideBar(onResult: (success: boolean) => void, target?: any) {
            if (!isBytedance()) {
                onResult?.call(target, false);
                return;
            }

            //@ts-ignore
            tt.checkScene({
                scene: "sidebar",
                success: (res: any) => {
                    console.log("check scene success: ", res.isExist);
                    onResult?.call(target, <boolean>res.isExist);

                },
                fail: (res: any) => {
                    console.log("check scene fail:", res);
                    onResult?.call(target, false);
                }
            });
        }

        /**
         * 跳转到抖音侧边栏
         * @param onResult 包含一个boolean参数的函数
         * @param target 上述函数的拥有者,如果是类的成员函数,需要传入this。普通或匿名函数忽略即可。
         * @returns 
         */
        static navigateToSidebar(onResult: (success: boolean) => void, target?: any) {
            if (!isBytedance()) {
                console.log("not douyin platform!");
                onResult?.call(target, false);
                return;
            }

            // @ts-ignore
            tt.navigateToScene({
                scene: "sidebar",
                success: () => {
                    console.log("navigate success");
                    onResult?.call(target, true);
                },
                fail: (res: any) => {
                    console.log("navigate failed reason:", res);
                    onResult?.call(target, false);
                },
            });
        }
    }

}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/889504.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

垂直分库分表、水平分库分表

垂直分库&#xff1a;分出来的数据库的结构完全不一样&#xff0c;垂直分库&#xff0c;更像单体项目到问服务项目过度&#xff0c;根据业务拆分多个模块&#xff0c;每个模块把数据单独抽离出来作为数据库&#xff0c;垂直分库就是根据不同的表业务放在不同放数据库里&#xf…

小程序项目实践(一)--项目的初始化以及前期的准备工作

目录 1.起步 1.1 uni-app 简介 1.2 开发工具 1.2.1 下载 HBuilderX 1.2.2 安装 HBuilderX 1.2.3 安装 scss/sass 编译 1.2.4 快捷键方案切换 1.2.5 修改编辑器的基本设置 1.3 新建 uni-app 项目 1.4 目录结构 1.5 把项目运行到微信开发者工具 1.6 使用 Git 管理项目 …

ViT模型技术学习

前言 最近多模态模型特别火&#xff0c;模型也越来越小&#xff0c;MiniCPM-2.6只有8B&#xff0c;里面采用的图片编码器是SigLipViT模型&#xff0c;一起从头学习ViT和Transformer&#xff01;本文记录一下学习过程&#xff0c;所以是自上而下的写&#xff0c;从ViT拆到Trans…

cmd设置文件夹共享和清除磁盘的只读属性

背景&#xff1a;备份vm虚拟机到新上架的IBM交换机服务器 备份方法&#xff1a;设置服务器D:\盘为共享&#xff0c;再在其他机器通过IP地址共享路径访问服务器D:\盘&#xff0c;进行复制备份 交换机服务器操作系统&#xff1a;Microsoft hyper-v server 2016英文版&#xff0…

k3s安装指定版本以及离线安装(docker)

首先下载你所需要版本的k3s安装包&#xff0c;目录结构如下所示&#xff0c;我这里是v1.19.15k3s2。 1.首先赋予可执行权限后进行安装。 # k3s 需要赋予可执行权限 sudo chmod x k3s sudo chmod x k3s-install.sh2.然后将k3s的二进制文件复制到/usr/local/bin/ cp k3s /us…

【测试用例设计】一个登录界面的测试用例设计

文章目录 1. 登录页面的测试用例设计 1. 登录页面的测试用例设计

2024 好玩有趣的nc(netcat)瑞士军刀,可以玩的对话工具哦!超级简单,包会,图文讲解,不讲虚话

一、nc是什么&#xff1f; 在Linux系统中&#xff0c;nc&#xff08;即netcat&#xff09;是一个非常强大的网络工具&#xff0c;常被昵称为“瑞士军刀”。它能够通过TCP或UDP协议读写网络连接&#xff0c;被广泛应用于网络调试和检测。 二、nc具体怎么进行通讯呢&#xff1f;&…

通信工程学习:什么是RIP路由信息协议

RIP&#xff1a;路由信息协议 RIP&#xff08;Routing Information Protocol&#xff09;路由信息协议是一种基于距离矢量算法的内部网关协议&#xff08;IGP&#xff09;&#xff0c;主要用于在自治系统&#xff08;AS&#xff09;内部进行路由信息的交换和传播。以下是关于RI…

【机器学习】随机森林算法(看我以弱博强)

目录 算法引入&#xff1a; 算法介绍&#xff1a; 1. 集成学习&#xff1a; 2. 训练过程&#xff1a; 3. 分类和回归&#xff1a; 算法优点&#xff1a; 算法缺点&#xff1a; 算法实现&#xff1a; 1. 数据准备 2. 划分数据集 3. 创建随机森林模型 4. 训练模型 5…

Python和C++的差异在哪里

1.编程应用领域 C&#xff1a;广泛应用于系统级开发、嵌入式系统、游戏开发等领域。C的底层控制和高性能使其成为这些领域的理想选择。 Python&#xff1a;广泛应用于数据科学、Web开发、人工智能等领域。Python的简洁语法和强大库支持使其成为这些领域的首选语言。 2.语法风…

代码随想录 (三)—— 哈希表部分刷题

当我们想使用哈希法来解决问题的时候&#xff0c;我们一般会选择如下三种数据结构。 数组set &#xff08;集合&#xff09;map(映射) 在java中有就是&#xff0c;hashmap, LinkedHashMap, TreeMap &#xff0c;HashTable 等 总结一下&#xff0c;当我们遇到了要快速判断一个…

vue-scrollto实现页面组件锚点定位

文章目录 前言背景操作指南安装及配置步骤vue组件中使用 参考文章 前言 博主介绍&#xff1a;✌目前全网粉丝3W&#xff0c;csdn博客专家、Java领域优质创作者&#xff0c;博客之星、阿里云平台优质作者、专注于Java后端技术领域。 涵盖技术内容&#xff1a;Java后端、大数据…

处理 Vue3 中隐藏元素刷新闪烁问题

一、问题说明 页面刷新&#xff0c;原本隐藏的元素会一闪而过。 效果展示&#xff1a; 页面的导航栏通过路由跳转中携带的 meta 参数控制导航栏的 显示/隐藏&#xff0c;但在实践过程中发现&#xff0c;虽然元素隐藏了&#xff0c;但是刷新页面会出现闪烁的问题。 项目源码&…

现代 C++ 模板教程 学习笔记

现代C模板教程 看了这个教程&#xff0c;自己记录一些内容&#xff0c;不一定靠谱。 为什么需要模板&#xff1f;简单来说就是为了少写点重复的代码&#xff0c;让编译器自动帮我们生成。为了少写点函数&#xff0c;就有了函数模板&#xff0c;为了少写点类&#xff0c;就有了…

探索未来:揭秘pymqtt,AI与物联网的新桥梁

文章目录 探索未来&#xff1a;揭秘pymqtt&#xff0c;AI与物联网的新桥梁背景&#xff1a;为什么选择pymqtt&#xff1f;什么是pymqtt&#xff1f;如何安装pymqtt&#xff1f;简单的库函数使用方法1. 配置MQTT连接2. 创建Mqtt对象3. 发布消息4. 订阅主题5. 运行MQTT客户端 场景…

transformer中的attention机制之旅

本文为学习笔记&#xff0c;若有相关交流或者需要补充的评论区欢迎 attention在深度学习之中最初是作为模型中的一个组件 用于提升模型精度&#xff0c;其原理是为了通过一定的计算方式获取信息中强化关注的特征。 attention相关的基础介绍可以参考其他博客中的内容: 一文看懂 …

C++ | Leetcode C++题解之第463题岛屿的周长

题目&#xff1a; 题解&#xff1a; class Solution {constexpr static int dx[4] {0, 1, 0, -1};constexpr static int dy[4] {1, 0, -1, 0}; public:int dfs(int x, int y, vector<vector<int>> &grid, int n, int m) {if (x < 0 || x > n || y <…

P1169 [ZJOI2007] 棋盘制作

P1169 [ZJOI2007] 棋盘制作 left[i][j]:代表从(i,j)(i,j)能到达的最左位置 right[i][j]:代表从(i,j)(i,j)能到达的最右位置 up[i][j]:代表从(i,j)(i,j)向上扩展最长长度. 悬线法&#xff1a; 假设求没有黑色格子的最大矩形&#xff0c;那么我们可以考虑对每个点计算&#x…

二、安装vmtools

1、 介绍 vmtools 安装后&#xff0c;可以让我们在 windows 下更好的管理 vm 虚拟机。可以设置 windows 和 centos 的共享文件夹 当时当我们发现安装虚拟机工具位置是灰色的 右击打开终端 在终端输入命令 yum list installed | grep open-vm-*yum list installed 命令会列出…

04:(寄存器开发)使用外部中断按键控制LED

寄存器开发 1、选择外部引脚配置2、上升沿触发/下降沿触发3、NVIC的配置4、完整代码 关于外部中断的AFIO&#xff0c;NVIC的基础知识请参考《stm32标准库入门教程》 链接: link 1、选择外部引脚配置 如上图所示&#xff1a;外部中断配置寄存器AFIO_EXTICR(1~4)中选择EXTI(0 ~ …