LayaAir之小游戏授权
  RlWeLU85QNwT 2023年11月02日 51 0

一 : 修改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 => {},
            });
        }
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

RlWeLU85QNwT