<template>
	<div class="auth-page" v-if="false">
		<u-popup v-model="showDialog" mode="center" :mask-close-able="false" custom-style="padding:0;background:none;">
			<img :src="pageinfo.pic_url" mode="widthFix" />
			<!-- #ifdef 	MP-WEIXIN -->
			<button :style="sureStyle" class="hotsopt" open-type="getUserInfo" v-if="!canIUseGetUserProfile" @getuserinfo="showLogin == true ? getoldUser() : ''"></button>
			<button :style="sureStyle" class="hotsopt" v-if="canIUseGetUserProfile == true" @tap="showLogin == true ? getUserProfile() : ''"></button>
			<!-- #endif -->
			<!-- #ifdef 	MP-ALIPAY -->
			<button :style="sureStyle" class="hotsopt" open-type="getAuthorize" @getAuthorize="onGetAuthorize" scope="userInfo"></button>
			<!-- #endif -->
			<button :style="cancelStyle" class="hotsopt" @click="close"></button>
		</u-popup>
		<coupon v-if="showCoupons" :coupon-message="couponMessage" :cform="ComeFrom" @goLook="goLook" @closeBtn="closeBtn"></coupon>
	</div>
</template>

<script>
	import coupon from "@/components/coupons/coupons";

	export default {
		components: {
			coupon,
		},
		data() {
			return {
				pageinfo: {},
				sureStyle: "",
				cancelStyle: "",
				showDialog: true,
				showCoupons: false,
				couponMessage: "",
				ComeFrom: "",
				showLogin: true,
				canIUseGetUserProfile: false,
				msg: {},
				isOpenAuth: 0, //是否开启强制授权
			};
		},
		created() {
			let mallConfig = uni.getStorageSync("basedata");
			if (mallConfig && mallConfig.mall && mallConfig.mall.setting && mallConfig.mall.setting.is_show_auth) {
				this.isOpenAuth = mallConfig.mall.setting.is_show_auth;
			}
			this.getUserInfo()
			// if(uni.getStorageSync('mall_UserInfo')&&uni.getStorageSync('mall_UserInfo').OpenId&&uni.getStorageSync('b2b_user')){
			// 	console.log(uni.getStorageSync('b2b_user').salesBaseInfo)
			// 	if(uni.getStorageSync('b2b_user').salesBaseInfo==''){
			// 		this.getLogin(uni.getStorageSync('mall_UserInfo').OpenId)
			// 	}
			// }
		},
		methods: {
			getoldUser() {
				uni.showToast({
					title: "电脑端暂时不能获取用户信息,请转到手机端使用！",
					icon: "none",
				});
				this.showDialog = false;
				this.$emit("gbAuth");
			},
			getUserProfile() {
				//新的获取用户资料
				var that = this;
				wx.getUserProfile({
					desc: "用于完善资料", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (info) => {
						this.msg = {
							Id: 0,
							AliasName: info.userInfo.nickName,
							Photo: info.userInfo.avatarUrl,
						};
						that.getUserInfo(info);
					},
					fail: () => {
						uni.showToast({
							title: "微信登录授权失败",
							icon: "none",
						});
					},
				});
			},
			getUserInfo() {
				var that = this;
				that.showDialog = true;
				wx.login({
					success: (res) => {
						let obj = {
							Source: 1,
							OpenId: "",
							Name: '微信小程序用户',
							Photo: 'https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1695262844000_280.png',
							Gender: 0,
							Moblie: "",
							SuperiorId: 0,
							code: res.code,
						};
						that.getCode(obj);
					},
					fail() {
						that.showDialog = false
						uni.showToast({
							title: '微信登录失败'
						})
					}
				})

			},
			close() {
				this.showDialog = false;
				this.$emit("gbAuth");
			},
			//调用获取code
			getCode(obj) {
				var that = this;
				uni.showLoading({
					title: "登录中.",
				});
				//这里请求接口
				var OpenId = "";
				that.request2({
						url: "/api/mall/GetHPWeChatOpenId",
						data: {
							Code: obj.code,
						},
					},
					(res) => {
						if (res.resultCode == 1) {
							obj.OpenId = res.data.openId;
							obj.Unoinid = res.data.unionid
							that.getLogin(obj);
						}
					}
				);
			},
			//登录
			getLogin(obj) {
				var that = this;
				that.showLogin = false;
				let pid = uni.getStorageSync("pid") ? uni.getStorageSync("pid").pid : 0;
				let Up = uni.getStorageSync("Up") ? uni.getStorageSync("Up").Up : 0;
				let SmallShopId = uni.getStorageSync("SmallShopId") ?
					uni.getStorageSync("SmallShopId").SmallShopId :
					0;
				let CounponPassword = uni.getStorageSync("CounponPassword") ?
					uni.getStorageSync("CounponPassword").CounponPassword :
					0;
				let KeyWord = uni.getStorageSync("KeyWord") ?
					uni.getStorageSync("KeyWord").KeyWord :
					"";
				let loginMsg = {
					Source: 1,
					OpenId: obj.OpenId,
					Name: obj.Name,
					Photo: obj.Photo,
					Gender: obj.Gender,
					Moblie: "",
					SuperiorId: pid,
					SmallShopId: SmallShopId,
					UserPageType: Up,
					CounponPassword: CounponPassword,
					KeyWord: KeyWord,
					Unoinid:obj.Unoinid
				}
				let url = "/api/AppletLogin/Login"
				// #ifdef MP-DI
				url = "/api/AppletLogin/HPSLLogin"
				// #endif
				that.request2({
						url,
						data: loginMsg,
					},
					(res) => {
						that.showLogin = true; //防止多次点击登录
						uni.hideLoading();
						if (res.resultCode == 1) {
							this.showDialog = false;
							uni.setStorageSync("mall_UserInfo", res.data);
							that.$emit("changeuserinfo");
							if (res.couponResultCode == 1) {
								if (uni.getStorageSync("ComeFrom")) {
									that.ComeFrom = uni.getStorageSync("ComeFrom").ComeFrom;
								}
								that.showCoupons = true;
								that.couponMessage = res.couponMessage;
							}
							uni.removeStorageSync("pid");
							uni.removeStorageSync("SmallShopId");
							uni.removeStorageSync("Up");
							uni.removeStorageSync("CounponPassword");
							uni.removeStorageSync("KeyWord");
							const {OpenId,Unoinid,Name} = res.data
							// let uid = uni.getStorageSync("mall_UserInfo").UserId;
							// that.msg.Id = uid;
							
							// #ifdef MP-AG
							this.agentUserLoginHandle(OpenId,Name)
							// #endif
							// #ifdef MP-DI
							this.directUserLoginHandle(OpenId,Unoinid,Name)
							// #endif
						}
					}
				);
			},
			directUserLoginHandle(OpenId,Unoinid,Name){
				let Pid = 0
				if(uni.getStorageSync('direct_user_id')){
					Pid = uni.getStorageSync('direct_user_id')
					if(isNaN(Pid)) Pid = 0
					uni.removeStorageSync('direct_user_id')
				}
				const parameters={
					OpenId,
					Unoinid,
					Pid
				}
				this.apipost("HePingGuoLvLogin_post", parameters, r =>{
					const u = r.data
					const user = {
						accountId: u.directCustomerId,
						customerId: u.directCustomerId,
						customerInfoChildrenId: u.directCustomerId,
						realNameAuthentication: 0,
						account: "",
						name:u.name,
						customerName: Name,
						photo: u.photo,
						secretKey: "",
						token: u.token,
						groupId: 0,
						isMember: 0,
						createBy: u.pid,
						EmLoginMobile: u.moblie,
						contactNumber: u.moblie,
						salesBaseInfo: u.erpBaseInfo?u.erpBaseInfo:u.salesBaseInfo,
						pid:u.erpBaseInfo?u.erpBaseInfo.employeeId:u.pid,
						erpBaseInfo: u.erpBaseInfo,
						count: 0,
						isVip:u.isVip,
						customerType: 1, //直客，2-同行  
						directCustomerId: u.directCustomerId //直客Id
					}
					uni.setStorageSync('b2b_user', user)
					this.$emit("changeuserinfo");
					uni.setStorageSync("lastLogin",new Date().getTime())
					this.$utils.setAttatchParameter(this)
				},e=>{
					uni.showToast({
						title:'用户登录失败',
						icon:'none'
					})
				})
			},
			agentUserLoginHandle(openId,Name){
				var that = this;
				let b2bLoginParam = {
					OpenId: openId
				}
				let b2bResult = {
					accountId: 0,
					customerId: 0,
					customerInfoChildrenId: 0,
					realNameAuthentication: 0,
					account: "",
					name: Name,
					customerName: "",
					photo: "",
					secretKey: "",
					token: "",
					groupId: 0,
					isMember: 0,
					createBy: "",
					EmLoginMobile: "",
					contactNumber: "",
					salesBaseInfo: {},
					count: 0,
					customerType: 2, //直客，2-同行  
					directCustomerId: 0, //直客Id
				}
				that.apipost("LvXiaoYou_post_GetLvXiaoYouCustomerLogin", b2bLoginParam, b2bRes => {
					var b2bData = b2bRes.data;
					if (b2bRes.resultCode == 1) {
						b2bResult.accountId = b2bData.customerAccountId;
						b2bResult.customerId = b2bData.customerId;
						b2bResult.customerInfoChildrenId = b2bData.customerInfoChildrenId;
						b2bResult.name = b2bData.name;
						b2bResult.customerName = b2bData.customerName;
						b2bResult.token = b2bData.token;
						b2bResult.isMember = b2bData.isMember;
						b2bResult.account = b2bData.account;
						b2bResult.createBy = b2bData.createBy;
						b2bResult.salesBaseInfo = b2bData.salesBaseInfo;
						b2bResult.groupId = b2bData.groupId;
						b2bResult.isVip=b2bData.isVip;
						b2bResult.contactNumber = b2bData.contactNumber;
						uni.setStorageSync('b2b_user', b2bResult)
						that.$emit("changeuserinfo");
						uni.setStorageSync("lastLogin",new Date().getTime())
					} else {
						uni.redirectTo({
							url: '/pages/jiuzhai/bind_Customer'
						})
					}
				}, (error2) => {
					uni.redirectTo({
						url: '/pages/jiuzhai/bind_Customer'
					})
				});
			},
			updateuserinfo(msg) {
				this.request2({
						url: "/api/MemberUser/SetMemberPhoto",
						data: msg,
					},
					(res) => {
						//静默处理不做任何提示
					},
					(error) => {}
				);
			},
			goLook() {
				this.showCoupons = false;
			},
			closeBtn() {
				this.showCoupons = false;
			}
		},
	};
</script>

<style>
	.auth-page img {
		width: 650rpx;
		height: 700rpx;
	}

	.auth-page .hotsopt {
		position: absolute;
		background: transparent;
		border: none;
		outline: none;
	}

	.auth-page .hotsopt::after {
		border: none;
	}
</style>
