<template>
	<view class="full-width full-height">
		<view style="min-height: 100%;">
			<view class="start">
				<image src="../../static/logo.jpg" mode="aspectFill" class="start_img"></image>
				<view class="start_title">ColorPark</view>
				<view class="start_text">{{startText}}</view>
				<view class="longTitle">
					<view>ColorPark，把爱穿在身上、棒在手心</view>
					你的第一份爱的定制在这里开始
				</view>
				<view class="footer_but">
					<!-- 小程序登录、用户信息相关接口调整 -->
					<button class="buttonGetUserInfo" hover-class="hover-class-bg" v-if="canIUseGetUserProfile"
						@tap="getUserProfile">
						<image src="../../static/icon_wechat.png" mode="aspectFill" class="icon_wechat"></image>
						<text style="margin-left: 13upx;">获取头像昵称</text>
					</button>
					<button v-else open-type="getUserInfo" @getuserinfo="getUserInfo" lang="zh_CN"
						class="buttonGetUserInfo" hover-class="hover-class-bg">
						<image src="../../static/icon_wechat.png" mode="aspectFill" class="icon_wechat"></image>
						<text style="margin-left: 13upx;">获取头像昵称</text>
					</button>
				</view>
				<image src="../../static/logo.jpg" mode="aspectFill" class="logoimage"></image>
			</view>
		</view>
		<view class="bottomText"> {{company}}</view>
	</view>
</template>

<script>
	import userService from '@/service/UserService';
	export default {
		components: {},
		computed: {
			startText(){
				return this.$Env.getmalltext()
			},
			company(){
				return this.$Env.getcompany()
			}
		},
		data() {
			return {
				long: false,
				LoupanList: [],
				userInfo: {
					nickname: null
				},
				code: null,
				canIUseGetUserProfile: false
			};
		},
		methods: {
			getUserProfile(e) {
				uni.getSetting({
					success(res){
						
					}
				})
				let that = this;
			    //推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
			    //开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
			    wx.getUserProfile({
				  lang:'zh_CN',
			      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			      success: (Userres) => {
					  that.$base.Userres = Userres
					  uni.showLoading({title: '正在登录中...',mask: true});
					  uni.login({
					  	provider: 'weixin',
					  	success: (weixinres) => {
					  		if (weixinres.code) {
					  			that.loginInfo = Userres.userInfo;
					  			that.loginInfo.encryptedData = Userres.encryptedData;
					  			that.loginInfo.iv = Userres.iv;
					  			that.loginInfo.code = weixinres.code;
					  			userService.setWxInfo(that.loginInfo);
					  			userService.cacheWxInfo(that.loginInfo);
					  			userService.login({
					  				s: 'Init.init',
					  				code: that.loginInfo.code,
					  				nickname: that.loginInfo.nickName || '微信用户',
					  				gender:that.loginInfo.gender,
					  				portrait: that.loginInfo.avatarUrl,
					  				encryptedData:that.loginInfo.encryptedData,
					  				iv:that.loginInfo.iv,
									promoter_id:that.$base.promoter_id ,// 推广的员的编号
									machine_id:that.$base.machine_id,//用户进入设备id
									appcode: that.$Env.getappcode()
					  			}).then(result => {
									uni.hideLoading();
					  				uni.showToast({title: '登录成功',icon: 'none'});
					  				if (that.code) {
					  					uni.redirectTo({
					  						url: '../index/machineDetail'
					  					})
					  				} else {
					  					uni.navigateBack();
					  				}
					  			}).catch(err => {
					  				uni.hideLoading();
					  				uni.showToast({title: err.msg,icon: 'none'});
					  			})
					  		} else {
								uni.hideLoading();
								uni.showToast({title: '登录失败！',icon: 'none'});
					  		}
					  	},
					  	fail: (err) => {
							uni.hideLoading();
							uni.showToast({title: '授权登录失败',icon: 'none'});
					  	}
					  })
			      },
				  fail(err) {
					uni.hideLoading();
					console.log("wx.getUserProfile获取用户信息失败", err)
					uni.showToast({title: '获取用户信息失败',icon: 'none'});
				  }
			    })
			},
			getUserInfo(e) {
				let that = this;
				uni.getSetting({
					success(res){
						//console.log('获取用户信息是否用户同意')
						//console.log(res.authSetting)
						//console.log(res.authSetting['scope.userInfo'])
					}
				})
				uni.getUserInfo({
					success(Userres) {
						console.log('getUserInfo 获取头像昵称')
						console.log(Userres)
						that.$base.Userres = Userres
						uni.showLoading({title: '正在登录中...',mask: true});
						uni.login({
							provider: 'weixin',
							success: (weixinres) => {
								if (weixinres.code) {
									that.loginInfo = Userres.userInfo;
									that.loginInfo.encryptedData = Userres.encryptedData;
									that.loginInfo.iv = Userres.iv;
									that.loginInfo.code = weixinres.code;
									userService.setWxInfo(that.loginInfo);
									userService.cacheWxInfo(that.loginInfo);
									userService.login({
										s: 'Init.init',
										code: that.loginInfo.code,
										nickname: that.loginInfo.nickName || '微信用户',
										gender:that.loginInfo.gender,
										portrait: that.loginInfo.avatarUrl,
										encryptedData: that.loginInfo.encryptedData,
										iv: that.loginInfo.iv,
										promoter_id: that.$base.promoter_id, // 推广的员的编号
										machine_id: that.$base.machine_id, //用户进入设备id
										appcode: that.$Env.getappcode()
									}).then(result => {
										uni.hideLoading();
										uni.showToast({title: '登录成功',icon: 'none'});
										if (that.code) {
											uni.redirectTo({
												url: '../index/machineDetail'
											})
										} else {
											uni.navigateBack();
										}
									}).catch(err => {
										console.log(err)
										uni.hideLoading();
										uni.showToast({title: err.msg,icon: 'none'});
									})
								} else {
									uni.hideLoading();
									uni.showToast({title: '授权登录失败',icon: 'none'});
								}
							},
							fail: (err) => {
								uni.hideLoading();
								uni.showToast({title: '授权登录失败',icon: 'none'});
							}
						})
					},
					fail(err) {
						uni.hideLoading();
						uni.showToast({title: '获取用户信息失败',icon: 'none'});
					}
				})
			},
			
			//打开权限设置页提示框
			showSettingToast(e) {
				const that = this
				wx.showModal({
					title: '温馨提示',
					content: '您已拒绝获取微信信息,请开启',
					confirmText: '去设置',
					showCancel: false,
					content: e,
					success: function(res) {
						if (res.confirm) {
							///打开授权设置
							uni.openSetting({
								success(res) {
									if (res.authSetting['scope.userInfo']) {
										that.getUserInfo(e)
									} else {
										that.showSettingToast(e)
									}
								},
								fail(err) {
									console.log(err)
								}
							})
						}
					}
				})
			}
		},
		onLoad(options) {
			console.log(options)
			if (options.code) {
				this.code = options.code
			}
		},
		onShow() {
			console.log('wx.getUserProfile')
			console.log(wx.getUserProfile)
			if (wx.getUserProfile) {
				console.log('wx.getUserProfile可以使用')
			  this.canIUseGetUserProfile = true
			}
			// // 判断用户有没有登录
			// if (!this.$AppContext.checkLogin()) {

			// } else {
			// 	//先登陆
			// 	let stat = this;
			// 	uni.login({
			// 		provider: 'weixin',
			// 		success: (res) => {
			// 			userService.login({s: 'Init.init',code: res.code}).then(data => {
			// 				if (stat.$AppContext.checkLogin()) {
			// 					stat.userInfo = data
			// 				}
			// 			}).catch(err => {
			// 				uni.showToast({
			// 					title: err.msg,
			// 					icon: 'none'
			// 				})
			// 			});
			// 		},
			// 		fail: (res) => {
			// 			uni.showToast({
			// 				title: res.msg,
			// 				icon: 'none'
			// 			})
			// 		}
			// 	});
			// }
		}
	};
</script>

<style lang="scss">
	page {
		width: 100%;
		height: 100%;
		background: #141319;
	}

	.start {
		padding-bottom: 60upx; padding-top: 200upx;
		.start_img {
			width: 161upx;
			height: 144upx;
			display: block;
			margin: 0upx auto 45upx;
		}

		.start_title {
			font-size: 38upx;
			font-weight: bold;
			text-align: center;
			color: #FFFFFF;
		}

		.start_text {
			text-align: center;
			font-size: 24upx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #FFFFFF;
			margin-top: 22upx;
		}

		.footer_but {
			color: #fff;
			text-align: center;
			line-height: 90upx;
			width: 610upx;
			height: 90upx;
			background: #F56364;
			border-radius: 44upx;
			margin: auto;
			position: relative;
			z-index: 99;
		}
	}

	.buttonGetUserInfo {
		background: #F56364;
		font-size: 28upx;
		color: #FFFFFF;
		height: 90upx;
		line-height: 90upx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.logoimage{
		width: 664upx; height: 757upx; opacity: 0.03; position: absolute; left: 0; bottom: 0;
	}
	.bottomText{
		margin-top: -60upx;text-align: center;color: #888888;font-size: 24upx; font-weight: 400; line-height: 60upx;
	}
	.longTitle{
		width: 418upx; height: 71upx; margin: 128upx auto 111upx; font-size: 24upx;
		font-family: PingFang SC; font-weight: 300; color: #FFFFFF; line-height: 48upx;text-align: center;
	}
	.icon_wechat{
		width: 34upx; height: 30upx;
	}
</style>
