【apicloud】webBrowser获取页面传参


apicloud中打开外部链接通常用以下两种方式:

  • webBrowser:更完美的呈现H5页面

  • openWin:利于H5与APP进行交互


本文主要接口webBrowser如何从链接中获取传参,并打开APP中的对应页面。


    //url设置方法

    // var appwinname   = 'group_vip_win';
    // var appwinurl    = 'widget://html/group_vip_win.html';
    
    // appwinurl = encodeURIComponent(appwinurl);
    // $(window).attr('location','https://iskeys.com?appfunction=orther&appwinname='+appwinname+'&appwinurl='+appwinurl+'&appPageParam_uid='+encodeURIComponent(uid)+'&appPageParam_nickname='+encodeURIComponent(nickname));

    
    
    
    function openUrl(url){

        var headerPos = $api.offset($api.byId('header')); //获取头部定位,此处自行更改,不需要可以替换

        //引入webBrowser模块,记住在控制平台中先加入模块,重新打包loader
        var browser = api.require('webBrowser');
        //这边使用openView便于自定义浏览器窗口布局,低需求的可以直接用集成好的open方法
        browser.openView({
            url: url,              //待访问路径
            scaleEnabled: 'false', //是否可以缩放 true/false
            rect: {
                x: 0,
                y: headerPos.h,  //Y轴定位,根据自己需求更改
                w: 'auto',
                h: api.winHeight - headerPos.h //高度计算,根据自己需求更改
            }
        }, function(ret, err) {
            //state加载状态,数字类型,取值范围:0-开始加载;1-加载进度发生变化;2-结束加载;3-title发生变化;4-url发生变化

            //对来源url分析是否需要调取APP内部页面
            if(ret.url){

                /********************************接下来开始分析URL是否需要唤起APP内部页面**************************************/
                ret.url = encodeURIComponent(ret.url);
                //自定义用法(通过传递winname/url/参数以自定义打开对应页面)
                //当appfunction 为 orther 时启用该解析方式
                var urlWinName   = funcUrlGet(ret.url,'appwinname');
                var urlWinUrl    = funcUrlGet(ret.url,'appwinurl');
                var urlPageParam = funcPageParamGet(ret.url);

                //常规用法案例:$(window).attr('location','https://www.w3cschool.cn/api/appapi/testApi?appfunction=project&appvalue=56875');
                if(!urlValue){
                    urlValue = '';
                }else{
                    urlValue = decodeURIComponent(urlValue);//进行解码
                }

                if(urlWinName){
                    if(urlWinName && urlWinUrl){
                        urlWinUrl = decodeURIComponent(urlWinUrl);//进行解码
                        api.openWin({
                            name: urlWinName,
                            url: urlWinUrl,
                            pageParam: urlPageParams
                        });
                        
                        //同时将路径中的参数移除,避免重复弹出
                        ret.url = funcUrlDel(ret.url,'appwinname');
                        ret.url = funcUrlDel(ret.url,'appwinurl');
                    }
                    //设置路径回跳,避免重复打开
                    isRefresh = 1;
                }  
            }

            switch (ret.state) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    //加载结束
                    if(url != ret.url){
                        //显示关闭按钮
                        $("#closeWin").show();
                    }

                    if(isRefresh == 1){
                        isRefresh = 0;
                        browser.historyBack(
                            function(ret, err) {
                                if (!ret.status) {
                                }
                            }
                        );

                    }
                    break;
                case 3:

                    $("#title").text(ret.title);
                        title = ret.title;
                    
                    break;
                case 4:
                    break;
                default:
                    break;
            }

        });
    }

    // 获取url中某个get参数
    function funcUrlGet(url,variable){
       var query = url.split("?");
       if(typeof query[1] == 'undefined'){
            return(false);
       }
       var vars = query[1].split("&");
       for (var i=0;i<vars.length;i++) {
           var pair = vars[i].split("=");
           if(pair[0] == variable){return pair[1];}
       }
       return(false);
    }

    // 获取url中的APP openWin所需的参数
    //pageParam参数默认自带前缀:  appPageParam_******* = ****
    function funcPageParamGet(url){
       
       var query = url.split("?");
       if(typeof query[1] == 'undefined'){
            return(false);
       }
       var vars = query[1].split("&");
       var param = new Object;
       for (var i=0;i<vars.length;i++) {
            vars[i] = decodeURIComponent(vars[i]);//进行解码
            if(vars[i].indexOf("appPageParam_") != -1){
                vars[i] = vars[i].replace(/appPageParam_/g,'');
                var pair = vars[i].split("=");
                param[pair[0]] = pair[1];
            }
       }
       if(param.length > 0){
           return param;
       }
       return(false);
    }

    // 删除url中某个get参数
    function funcUrlDel(url,name){
        var loca = url.split("?");
        var baseUrl = loca[0];
        if (loca[1].indexOf(name)>-1) {
            var obj = {}
            var arr = loca[1].split("&");
            for (var i = 0; i < arr.length; i++) {
                arr[i] = arr[i].split("=");
                obj[arr[i][0]] = arr[i][1];
            };
            delete obj[name];
            var url = baseUrl + '?' + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
            return url
        };
    }





猿教程
请先登录后发表评论
  • 最新评论
  • 总共0条评论