import * as types from "./mutation-types";
import { getStorageSync, setStorageSync, setStorage, clearStorageSync } from '@/com/utils.js'
import jwt from '@/api/jwt.js'
import { http } from '@/api/index.js'

const actions = {
	[types.USER_INFO]({ commit }, userInfo) {
		let _data = {};
		if (userInfo) {
			console.log(userInfo);
			_data = userInfo;
			// setStorage('userInfo',userInfo)
		} else {
			_data = getStorageSync('userInfo');
		}
		commit(types.USER_INFO, _data);
	},
	logout({ commit }) {
		clearStorageSync()
		jwt.clearAccessToken()
	},
	async getUserInfo({ commit, dispatch }, userInfo) {
		try {
			const { data } = await http.get('/api/users/info')
			if(data.data.info.username=='admin'){
				data.data.permissions_slug.push('super')
			}
			dispatch(types.USER_INFO, data.data)
			
		} catch (error) {
			dispatch('logout')
			uni.reLaunch({
				url:'/pages/login/login'
			})
		}
	}

};

export default actions;