LayaAir之小游戏授权
更新:HHH   时间:2023-1-7


一 : 修改wx.d.ts

①,加入按钮(接口)

/**
 * 按钮
 */
interface UserInfoButton {
  destroy(): void;
  hide(): void;
  onTap(callback: (res) => void): void;
  offTap(callback: () => void): void;
  show(): void;
}

如下图所示:

②,加入createUserInfoButton方法 , 在 wx模块中:

  /**
   * 获取用户的当前设置。
   */
  export function getSetting(object: _getSettingObject): void;

二:使用

①,构建按钮攻玩家点击授权

        private createUser(): UserInfoButton{
            let button:UserInfoButton=wx.createUserInfoButton({type:'text',text:'',style:{width:640,height:1136,backgroundColr:'#ff0000',color:'#ff0000',textAlign:'center',fontSize:16,borderRadius:4}});
            button.onTap((res)=>{
                this.wxAuthorization( button );
            })
            return button;
        }

②,授权方法

        private wxAuthorization : Function = ( $button? :  UserInfoButton ) : void => {
            wx.getSetting({
                success:(result: _getSettingSuccessObject) : void =>{
                    if( result.authSetting['scope.userInfo'] ){//已经授权了
                        if( $button ){
                            $button.hide();
                            $button.offTap;
                        }
                        //获取微信玩家信息
                        wx.getUserInfo({
                            withCredentials: true,
                            lang: "zh_CN",
                            success: (result: _getUserInfoSuccessObject) : void => {
                                console.log(`玩家信息 : `);
                                console.log( result.userInfo );
                                model.WC_UserInfo_VO.Instance.UserInfo = result;
                                //创建转发功能(!important)
                             const $general : small_lib.IAopConfigFile<config.Type_File_Config> =  config.AOPConfigManager.Instance.getFile( config.Type_File_Config._General_);

                             const $shere_conf : NodeList|Node = $general.getInfoByFlag( "share" );
                             this.showShareMenu($general.getValue<string>( $shere_conf , "title" ) ,  $general.getValue<string>( $shere_conf , "png" ));
                                this._isWait_count ++;
                                this.enter2Game();
                            },
                            fail: () : void => {
                                console.warn(`微信获取玩家信息失败!(授权)`);
                            },
                            complete: () : void => {

                            }
                        });
                    }else{//授权失败
                        this.createUser();
                    }
                },
                fail: () : void => {
                    this.createUser();
                },
                complete: () : void =>{

                }
            });
        }

三:补充

①,转发功能

        /**
         * 创建转发功能
         */
        private showShareMenu : Function = ( $title : string , $img : string ) : void => {
            console.log(`分享参数 : ${$title} / ${$img}`);
            let $self = this;
            wx.showShareMenu({
                withShareTicket: true,
                success: () => {
                    console.log(`显示分享按钮成功!`);
                    wx.onShareAppMessage(() : any => {
                        return {
                            title: $title,
                            imageUrl: $img
                        }
                    });
                },
                fail: () : void => {
                    console.warn(`显示分享按钮失败!`);
                },
                complete: () : void => {},
            });
        }
返回开发技术教程...