import store from '@/store/index.js';
import {login} from '@/api/user.js';
import { getUserInfo } from "@/api/index.js";
import getters from '../store/getters.js'

//获取用户信息
export async function getUserProfile() {
	// getUserInfo().then(function (resporse) {
	// 	let temp;
	// 	if (resporse.salt === "物业人员") {
	// 		temp = 'user_manager';
	// 		this.$store.commit('changeType', {type: temp});
	// 		console.log(this.$store.getters.type)
	// 	} else {
	// 		temp = 'user_applicant';
	// 		this.$store.commit('changeType', {type: temp});
	// 	}
	// 	uni.setStorageSync('userInfo', JSON.stringify(resporse));
	// })
	const resporse = await getUserInfo();
	if (resporse.salt === "物业人员") {
		store.commit('changeType', {type: 'user_manager'});
		console.log(store.getters.type)
	} else {
		store.commit('changeType', {type: 'user_applicant'});
	}
	uni.setStorageSync('userInfo', JSON.stringify(resporse));
}

export function getLogin() {
	return new Promise((resolve, reject) => {
		wx.login({
			onlyAuthorize: true,
			success(data) {
				console.log('登录参数', data, uni.getStorageSync('sessionId'))
				login(data.code).then(res => {
					uni.setStorageSync('sessionId', res);
					resolve('登录成功');
				});
			},
			fail(err) {
				console.log('login fail', err);
				uni.showToast({
					title: '登录错误',
					icon: 'none'
				});
				reject('登录错误');
			},
		});
	})
}
