<template>
	<view class="full-width full-height">

		<view v-if="system_config.baking_diy.value == 0"  @tap="doubleTap" @touchstart="touchStart" @touchend="touchEnd" style="position: fixed; z-index: 99; right: 0; bottom: 524upx; display: flex;align-items: center;justify-content: center;
		   width: 100upx; height: 88upx; background: #F29F3D; border-radius: 44upx 0px 0px 44upx;">
			<view style="font-size: 24upx; font-weight: 800; color: #FFFFFF;">
				<view>定制</view>
				<view>体验</view>
			</view>
		</view>

		<!-- 无设备 -->
		<view style="display: flex; justify-content: center;" :style="{height: 'calc(100% - ' + titleHeight +'px)'}">
			<view style="margin-top: 231upx;">
				<image src="../../static/icon_diy.png" mode="" style="width: 513upx; height: 251upx;"></image>
				<view class="start_title" style="margin-top: 120upx;">定制站必须扫码才能使用</view>
			</view>
		</view>

		<!-- 扫码定制 -->
		<view @click="scanning" style="width: 460upx; position: fixed; bottom: 180upx; margin: auto; 
		height: 164upx; background: #F5DFA4 ;left: 0; right: 0; z-index: 99; border-radius: 20upx; 
		display: flex; align-items: center;">
			<view style="background: #FFFFFF;width: 120upx;
				height: 120upx;border-radius: 50%; margin: 22upx 30upx;text-align: center;">
				<image style="height: 61upx; width: 61upx;margin:32upx auto;" src="../../static/icon_saoyisao.png"
					mode=""></image>
			</view>
			<view style="color: #333333 ; font-weight: bold;font-size: 34upx;">开始扫码定制</view>
		</view>
		<!-- 登录弹框 -->
		<Signin ref="Signin"></Signin>
	</view>
</template>

<script>
	import userService from '@/service/UserService';
	import Signin from './Signin.vue';
	var pixelRationum = null;
	export default {
		components: {
			Signin
		},
		computed: {

		},
		data() {
			return {
				titleHeight: 0,
				animstart: false,
				bganimstart: false,
				touchStartTime: null,
				touchEndTime: null,
				lastTapTime: null,
				system_config: null,
			};
		},
		methods: {

			getQueryString(url, name) {
				var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
				var r = url.substr(1).match(reg)
				if (r != null) {
					return r[2]
				}
				return null;
			},

			scanning() {
				if (!this.$AppContext.checkLogin()) {
					this.tologo('navigateTo')
				} else {
					let that = this;
					/*允许从相机和相册扫码
						QR_CODE 普通二维码
						WX_CODE 小程序二维码
						生成二维码链接
					*/
					uni.scanCode({
						success: function(res) {
							console.log(res)
							if (res.scanType == 'QR_CODE') {
								if (that.getQueryString(res.result, 'machine_id') && that.getQueryString(res
										.result, 'machine_id') != "" && that.getQueryString(res.result,
										'machine_id') != null && that.getQueryString(res.result,
										'machine_id') != "null") {
									uni.navigateTo({
										url: '../index/machineDetail?machine_id=' + that
											.getQueryString(
												res.result, 'machine_id')
									});
								} else {
									uni.showToast({
										title: '扫码未能识别色彩站，请检查二维码',
										icon: 'none',
										duration: 2000
									});
								}
							} else {
								if (res.path) {
									if (that.getQueryString(res.path, 'machine_id') && that.getQueryString(res
											.path, 'machine_id') != "" && that.getQueryString(res
											.path, 'machine_id') != null && that.getQueryString(res.path,
											'machine_id') != "null") {
										uni.navigateTo({
											url: '../index/machineDetail?machine_id=' + that
												.getQueryString(res.path, 'machine_id')
										});
									} else {
										uni.showToast({
											title: '扫码未能识别色彩站，请检查二维码',
											icon: 'none',
											duration: 2000
										});
									}
								} else {
									uni.showToast({
										title: '扫码未能识别色彩站，请检查二维码',
										icon: 'none',
										duration: 2000
									});
								}
							}
						},
						fail: function(err) {
							console.log(err)
						},
					});
				}
			},
			//防止重复点击
			touchStart(e) {
				this.touchStartTime = e.timeStamp;
			},
			touchEnd(e) {
				this.touchEndTime = e.timeStamp;
			},
			doubleTap(e) {
				//console.log("点击")
				// 控制点击事件在350ms内触发，加这层判断是为了防止长按时会触发点击事件
				if (this.touchEndTime - this.touchStartTime < 350) {
					//console.log("点击1")
					// 当前点击的时间
					const currentTime = e.timeStamp;
					const lastTapTime = this.lastTapTime;
					// 更新最后一次点击时间
					this.lastTapTime = currentTime;
					// 如果两次点击时间在300毫秒内，则认为是双击事件
					if (currentTime - lastTapTime > 300) {
						if (!this.$AppContext.checkLogin()) {
							this.tologo('navigateTo')
							//console.log("点击2")
							// uni.navigateTo({
							// 	url: '../index/machineDetail?machine_id=' + this.$Env.getmachineId()
							// });
						} else {
							//console.log("点击3")
							if (this.$Env.getmachineId() && this.$Env.getmachineId() != "") {
								uni.navigateTo({
									url: '../index/machineDetail?machine_id=' + this.$Env.getmachineId()
								});
							}
						}
					}
				}
			},

			tologo(e) {
				//先登陆
				let that = this;
				uni.login({
					provider: 'weixin',
					success: (res) => {
						userService.login({
							s: 'Init.init',
							code: res.code,
							appcode: 'lite'
						}).then(r => {
							if (e == 'navigateTo') {
								uni.navigateTo({
									url: '../index/machineDetail?machine_id=' + that.$Env.getmachineId() + '&worksid=0'
								});
							}
						}).catch(err => {
							that.$refs.Signin.init();
						});
					},
					fail: (res) => {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},

			//tologo(e) {
			// 	//先登陆
			// 	let that = this;
			// 	uni.login({
			// 		provider: 'weixin',
			// 		success: (res) => {
			// 				//console.log(res)
			// 			userService.login({
			// 				s: 'Init.init',
			// 				code: res.code,
			// 				appcode: that.$Env.getappcode()
			// 			}).then(r => {
			// 				//console.log(r )
			// 				//console.log("点击4")
			// 				// if (e == 'navigateTo') {
			// 				// 	uni.navigateTo({
			// 				// 		url: '../index/machineDetail?machine_id=' + that.$Env
			// 				// 			.getmachineId() + '&worksid=0'
			// 				// 	});
			// 				// }
			// 			}).catch(err => {
			// 				console.log(err )
			// 				that.$refs.Signin.init()
			// 			});
			// 		},
			// 		fail: (res) => {
			// 			console.log(res)
			// 			uni.showToast({
			// 				title: res.msg,
			// 				icon: 'none'
			// 			})
			// 		}
			// 	});
			// },

			//登录跳进去
			logoToHome() {
				const that = this
				uni.getSetting({
					success(res) {
						if (res.authSetting['scope.userLocation']) { //用户同意授权获取定位信息
							that.getLocation()
						} else { //用户拒绝授权再次请求授权
							that.getAuthorize()
						}
					}
				})
			},


			systemConfig() {
				var _this = this
				wx.request({
					url: _this.$Env.getDevBaseURL() + '/api/system_config/detail',
					method: 'GET',
					data: {},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Accept': 'application/json'
					},
					success: function(res) {
						_this.system_config = res.data.data
						uni.stopPullDownRefresh();
						uni.hideLoading();
					},
					fail: function(err) {
						uni.stopPullDownRefresh();
						uni.hideLoading();
					},
					complete: function(end) {
						uni.stopPullDownRefresh();
						uni.hideLoading();
					}
				})
			}
		},
		//下拉加载
		onPullDownRefresh() {
			uni.showLoading({
				title: '正在刷新中...',
				mask: true
			})
			this.systemConfig()
		},
		//上拉加载
		onReachBottom() {},
		//滑动
		onPageScroll(t) {},
		//分享好友
		onShareAppMessage() {
			return {
				title: this.$Env.getmalltext(),
				imageUrl: '',
				path: '',
				success: (res) => {},
			}
		},
		//分享朋友圈
		onShareTimeline() {
			return {
				title: this.$Env.getmalltext(),
				imageUrl: '',
				path: '',
				query: 'kjbfrom=pyq'
			}
		},
		onLoad(options) {
			if (options.q) { // 分销系统 获取二维码的携带的链接信息
				let qrUrl = decodeURIComponent(options.q)
				let promoter_id = qrUrl.replace(/[^0-9]/ig, "")
				this.$base.promoter_id = promoter_id
			}
		},
		onShow() {
			this.systemConfig()
		}
	};
</script>

<style lang="scss">
	@import "./css/main.css";

	page {
		background: #ffffff !important;
		width: 100%;
		margin: 0;
		padding: 0;
	}

	.start_title {
		text-align: center;
		font-size: 36upx;
		font-weight: 400;
		color: #333333;
		line-height: 20upx;
	}
</style>
