<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import { SIGN_IN_CLIENTS, SIGN_IN_TYPES } from 'config/user';
import { signin } from 'api/user';
import { bindMsg } from 'api/bindMsg';
export default {
  async onLaunch(options) {
		uni.login({
			provider:'weixin',
			success: async res => {
				try {
					const params ={
						client: SIGN_IN_CLIENTS['mp'],
						type: SIGN_IN_TYPES['mp'],
						data: { 
							credential: 'enterprise',
							identifier: res.code,
						}
					}
					const data = await signin(params);
					if (!data || !data.token) {
						uni.showToast({
							icon: 'none',
							title: '登录返回信息有误'
						});
						return;
					};
					this.cacheData(data);
					const pam = {}
					const data1 = await bindMsg(pam)
					this.cacheDataA(data1);
				} catch (err) {
					console.log(err)
					uni.showToast({
						icon: 'none',
						title: err || '登录失败'
					})
				}
			}
		})
  },
	methods: {
		...mapMutations('user', ['setToken', 'setUser','setBindMsg']),
		// 缓存user data
		cacheData(data) {
			const { token } = data;
			this.setToken(token);
			this.setUser(data);
			// console.log(this.$store.state.user.token)
			// console.log(this.$store.state.user.user.wxInfo)
		},
		cacheDataA(data){
			this.setBindMsg(data)
			// console.log(this.$store.state.user.bindMsg)
		}
	},
};
</script>

<style lang="scss">
@import 'colorui/main.scss';
@import 'colorui/icon.scss';
@import 'colorui/animation.scss';
@import 'common/style/global';
@import 'common/style/iconfont';
</style>
