|
|
/**
|
|
|
* YOHO-SDK
|
|
|
*
|
|
|
* 与原生 APP 交互的代码
|
|
|
* 所有函数要做降级处理
|
|
|
* 假如不是 YOHO App,在浏览器实现对应的功能
|
|
|
* 浏览器不支持的功能,给出提示,控制台不能报错,不影响后续代码执行
|
|
|
*
|
|
|
* 希望能与 微信 JS-SDK 一样方便
|
|
|
*/
|
|
|
import cookie from 'yoho-cookie';
|
|
|
|
|
|
/* 空方法 */
|
|
|
const nullFun = () => {};
|
|
|
|
|
|
let isYohoBuy = /YohoBuy/i.test(navigator.userAgent || '');
|
|
|
let $appLink = document.querySelector('#yoho-app-link');
|
|
|
|
|
|
if (isYohoBuy && !$appLink) {
|
|
|
let body = document.querySelector('body');
|
|
|
|
|
|
$appLink = document.createElement('a');
|
|
|
$appLink.id = 'yoho-app-link';
|
|
|
$appLink.href = 'javascript:;';
|
|
|
$appLink.style.display = 'none';
|
|
|
$appLink.className = 'no-intercept';
|
|
|
|
|
|
body.appendChild($appLink);
|
|
|
|
|
|
$appLink.onclick = (evt) => {
|
|
|
evt.stopPropagation();
|
|
|
};
|
|
|
}
|
|
|
|
|
|
const yoho = {
|
|
|
/**
|
|
|
* 判断是否是 APP
|
|
|
*/
|
|
|
isApp: /YohoBuy/i.test(navigator.userAgent || '') || /YohoBuy/i.test(navigator.userAgent || ''),
|
|
|
isiOS: /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(navigator.userAgent || ''),
|
|
|
isAndroid: /Android/i.test(navigator.userAgent || ''),
|
|
|
isYohoBuy: isYohoBuy,
|
|
|
|
|
|
/**
|
|
|
* JS 与 APP 共享的对象
|
|
|
*/
|
|
|
data: window.yohoInterfaceData,
|
|
|
|
|
|
/**
|
|
|
* 判断是否是 登录
|
|
|
*/
|
|
|
isLogin() {
|
|
|
return cookie.get('_YOHOUID');
|
|
|
},
|
|
|
|
|
|
ready(callback) {
|
|
|
if (this.isApp || this.isYohoBuy) {
|
|
|
document.addEventListener('deviceready', callback);
|
|
|
} else {
|
|
|
return callback();
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转至指定index的tab(从0开始)
|
|
|
* @param args 传递给 APP 的参数 {"index":tab_index}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goTab(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
args.showScrollbar = 'no';
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.tab',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转至登录页面
|
|
|
* @param args 传递给 APP 的参数 {""}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goLogin(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.login',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 获取app版本号
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
getAppVersion(args, success, fail) {
|
|
|
if (window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'get.appversion',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 退出登录,清除本地用户数据
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goLogout(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.loginout',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 设置shoppingkey
|
|
|
* @param args 传递给 APP 的参数 {"shoppingkey":""}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goShopingKey(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.shoppingkey',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转至购物车页面
|
|
|
* @param args 传递给 APP 的参数 {""}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goShopingCart(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.shopingCart',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转地址页面 1:地址选择页面 2:地址管理页面
|
|
|
* @param args 传递给 APP 的参数 {"type":"1"}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goAddress(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.address',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转至图片浏览页面;images:浏览图片的url index:点击的图片序号
|
|
|
* @param args 传递给 APP 的参数 {"images":[imgUrl1,imgUrl2...],"index":"1"}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goImageBrowser(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.imageBrowser',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转至新页面(页面内容为html)
|
|
|
* @param args 传递给 APP 的参数 {"url":""}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goNewPage(args) {
|
|
|
if (this.isYohoBuy) {
|
|
|
|
|
|
let url = args.url;
|
|
|
|
|
|
if (url.indexOf('openby:') < 0) {
|
|
|
delete args.url;
|
|
|
if (args.header && args.header.headerid !== void 0) {
|
|
|
args.headerid = args.header.headerid;
|
|
|
delete args.header;
|
|
|
}
|
|
|
url += (url.indexOf('?') >= 0 ? '&' : '?') + 'openby:yohobuy=' + JSON.stringify({
|
|
|
action: 'go.h5',
|
|
|
params: {
|
|
|
islogin: 'N',
|
|
|
type: 0,
|
|
|
updateflag: Date.now() + '',
|
|
|
url: url,
|
|
|
param: args
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
if (window.parent && window.parent !== window) {
|
|
|
window.parent.blkDocument.goNewPage({url});
|
|
|
} else {
|
|
|
$appLink.href = url;
|
|
|
$appLink.click();
|
|
|
if (this.isiOS) {
|
|
|
$appLink.click();
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
if (args.url) {
|
|
|
window.open(args.url);
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转至支付页面
|
|
|
* @param args 传递给 APP 的参数 {"orderid":"098768"}
|
|
|
* @param success 调用成功的回调方法
|
|
|
* @param fail 调用失败的回调方法
|
|
|
*/
|
|
|
goPay(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.pay',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 返回上一级页面
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goBack(args, success, fail) {
|
|
|
if ((this.isApp || this.isYohoBuy) && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.back',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
history.go(-1);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 新的返回上一级页面
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goNewBack(args, success, fail) {
|
|
|
if (this.isYohoBuy && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.newback',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
history.go(-1);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 联系电话
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goTel(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.tel',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 获取频道
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
getChannel(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'get.channel',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 设置频道
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
setChannel(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'set.channel',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 频道跳转
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goChannel(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.channel',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 分享
|
|
|
* @param args {"title":"标题","des":"描述","img":"icon地址","url":"网页地址"}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goShare(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.share',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转到搜索页面
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goSearch(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.search',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转到设置页面
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goSetting(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.setting',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转到设置头像
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goSetAvatar(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.setAvatar',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转到收藏管理
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goPageView(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.pageView',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 更新头部信息
|
|
|
* @param args {""}
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
updateNavigationBar(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'update.navigationBarStyle',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 显示 loading
|
|
|
* @param args Boolen
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
showLoading(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.loading',
|
|
|
arguments: {
|
|
|
show: args ? 'yes' : 'no'
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 显示返回滑块
|
|
|
* @param args Boolean
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
blkBackStatus(args, success, fail) {
|
|
|
if (this.isYohoBuy && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.blkBackStatus',
|
|
|
arguments: {
|
|
|
isHidden: args ? 'Y' : 'N'
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 原生调用 JS 方法
|
|
|
* @param name 方法名
|
|
|
* @param callback 回调
|
|
|
*/
|
|
|
addNativeMethod(name, callback) {
|
|
|
// 延迟 500ms 注入
|
|
|
setTimeout(function() {
|
|
|
if (window.yohoInterface) {
|
|
|
window.yohoInterface[name] = callback;
|
|
|
}
|
|
|
}, 500);
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 跳转到 app 促销商品列表
|
|
|
* @param args
|
|
|
* @param success
|
|
|
* @param fail
|
|
|
*/
|
|
|
goCouponProductList(args, success, fail) {
|
|
|
if (this.isApp && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.couponProductList',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
goPage: function(action, params) {
|
|
|
var url = window.location.protocol + '//m.yohobuy.com/';
|
|
|
|
|
|
if (this.isYohoBuy && window.yohoInterface) {
|
|
|
url = url + '?openby:yohobuy=' + JSON.stringify({
|
|
|
action,
|
|
|
params
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if ($appLink) {
|
|
|
$appLink.href = url;
|
|
|
$appLink.click();
|
|
|
}
|
|
|
},
|
|
|
|
|
|
finishPage(args, success, fail) {
|
|
|
if (this.isYohoBuy && window.yohoInterface) {
|
|
|
window.yohoInterface.triggerEvent(success || nullFun, fail || nullFun, {
|
|
|
method: 'go.finish',
|
|
|
arguments: args
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
// tip(tipInfo);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
export default yoho; |
...
|
...
|
|