LayaAir之小游戏授权
admin
2023-02-16 23:00:03
0

一 : 修改wx.d.ts

①,加入按钮(接口)

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

如下图所示:
LayaAir之小游戏授权
②,加入createUserInfoButton方法 , 在 wx模块中:
LayaAir之小游戏授权

  /**
   * 获取用户的当前设置。
   */
  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.AOPConfigManager.Instance.getFile( config.Type_File_Config._General_);

                             const $shere_conf : NodeList|Node = $general.getInfoByFlag( "share" );
                             this.showShareMenu($general.getValue( $shere_conf , "title" ) ,  $general.getValue( $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 => {},
            });
        }

相关内容

热门资讯

不规范视频会议可能造成敏感信息... 国家安全部今天发布安全提示文章。视频会议系统能够打破时空限制,高效又便捷,逐渐成为大家沟通工作、传递...
伊朗总统办公室主任:总统与革命... 新华社德黑兰5月5日电(记者陈霄 沙达提) 据伊朗塔斯尼姆通讯社5日报道,伊朗总统办公室主任哈吉·米...
人民日报:任何企图否定东京审判... 原标题:捍卫历史正义 维护国际秩序——写在东京审判开庭80周年之际钟 佳 黄惠康 《人民日报》(20...
中学生进大厂体验:1个月拿了1... 中学生进大厂体验:1个月拿了1.5万  【中学生进大厂体验:1个月拿了1.5万】2024年年初,19...
多位省委书记省长会见王传福 从... 多位省委书记省长会见王传福  【多位省委书记省长会见王传福】4月27日,河南省委书记刘宁在郑州会见比...
上海汇正财经服务费能退吗?震荡...   部分投资者关心上海汇正财经服务费能退吗,但在二季度A股震荡加剧、地缘与油价双重扰动的复杂环境下,...
市场稀缺权益来袭!从全球急难救...   现在信用卡的功能越来越细化,也越来越人性化了,比如平安银行信用卡新推出的以“一生守护 十分平安”...
美官员:美商船穿越霍尔木兹海峡... 当地时间5月5日,央视记者获悉,两艘搭载美军安全队员的美国商船在通过霍尔木兹海峡期间曾遭伊朗袭击。美...
日本参议员:对俄制裁损害日本国... 正在俄罗斯访问的日本国会参议员铃木宗男5月5日对媒体表示,日本对俄制裁同样损害了日本国家利益。铃木说...
美国务卿称美国正推进对伊朗“极... △美国国务卿鲁比奥(资料图)当地时间5月5日,美国国务卿鲁比奥在媒体简报会上称,美军正在霍尔木兹海峡...