<template>
  <view class="container">
    
  </view>
</template>

<script>
import { loginByMp } from '@/api/user'
export default {
	data() {
		return {
			code:null,
		}
	},
	computed: {
	},
	onLoad(option) 
	{
		// #ifdef H5
		if (this.checkWeixin()) {
			let code = this.getUrlCode('code')
			if (code==null||code==undefined) {
				uni.showToast({
					icon:'none',
					title: "code不存在",
				});
			} else {
				this.code = code
				this.loginByMp();
			}
		} else {
			uni.showToast({
				icon:'none',
				title: "请在微信中打开",
			});
			//跳转到手机号密码登录
		}
		// #endif
	},
	onUnload() {
	},
	mounted() {
    
	},
	methods: {
		loginByMp(){
			let code = this.code // 获取开发code
			var tuiUserId=uni.getStorageSync('tuiUserId');
			uni.showLoading({
				title:"处理中..."
			})
			loginByMp({
			   code: code,
			   tuiUserId:tuiUserId
			})
			.then( res => {
				if(res.code==200||res.code==40163){
					uni.hideLoading();
					uni.showToast({
						icon:'none',
						title: "登录成功",
					});
					uni.setStorageSync('AppToken',res.token);
					uni.setStorageSync('userInfo',JSON.stringify(res.user));
					//that.getUserInfo()
					uni.$emit('refreshLogin');
					uni.reLaunch({
						url:"/pages/home/index"
					})
				}
				else{
					uni.hideLoading();
					uni.showToast({
						icon:'none',
						title: res.msg,
					});
				}
			   
			 })
			 .catch(error => {
				console.log(error)
				uni.hideLoading();
				uni.showToast({
					icon:'none',
					title: "登录接口调用失败",
				});
			 })
		},
		checkWeixin(){
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/micromessenger/i) == 'micromessenger') {
				return true;
			} else {
				return false;
			}
		},
		//URL地址是否存在CODE
		getUrlCode(name) {
			return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1]
				.replace(/\+/g, '%20')) || null
		}
	}
}
</script>

<style lang="scss">
</style>
