// router/index.js
import Vue from 'vue'
import Router from 'uni-simple-router'
import { Ichunt_Api, Api_Url } from '@/util/api.js';
import { request, getCookie, setCookie } from '@/util/util.js'
Vue.use(Router)
//初始化
const router = new Router({
    h5: {
        loading: false,
    },
    APP: {
        animation: {
            animationType: 'pop-in',
            animationDuration: 300
        }
    },
    encodeURI: false,
    routes: [{
            path: '/pages/index/index',
            aliasPath: '/',
            meta: {
                title: '首页'
            }
        },
        {
            path: '/pages/search/search',
            aliasPath: '/search',
            meta: {
                title: '搜索首页'
            }
        },
        {
            path: '/pages/search/searchlist',
            aliasPath: '/s',
            meta: {
                title: '搜索列表'
            }
        },
        {
            path: '/pages/car/car',
            aliasPath: '/joincart',
            meta: {
                title: '购物车'
            }
        },
        {
            path: '/pages/classp/map',
            aliasPath: '/sitmap',
            meta: {
                title: '分类选型'
            }
        },
        {
            path: '/pages/classp/list',
            aliasPath: '/optimum',
            meta: {
                title: '分类列表'
            }
        },
        {
            path: '/pages/brand/brand',
            aliasPath: '/brand',
            meta: {
                title: '品牌详情'
            }
        },
		{
		    path: '/pages/detail/detail/',
		    aliasPath: '/item',
		    meta: {
		        title: '商品详情'
		    }
		},
        {
            path: '/pages/mine/login',
            aliasPath: '/login',
            meta: {
                title: '登录'
            }
        },
        {
            path: '/pages/mine/forget',
            aliasPath: '/forget',
            meta: {
                title: '忘记密码'
            }
        },
        {
            path: '/pages/mine/agreements',
            aliasPath: '/reg/agreements',
            meta: {
                title: '协议'
            }
        },
        {
            path: '/pages/mine/user',
            aliasPath: '/user',
            meta: {
                title: '会员中心',
				auth:true
            }
        },
        {
            path: '/pages/mine/userOrder',
            aliasPath: '/userOrder',
            meta: {
                title: '订单列表'
            }
        },
        {
            path: '/pages/mine/userCoupon',
            aliasPath: '/userCoupon',
            meta: {
                title: '优惠券'
            }
        },
        {
            path: '/pages/mine/userAddress',
            aliasPath: '/user/reciveaddress',
            meta: {
                title: '收货地址'
            }
        },
        {
            path: '/pages/mine/userAddressAdd',
            aliasPath: '/userAddressAdd',
            meta: {
                title: '新增地址'
            }
        },
        {
            path: '/pages/mine/userAddressEdit',
            aliasPath: '/userAddressEdit',
            meta: {
                title: '编辑地址'
            }
        },
        {
            path: '/pages/mine/coupdz',
            aliasPath: '/user/coupdz',
            meta: {
                title: '京东卡'
            }
        },
        {
            path: '/pages/mine/zqsx',
            aliasPath: '/activity/zqsx',
            meta: {
                title: '账期服务'
            }
        },
        {
            path: '/pages/mine/news',
            aliasPath: '/news',
            meta: {
                title: '新闻'
            }
        },
        {
            path: '/pages/mine/article',
            aliasPath: '/article',
            meta: {
                title: '新闻详情'
            }
        },
        {
            path: '/pages/mine/about',
            aliasPath: '/about',
            meta: {
                title: '关于我们'
            }
        }
    ]
});

//全局路由前置守卫
router.beforeEach((to, from, next) => {
	if(to.meta.auth){
		if(!getCookie('Yo4teW_uid')||getCookie('Yo4teW_uid')==0){
			uni.navigateTo({
			  url: '/login?referer='+to.aliasPath
			})
		}
	}
	
    //写入  Yo4teW_gid Yo4teW_csrf
    if (!getCookie("Yo4teW_gid")) {
        request(Api_Url + "/public/apiInit", 'get', {}).then(res => {
            if (res.err_code === 0) {
                setCookie('Yo4teW_gid', res.data.Yo4teW_gid, 15);
                setCookie('Yo4teW_csrf', res.data.Yo4teW_csrf, 15);
                setCookie('Yo4teW_uid', res.data.Yo4teW_uid, 15);
                next()
            }
        });
        return
    }
    next()
})
// 全局路由后置守卫
router.afterEach((to, from) => {})
export default router;