<template>
	<view>
	<view v-if="imStaff&&!isLogin" class="auth">
		<image src="/static/login-logo.png" mode=""></image>
			提示：工作人员登录后才能显示会员管理信息<br/>
			请点击
			<button v-if="!isAuth" type='primary' class="allow" open-type="getUserInfo" @getuserinfo="getUserInfo">授权</button>
			<button v-if="isAuth" type="primary" class="allow"  open-type="getPhoneNumber"  style="font-size: 12px;" @getphonenumber="getPhoneNumber">绑定手机</button>
	</view>
	<view v-if="imMember&&(!isLogin)" class="auth">
		<image src="/static/login-logo.png" mode=""></image>
			提示：会员登录后才能使用会员功能<br/>
			请点击
			<button v-if="!isAuth" type='primary' class="allow" open-type="getUserInfo" @getuserinfo="getUserInfo">授权</button>
			<button v-if="isAuth" type="primary" class="allow"  open-type="getPhoneNumber"  style="font-size: 12px;" @getphonenumber="getPhoneNumber">绑定手机</button>
	</view>
	<uni-popup ref="popup" type="center">
		<view class="uni-tip">
			<view class="uni-tip-title">登录失败</view>
			<view class="uni-tip-content">您授权的手机号与后台信息不匹配，请联系客服人员{{kf_number}}</view>
			<button class="uni-tip-button" @click="closePopup">确定</button>
		</view>
	</uni-popup>

	</view>
</template>

<script>
var md5 = require("../../common/md5.js");
import uniPopup from '@/components/uni-popup/uni-popup.vue';
export default {
	components: {
		uniPopup,
	},
	data(){
		return {
				iv:'',
				encryptedData:'',
				session_key:getApp().globalData.session_key,
				open_id: getApp().globalData.open_id,
				imStaff:getApp().globalData.imStaff,
				imMember:getApp().globalData.imMember,
			    isAuth:getApp().globalData.isAuth,
				isBind:getApp().globalData.isBind,
				isLogin:getApp().globalData.isLogin,
				kf_number:getApp().globalData.kf_number,
				user_type:'',
		}
	},
	onLoad() {
		// console.log("是否授权"+getApp().globalData.isAuth);
		// console.log("是否绑定"+getApp().globalData.isBind);
		// console.log("是否登录"+getApp().globalData.isLogin);
		// console.log("是否工作人员"+getApp().globalData.imStaff);
		// console.log("是否客户"+getApp().globalData.imMember);
	},
	watch:{
		user_type(){
			console.log("监听到用户类型了！");
			// this.judgeStaff(getApp().globalData.user_type);
		},
	},
	methods:{
		loginRequest(){
			uni.login({
				success: (res) => {
					uni.request({
						url: getApp().globalData.shareUrl, //需要设置为全局
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
						},
						data: {
							method: 'login',
							timestamp: getApp().globalData.globalTimestamp,
							code: res.code,
							sign: md5('login' + getApp().globalData.globalTimestamp)
						},
						success: res => {
							// 通过openid发起会员登录
							console.log(res);
							if(res.data.msg.status===1){
								getApp().globalData.isAuth=true;//用户已授权 手机号未绑定
							}else if(res.data.msg.status===2){
								getApp().globalData.isAuth=true;
								getApp().globalData.isBind=true;//用户手机号已绑定 进入调用用户信息流程来判断用户类型
							    this.getSelInfo();
							};
							getApp().globalData.open_id = res.data.msg.openId;
							getApp().globalData.session_key = res.data.msg.session_key;
							console.log('会员&工作人员尝试登录');
						},
					});
				},
			});
		},
		getSelInfo(){
			let that = this;
			uni.request({
				url: getApp().globalData.shareUrl, 
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				data: {
					sign: md5('get_member_info' + getApp().globalData.globalTimestamp),
					method: 'get_member_info',
					timestamp: getApp().globalData.globalTimestamp,
					openId: getApp().globalData.open_id,
				},
				success:(res)=>{
					if(res.data.code===200){
						getApp().globalData.user_id=res.data.msg.id;
						getApp().globalData.user_type=res.data.msg.type;
						getApp().globalData.member_count=res.data.msg.member_count;
						getApp().globalData.user_adminid=res.data.msg.adminid;
						getApp().globalData.user_headimg=res.data.msg.headimg;
						getApp().globalData.user_name=res.data.msg.name;
						getApp().globalData.user_nickname=res.data.msg.nickname;
						getApp().globalData.user_sex=res.data.msg.sex;
						getApp().globalData.user_status=res.data.msg.status;
						getApp().globalData.user_birthday=res.data.msg.birthday;
						getApp().globalData.user_idnum=res.data.msg.idnum;
						getApp().globalData.user_phone=res.data.msg.phone;
						getApp().globalData.user_email=res.data.msg.email;
						getApp().globalData.user_home_address=res.data.msg.home_address;
						getApp().globalData.user_send_address=res.data.msg.send_address;
						getApp().globalData.user_pcode=res.data.msg.pcode;
						getApp().globalData.user_number=res.data.msg.number;
						getApp().globalData.user_firsttime=res.data.msg.firsttime;
						getApp().globalData.user_addtime=res.data.msg.addtime;
						getApp().globalData.user_balance=res.data.msg.balance;
						getApp().globalData.user_deadline=res.data.msg.deadline;
						getApp().globalData.user_doctor=res.data.msg.doctor;
						getApp().globalData.user_doctor_name=res.data.msg.doctor_name;
						getApp().globalData.user_online_doctor=res.data.msg.online_doctor;
						getApp().globalData.user_online_doctor_name=res.data.msg.online_doctor_name;
						getApp().globalData.user_kf=res.data.msg.kf;
						getApp().globalData.user_kf_name=res.data.msg.kf_name;
						getApp().globalData.member_count=res.data.msg.member_count;
						getApp().globalData.user_health_doc=res.data.msg.health_doc;
						getApp().globalData.roomId = res.data.msg.type === '0' ? res.data.msg.rooms[0] :res.data.msg.rooms;
						if(getApp().globalData.user_deadline*1000<new Date().getTime()){
							getApp().globalData.deadline=true;
						}
						console.log("读取用户个人信息成功");
						that.judgeStaff(getApp().globalData.user_type);
					}
				},
				fail:()=>{
					console.log("读取用户个人信息失败");
				},
			});
		},
		judgeStaff(type){
			if(type==0){
				if(!getApp().globalData.imMember){
					this.$refs.popup.open();
					getApp().globalData.isSignOut=true;
					getApp().globalData.isAuth=false;
					getApp().globalData.isBind=false;
					getApp().globalData.isLogin=false;
					this.signout();
				}else{
					getApp().globalData.isLogin=true;
					console.log("该用户登录成功");
					if(getApp().globalData.user_deadline*1000<new Date().getTime()){
						getApp().globalData.deadline=true;
					}
					this.refuseBtn();
					//登陆成功再获取一次用户信息，登录状态不同获取的信息不一致
				}
			}else{
				if(!getApp().globalData.imStaff){
					this.$refs.popup.open();
					getApp().globalData.isSignOut=true;
					getApp().globalData.isAuth=false;
					getApp().globalData.isBind=false;
					getApp().globalData.isLogin=false;
					this.signout();
				}else{
					getApp().globalData.isLogin=true;
					console.log("该医生&客服登录成功");
					this.goChatlist();
					// this.getSelInfo();
				}
			}
		},
		getUserInfo(e) {
				if (e.detail.errMsg == "getUserInfo:ok") {
					console.log(e);
					getApp().globalData.isAuth = true;
					getApp().globalData.user_headimg = e.detail.userInfo.avatarUrl;
					getApp().globalData.user_name = e.detail.userInfo.nickName;
					this.iv = e.detail.iv;
					this.encryptedData = e.detail.encryptedData;
					this.isAuth=true;
					console.log("用户信息授权成功");
				} else {
					console.log("用户信息授权失败");
					this.isAuth = false;
					getApp().globalData.isAuth = false;
				}
			},
		getPhoneNumber(e){
				console.log(e);
				let that = this;
				 if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
				  that.phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key);
				} else { //
				      that.isNeedPhone = false;
				}
			},
		phoneRequest(myIv,myEncryptedData,sKey){
				let that = this;
				uni.request({
					url:getApp().globalData.shareUrl, //需要设置为全局
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						method: 'auth',
						timestamp: getApp().globalData.globalTimestamp, 
						sign: md5('auth' + getApp().globalData.globalTimestamp),
						iv:myIv,
						encryptedData:myEncryptedData,
						session_key:sKey,
						openId: that.open_id,
					},
					success: res => {
						console.log(res.data.code);
						if(res.data.code===200){
							console.log("手机号绑定成功!");
							getApp().globalData.isBind=true;
							that.uploadHeadimg();
						}else{
							this.$refs.popup.open();
							getApp().globalData.isSignOut=true;
							getApp().globalData.isAuth=false;
							getApp().globalData.isBind=false;
							getApp().globalData.isLogin=false;
							this.isBind = false;
						}
					} 
				});
			},
		uploadHeadimg(){
			let that = this;
			uni.request({
				url: getApp().globalData.shareUrl, 
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				data: {
					sign: md5('update_member_info' + getApp().globalData.globalTimestamp),
					method: 'update_member_info',
					timestamp: getApp().globalData.globalTimestamp, 
					openId:getApp().globalData.open_id,
					key:'headimg',
					value:getApp().globalData.user_headimg,
				},
				success:(res)=>{
					if(res.data.code===200){
						getApp().globalData.user_headimg=res.data.msg.headimg;
						that.getSelInfo()
						console.log("将微信头像作为用户头像");
					}
				},
				fail:()=>{
					console.log("微信头像作为用户头像更新失败");
				},
			});
		},
		refuseBtn(){
			uni.navigateBack({
				delta: 1
			});
		},
		goChatlist(){
			uni.reLaunch({
				url: '../index/chatList/index',
				success: res => {},
				fail: () => {},
				complete: () => {}
			});
			return true;
		},
		signout(){
			getApp().globalData.isAuth=false;
			getApp().globalData.isBind=false;
			getApp().globalData.isLogin=false;
			uni.request({
				url: getApp().globalData.shareUrl, 
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				data: {
					sign: md5('update_member_info' + getApp().globalData.globalTimestamp),
					method: 'update_member_info',
					timestamp: getApp().globalData.globalTimestamp, 
					uid:getApp().globalData.user_id,
					openId:getApp().globalData.open_id,
					key:'login_status',
					value:0,
				},
				success:(res)=>{
					if(res.data.code===200){
						getApp().globalData.login_status=res.data.msg.login_status;
						getApp().globalData.user_headimg=res.data.msg.headimg;
						getApp().globalData.user_name=res.data.msg.name;
						getApp().globalData.user_birthday=res.data.msg.birthday;
						getApp().globalData.user_idnum=res.data.msg.idnum;
						getApp().globalData.user_phone=res.data.msg.phone;
						getApp().globalData.user_email=res.data.msg.email;
						getApp().globalData.user_home_address=res.data.msg.home_address;
						getApp().globalData.user_send_address=res.data.msg.send_address;
						getApp().globalData.user_pcode=res.data.msg.pcode;
						getApp().globalData.user_number=res.data.msg.number;
						getApp().globalData.user_firsttime=res.data.msg.firsttime;
						getApp().globalData.user_balance=res.data.msg.balance;
						getApp().globalData.user_deadline=res.data.msg.deadline;
						getApp().globalData.user_doctor_name=res.data.msg.doctor_name;
						getApp().globalData.user_online_doctor_name=res.data.msg.online_doctor_name;
						getApp().globalData.kf_name=res.data.msg.kf_name;
						console.log("退出登录成功！");
					}
				},
				fail:()=>{
					console.log("999");
				},
			});
		},
		closePopup(){
			getApp().globalData.isSignOut=true;
			getApp().globalData.isAuth=true;
			getApp().globalData.isBind=false;
			getApp().globalData.isLogin=false;
			
				this.$refs.popup.close();
				uni.reLaunch({
						url: '../index/chooseId/index',
						success: res => {},
						fail: () => {},
						complete: () => {}
				});
		},
	}
}
</script>

<style>
   .auth{
	   margin-top: 0;
	   text-align: center;
	   display: flex;
	   flex-direction: column;
	   justify-content: center;
	   align-items: center;
	   padding: 100upx;
	   vertical-align: middle;
   }
   .auth image{
	   width: 200upx;
	   height: 200upx;
   }
   .auth view {
	   display: flex;
	   width: 490upx;
	   justify-content: space-between;
   }
   .auth-title {
	   font-size: 32upx;
   }
   .auth-content {
   	   font-size: 26upx;
	   color: #a7aaa9;
   }
   .allow {
	   background-color: #27BCEF;
	   margin: 20rpx 0 200rpx;
	   text-align: center;
	   vertical-align: middle;
	   touch-action: manipulation;
	   cursor: pointer;
	   background-image: none;
	   white-space: nowrap;
	   user-select: none;
	   font-size: 14px;
	   border: 0 !important;
	   position: relative;
	   text-decoration: none;
	   height: 44px;
	   width: 250rpx;
	   line-height: 44px;
	   box-shadow: inset 0 0 0 1px #27BCEF;
	   background: #fff !important;
	   color: #27BCEF !important;
	   display: inline-block;
	   border-radius: 10rpx;

   }
   .refuse {
	   background-color: #19be6b;
	   margin: 20rpx 20rpx 200rpx 20rpx;
	   text-align: center;
	   vertical-align: middle;
	   touch-action: manipulation;
	   cursor: pointer;
	   background-image: none;
	   white-space: nowrap;
	   user-select: none;
	   font-size: 14px;
	   border: 0 !important;
	   position: relative;
	   text-decoration: none;
	   height: 44px;
	   width: 250rpx;
	   line-height: 44px;
	   box-shadow: inset 0 0 0 1px #8a8a8a;
	   background: #fff !important;
	   color: #8a8a8a !important;
	   display: inline-block;
	   border-radius: 10rpx;

   }
   .margin-top-3{
	   margin-top: 10%;
   }
   
   
   .uni-tip {
   	 	padding: 30rpx;
   	 	width: 600rpx;
   		height: 340rpx;
   	 	background: #fff;
   	 	box-sizing: border-box;
   		border:2rpx solid #fff;
   	 	border-radius: 10rpx;
   	}
   	 
   	.uni-tip-title {
   	 	text-align: center;
		height: 40rpx;
   	 	/* font-weight: bold; */
   	 	font-size: 30rpx;
		background: #fff;
   	 	/* color: #333; */
   	}
   	 
   	.uni-tip-content {
		text-align: center;
		background: #fff;
   	 	padding: 15px;
   	 	font-size: 28rpx;
		height: 70rpx;
		line-height: 1.8;
   	 	/* color: #666; */
   	}
	 
	.uni-tip-button {
		width: 200rpx;
		height: 60rpx;
		line-height: 60rpx;
	 	margin-top: 30rpx;
	 	/* display: flex; */
		text-align: center;
		background: #fff;
		font-size: 28rpx;
		color: #c02924;
		border: solid 1rpx #c02924;
		border-radius: 30rpx;
	}
   
   
</style>