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

const channelMap = {
    'baidu.com': 100000000000055,
    'so.com': 100000000000049,
    'sogou.com': 100000000000053,
    'bing.com': 100000000000057,
    'm.sm.cn': 100000000000059,
    'google.com': 100000000000061
};

const getMktcBySeo = () => {
    var mktc,
        rf = document.referrer;

    for (var domain in channelMap) {
        if (rf.indexOf(domain) > -1) {
            mktc = channelMap[domain];
            break;
        }
    }
    return mktc;
};

const getAppPath = () => {
    let qs = utils.queryString();
    let ct = qs.mkt_code || qs.union_type || cookies.cookie('unionTypeYas') || getMktcBySeo() || '100000000000349';

    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);
            }
        }
    }
};