egret获得token

根据web的url来决定是否从login界面输入账户密码登陆,或者根据url携带的token来登陆。

在index.html中获得url并缓存,然后在入口函数Main.ts中取出来解析,根据解析结果决定是否显示登陆界面。

关键是index.html中添加这一段

            //NOTE: andrew 在这里添加
            var urlKey = "urlKey";
            var urlValue = window.location.href;
            console.log("urlValue = ",urlValue);
            egret.localStorage.removeItem(urlKey);
            egret.localStorage.setItem(urlKey,);

完整如下:

<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <title>什么啊</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="full-screen" content="true" />
    <meta name="screen-orientation" content="landscape" />
    <meta name="x5-fullscreen" content="true" />
    <meta name="360-fullscreen" content="true" />
    <style>
        html,
        body {
            -ms-touch-action: none;
            background: #888888;
            padding: 0;
            border: 0;
            margin: 0;
            height: 100%;
        }
    </style>
</head>

<body>
    <div style="margin: auto;width: 100%;height: 100%;" class="egret-player" data-entry-class="Main" data-orientation="landscape" data-scale-mode="exactFit" data-frame-rate="30"
data-content-width="1280" data-content-height="720" data-multi-fingered="2" data-show-fps="false" data-show-log="false" data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9">
</div>
<script>
    var loadScript = function (list, callback) {
        var loaded = 0;
        var loadNext = function () {
            loadSingleScript(list[loaded], function () {
                loaded++;
                if (loaded >= list.length) {
                    callback();
                } else {
                    loadNext();
                }
            })
        };
        loadNext();
    };

    var loadSingleScript = function (src, callback) {
        var s = document.createElement('script');
        s.async = false;
        s.src = src;
        s.addEventListener('load', function () {
            s.parentNode.removeChild(s);
            s.removeEventListener('load', arguments.callee, false);
            callback();
        }, false);
        document.body.appendChild(s);
    };

    var xhr = new XMLHttpRequest();
    xhr.open('GET', './manifest.json?v=' + Math.random(), true);
    xhr.addEventListener("load", function () {
        var manifest = JSON.parse(xhr.response);
        var list = manifest.initial.concat(manifest.game);



        loadScript(list, function () {
            //NOTE: andrew 在这里添加
            var urlKey = "urlKey";
            var urlValue = window.location.href;
            console.log("urlValue = ",urlValue);
            egret.localStorage.removeItem(urlKey);
            egret.localStorage.setItem(urlKey,);

            /**
             * {
             * "renderMode":, //Engine rendering mode, "canvas" or "webgl"
             * "audioType": 0 //Use the audio type, 0: default, 2: web audio, 3: audio
             * "antialias": //Whether the anti-aliasing is enabled in WebGL mode, true: on, false: off, defaults to false
             * "calculateCanvasScaleFactor": //a function return canvas scale factor
             * }
             **/
            egret.runEgret({
                renderMode: "webgl",
                audioType: 0,
                calculateCanvasScaleFactor: function (context) {
                    var backingStore = context.backingStorePixelRatio ||
                        context.webkitBackingStorePixelRatio ||
                        context.mozBackingStorePixelRatio ||
                        context.msBackingStorePixelRatio ||
                        context.oBackingStorePixelRatio ||
                        context.backingStorePixelRatio || 1;
                    return (window.devicePixelRatio || 1) / backingStore;
                }
            });
        });
    });
    xhr.send(null);
</script>
</body>

</html>

在Main.ts中取出url

class Main extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {

        egret.log("onAddToStage");
        let urlValue = egret.localStorage.getItem("urlKey");
        egret.log("urlValue = ",urlValue);
    }
}

获取url的参数如下:

  其他参数获取介绍: 
//设置或获取对象指定的文件名或路径。
alert(window.location.pathname);


//设置或获取整个 URL 为字符串。
alert(window.location.href);


//设置或获取与 URL 关联的端口号码。
alert(window.location.port);




//设置或获取 URL 的协议部分。
alert(window.location.protocol);


//设置或获取 href 属性中在井号“#”后面的分段。
alert(window.location.hash);


//设置或获取 location 或 URL 的 hostname 和 port 号码。
alert(window.location.host);


//设置或获取 href 属性中跟在问号后面的部分。
alert(window.location.search);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值