goto.js 2.75 KB
import wx from 'weixin-js-sdk';
import {linkToMiniApp,invokeMethod} from "../utils/common";

let env ;
export default class {
    constructor(_env) {
        env = _env;
        this.createButton();
    }

    createButton() {
        if (!document.getElementById('yosdk-goto') && env !== 'miniprogram') {
            let a = document.createElement('a')
            a.style.position = 'fixed';
            a.style.top = 0;
            a.style.left = 0;
            a.style.border = 'none';
            a.style.outline = 'none';
            a.style.resize = 'none';
            a.style.background = 'transparent';
            a.style.color = 'transparent';
            a.setAttribute('id', 'yosdk-goto');
            a.setAttribute('href', '');
            document.body.appendChild(a);
        }
    }

    link(event) {
        let element = event.currentTarget;
        let type = element.getAttribute('data-type') || 'other';
        let id = element.getAttribute('data-id') || '';
        let url = element.getAttribute('data-url') || '';
        let name = element.getAttribute('data-name') || '';
        let linkUrl = '';
        if (env === 'miniprogram') {
            if (type === 'product') {
                linkUrl = '/pages/goodsDetail/goodsDetail?productSkn=' + id + '&page_name=goodsList&page_param=5'
            } else if (type === 'brand') {
                linkUrl = '/pages/goodsList/brandStore?shop_id=' + id + '&shop_name=' + name
            } else {
                linkUrl = url.split('?')[0];
            }
            return linkToMiniApp(linkUrl, type);
        } else if (env === 'pc'){
            if (type === 'product') {
                linkUrl = 'https://www.yohobuy.com/product/'+id+'.html'
            } else if (type === 'brand') {
                linkUrl ='https://www.yohobuy.com/shop/'+name+'-'+id+'.html'
            } else {
                linkUrl = url
            }
        } else {
            if (type === 'product') {
                linkUrl = 'https://m.yohobuy.com/product/'+id+'.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":'+id+'}}'
            } else if (type === 'brand') {
                linkUrl = 'https://m.yohobuy.com/product/index/brand?shop_id='+id+'&openby:yohobuy={"action":"go.shop","params":{"shop_id":"'+id+'","shop_template_type":"1","shop_name":"'+name+'","is_red_shop":"1"}}'
            } else {
                linkUrl = url
            }
        }
        let dom = document.getElementById('yosdk-goto');
        dom.setAttribute('href', linkUrl)
        return dom.click();
    }


    goBack () {
        if(env === 'app'){
            invokeMethod({method:'go.back'})
        }else if(env === 'miniprogram'){
            wx.miniProgram.navigateBack();
        }else{
            history.go(-1);
        }
    }

}