openapp.js 1.2 KB
/**
 * 移动端尝试打开 app
 */
import utils from './utils';

const getAppPath = () => {
    let qs = utils.queryString();
    let ct = qs.mkt_code || qs.union_type;

    delete qs.openapp; // 防止重复打开

    let shareParams = qs.share_id ? '"share":"/operations/api/v5/webshare/getShare","shareparam":{"share_id":"' + qs.share_id + '"},' : '',
        appPath = 'yohobuy://yohobuy.com/goapp?ct='+ ct +'&openby:yohobuy={"action":"go.h5",' + shareParams +
        '"params":{"islogin":"N","url":"http://feature.yoho.cn' + location.pathname + '","param":' + JSON.stringify(qs) + '}}';

    return appPath;
};

export default {
    init() {
        if (utils.queryString().openapp) {
            let u = navigator.userAgent.toLowerCase();
            let isiOS = u.indexOf('os') > -1 || u.indexOf('iphone') > -1 || u.indexOf('mac') > -1 || u.indexOf('ipad') > -1;
            let appPath = getAppPath();

            if (isiOS) {
                window.location.href = appPath;
            } else {
                let ifr = document.createElement('iframe');
                ifr.src = appPath;
                ifr.style.display = 'none';
                document.body.appendChild(ifr);
            }
        }
    }
};