router.js 1.03 KB
import Promise from '../vendors/es6-promise';
import wx from '../utils/wx';
import {parse, stringify} from '../vendors/query-stringify';
import gominiapp from './gominiapp';

// 跳转到小程序内
// router.go('https://miniapp.yohobuy.com/pages/index/index?xxxx=ghh&ytty=444');

// 跳转到小程序外
// router.go('https://miniapp.yohobuy.com/pages/index/index?app=yohobuy&xxxx=ghh&ytty=444');

const miniAppDomain = 'miniapp.yohobuy.com';

export default {
    go(url = '') {
        if (!url) {
            return Promise.reject('error url');
        }
        const [uri, search] = url.split('?');
        const path = uri.split(miniAppDomain)[1];
        const qs = parse(search);

        if (!path) {
            return Promise.reject('error path');
        }

        if (qs.app) {
            return gominiapp({
                app: qs.app,
                path: `${path}?${stringify(qs)}`
            });
        } else {
            return wx.navigateTo({
                url: `${path}?${stringify(qs)}`
            });
        }
    }
};