<template>
	<view class="full-width full-height">
		<!-- v-if="system_config.golf_diy.value == 0" -->
		<!-- v-if="system_config.golf_diy.value == 0" -->
		<view  v-if="system_config.golf_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: #43cf7c; border-radius: 44upx 0px 0px 44upx;">
			<view style="font-size: 24upx; font-weight: 800; color: #FFFFFF;">
				<view>定制</view>
				<view>体验</view>
			</view>
		</view>
		<!-- <view @click="Uploadpictures()"
		style="position: fixed; z-index: 99; right: 0; bottom: 724upx; display: flex;align-items: center;justify-content: center;
		width: 100upx; height: 88upx; background: #43cf7c; border-radius: 44upx 0px 0px 44upx;">
			<view style="font-size: 24upx; font-weight: 800; color: #FFFFFF;">
				<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: 60upx;">定制站必须扫码才能使用</view>
			</view>
		</view>
		<!-- 扫码定制 -->
		<view @click="scanning" style="width: 460upx; position: fixed; bottom: 100upx; margin: auto; 
		height: 164upx; background: rgba(92, 230, 181, 0.4); 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: #FFFFFF; font-size: 36upx;">开始扫码定制</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: {
			// Uploadpictures(){
			// 	var that = this
			// 	wx.chooseMessageFile({
			// 		count: 1,
			// 		type: 'image',
			// 		success(res) {
			// 			console.log(res);
			// 			// tempFilePath可以作为img标签的src属性显示图片
			// 			let tempFilePaths = [];
			// 			res.tempFiles.forEach(item => {
			// 				tempFilePaths.push(item.path)
			// 			})

			// 			var size = res.tempFiles[0].size;
			// 			var path = res.tempFiles[0].path;
			// 			var formatImage = path.split(".")[(path.split(".")).length - 1];
			// 			if (formatImage != "png" && formatImage != "jpg" && formatImage != "jpeg") {
			// 				return wx.showToast({
			// 					title: '只能上传.png、.jpg、.jpep 格式',
			// 					icon: 'none',
			// 					image: '',
			// 					duration: 2000,
			// 					mask: true,
			// 				})
			// 			}
			// 			that.uploadDIY(tempFilePaths, 0, 0, 0, tempFilePaths.length);
			// 		}
			// 	})
			// },
			/* 函数描述：作为上传文件时递归上传的函数体体；
			 * 参数描述： 
			 * filePaths是文件路径数组
			 * successUp是成功上传的个数->0
			 * failUp是上传失败的个数->0
			 * i是文件路径数组的指标->0
			 * length是文件路径数组的长度
			 * machine_id=user_id=
			 */
			// uploadDIY(tempFilePaths, successUp, failUp, i, length) {
			// 	let machine_id = this.machine_id || 0
			// 	let user_id = userService.getUserInfo().id || 0
			// 	var _this = this
			// 	wx.request({
			// 	  url: this.$Env.getDevBaseURL() + '/api/AliossSign/getSign?machine_id=' + machine_id + '&user_id=' + user_id,
			// 	  method: 'GET',
			// 	  data:'',
			// 	  header: {
			// 		'content-type':'application/x-www-form-urlencoded',
			// 		'Accept': 'application/json'
			// 	  },
			// 	  success: function (res) {
			// 			//这里是进度条
			// 			_this.percent = 0
			// 			var atter = res.data
			// 			let time = new Date().getTime() + Math.floor((Math.random() * 1000) + 1)
			// 			var uploadTask = wx.uploadFile({
			// 				url: atter.host.replace("http:", "https:"),
			// 				filePath: tempFilePaths[i],
			// 				name: 'file',
			// 				formData: {
			// 					'key': atter.dir + time + '.jpg',
			// 					'OSSAccessKeyId': atter.accessid,
			// 					'policy': atter.policy,
			// 					'Signature':  atter.signature,
			// 					'success_action_status': '200',
			// 					'callback':atter.callback,
			// 				},
			// 				success: function(res) {
			// 					successUp++;
			// 					if (res.statusCode != 200) {
			// 						// console.log({errCode: '599', msg:'其他错误'})
			// 						return;
			// 					}
			// 					if (res.statusCode == 200){
			// 					  _this.percent = 100
			// 					  uni.showToast({ title: '上传成功', icon: 'none',duration: 3000 })
			// 					  console.log(atter.host + '/' + atter.dir + time + '.jpg')
			// 					}
			// 				},
			// 				fail: function(err) {
			// 					failUp++;
			// 					wx.showToast({title: '上传失败！',icon: 'none',duration: 1200})
			// 				},
			// 				complete: function(err) {
			// 					console.log('总共' + successUp + '张上传成功,' + failUp + '张上传失败！');
			// 				}
			// 			})
			// 			uploadTask.onProgressUpdate((res) => {
			// 				console.log('上传进度', res.progress + " " + new Date())
			// 				console.log('已经上传的数据长度', res.totalBytesSent)
			// 				console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
			// 			})
			// 		}
			// 	})
			// },
			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')) {
									uni.navigateTo({
										url: '../mall/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')) {
										uni.navigateTo({
											url: '../mall/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) {
				// 控制点击事件在350ms内触发，加这层判断是为了防止长按时会触发点击事件
				if (this.touchEndTime - this.touchStartTime < 350) {
					// 当前点击的时间
					const currentTime = e.timeStamp;
					const lastTapTime = this.lastTapTime;
					// 更新最后一次点击时间
					this.lastTapTime = currentTime;
					// 如果两次点击时间在300毫秒内，则认为是双击事件
					if (currentTime - lastTapTime > 300) {
						if (!this.$AppContext.checkLogin()) {
							this.tologo('navigateTo')
						} else {
							uni.navigateTo({
								url: '../mall/machineDetail?machine_id=' + this.$Env.getmachineId() + '&worksid=0'
							});
							// uni.navigateTo({
							// 	url: '../index/index',
							// })
						}
					}
				}
			},
			tologo(e) {
				// 先登陆
				let that = this;
				uni.login({
					provider: 'weixin',
					success: (res) => {
						userService.login({
							s: 'Init.init',
							code: res.code,
							appcode: that.$Env.getappcode()
						}).then(r => {
							if (e == 'navigateTo') {
								uni.navigateTo({
									url: '../mall/machineDetail?machine_id=' + that.$Env.getmachineId() + '&worksid=0'
								});
							}
						}).catch(err => {
							that.$refs.Signin.init()
						});
					},
					fail: (res) => {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},
			systemConfig() {
				var _this = this
				wx.request({
					url: _this.$Env.getDevBaseURL() + '/api/system_config/detail',
					method: 'GET',
					data: {},
					header: {
						//设置参数内容类型为x-www-form-urlencoded
						'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() {
			// let sys = this.$base.appInformation ? this.$base.appInformation:wx.getSystemInfoSync();// 自定义页头适配
			// pixelRationum = sys.windowWidth / 375
			// let navBarHeight = sys.screenHeight - sys.windowHeight - sys.statusBarHeight;
			// this.titleHeight = sys.statusBarHeight + 46
			this.systemConfig()
		}
	};
</script>

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

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

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