Authored by 杨延青

Merge branch 'feature/miniappJump' into 'release/6.8.1'

Feature/miniapp jump



See merge request !1579
... ... @@ -76,7 +76,8 @@
<link rel="apple-touch-startup-image" href="https://cdn.yoho.cn/h5/forios/startup/startup-orange-5.png" media="screen and (max-device-width: 640px)">
</head>
<body class="{{pageStyle}}{{#if isWechat}} wechat-body{{/if}}{{#if width750}} width750{{/if}}{{#if isPassportPage}} passport-body{{/if}}{{#if isStarIndexPage}} star-index-bg{{/if}}{{#if isStarDetailPage}} star-class-body{{/if}}{{#if isInstallmentPage}}{{#unless notOpen}} installment-body{{/unless}}{{/if}}{{#if @root.isMarsApp}} is-mars-app{{/if}}{{#if @root.isNowApp}} is-now-app{{/if}}">
<div class="main-wrap" id="main-wrap" {{#if appPath}}data-apppath='{{{appPath}}}'{{/if}} {{#if miniPath}}data-miniPath='{{{miniPath}}}'{{/if}} {{#if @root.wap.wechat.backDownload}}data-backDownload='{{@root.wap.wechat.backDownload}}'{{/if}}>
<div class="main-wrap" id="main-wrap"{{#if appPath}} data-apppath="{{{appPath}}}"{{/if}}{{#if miniPath}} data-miniPath="{{{miniPath}}}"{{/if}}{{#if @root.wap.wechat.miniAutoJump}} data-miniautojump="1"{{/if}}{{#if @root.wap.wechat.backDownload}} data-backDownload="{{@root.wap.wechat.backDownload}}"{{/if}}>
{{#if systemUpdate}}
{{> updata}}
{{/if}}
... ...
... ... @@ -563,6 +563,12 @@ function timedCutProductInit() {
});
}
function miniProgramHandleInit() {
if (window.__wxjs_environment === 'miniprogram') {
require('./miniprogram');
}
}
$(function() {
if ($('.over').length) {
// 过期/删除 状态的 活动
... ... @@ -619,8 +625,10 @@ $(function() {
timedCutProductInit();
// 小程序相关处理
if (window.__wxjs_environment === 'miniprogram') {
require('./miniprogram');
if (!window.WeixinJSBridge || !window.WeixinJSBridge.invoke) {
document.addEventListener('WeixinJSBridgeReady', miniProgramHandleInit, false);
} else {
miniProgramHandleInit();
}
// 商品曝光事件上报
... ...
/* global wx */
import $ from 'yoho-jquery';
import tip from 'js/plugin/tip';
import {transToMiniappPath} from 'js/common/miniapp-path-rules';
class LinkHandle {
constructor() {
... ... @@ -39,58 +40,13 @@ class LinkHandle {
* 转换为小程序页面地址
*/
transToPath(href) {
let path = '';
let openbyParamsArr = href.split(/openby:yohobuy(=|=)/);
let paramsStr = openbyParamsArr[openbyParamsArr.length - 1];
let paramsObj = {};
let transData = transToMiniappPath(href);
try {
paramsObj = JSON.parse(paramsStr);
} catch (error) {
console.error('paramsStr is: ', paramsStr);
}
if (paramsObj.action === 'go.productDetail' && paramsObj.params && paramsObj.params.product_skn) { // 商品详情页
path = `/pages/goodsDetail/goodsDetail?productSkn=${paramsObj.params.product_skn}&page_name=home`;
} else if (paramsObj.action === 'go.list') { // 列表页、专区
let queryParams = this.transOpenbyParams(paramsObj.params);
path = `/pages/goodsList/goodsList${queryParams}`;
} else if (paramsObj.action === 'go.poollist') { // 商品池
let queryParams = this.transOpenbyParams(paramsObj.params);
path = `/pages/goodsList/productPool${queryParams}`;
} else if (paramsObj.action === 'go.h5' && paramsObj.params && paramsObj.params.url) { // 活动模板
let queryParams = this.transOpenbyParams(paramsObj.params.param);
let url = `${paramsObj.params.url}${queryParams}`;
path = `/pages/webview/webview?page_name=home&url=${url}`;
} else if (paramsObj.action === 'go.shop') { // 店铺
path = `/pages/goodsList/brandStore?shopId=${paramsObj.params.shop_id}`;
} else {
if (!transData.path) {
tip.show('暂不支持,请使用Yoho!buy有货APP选购');
}
return path;
}
/**
* 转换 openby 参数
*/
transOpenbyParams(params) {
let paramsArr = [];
if (params && $.isPlainObject(params)) {
for (let key in params) {
if (params.hasOwnProperty(key)) {
paramsArr.push(`${key}=${params[key]}`);
}
}
} else {
console.info('params is:', params);
}
return paramsArr.length ? `?${paramsArr.join('&')}` : '';
return transData.path || '';
}
}
... ...
... ... @@ -288,6 +288,8 @@ $.extend({
// 尝试打开 APP
require('./common/open-app');
// 尝试打开 MINIAPP
require('./common/open-miniapp');
}());
$header.on('touchstart', 'a', function() {
... ...
const _isPlainObject = (obj) => {
if (typeof(obj) !== 'object' || obj.nodeType || obj !== null && obj !== undefined && obj === obj.window) { // eslint-disable-line
return false;
}
if (obj.constructor &&
!Object.prototype.hasOwnProperty.call(obj.constructor.prototype, 'isPrototypeOf')) {
return false;
}
return true;
};
const _qsStringfy = (params) => {
let paramsArr = [];
if (params && _isPlainObject(params)) {
for (let key in params) {
if (params.hasOwnProperty(key)) {
paramsArr.push(`${key}=${params[key]}`);
}
}
} else {
console.info('params is:', params);
}
return paramsArr.length ? `?${paramsArr.join('&')}` : '';
};
// 有货跳转规则转化为有货精选路由
const transToMiniappPath = (href) => {
let path = '';
let openbyParamsArr = href.split(/openby:yohobuy(=|=)/);
let paramsStr = openbyParamsArr[openbyParamsArr.length - 1];
let paramsObj = {};
try {
paramsObj = JSON.parse(paramsStr);
} catch (error) {
console.error('paramsStr is: ', paramsStr);
}
switch (paramsObj.action) {
case 'go.productDetail': // 商品详情页
if (paramsObj.params && paramsObj.params.product_skn) {
path = `/pages/goodsDetail/goodsDetail?productSkn=${paramsObj.params.product_skn}&page_name=home`;
}
break;
case 'go.list': // 列表页、专区
path = `/pages/goodsList/goodsList${_qsStringfy(paramsObj.params)}`;
break;
case 'go.poollist': // 商品池
path = `/pages/goodsList/productPool${_qsStringfy(paramsObj.params)}`;
break;
case 'go.h5':
if (paramsObj.params && paramsObj.params.url) {
path = `/pages/webview/webview?page_name=home&url=${paramsObj.params.url}${_qsStringfy(paramsObj.params.param)}`; // eslint-disable-line
}
break;
case 'go.shop':
path = `/pages/goodsList/brandStore?shopId=${paramsObj.params.shop_id}`;
break;
default:
break;
}
return {
path: path,
action: paramsObj.action
};
};
module.exports = {
transToMiniappPath
};
... ...
import {transToMiniappPath} from './miniapp-path-rules';
const jumpAction = ['go.productDetail', 'go.list', 'go.shop', 'go.poollist'];
let retry = 6;
function goMiniapp(apppath) {
retry--;
if (!window.wx) {
if (retry > 0) {
setTimeout(function() {
goMiniapp(apppath);
}, 300);
}
return;
}
let transData = transToMiniappPath(apppath);
if (transData.path && jumpAction.indexOf(transData.action) >= 0) {
window.wx.miniProgram.redirectTo({url: transData.path});
}
}
function init() {
if (window.__wxjs_environment !== 'miniprogram') {
return;
}
// 添加小程序webview页面标识
const bodyClass = 'miniapp-body';
const $body = document.getElementsByTagName('body')[0];
let classNames = ($body.className || '').split(' ');
if (classNames.indexOf(bodyClass) < 0) {
classNames.push(bodyClass);
}
$body.className = classNames.join(' ');
// 自动跳转至小程序对应页面
const appInfo = document.getElementById('main-wrap').dataset;
if (appInfo.miniautojump && appInfo.apppath) {
goMiniapp(appInfo.apppath);
}
}
if (!window.WeixinJSBridge || !window.WeixinJSBridge.invoke) {
document.addEventListener('WeixinJSBridgeReady', init, false);
} else {
init();
}
... ...