<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/GetWeChatOpenId",
						data: {
							Code: obj.code,
						},
					},
					(res) => {
						if (res.resultCode == 1) {
							obj.OpenId = res.data;
							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,
				}
				that.request2({
						url: "/api/AppletLogin/Login",
						data: loginMsg,
					},
					(res) => {
						that.showLogin = true; //防止多次点击登录
						uni.hideLoading();
						if (res.resultCode == 1) {
							this.showDialog = false;
							//小程序用户名称
							let mallUserName = res.data.Name;
							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");
							let uid = uni.getStorageSync("mall_UserInfo").UserId;
							that.msg.Id = uid;
							//HK 2023-10-31 新增
							let b2bLoginParam = {
								OpenId: res.data.OpenId
							}
							let b2bResult = {
								accountId: 0,
								customerId: 0,
								customerInfoChildrenId: 0,
								realNameAuthentication: 0,
								account: "",
								name: mallUserName,
								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.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;
			},
			// -------------------------------支付宝登录---------------------------------
			onGetAuthorize(res) {
				let that = this;
				my.getOpenUserInfo({
					success: (_res) => {
						let userInfo = JSON.parse(_res.response).response; // 以下方的报文格式解析两层 response
						that.getUserInfoAli(userInfo);
					},
				});
			},
			getUserInfoAli(info) {
				var that = this;
				that.showDialog = true;
				my.getAuthCode({
					scopes: "auth_base", // 主动授权（弹框）：auth_user，静默授权（不弹框）：auth_base
					success: (res) => {
						if (res.authCode) {
							let obj = {
								Name: info.nickName,
								Photo: info.avatar ? info.avatar : "123",
								Gender: info.gender === "m" ? 1 : info.gender === "f" ? 2 : 0,
								Moblie: "",
								code: res.authCode,
							};
							that.getCodeAli(obj);
						}
					},
					fail: () => {
						uni.showToast({
							title: "授权失败",
							icon: "none",
						});
					},
				});
			},
			//调用获取code
			getCodeAli(obj) {
				var that = this;
				uni.showLoading({
					title: "登录中..",
				});
				//这里请求接口
				that.request2({
						url: "/api/mall/GetAliCode",
						data: {
							Code: obj.code,
						},
					},
					(res) => {
						if (res.resultCode == 1) {
							obj.OpenId = res.data;
							that.getLoginAli(obj);
						}
					}
				);
			},
			//登录
			getLoginAli(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 :
					"";
				that.request2({
						url: "/api/AppletLogin/Login",
						data: {
							Source: 2,
							OpenId: obj.OpenId,
							Name: obj.Name,
							Photo: obj.Photo,
							Gender: obj.Gender,
							Moblie: "",
							SuperiorId: pid,
							SmallShopId: SmallShopId,
							UserPageType: Up,
							CounponPassword: CounponPassword,
							KeyWord: KeyWord,
						},
					},
					(res) => {
						uni.hideLoading();
						that.showLogin = true; //防止多次点击登录
						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");
							let uid = uni.getStorageSync("mall_UserInfo").UserId;
							that.msg.Id = uid;
							let IsOpenMiAi = uni.getStorageSync("basedata") ?
								uni.getStorageSync("basedata").mall.setting.is_show_miai :
								0; //是否开启相亲模式
							if (IsOpenMiAi == 0) {
								//不是相亲模式的时候去更新头像和昵称
								//  #ifdef MP-WEIXIN
								that.updateuserinfo(that.msg); //更新用户头像
								// #endif
							}
						}
					}
				);
			},
		},
	};
</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>
