import { computed, unref } from 'vue';
import { useUserStore } from '@/store/user.js';
import { REDIRECT_KEY } from '@/settings/enums.js';

// 登录后可进入页面
const loginList = [
];

const navigateToInterceptor = {
	invoke({ url }) {
		const userStore = useUserStore();
		const getToken = computed(() => userStore.getToken);
		const path = url.split('?')[0];
		if (unref(getToken)) return true;

		console.log('出发前', path, loginList.includes(path));
		if (loginList.includes(path)) {
			uni.showModal({
				title: '提示',
				content: '未登录请先登录',
				success: function (res) {
					if (res.confirm) {
						uni.setStorageSync(REDIRECT_KEY, url);
					} else if (res.cancel) {
					}
				}
			});
			return false;
		}
		return true;
	},
	success(args) {
		console.log('成功', args);
	},
	fail(err) {
		console.log('失败', err);
	}
};

export const routeInterceptor = {
	install() {
		uni.addInterceptor('navigateTo', navigateToInterceptor);
		uni.addInterceptor('reLaunch', navigateToInterceptor);
		uni.addInterceptor('redirectTo', navigateToInterceptor);
	}
};
