<template >
	<view>
		<u-toast ref="uToast"  />
		<u-loading :show="false"></u-loading>
		<u-modal v-model="show" :content="content" :confirm-text="confirmtext?confirmtext:'确定'" @confirm="confirmBtn" :title="title?title:'提示'"></u-modal>
		<u-modal v-model="show2" :show-cancel-button="true" :content="content" :confirm-text="confirmtext?confirmtext:'确定'" :cancel-text="canceltext?canceltext:'取消'" @confirm="confirmBtn" @cancel="cancelBtn" :title="title?title:'提示'"></u-modal>
		
		<u-navbar v-if="mytitle" :title="mytitle" :is-back="noback" :border-bottom="!noback">
				<view class="slot-wrap"></view>	
		</u-navbar>
		<view v-show="false"  :login="islogin()" ></view>	
 		
	</view>
</template>

<script>
	import * as API from '@/apis/common.js'
	import * as API_WeiXin from '@/apis/weixin.js'
	import * as API_user from '@/apis/user.js'
	
	import {
		getUrlParam,
		getWeixinRedirectURI,
		isWeiXin
	} from '@/utils'
 	export default {
		name:"Common",
		props:{
			login: {
				require: false,
				default: false,
			},
			noback:{
				require: false,
				default: true,
			},
			mytitle: {
				require: false,
				default: false,
			},
		},
		data() {
			return {
				
				isloginBl:true,
				show:false,
				show2:false,
				fnc:null,
				fncCancel:null,
				fncBl:false,
				content:null,
				confirmtext:null,
				title:null,
				canceltext:null,
			};
			
		},methods:{
			
			 getOpenId() {
				const code = getUrlParam('code');
				
				if (!code) {
					var url = document.URL;
					
					window.location.href = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
				} else {
			 
					 
					API_WeiXin.getDataByCode(code).then(response => {
						var openId=response.data.openid;
						this.carhelp.setOpenId(response.data.openid)
						//var linkUrl = document.URL.replace(/\?code=(.*?)&state=STATE/g, '');
						//window.location = linkUrl; //隐藏参数
						//return Promise.resolve(response.openid);
						this.$emit("myOpenId")
						this.getLogin(openId)
						
					}).catch(error => {
						console.log(error);
					});
			
				}
			
			},
			getLogin(openId){
				 
				if(this.login){
					
					API_user.findByOpenId({
						openId:openId
					}).then(response2 => {
						  
						  
						if(response2.data){
							var token = response2.data ? response2.data.token : '';
							this.carhelp.setToken(token);
							this.carhelp.setPersonInfo(response2.data.token)	
							
							this.$emit("myLogin")
							// uni.switchTab({
							// 	url: '/pages/index/index'
							// });
						}
							
					}).catch(error => {	
						 
							console.log(error);
							uni.reLaunch({
								url: '/pages/index/login'
							})
					})
				}
			},
			islogin(){
				if(!this.carhelp.getOpenId()){
					this.getOpenId();
				}else{
					
				}
				if(this.login&&this.carhelp.getOpenId()){
					this.getLogin(this.carhelp.getOpenId());
				}
				
			},
			goError(message){
				this.setFnc(function(){
					uni.switchTab({
					    url: '/pages/index/index'
					});
				})
				this.alert2(message?message:'访问链接异常','前往首页')
				
			},
			setFnc(fnc,fncCancel){
				
				this.fnc=fnc;
				this.fncCancel=fncCancel;
				this.fncBl=true
			},
			alert2(content,confirmtext,title){
				this.show=true;
				this.content=content;
				this.confirmtext=confirmtext;
				this.title=title;
				 uni.hideLoading();
			},
			confirm(content,confirmtext,title,canceltext){
				this.show2=true;
				this.content=content;
				this.confirmtext=confirmtext;
				this.canceltext=canceltext;
				this.title=title;
				 uni.hideLoading();
			},
			cancelBtn(){
				if(this.fncCancel){
					this.fncCancel()
					this.fncBl=false;	
				}
			},
			confirmBtn(){
				
				if(this.fncBl){
					this.fnc()
					this.fncBl=false;	
				}
			},
			showLoading(bl,message){
				if(bl==null ||bl){
					// request  要改同步改
					uni.showLoading({
						mask:true,title:'加载中...'
					})
				}else{
					  uni.hideLoading();
					  if(message){
						  this.alert(message)
					  }
				}
				
			},
			alert(message,fnc){
				
				this.$refs.uToast.show({
					title: message,
					type:'default',
					position:'bottom',
					callback:fnc
				})
			}
		},mounted(){
				
		},destroyed(){
			
		}
		
	}
</script>

<style>
	.navbar-right {
				display: flex;
				margin-right: 20rpx;
				span{
					color:rgb(96, 98, 102);
					margin-left: 3px;
				}
			}
			.slot-wrap {
					display: flex;
					align-items: center;
					flex: 1;
				}
</style>
