import store from '@/store/index.js'
import authorize from '@/common/config/authorize.js'
import { userInfoLogin } from "@/api/use.js"
// 根据微信的code获取用户登录状态:1已登录过 -1未登录过
const wxLoginAuthorize = function(){
	authorize.getCode('weixin').then(wechatcode =>{
		authorize.getUserInfo('weixin').then(wxResponse =>{
			userInfoLogin({code:wechatcode,encryptedData:wxResponse.encryptedData,iv:wxResponse.iv}).then(response =>{
				store.commit('updateStatus',response.data)
				store.commit('login',response.data);
				store.commit('wxLogin',wxResponse.userInfo);
				uni.setStorageSync('token',response.data.token)
				uni.removeStorageSync('sessionid')
				uni.setStorageSync('sessionid','JSESSIONID='+response.data.sessionId)
			}).catch(error =>{
				uni.removeStorageSync('sessionid')
				uni.setStorageSync('sessionid','JSESSIONID='+error.data)
				store.commit('logout',error.data)
				store.commit('updateStatus',error.data)
				store.commit('wxLogin',wxResponse.userInfo);
			})
		})
	})
}
const wxLoginQuick = function(){// 根据微信的code获取用户登录状态:1已登录过 -1未登录过跳转
		authorize.getCode('weixin').then(wechatcode =>{
			// 根据微信的code获取用户登录状态:1已登录过 -1未登录过
			authorize.getUserInfo('weixin').then(wxResponse =>{
				userInfoLogin({code:wechatcode,encryptedData:wxResponse.encryptedData,iv:wxResponse.iv}).then(response =>{
					console.log(response)
					store.commit('updateStatus',response.data)
					store.commit('login',response.data);
					store.commit('wxLogin',wxResponse.userInfo);
					uni.setStorageSync('token',response.data.token)
					uni.removeStorageSync('sessionid')
					uni.setStorageSync('sessionid','JSESSIONID='+response.data.sessionId)
					if(response.data.userIdentity ==1){
						uni.navigateTo({url:'/seller/pages/index/index'})
					}else if(response.data.userIdentity === 3){
						uni.navigateTo({url:'/supplier/pages/index/index'})
					}else{
						uni.switchTab({url:'/pages/tabBar/user/user'})
					}
				}).catch(error =>{
					uni.removeStorageSync('sessionid')
					uni.setStorageSync('sessionid','JSESSIONID='+error.data)
					store.commit('logout',error.data)
					store.commit('updateStatus',error.data)
					store.commit('wxLogin',wxResponse.userInfo);
				})
			})
		})
}
export default{
	wxLoginAuthorize,
	wxLoginQuick
}
