Commit bdc1051f9587984a5c40b7eb869726a45bc2cb91

Authored by zhiyangdu
1 parent d84205f6

解决单页面拿不到参数

Showing 1 changed file with 201 additions and 171 deletions
static/js/cnLive_openinstall.js
@@ -2,217 +2,247 @@ @@ -2,217 +2,247 @@
2 /*web页面向app传递的json数据(json string/js Object),应用被拉起或是首次安装时,通过相应的android/ios api可以获取此数据*/ 2 /*web页面向app传递的json数据(json string/js Object),应用被拉起或是首次安装时,通过相应的android/ios api可以获取此数据*/
3 //获取url地址里的参数 3 //获取url地址里的参数
4 function getUrlParam(name) { 4 function getUrlParam(name) {
5 - var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");  
6 - var r = window.location.search.substr(1).match(reg);  
7 - if (r != null) return r[2];  
8 - return ""; 5 + var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  6 + var r = window.location.hash.substr(1).match(reg);
  7 + if (r != null) return r[2];
  8 + return "";
9 } 9 }
10 //获取url地址里的参数转成json格式 10 //获取url地址里的参数转成json格式
11 function parseQueryString() { 11 function parseQueryString() {
12 - var reg_url = /^[^\?]+\?([\w\W]+)$/,  
13 - reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,  
14 - arr_url = reg_url.exec(window.location.href),  
15 - ret = {};  
16 - if (arr_url && arr_url[1]) {  
17 - var str_para = arr_url[1],  
18 - result;  
19 - while ((result = reg_para.exec(str_para)) != null) {  
20 - ret[result[1]] = result[2]; 12 + var reg_url = /^[^\?]+\?([\w\W]+)$/,
  13 + reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  14 + arr_url = reg_url.exec(window.location.href),
  15 + ret = {};
  16 + if (arr_url && arr_url[1]) {
  17 + var str_para = arr_url[1],
  18 + result;
  19 + while ((result = reg_para.exec(str_para)) != null) {
  20 + ret[result[1]] = result[2];
  21 + }
21 } 22 }
22 - }  
23 - return ret; 23 + return ret;
24 } 24 }
25 //获取xxx.html 25 //获取xxx.html
26 function GetUrlRelativePath() { 26 function GetUrlRelativePath() {
27 - var url = document.location.toString();  
28 - var arrUrl = url.split("//"); 27 + var url = document.location.toString();
  28 + var arrUrl = url.split("//");
29 29
30 - var start = arrUrl[1].indexOf("/");  
31 - var relUrl = arrUrl[1].substring(start); //stop省略,截取从start开始到结尾的所有字符 30 + var start = arrUrl[1].indexOf("/");
  31 + var relUrl = arrUrl[1].substring(start); //stop省略,截取从start开始到结尾的所有字符
32 32
33 - if (relUrl.indexOf("?") != -1) {  
34 - relUrl = relUrl.split("?")[0];  
35 - }  
36 - return relUrl; 33 + if (relUrl.indexOf("?") != -1) {
  34 + relUrl = relUrl.split("?")[0];
  35 + }
  36 + return relUrl;
37 } 37 }
38 38
39 //获取wakeupOrInstall传来的参数 39 //获取wakeupOrInstall传来的参数
40 function parseQueryStringData(data) { 40 function parseQueryStringData(data) {
41 - var reg_url = /^[^\?]+\?([\w\W]+)$/,  
42 - reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,  
43 - arr_url = reg_url.exec(data),  
44 - ret = {};  
45 - if (arr_url && arr_url[1]) {  
46 - var str_para = arr_url[1],  
47 - result;  
48 - while ((result = reg_para.exec(str_para)) != null) {  
49 - ret[result[1]] = result[2]; 41 + var reg_url = /^[^\?]+\?([\w\W]+)$/,
  42 + reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  43 + arr_url = reg_url.exec(data),
  44 + ret = {};
  45 + if (arr_url && arr_url[1]) {
  46 + var str_para = arr_url[1],
  47 + result;
  48 + while ((result = reg_para.exec(str_para)) != null) {
  49 + ret[result[1]] = result[2];
  50 + }
50 } 51 }
51 - }  
52 - return ret; 52 + return ret;
53 } 53 }
54 //唤起app 54 //唤起app
55 function wakeupOrInstall(data) { 55 function wakeupOrInstall(data) {
56 - var timestamp = Date.parse(new Date());  
57 - openInstallData = parseQueryStringData("http://wjjh5.cnlive.com?" + data);  
58 - openInstallData.channelCodeName = channelCodeName;  
59 - openInstallData.timestamp = timestamp;  
60 - openInstallData.changeSecDefChannel = true; //安卓默认定位二级频道  
61 - //console.log(openInstallData);  
62 - mm = new OpenInstall(  
63 - {  
64 - /*appKey必选参数,openinstall平台为每个应用分配的ID*/  
65 - appKey: appKey,  
66 - /*可选参数,自定义android平台的apk下载文件名;个别andriod浏览器下载时,中文文件名显示乱码,请慎用中文文件名!*/  
67 - //apkFileName : 'com.fm.openinstalldemo-v2.2.0.apk',  
68 - /*可选参数,是否优先考虑拉起app,以牺牲下载体验为代价*/  
69 - //preferWakeup:true,  
70 - /*自定义遮罩的html*/  
71 - //mask:function(){  
72 - // 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>"  
73 - //},  
74 - /*openinstall初始化完成的回调函数,可选*/  
75 - channelCode: channelName,  
76 - onready: function() {  
77 - /*在app已安装的情况尝试拉起app*/  
78 - /*不管是iOS还是安卓必须在openInstall后台打开启用scheme按钮才能执行schemeWakeup方法*/  
79 - /*在ios9.2以下schemeWakeup方法才能在openInstall初始化完毕之后执行拉起app操作, 56 + var timestamp = Date.parse(new Date());
  57 + openInstallData = parseQueryStringData("http://wjjh5.cnlive.com?" + data);
  58 + openInstallData.channelCodeName = channelCodeName;
  59 + openInstallData.timestamp = timestamp;
  60 + openInstallData.changeSecDefChannel = true; //安卓默认定位二级频道
  61 + //console.log(openInstallData);
  62 + mm = new OpenInstall(
  63 + {
  64 + /*appKey必选参数,openinstall平台为每个应用分配的ID*/
  65 + appKey: appKey,
  66 + /*可选参数,自定义android平台的apk下载文件名;个别andriod浏览器下载时,中文文件名显示乱码,请慎用中文文件名!*/
  67 + //apkFileName : 'com.fm.openinstalldemo-v2.2.0.apk',
  68 + /*可选参数,是否优先考虑拉起app,以牺牲下载体验为代价*/
  69 + //preferWakeup:true,
  70 + /*自定义遮罩的html*/
  71 + //mask:function(){
  72 + // 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>"
  73 + //},
  74 + /*openinstall初始化完成的回调函数,可选*/
  75 + channelCode: channelName,
  76 + onready: function() {
  77 + /*在app已安装的情况尝试拉起app*/
  78 + /*不管是iOS还是安卓必须在openInstall后台打开启用scheme按钮才能执行schemeWakeup方法*/
  79 + /*在ios9.2以下schemeWakeup方法才能在openInstall初始化完毕之后执行拉起app操作,
80 ios9.2以上必须通过点击触发wakeupOrInstall方法才能拉起app */ 80 ios9.2以上必须通过点击触发wakeupOrInstall方法才能拉起app */
81 - /*在安卓里只要打开了启用scheme按钮,schemeWakeup方法就会执行*/  
82 - /*隐藏以下代码是为了解决安卓网家家app内部打开h5会弹出跳转外部应用的bug*/  
83 - //var m = this;  
84 - //m.schemeWakeup();  
85 - }  
86 - },  
87 - openInstallData  
88 - ); 81 + /*在安卓里只要打开了启用scheme按钮,schemeWakeup方法就会执行*/
  82 + /*隐藏以下代码是为了解决安卓网家家app内部打开h5会弹出跳转外部应用的bug*/
  83 + //var m = this;
  84 + //m.schemeWakeup();
  85 + }
  86 + },
  87 + openInstallData
  88 + );
89 89
90 - mm.wakeupOrInstall(); 90 + mm.wakeupOrInstall();
91 } 91 }
92 // channelName通过channelName获取的渠道包名channelCodeName获取到的渠道包名 92 // channelName通过channelName获取的渠道包名channelCodeName获取到的渠道包名
93 // var channelName = "tg_dstg", 93 // var channelName = "tg_dstg",
94 - var channelName = getUrlParam("channelName"),  
95 - pathPrefix = "",  
96 - channelCodeName = "TGDSTG",  
97 - sid = getUrlParam("sid"), //sid  
98 - mm,  
99 - wrtcTop,  
100 - wrtcBtn,  
101 - type = "shop",  
102 - openInstallData; 94 +var channelName = getUrlParam("channelName"),
  95 + pathPrefix = "",
  96 + channelCodeName = "TGDSTG",
  97 + sid = getUrlParam("sid"), //sid
  98 + mm,
  99 + wrtcTop,
  100 + wrtcBtn,
  101 + type = "shop",
  102 + openInstallData;
103 // if (channelName.indexOf('xwh') > -1) { 103 // if (channelName.indexOf('xwh') > -1) {
104 // channelName = 'xwh' 104 // channelName = 'xwh'
105 // } 105 // }
106 -  
107 // 判断测试环境和生产环境给appKey赋值 106 // 判断测试环境和生产环境给appKey赋值
108 // appKey = 'gzqtwt' //测试appKey 107 // appKey = 'gzqtwt' //测试appKey
109 let appKey; 108 let appKey;
110 let url = document.domain; 109 let url = document.domain;
111 -if (url.toLowerCase() === "managemall.cnlive.com" || url.toLowerCase() === "manageshop.cnlive.com") {  
112 - appKey = "vfkj9x"; //正式appKey 110 +if (
  111 + url.toLowerCase() === "managemall.cnlive.com" ||
  112 + url.toLowerCase() === "manageshop.cnlive.com"
  113 +) {
  114 + appKey = "vfkj9x"; //正式appKey
113 } else { 115 } else {
114 - appKey = "gzqtwt"; //测试appKey 116 + appKey = "gzqtwt"; //测试appKey
115 } 117 }
116 /** 118 /**
117 * 初始化openInstall方法 119 * 初始化openInstall方法
118 * @param {Object} schemeParams [从接口取到的schemeParams参数,对象形式的,未做任何处理] 120 * @param {Object} schemeParams [从接口取到的schemeParams参数,对象形式的,未做任何处理]
119 */ 121 */
120 function openInstallReady(schemeParams) { 122 function openInstallReady(schemeParams) {
121 -  
122 - // 初始化数据  
123 - var timestamp = Date.parse(new Date());  
124 - openInstallData = parseQueryString();  
125 - openInstallData.channelCodeName = channelCodeName;  
126 - openInstallData.timestamp = timestamp;  
127 - openInstallData.changeSecDefChannel = true; //安卓默认定位二级频道  
128 - openInstallData.type = type;  
129 -  
130 - // 初始化通过openinstall唤起APP参数  
131 - let share_mold, shop_type, shop_to, shop_shop_type, shop_ext;  
132 -  
133 - // 判断如果传入了schemeParams的话,就不执行从地址栏取参数的操作,重新初始化一次OpenInstall  
134 - if (schemeParams && typeof schemeParams === 'object' && schemeParams.share_mold && schemeParams.shop_type && schemeParams.shop_to && schemeParams.shop_shop_type) {  
135 -  
136 - // 处理通过openinstall唤起APP参数  
137 - share_mold = schemeParams.share_mold;  
138 - shop_type = schemeParams.shop_type;  
139 - shop_to = schemeParams.shop_to;  
140 - shop_shop_type = schemeParams.shop_shop_type;  
141 - shop_ext = (schemeParams.shop_ext ? schemeParams.shop_ext : '');  
142 - } else {  
143 -  
144 - // 处理通过openinstall唤起APP参数  
145 - share_mold = encodeURIComponent(decodeURIComponent(openInstallData.share_mold));  
146 - shop_type = encodeURIComponent(decodeURIComponent(openInstallData.shop_type));  
147 - shop_to = encodeURIComponent(decodeURIComponent(openInstallData.shop_to));  
148 - shop_shop_type = encodeURIComponent(decodeURIComponent(openInstallData.shop_shop_type));  
149 - shop_ext = encodeURIComponent(decodeURIComponent(openInstallData.shop_ext));  
150 - }  
151 -  
152 - // 把值链接的方式绑定到shopShoptype  
153 - 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 : '');  
154 -  
155 - // Base64位编码加密  
156 - // var CryptoJS = require('crypto-js')  
157 - var wordArray = CryptoJS.enc.Utf8.parse(shareString);  
158 - let shopData = encodeURIComponent(CryptoJS.enc.Base64.stringify(wordArray));  
159 -  
160 - // 设置OpenInstall自定义参数  
161 - openInstallData.shopData = shopData;  
162 -  
163 - mm = new OpenInstall({  
164 - /*appKey必选参数,openinstall平台为每个应用分配的ID*/  
165 - appKey: appKey,  
166 - /*可选参数,自定义android平台的apk下载文件名;个别andriod浏览器下载时,中文文件名显示乱码,请慎用中文文件名!*/  
167 - //apkFileName : 'com.fm.openinstalldemo-v2.2.0.apk',  
168 - /*可选参数,是否优先考虑拉起app,以牺牲下载体验为代价*/  
169 - //preferWakeup:true,  
170 - /*自定义遮罩的html*/  
171 - //mask:function(){  
172 - // 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>"  
173 - //},  
174 - /*openinstall初始化完成的回调函数,可选*/  
175 - channelCode: channelName,  
176 - onready: function() {  
177 - /*在app已安装的情况尝试拉起app*/  
178 - /*不管是iOS还是安卓必须在openInstall后台打开启用scheme按钮才能执行schemeWakeup方法*/  
179 - /*在ios9.2以下schemeWakeup方法才能在openInstall初始化完毕之后执行拉起app操作, 123 + // 初始化数据
  124 + var timestamp = Date.parse(new Date());
  125 + openInstallData = parseQueryString();
  126 + openInstallData.channelCodeName = channelCodeName;
  127 + openInstallData.timestamp = timestamp;
  128 + openInstallData.changeSecDefChannel = true; //安卓默认定位二级频道
  129 + openInstallData.type = type;
  130 +
  131 + // 初始化通过openinstall唤起APP参数
  132 + let share_mold, shop_type, shop_to, shop_shop_type, shop_ext;
  133 +
  134 + // 判断如果传入了schemeParams的话,就不执行从地址栏取参数的操作,重新初始化一次OpenInstall
  135 + if (
  136 + schemeParams &&
  137 + typeof schemeParams === "object" &&
  138 + schemeParams.share_mold &&
  139 + schemeParams.shop_type &&
  140 + schemeParams.shop_to &&
  141 + schemeParams.shop_shop_type
  142 + ) {
  143 + // 处理通过openinstall唤起APP参数
  144 + share_mold = schemeParams.share_mold;
  145 + shop_type = schemeParams.shop_type;
  146 + shop_to = schemeParams.shop_to;
  147 + shop_shop_type = schemeParams.shop_shop_type;
  148 + shop_ext = schemeParams.shop_ext ? schemeParams.shop_ext : "";
  149 + } else {
  150 + // 处理通过openinstall唤起APP参数
  151 + share_mold = encodeURIComponent(
  152 + decodeURIComponent(openInstallData.share_mold)
  153 + );
  154 + shop_type = encodeURIComponent(
  155 + decodeURIComponent(openInstallData.shop_type)
  156 + );
  157 + shop_to = encodeURIComponent(
  158 + decodeURIComponent(openInstallData.shop_to)
  159 + );
  160 + shop_shop_type = encodeURIComponent(
  161 + decodeURIComponent(openInstallData.shop_shop_type)
  162 + );
  163 + shop_ext = encodeURIComponent(
  164 + decodeURIComponent(openInstallData.shop_ext)
  165 + );
  166 + }
  167 +
  168 + // 把值链接的方式绑定到shopShoptype
  169 + let shareString =
  170 + "shop_shop_type=" +
  171 + shop_shop_type +
  172 + "&shop_to=" +
  173 + shop_to +
  174 + "&shop_type=" +
  175 + shop_type +
  176 + "&share_mold=" +
  177 + share_mold +
  178 + (shop_ext ? "&shop_extra=" + shop_ext : "");
  179 +
  180 + // Base64位编码加密
  181 + // var CryptoJS = require('crypto-js')
  182 + var wordArray = CryptoJS.enc.Utf8.parse(shareString);
  183 + let shopData = encodeURIComponent(CryptoJS.enc.Base64.stringify(wordArray));
  184 +
  185 + // 设置OpenInstall自定义参数
  186 + openInstallData.shopData = shopData;
  187 +
  188 + mm = new OpenInstall(
  189 + {
  190 + /*appKey必选参数,openinstall平台为每个应用分配的ID*/
  191 + appKey: appKey,
  192 + /*可选参数,自定义android平台的apk下载文件名;个别andriod浏览器下载时,中文文件名显示乱码,请慎用中文文件名!*/
  193 + //apkFileName : 'com.fm.openinstalldemo-v2.2.0.apk',
  194 + /*可选参数,是否优先考虑拉起app,以牺牲下载体验为代价*/
  195 + //preferWakeup:true,
  196 + /*自定义遮罩的html*/
  197 + //mask:function(){
  198 + // 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>"
  199 + //},
  200 + /*openinstall初始化完成的回调函数,可选*/
  201 + channelCode: channelName,
  202 + onready: function() {
  203 + /*在app已安装的情况尝试拉起app*/
  204 + /*不管是iOS还是安卓必须在openInstall后台打开启用scheme按钮才能执行schemeWakeup方法*/
  205 + /*在ios9.2以下schemeWakeup方法才能在openInstall初始化完毕之后执行拉起app操作,
180 ios9.2以上必须通过点击触发wakeupOrInstall方法才能拉起app */ 206 ios9.2以上必须通过点击触发wakeupOrInstall方法才能拉起app */
181 - /*在安卓里只要打开了启用scheme按钮,schemeWakeup方法就会执行*/  
182 - /*隐藏以下代码是为了解决安卓网家家app内部打开h5会弹出跳转外部应用的bug*/  
183 - //var m = this;  
184 - //m.schemeWakeup();  
185 - },  
186 - }, openInstallData); 207 + /*在安卓里只要打开了启用scheme按钮,schemeWakeup方法就会执行*/
  208 + /*隐藏以下代码是为了解决安卓网家家app内部打开h5会弹出跳转外部应用的bug*/
  209 + //var m = this;
  210 + //m.schemeWakeup();
  211 + }
  212 + },
  213 + openInstallData
  214 + );
187 } 215 }
188 216
189 $.ajax({ 217 $.ajax({
190 - url: "//apiwjj.cnlive.com/Daren/common/getLatestApk.action",  
191 - type: "GET",  
192 - dataType: "json",  
193 - data: {  
194 - channelId: channelName  
195 - },  
196 - success: function(res) {  
197 - channelCodeName = res.data.apkChannelName;  
198 - if (window.location.href.indexOf("singingActivityDescription.html") > -1) {  
199 - wakeupOrInstallWrtcTop();  
200 - var item =  
201 - "type=web&pathPrefix=/cnBlockMore.html&channelName=901&blockId=608&blockType=27&contentType=14&title=%E4%BD%9C%E5%93%81%E5%B1%95%E7%A4%BA&moreUrl=http%3A%2F%2Fcms.cnlive.com%3A8768%2FcontentApi%2FtcysgList&isVotingSearch=%E6%8C%89%E7%BC%96%E5%8F%B7%2F%E5%90%8D%E7%A7%B0%E6%90%9C%E7%B4%A2";  
202 - wakeupOrInstallWrtcBtn(item);  
203 - } else if (window.location.href.indexOf("cnLtyInvitation.html") > -1) {  
204 - //领头雁邀请页面拉起领头雁报名页面处理  
205 - var item =  
206 - "type=web&pathPrefix=/cnLty.html&channelName=&isShowTitle=true&inviterUid=" +  
207 - sid;  
208 - wakeupOrInstallLtyInvitationTop(item);  
209 - } else {  
210 - openInstallReady(); 218 + url: "//apiwjj.cnlive.com/Daren/common/getLatestApk.action",
  219 + type: "GET",
  220 + dataType: "json",
  221 + data: {
  222 + channelId: channelName
  223 + },
  224 + success: function(res) {
  225 + channelCodeName = res.data.apkChannelName;
  226 + if (
  227 + window.location.href.indexOf("singingActivityDescription.html") > -1
  228 + ) {
  229 + wakeupOrInstallWrtcTop();
  230 + var item =
  231 + "type=web&pathPrefix=/cnBlockMore.html&channelName=901&blockId=608&blockType=27&contentType=14&title=%E4%BD%9C%E5%93%81%E5%B1%95%E7%A4%BA&moreUrl=http%3A%2F%2Fcms.cnlive.com%3A8768%2FcontentApi%2FtcysgList&isVotingSearch=%E6%8C%89%E7%BC%96%E5%8F%B7%2F%E5%90%8D%E7%A7%B0%E6%90%9C%E7%B4%A2";
  232 + wakeupOrInstallWrtcBtn(item);
  233 + } else if (window.location.href.indexOf("cnLtyInvitation.html") > -1) {
  234 + //领头雁邀请页面拉起领头雁报名页面处理
  235 + var item =
  236 + "type=web&pathPrefix=/cnLty.html&channelName=&isShowTitle=true&inviterUid=" +
  237 + sid;
  238 + wakeupOrInstallLtyInvitationTop(item);
  239 + } else {
  240 + openInstallReady();
  241 + }
  242 + },
  243 + error: function(err) {
  244 + // alert("接口请求失败!")
211 } 245 }
212 - },  
213 - error: function(err) {  
214 - // alert("接口请求失败!")  
215 - }  
216 }); 246 });
217 // 本地测试 247 // 本地测试
218 -openInstallReady();  
219 \ No newline at end of file 248 \ No newline at end of file
  249 +openInstallReady();