import { apiUserInfo, apiUserTabBar } from "@/api/user";
import {defaultTabBarListConfig} from '@/utils/config.js'
const token = uni.getStorageSync("token") || "";
let lastTabbarList=[]//上次请求的tabbar数据
const userModules = {
	state: {
		token: token,
		userInfo: {},

		tabbarList: [], //用户的tabbar
	},
	mutations: {
		// 设置token
		setToken(state, data) {
			state.token = data;
			uni.setStorageSync("token", data);
		},

		// 设置个人信息
		setUserInfo(state, data) {
			state.userInfo = data;
		},

		// 设置tabbar权限
		setTabBar(state, data) {
			if(lastTabbarList.toString()===data.toString()) return
			lastTabbarList=data
			// 默认的tabbar数据
			let defaultTabBarList = defaultTabBarListConfig;
			let arr=defaultTabBarList.filter(item=>{
				if(data.some(e=>e===item.key)){
					return item
				}
			})
			state.tabbarList=arr
		},
	},
	actions: {
		// 获取个人信息
		async getUserInfo(context) {
			const res = await apiUserInfo();
			if (res.code === 200) {
				context.commit("setUserInfo", res.data);
			}
		},

		async getTabBar(context) {
			const res = await apiUserTabBar();
			if (res.code === 200) {
				context.commit("setTabBar", res.data);
			}
		},
	},
};
export default userModules;
