cnLive_openinstall.js 9.06 KB
//openinstall初始化时将与openinstall服务器交互,应尽可能早的调用
/*web页面向app传递的json数据(json string/js Object),应用被拉起或是首次安装时,通过相应的android/ios api可以获取此数据*/
//获取url地址里的参数
function getUrlParam(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    if (window.location.hash.indexOf("?") < 0) {
        return null;
    }
    let r = window.location.hash.split("?")[1].match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}
//获取url地址里的参数转成json格式
function parseQueryString() {
    var reg_url = /^[^\?]+\?([\w\W]+)$/,
        reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
        arr_url = reg_url.exec(window.location.href),
        ret = {};
    if (arr_url && arr_url[1]) {
        var str_para = arr_url[1],
            result;
        while ((result = reg_para.exec(str_para)) != null) {
            ret[result[1]] = result[2];
        }
    }
    return ret;
}
//获取xxx.html
function GetUrlRelativePath() {
    var url = document.location.toString();
    var arrUrl = url.split("//");

    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start); //stop省略,截取从start开始到结尾的所有字符

    if (relUrl.indexOf("?") != -1) {
        relUrl = relUrl.split("?")[0];
    }
    return relUrl;
}

//获取wakeupOrInstall传来的参数
function parseQueryStringData(data) {
    var reg_url = /^[^\?]+\?([\w\W]+)$/,
        reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
        arr_url = reg_url.exec(data),
        ret = {};
    if (arr_url && arr_url[1]) {
        var str_para = arr_url[1],
            result;
        while ((result = reg_para.exec(str_para)) != null) {
            ret[result[1]] = result[2];
        }
    }
    return ret;
}
//唤起app
function wakeupOrInstall(data) {
    var timestamp = Date.parse(new Date());
    openInstallData = parseQueryStringData("http://wjjh5.cnlive.com?" + data);
    openInstallData.channelCodeName = channelCodeName;
    openInstallData.timestamp = timestamp;
    openInstallData.changeSecDefChannel = true; //安卓默认定位二级频道
    //console.log(openInstallData);
    mm = new OpenInstall(
        {
            /*appKey必选参数,openinstall平台为每个应用分配的ID*/
            appKey: appKey,
            /*可选参数,自定义android平台的apk下载文件名;个别andriod浏览器下载时,中文文件名显示乱码,请慎用中文文件名!*/
            //apkFileName : 'com.fm.openinstalldemo-v2.2.0.apk',
            /*可选参数,是否优先考虑拉起app,以牺牲下载体验为代价*/
            //preferWakeup:true,
            /*自定义遮罩的html*/
            //mask:function(){
            //  return "<div id='openinstall_shadow' style='position:fixed;left:0;top:0;background:rgba(0,255,0,0.5);filter:alpha(opacity=50);width:100%;height:100%;z-index:10000;'></div>"
            //},
            /*openinstall初始化完成的回调函数,可选*/
            channelCode: channelName,
            onready: function() {
                /*在app已安装的情况尝试拉起app*/
                /*不管是iOS还是安卓必须在openInstall后台打开启用scheme按钮才能执行schemeWakeup方法*/
                /*在ios9.2以下schemeWakeup方法才能在openInstall初始化完毕之后执行拉起app操作,
          ios9.2以上必须通过点击触发wakeupOrInstall方法才能拉起app */
                /*在安卓里只要打开了启用scheme按钮,schemeWakeup方法就会执行*/
                /*隐藏以下代码是为了解决安卓好家园App内部打开h5会弹出跳转外部应用的bug*/
                //var m = this;
                //m.schemeWakeup();
            }
        },
        openInstallData
    );

    mm.wakeupOrInstall();
}
// channelName通过channelName获取的渠道包名channelCodeName获取到的渠道包名
// var channelName = "tg_dstg",
var channelName = getUrlParam("channelName") || "tg_dstg",
    pathPrefix = "",
    channelCodeName = "",
    sid = getUrlParam("sid"), //sid
    mm,
    wrtcTop,
    wrtcBtn,
    type = "shop",
    openInstallData;
// if (channelName.indexOf('xwh') > -1) {
//   channelName = 'xwh'
// }
// 判断测试环境和生产环境给appKey赋值
// appKey = 'sgwcgm' //测试appKey
// appKey = 'vfkj9x' //正式appKey
let appKey;
let url = document.domain;
if (
    url.toLowerCase() === "managemall.cnlive.com" ||
    url.toLowerCase() === "manageshop.cnlive.com"
) {
    appKey = "vfkj9x"; //正式appKey
} else {
    appKey = "sgwcgm"; //测试appKey
}
/**
 * 初始化openInstall方法
 * @param {Object} schemeParams [从接口取到的schemeParams参数,对象形式的,未做任何处理]
 */
function openInstallReady(schemeParams) {
    // 初始化数据
    var timestamp = Date.parse(new Date());
    openInstallData = parseQueryString();
    openInstallData.channelCodeName = channelCodeName;
    openInstallData.timestamp = timestamp;
    openInstallData.changeSecDefChannel = true; //安卓默认定位二级频道
    openInstallData.type = type;

    // 初始化通过openinstall唤起APP参数
    let share_mold, shop_type, shop_to, shop_shop_type, shop_ext;

    // 判断如果传入了schemeParams的话,就不执行从地址栏取参数的操作,重新初始化一次OpenInstall
    if (
        schemeParams &&
        typeof schemeParams === "object" &&
        schemeParams.share_mold &&
        schemeParams.shop_type &&
        schemeParams.shop_to &&
        schemeParams.shop_shop_type
    ) {
        // 处理通过openinstall唤起APP参数
        share_mold = schemeParams.share_mold;
        shop_type = schemeParams.shop_type;
        shop_to = schemeParams.shop_to;
        shop_shop_type = schemeParams.shop_shop_type;
        shop_ext = schemeParams.shop_ext ? schemeParams.shop_ext : "";
    } else {
        // 处理通过openinstall唤起APP参数
        share_mold = encodeURIComponent(
            decodeURIComponent(openInstallData.share_mold)
        );
        shop_type = encodeURIComponent(
            decodeURIComponent(openInstallData.shop_type)
        );
        shop_to = encodeURIComponent(
            decodeURIComponent(openInstallData.shop_to)
        );
        shop_shop_type = encodeURIComponent(
            decodeURIComponent(openInstallData.shop_shop_type)
        );
        shop_ext = encodeURIComponent(
            decodeURIComponent(openInstallData.shop_ext)
        );
    }

    // 把值链接的方式绑定到shopShoptype
    let shareString =
        "shop_shop_type=" +
        shop_shop_type +
        "&shop_to=" +
        shop_to +
        "&shop_type=" +
        shop_type +
        "&share_mold=" +
        share_mold +
        (shop_ext ? "&shop_extra=" + shop_ext : "");

    // Base64位编码加密
    // var CryptoJS = require('crypto-js')
    var wordArray = CryptoJS.enc.Utf8.parse(shareString);
    let shopData = encodeURIComponent(CryptoJS.enc.Base64.stringify(wordArray));

    // 设置OpenInstall自定义参数
    openInstallData.shopData = shopData;

    mm = new OpenInstall(
        {
            /*appKey必选参数,openinstall平台为每个应用分配的ID*/
            appKey: appKey,
            /*可选参数,自定义android平台的apk下载文件名;个别andriod浏览器下载时,中文文件名显示乱码,请慎用中文文件名!*/
            //apkFileName : 'com.fm.openinstalldemo-v2.2.0.apk',
            /*可选参数,是否优先考虑拉起app,以牺牲下载体验为代价*/
            //preferWakeup:true,
            /*自定义遮罩的html*/
            //mask:function(){
            //  return "<div id='openinstall_shadow' style='position:fixed;left:0;top:0;background:rgba(0,255,0,0.5);filter:alpha(opacity=50);width:100%;height:100%;z-index:10000;'></div>"
            //},
            /*openinstall初始化完成的回调函数,可选*/
            channelCode: channelName,
            onready: function() {
                /*在app已安装的情况尝试拉起app*/
                /*不管是iOS还是安卓必须在openInstall后台打开启用scheme按钮才能执行schemeWakeup方法*/
                /*在ios9.2以下schemeWakeup方法才能在openInstall初始化完毕之后执行拉起app操作,
			  ios9.2以上必须通过点击触发wakeupOrInstall方法才能拉起app */
                /*在安卓里只要打开了启用scheme按钮,schemeWakeup方法就会执行*/
                /*隐藏以下代码是为了解决安卓好家园App内部打开h5会弹出跳转外部应用的bug*/
                //var m = this;
                //m.schemeWakeup();
            }
        },
        openInstallData
    );
}
if (channelName) {
    $.ajax({
        url: "//apiwjj.cnlive.com/Daren/common/getLatestApk.action",
        type: "GET",
        dataType: "json",
        data: {
            channelId: channelName
        },
        success: function(res) {
            channelCodeName = res.data.apkChannelName;
            openInstallReady();
        },
        error: function(err) {
            // alert("接口请求失败!")
        }
    });
}