<template>
	<view class="page">
		<u-navbar :is-back="false" :border-bottom="false" :background="bgc" title-color='#2E4975' title-size='36'
			height='8'></u-navbar>
		<image src="https://api.ccttiot.com/smartmeter/img/static/u3lZnmhqSqkMd7gHKtMD" mode="" class="imgbj"></image>
		<view class="title">
			<text v-if="userType != 00" @click="btnshanghu">商户中心</text>
		</view>
		<view class="serchshop">
			<view class="dingwei" @click="btnweizhi">
				<image src="https://api.ccttiot.com/smartmeter/img/static/uSDeC51wazjnZ9cFQx6l" mode="" class="dwimg">
				</image>
				{{tit}}
				<image src="https://api.ccttiot.com/smartmeter/img/static/u0FZH7ZLG168c7W0FUMk" mode="" class="xlimg">
				</image>
			</view>
			<view class="search">
				<input type="text" v-model="sousuovalue" @confirm="confirm" placeholder="搜索门店" />
				<u-icon name="search"></u-icon>
			</view>
		</view>
		<view class="" style="margin-top: 48rpx;">
			<view class="tabqh">
				<view @click="btntabindex(0)" :class="tabindex == 0 ? 'activetab' : ''">
					<text>全部</text>
					<image v-if="tabindex == 0" src="https://api.ccttiot.com/smartmeter/img/static/uHFOqckZJiCEP9JOtMvj"
						mode=""></image>
				</view>
				<view @click="btntabindex(2)" :class="tabindex == 2 ? 'activetab' : ''">
					<text>棋牌</text>
					<image v-if="tabindex == 2" src="https://api.ccttiot.com/smartmeter/img/static/uQ6c41aBHWUOD02aK9WQ"
						mode=""></image>
				</view>
				<view @click="btntabindex(1)" :class="tabindex == 1 ? 'activetab' : ''">
					<text>茶室</text>
					<image v-if="tabindex == 1" src="https://api.ccttiot.com/smartmeter/img/static/uQ6c41aBHWUOD02aK9WQ"
						mode=""></image>
				</view>
				<view @click="btntabindex(3)" style="width: 34%;" :class="tabindex == 3 ? 'activetab' : ''">
					<text>台球</text>
					<image v-if="tabindex == 3" style="transform: rotateY(180deg)"
						src="https://api.ccttiot.com/smartmeter/img/static/uQ6c41aBHWUOD02aK9WQ" mode=""></image>
				</view>
				<view @click="btntabindex(4)" :class="tabindex == 4? 'activetab' : ''">
					<text>其他</text>
					<image v-if="tabindex == 4" src="https://api.ccttiot.com/smartmeter/img/static/uHFOqckZJiCEP9JOtMvj"
						mode=""></image>
				</view>
			</view>
			<view class="shoplist" @touchstart="onTouchStart" @touchend="onTouchEnd" @touchcancel="onTouchCancel">
				<view class="shop_item" v-for="(item,index) in listshop" :key="index"
					@click="btndetail(item.storeId,item)">
					<view class="lt">
						<image :src="item.pictures[0]" mode="aspectFill"></image>
					</view>
					<view class="rt">
						<view class="name">
							{{item.name == undefined ? '--' : item.name}}
						</view>
						<view class="dizhi">
							{{item.address.length > 13 ? item.address.slice(0,13) + '...' : item.address}}
						</view>
						<view class="storebq">
							<image v-if="item.typeTags && item.typeTags.some(tag => tag == 1)"
								src="https://api.ccttiot.com/smartmeter/img/static/u7ilmXTNlXUbbvfSgOa1" mode="">
							</image>
							<image v-if="item.typeTags && item.typeTags.some(tag => tag == 2)"
								src="https://api.ccttiot.com/smartmeter/img/static/umA2EUZkovZEA0hCLmP3" mode="">
							</image>
							<image v-if="item.typeTags && item.typeTags.some(tag => tag == 3)"
								src="https://api.ccttiot.com/smartmeter/img/static/uPjeCCnwjiJlCNqURltX" mode="">
							</image>
							<image v-if="item.typeTags && item.typeTags.some(tag => tag == 4)"
								src="https://api.ccttiot.com/smartmeter/img/static/uvbY7v6slwyqVghulsZW" mode="">
							</image>
						</view>
						<view class="juli">
							<view class="kmjuli">
								距我{{item.distance == undefined ? '--' : item.distance}}km
							</view>
							<view class="juliimg">
								<image @click.stop="btndaohang(item.lng,item.lat,item.name,item.address)"
									src="https://api.ccttiot.com/smartmeter/img/static/ufhdTzq4mHUlHgCKgI5Y" mode="">
								</image>
								<image @click.stop="btntel(item.serverPhone)"
									src="https://api.ccttiot.com/smartmeter/img/static/uOxtHZOxGREoFVXEv73f" mode="">
								</image>
							</view>
						</view>
						<view class="yuding">
							<image src="https://api.ccttiot.com/smartmeter/img/static/u7zHXOY1SGwic7jQESUR" mode=""
								class="one"></image>
							预定包间
							<image src="https://api.ccttiot.com/smartmeter/img/static/urIGUWi7cHWqp9qem3Ag" mode=""
								class="two"></image>
						</view>
					</view>
				</view>
				<view class="" style="width: 100%;text-align: center;margin-top: 40rpx;font-size: 34rpx;color: #ccc;">
					没有更多门店啦，敬请期待吧...
				</view>
			</view>
		</view>
		<view class="jiameng" v-if="guangaoflag" @click="btnjiameng">
			<view class="gb" @click.stop="guangaoflag = false">
				关闭 <text>×</text>
			</view>
			<image src="https://api.ccttiot.com/smartmeter/img/static/uuoqWaq7HnXBteQayaz4" mode=""></image>
		</view>
		<view class="xuanfu" v-if="imgflag">
			<image @click="btnopen(1)" src="https://api.ccttiot.com/smartmeter/img/static/uKeCHdot1z4QJldDe3UL" mode="">
			</image>
			<image @click="btnopen(2)" src="https://api.ccttiot.com/smartmeter/img/static/u4lmGbm77OWC2Uik2737" mode="">
			</image>
			<image @click="btnopen(3)" src="https://api.ccttiot.com/smartmeter/img/static/uUjamKC7Kvd9KMLLOeBD" mode="">
			</image>
			<image @click="btnopen(6)" src="https://api.ccttiot.com/smartmeter/img/static/un3Pb0hkKl0Nu9SnaW3W" mode="">
			</image>
			<image @click="btnopen(4)" src="https://api.ccttiot.com/smartmeter/img/static/uh58EYwwWo92FYNe19Yx" mode="">
			</image>
			<image @click="btnopen(5)" src="https://api.ccttiot.com/smartmeter/img/static/uONMVGcplNBtVnjlHaeh" mode="">
			</image>
		</view>
		<view class="xuanfus" v-else>
			<image @click="btnopen(1)" src="https://api.ccttiot.com/smartmeter/img/static/uKeCHdot1z4QJldDe3UL" mode="">
			</image>
			<image @click="btnopen(2)" src="https://api.ccttiot.com/smartmeter/img/static/u4lmGbm77OWC2Uik2737" mode="">
			</image>
			<image @click="btnopen(6)" src="https://api.ccttiot.com/smartmeter/img/static/un3Pb0hkKl0Nu9SnaW3W" mode="">
			</image>
			<image @click="btnopen(3)" src="https://api.ccttiot.com/smartmeter/img/static/uUjamKC7Kvd9KMLLOeBD" mode="">
			</image>
			<image @click="btnopen(4)" src="https://api.ccttiot.com/smartmeter/img/static/uh58EYwwWo92FYNe19Yx" mode="">
			</image>
			<image @click="btnopen(5)" src="https://api.ccttiot.com/smartmeter/img/static/uONMVGcplNBtVnjlHaeh" mode="">
			</image>
		</view>
		<view class="karaokeroom" v-if="jinxinflag">
			<view class="top">
				正在进行中的订单
			</view>
			<view class="roomlist">
				<view class="room_item" v-for="(item,index) in jinxinglist" :key="index"
					:id="mdindex == index ? 'active' : ''" @click="btnmd(index,item)">
					<view class="lt">
						<image :src="item.picture" mode=""></image>
					</view>
					<view class="rt" style="font-size: 22rpx;">
						<view class="">
							<text>预约门店：</text> <text>{{item.storeName == null ? '--' : item.storeName}}</text>
						</view>
						<view class="">
							<text>预约包厢：</text> <text>{{item.roomName == null ? '--' : item.roomName}}</text>
						</view>
						<view class="">
							<text>开始时间：</text>
							<text>{{item.reserveStartTime == null ? '--' : item.reserveStartTime}}</text>
						</view>
						<view class="">
							<text>结束时间：</text> <text>{{item.reserveEndTime == null ? '--' : item.reserveEndTime}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="anniu" @click="btnanniu">
				{{jinxintxt}}
			</view>
		</view>
		<image v-if="jinxinflag" @click="jinxinflag = false" class="jinxinx"
			src="https://api.ccttiot.com/smartmeter/img/static/uH5fAInJQYhf3wlXkuYI" mode=""></image>
		<view class="mask" v-if="jinxinflag"></view>
		<!-- 厕所 --><!-- 厕所 --><!-- 厕所 --><!-- 厕所 --><!-- 厕所 --><!-- 厕所 --><!-- 厕所 --><!-- 厕所 --><!-- 厕所 -->
		<view class="cesuoul" v-if="cesuoflag">
			<view class="cesuoli" v-for="(item,index) in cesuolist" :key="index">
				<view class="caocuo">
					<view class="name">
						{{item.name}}
					</view>
					<view class="kaiguan" @click="btncs(item)">
						开门
					</view>
				</view>
			</view>
			<view class="anniuqx" @click="cesuoflag = false">
				取消
			</view>
		</view>
		<view class="mask" v-if="cesuoflag"></view>
		<view class="lxlanya" v-if="lanyaflag">
			<view class="lxtit">
				蓝牙控制开关
			</view>
			<view class="cha" @click.stop="btncha">
				×
			</view>
			<view class="lxzt">
			    <view v-if="ver_dataflag == 1" style="color: red;">
			    	<image src="https://api.ccttiot.com/smartmeter/img/static/u6akzRBuxxP94XlUcMtV" mode=""></image>
			    	<text>未连接</text>
			    </view>
			    <view v-if="ver_dataflag == 2" style="color: #ccc;">
			    	<image src="https://api.ccttiot.com/smartmeter/img/static/u6akzRBuxxP94XlUcMtV" mode=""></image>
			    	<text>连接中</text>
			    </view>
			    <view v-if="ver_dataflag == 3" style="color: #48893B;">
			    	<image src="https://api.ccttiot.com/smartmeter/img/static/uU0nfPcXVEhbEsvECEQg" mode=""></image>
			    	<text>已连接</text>
			    </view>
			</view> 
			<view class="kongzhi">
		      <!--  <view class="lxkai" @click.stop="btngb">
		        	关闭
		        </view> -->
		        <view class="lxkai" @click.stop="btnkq">
		        	开启
		        </view>				
			</view>
		</view>
		<view class="mask" v-if="lanyaflag"></view>
	</view>
</template>
<script>
	var xBlufi = require("@/components/blufi/xBlufi.js"); 
	export default {
		data() {
			return {
				guangaoflag: true,
				jinxintxt: '开包厢',
				imgflag: true,
				longitude: '',
				latitude: '',
				tit: '',
				listshop: [],
				cityid: '',
				pagesum: 1,
				pagesize: 10,
				touchStartX: 0, // 触屏起始点x
				touchStartY: 0, // 触屏起始点y  
				xiaflag: true,
				total: '',
				qingqiuflag: true,
				jinxinglist: [],
				mdindex: -1,
				viewType: '',
				jinxinflag: false,
				sousuovalue: '',
				tabindex: 0,
				type: 0,
				userType: '00',
				cesuolist: [],
				cesuoflag: false,
				csindex: -1,
				ver_dataflag:2,
				devicesarr:[],
				deviceIds:'',
				name:'',
				deviceId:'',
				lanyaflag:false,
				lanyamac:'',
				lanyaobj:{}
			}
		},
		// 分享到好友（会话）
		onShareAppMessage: function() {
			return {
				title: '共享时光屋',
				path: '/pages/nearbystores/index'
			}
		},
		// 分享到朋友圈  
		onShareTimeline: function() {
			return {
				title: '共享时光屋',
				query: '',
				path: '/pages/nearbystores/index'
			}
		},
		onLoad(option) {
			wx.openBluetoothAdapter({
			  success: function (res) {
			    console.log('蓝牙适配器初始化成功')
				xBlufi.initXBlufi(1)
			  },
			  fail: function (err) {
			    console.error('蓝牙适配器初始化失败，可能是因为没有权限', err)
			  }
			})
			if (option.cityId) {
				this.cityid = option.cityId
				this.tit = option.cityName
				this.longitude = ''
				this.latitude = ''
				this.getweizhi()
			} else {
				this.getweizhi()
			}
			if(option.q){
				function getQueryParam(url, paramName) {
					let regex = new RegExp(`[?&]${paramName}=([^&]*)`)
					let results = regex.exec(url);
					return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null
				}
				let sceneValue = option.q
				let decodedValue = decodeURIComponent(sceneValue)
				let id = getQueryParam(decodedValue, 'sn')
				console.log(id);
				let that = this
				let data = {
					sn: id,
				}
				that.$u.get(`/app/device/isBind?deviceNo=${id}`).then(res => {
					if (res.data == 2) {
						uni.showToast({
							title: '该设备已被绑定',
							icon: 'none',
							duration: 2000
						})
					} else if (res.data == 1) {
						uni.showModal({
							title: '提示',
							content: '该设备未绑定,你需进行绑定吗？',
							success: function(res) {
								if (res.confirm) {
									that.$u.put("/app/device/bind", data).then(res => {
										if (res.code == 200) {
											uni.showToast({
												title: '绑定成功',
												icon: 'success',
												duration: 2000
											})
											that.userType = '01'
										}else{
											uni.showToast({
												title: res.msg,
												icon: 'none',
												duration: 2000
											})
										}
									})
								}
							}
						})
					} else if (res.data == 0) {
						uni.showToast({
							title: '该设备为录入，请先录入',
							icon: 'none',
							duration: 2000
						})
					}
				})
			}
			this.getinfo()
			this.getorder()
		},
		onShow() {
			this.guangaoflag = true
			xBlufi.notifyStartDiscoverBle({'isStart': true})
			xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
		},
		methods: {
			// 蓝牙开启
			btnkq(){
				if(this.lanyaobj.gateUnlockMode == 1){
					xBlufi.notifySendCustomData({
						customData: '11km' + this.lanyaobj.gateUnlockTime + '@'
					})
					let data = {
						mac:this.lanyamac,
						reason:'正常开门',
						command:'11km' + this.lanyaobj.gateUnlockTime + '@',
						result:true
					}
					this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
				}else if(this.lanyaobj.gateUnlockMode == 2){
					xBlufi.notifySendCustomData({
						customData: '11gm' + this.lanyaobj.gateUnlockTime + '@'
					})
					let data = {
						mac:this.lanyamac,
						reason:'反向开门',
						command:'11gm' + this.lanyaobj.gateUnlockTime + '@',
						result:true
					}
					this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
				}else if(this.lanyaobj.unlockMode == 1){
					xBlufi.notifySendCustomData({
						customData: '11km' + this.lanyaobj.unlockTime + '@'
					})
					let data = {
						mac:this.lanyamac,
						reason:'正常开门',
						command:'11km' + this.lanyaobj.unlockTime + '@',
						result:true
					}
					this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
				}else{
					xBlufi.notifySendCustomData({
						customData: '11gm' + this.lanyaobj.unlockTime + '@'
					})
					let data = {
						mac:this.lanyamac,
						reason:'反向开门',
						command:'11gm' + this.lanyaobj.unlockTime + '@',
						result:true
					}
					this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
				}
			},
			// 蓝牙关闭
			btngb(){
				xBlufi.notifySendCustomData({
					customData: '11close'
				})
				let data = {
					mac:this.lanyamac,
					reason:'关闭',
					command:'11close',
					result:true
				}
				this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
			},
			// 点击蓝牙弹出隐藏
			btncha(){
				this.lanyaflag = false
				this.ver_dataflag = 2
				xBlufi.notifyConnectBle({
				    isStart: false,
				    deviceId: this.deviceId,
				    name: this.name
				})
			},
			// 连接蓝牙函数
			getlanya() {
				if (this.ver_dataflag != 3) {
					this.ver_dataflag = 2
					let that = this
					if (that.deviceId != '') {
						let systemInfo = uni.getSystemInfoSync()
							if (systemInfo.platform == 'android') {
								that.lanyaflag = true
								that.deviceId = that.deviceId.match(/.{2}/g).reverse().join(':')
								xBlufi.notifyStartDiscoverBle({
									'isStart': false 
								})
								console.log("进入蓝牙连接", that.deviceId)
								console.log("进入that.name", that.name)
								xBlufi.notifyConnectBle({
									isStart: true,
									deviceId: that.deviceId,
									name: that.name
								})
							} else if (systemInfo.platform == 'ios') {
								// 当前设备是 iOS 
								that.lanyaflag = true
							    that.findDevice()
						}else{
							// that.lanyaflag = true
							console.log('其他');
						}
					}
				}
			},
			findDevice() {
			    const matchedDevice = this.devicesarr.find(device => {
			        console.log(device.name, '111') 
			        console.log("IOSMAC", device)
			        return device.name.slice(-12) == this.deviceId.slice(-12)
			    })
			    if (matchedDevice) {
			        xBlufi.notifyStartDiscoverBle({
			            'isStart': false
			        })
			        xBlufi.notifyConnectBle({
			            isStart: true,
			            deviceId: matchedDevice.deviceId,
			            name: matchedDevice.name
			        })
			        this.deviceId = matchedDevice.deviceId
			        this.name = matchedDevice.name
			        console.log(this.deviceId, '11221')
			    } else {
			        console.log("未找到匹配的设备，继续查找...")
			        setTimeout(this.findDevice.bind(this), 1000) // 使用 bind 保持 this 上下文
			    }
			},
			// 获取附近蓝牙设备列表
			funListenDeviceMsgEvent: function (options) {
				switch (options.type) {
					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
						if (options.result) {
							this.devicesarr = options.data
						}
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
						console.log("连接回调：" + JSON.stringify(options))
						if (options.result == true) { 
							setTimeout(()=>{
								this.ver_dataflag = 3
							},2000)
							xBlufi.notifyInitBleEsp32({
								deviceId: this.deviceId
							})
							this.deviceIds = options.data.deviceId
							this.name = this.name
						} 
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
						this.ver_dataflag = 3
						console.log("1收到设备发来的自定义数据结果：", options.data)
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
						if (!options.result) {
							console.log('蓝牙未开启')
							return
						}
						break;
				}
			},
			// 点击切换门店类型
			btntabindex(num) {
				this.tabindex = num
				this.type = num
				this.pagesum = 1
				this.listshop = []
				this.getshop()
			},
			// 点击跳转到商户中心
			btnshanghu() {
				uni.navigateTo({
					url: '/page_shanghu/index'
				})
			},
			// 点击选择开包厢
			btnmd(index, item) {
				this.lanyaobj = item
				this.viewType = item.reType
				this.mdindex = index
			},
			// 请求正在进行中的订单
			getorder() {
				this.$u.get(`/app/order/getInProgressOrderList`).then(res => {
					if (res.code == 200) {
						this.jinxinglist = res.data
						this.lanyaobj = res.data[0]
					}
				})
			},
			// 点击进行导航
			btndaohang(lng, lat, name, addres) {
				uni.openLocation({
					latitude: lat, //纬度-目的地/坐标点
					longitude: lng, //经度-目的地/坐标点
					name: name, //地点名称
					address: addres //详细地点名称
				})
			},
			// 点击拨打电话
			btntel(serverPhone) {
				uni.makePhoneCall({
					phoneNumber: serverPhone,
					success: function(res) {
						console.log('拨打电话成功', res)
					},
					fail: function(err) {
						console.error('拨打电话失败', err)
						uni.showToast({
							title: '取消拨打电话',
							icon: 'none'
						})
					}
				})
			},
			// 选择订单后进行操作
			btnanniu() {
				if (this.jinxintxt == '开店门') {
					let storeId = this.jinxinglist[this.mdindex].storeId
					console.log(this.jinxinglist[this.mdindex], this.mdindex);
					this.$u.get(`app/store/openGate/${storeId}`).then(res => {
						if (res.code == 200) {
							this.jinxinflag = false
							this.mdindex = -1
							uni.showToast({
								title: '开启店门成功',
								icon: 'success',
								duration: 2000
							})
						} else if(res.code == 501){
							this.jinxinflag = false
							this.mdindex = -1
							this.deviceId = res.msg
							this.lanyamac = res.msg
							this.getlanya()
						}else{
							this.mdindex = -1
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				} else if (this.jinxintxt == '开包厢') {
					let roomId = this.jinxinglist[this.mdindex].roomId
					this.$u.post(`/app/device/adminAll/${roomId}/switch?open=true&orderNo=${this.jinxinglist[this.mdindex].orderNo}`).then(res => {
						if (res.code == 200) {
							this.jinxinflag = false
							this.mdindex = -1
							uni.showToast({
								title: '开启包厢门成功',
								icon: 'success',
								duration: 2000
							})
						} else {
							this.mdindex = -1
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				} else if (this.jinxintxt == '去续单') {
					this.jinxinflag = false
					let roomId = this.jinxinglist[this.mdindex].roomId
					let orderNo = this.jinxinglist[this.mdindex].orderNo
					uni.navigateTo({
						url: '/page_user/xudan?roomId=' + roomId + '&orderNo=' + orderNo + '&viewType=' + this.viewType
					})
				}
			},
			// 点击进行开店门开包厢等操作
			btnopen(num) {
				if (num == 1) {
					if (this.jinxinglist.length > 1) {
						this.jinxinflag = true
						this.jinxintxt = '开店门'
					} else if (this.jinxinglist.length == 1) {
						let storeId = this.jinxinglist[0].storeId
						this.$u.get(`app/store/openGate/${storeId}`).then(res => {
							if (res.code == 200) {
								uni.showToast({
									title: '开启店门成功',
									icon: 'success',
									duration: 2000
								})
							} else if(res.code == 501){
							    this.deviceId = res.msg
							    this.lanyamac = res.msg
							    this.getlanya()
						    }else{
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								})
							}
						})
					} else {
						uni.showToast({
							title: '您当前没有订单',
							icon: 'none',
							duration: 2000
						})
					}
				} else if (num == 2) {
					if (this.jinxinglist.length > 1) {
						this.jinxinflag = true
						this.jinxintxt = '开包厢'
					} else if (this.jinxinglist.length == 1) {
						let roomId = this.jinxinglist[0].roomId
						this.$u.post(`/app/device/adminAll/${roomId}/switch?open=true&orderNo=${this.jinxinglist[0].orderNo}`).then(res => {
							if (res.code == 200) {
								uni.showToast({
									title: '开启包厢门成功',
									icon: 'success',
									duration: 2000
								})
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								})
							}
						})
					} else {
						uni.showToast({
							title: '您当前没有订单',
							icon: 'none',
							duration: 2000
						})
					}
				} else if (num == 3) {
					if (this.jinxinglist.length > 1) {
						this.jinxinflag = true
						this.jinxintxt = '去续单'
					} else if (this.jinxinglist.length == 1) {
						let roomId = this.jinxinglist[0].roomId
						let orderNo = this.jinxinglist[0].orderNo
						uni.navigateTo({
							url: '/page_user/xudan?roomId=' + roomId + '&orderNo=' + orderNo
						})
					} else {
						uni.showToast({
							title: '您当前没有订单',
							icon: 'none',
							duration: 2000
						})
					}
				} else if (num == 4) {
					uni.scanCode({
						onlyFromCamera: true,
						scanType: ['qrCode'],
						success: res => {
							function getQueryParam(url, paramName) {
								let regex = new RegExp(`[?&]${paramName}=([^&]*)`)
								let results = regex.exec(url);
								return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null
							}
							console.log(res);
							let sceneValue = res.result
							let decodedValue = decodeURIComponent(sceneValue)
							let id = getQueryParam(decodedValue, 'sn')
							let storeCode = getQueryParam(decodedValue, 'd')
							let roomCode = getQueryParam(decodedValue, 'f')
							if (id) {
								let that = this
								let data = {
									sn: id,
								}
								that.$u.get(`/app/device/isBind?deviceNo=${id}`).then(res => {
									if (res.data == 2) {
										uni.showToast({
											title: '该设备已被绑定',
											icon: 'none',
											duration: 2000
										})
									} else if (res.data == 1) {
										uni.showModal({
											title: '提示',
											content: '该设备未绑定,你需进行绑定吗？',
											success: function(res) {
												if (res.confirm) {
													that.$u.put("/app/device/bind", data).then(res => {
														if (res.code == 200) {
															uni.showToast({
																title: '绑定成功',
																icon: 'success',
																duration: 2000
															})
															that.userType = '01'
														}
													})
												}
											}
										})
									} else if (res.data == 0) {
										uni.showToast({
											title: '该设备为录入，请先录入',
											icon: 'none',
											duration: 2000
										})
									}
								})
							} else if (storeCode) {
								this.$u.get(`app/getId?storeCode=${storeCode}`).then(resp => {
									if (resp.code == 200) {
										uni.navigateTo({
											url:'/pages/index/index?storeId=' + resp.data
										})
									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 2000
										})
									}
								})
							} else if (roomCode) {
								this.$u.get(`app/getId?roomCode=${roomCode}`).then(resp => {
									if (resp.code == 200) {
										console.log('这是 roomId:', resp.data);
										let roomId = resp.data
										this.$u.get(`/app/order/getInProgressOrderList`).then(res => {
											if (res.code == 200) {
												if (res.data == null || res.data.length == 0) {
													uni.navigateTo({
														url: '/pages/myorder/index?roomId=' + roomId
													})
												} else {
													uni.navigateTo({
														url: '/page_user/dingdanxq?orderNo=' + res.data[0].orderNo + '&viewType=' + res.data[0].roomType2
													})
												}
											} else if (res.code == 401) {
												uni.navigateTo({
													url: '/pages/login/index'
												})
											} else {
												uni.showToast({
													title: res.msg,
													icon: 'none',
													duration: 2000
												})
											}
										})
									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 2000
										})
									}
								})
							} else {
								console.log('URL 中未找到 storeCode 或 roomCode')
							}
						}
					})
				} else if (num == 5) {
					uni.switchTab({
						url: '/pages/tj'
					})
				} else if (num == 6) {
					if (this.jinxinglist.length > 0) {
						let storeId = this.jinxinglist[0].storeId
						this.$u.get(`/app/toilet/list?storeId=${storeId}`).then(res => {
							if (res.code == 200) {
								if (res.rows == null || res.rows.length == 0) {
									uni.showToast({
										title: '暂无卫生间',
										icon: 'none',
										duration: 2000
									})
								} else {
									this.cesuolist = res.rows
									this.lanyaobj = res.rows[0]
									if (this.cesuolist.length == 1) {
										this.$u.get(`/app/room/openToilet/${this.cesuolist[0].toiletId}`).then(res => {
											if (res.code == 200) {
												uni.showToast({
													title: '打开成功',
													icon: 'success',
													duration: 2000
												})
												this.cesuoflag = false
											} else if(res.code == 501){
											    this.deviceId = res.msg
											    this.lanyamac = res.msg
											    this.getlanya()
										    }else{
												uni.showToast({
													title: res.msg,
													icon: 'none',
													duration: 2000
												})
											}
										})
									} else {
										this.cesuoflag = true
									}
								}
							}
						})
					} else {
						uni.showToast({
							title: '当前没有订单，请先下单',
							icon: 'none',
							duration: 2000
						})
					}
				}
			},
			// 点击打开厕所门
			btncs(item) {
				this.lanyaobj = item
				console.log(item);
				if (item.deviceId == null) {
					uni.showToast({
						title: '该厕所未安装设备',
						icon: 'none',
						duration: 2000
					})
				} else {
					this.$u.get(`/app/room/openToilet/${item.toiletId}`).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '打开成功',
								icon: 'success',
								duration: 2000
							})
							this.cesuoflag = false
						}else if(res.code == 501){
							this.cesuoflag = false
							this.deviceId = res.msg
							this.lanyamac = res.msg
							this.getlanya()
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				}
			},
			// 点击输入框确定执行搜索
			confirm() {
				this.pagesum = 1
				this.getshop()
				console.log(this.sousuovalue,11)
			},
			// 根据定位来获取门店
			getshop() {
				let data = {
					phoneLon: this.longitude,
					phoneLat: this.latitude,
					cityId: this.cityid,
					keyword: this.sousuovalue,
					type: this.type
				}
				this.$u.post(`/app/getStoreListByLocation?pageNum=${this.pagesum}&pageSize=${this.pagesize}`, data).then(res => {
					if (res.code == 200) {
						this.xiaflag = true
						this.total = res.total
						if (this.pagesum > 1) {
							this.pagesum++
							this.listshop = this.listshop.concat(res.rows)
							this.qingqiuflag = true
						} else {
							this.pagesum++
							this.listshop = res.rows
							this.qingqiuflag = true
						}
						console.log(this.listshop)
					}
				})
			},
			// 获取用户详情
			getinfo() {
				this.$u.get(`/getAppInfo`).then(res => {
					if (res.code == 200) {
						this.userType = res.user.userType
						uni.setStorageSync('user', res.data)
					} else if (res.code == 401) {
						this.jingmo()
					}
				})
			},
			// 静默登录
			jingmo() {
				let that = this
				wx.login({
					success(res) {
						if (res.code) {
							console.log('登录！', res)
							let data = {
								jsCode: res.code,
							}
							that.$u.post(`/loginByopenid?jsCode=${res.code}`, data).then(res => {
								if (res.code == 200) {
									console.log("老用户登录", res)
									that.getinfo()
									wx.setStorageSync('token', res.token)
								}else{
									uni.showModal({
										    title: '温馨提示',  
										    content: '您还未登录，是否前去登录？',  
										    success: function (res) {  
										        if (res.confirm) {
													uni.reLaunch({
														url:'/pages/login/index'
													})
										        } else if (res.cancel) {
										         
										    }  
										}  
									})
								}
							})
						}
					},
				})
			},
			// 获取自身当前经纬度
			getweizhi() {
				uni.getLocation({
					type: 'wgs84',
					success: res => {
						this.longitude = res.longitude
						this.latitude = res.latitude
						this.getshop()
						this.$u.get(`app/getCity?lon=${this.longitude}&lat=${this.latitude}`).then(res => {
							if (res.code == 200) {
								if (this.cityid == '') {
									this.tit = res.data.cityName
								}
							}
						})
					},
					fail: err => {
						// 获取失败，err为错误信息
						console.log('getLocation err:', err)
					}
				})
			},
			// 按下滑动
			onTouchStart(e) {
				this.imgflag = false
				this.touchStartX = e.touches[0].clientX
				this.touchStartY = e.touches[0].clientY
			},
			// 松开滑动
			onTouchEnd(e) {
				this.imgflag = true
				let deltaX = e.changedTouches[0].clientX - this.touchStartX
				let deltaY = e.changedTouches[0].clientY - this.touchStartY
				if (Math.abs(deltaX) > 50 && Math.abs(deltaX) > Math.abs(deltaY)) {
					if (deltaX >= 0) {
						// console.log("左滑")  
					} else {
						// console.log("右滑")  
					}
				} else if (Math.abs(deltaY) > 50 && Math.abs(deltaX) < Math.abs(deltaY)) {
					if (deltaY < 100) {
						if (this.qingqiuflag == true) {
							let sum = (this.pagesum - 1) * this.pagesize
							console.log(sum, this.total)
							if (this.total > sum) {
								this.qingqiuflag = false
								this.getshop()
							}
						}
					} else {
						console.log("下滑")
					}
				}
			},
			// 滑动取消
			onTouchCancel() {
				this.imgflag = true
			},
			// 点击跳转门店详情页
			btndetail(storeId, item) {
				uni.reLaunch({
					url: '/pages/index/index?storeId=' + storeId + '&tit=' + item.name + '&distance=' + item.distance
				})
			},
			// 点击跳转到加盟合作页
			btnjiameng() {
				uni.navigateTo({
					url: '/page_user/zhaoshang'
				})
			},
			// 点击选择定位位置
			btnweizhi() {
				uni.navigateTo({
					url: '/page_user/city'
				})
			}
		}
	}
</script>

<style lang="scss">
	// 离线蓝牙开关
	.lxlanya{
		position: fixed;
		top: 40%;
		left: 50%;
		transform: translateX(-50%);
		width: 500rpx;
		max-height:600rpx;
		background-color: #fff;
		border-radius: 30rpx;
		z-index: 9;
		padding: 30rpx 40rpx;
		box-sizing: border-box;
		.cha{
			font-size: 50rpx;
			position: absolute;
			top: 20rpx;
			right: 30rpx;
			font-weight: 600;
		}
		.lxzt{
			margin-top: 30rpx;
			padding: 0 20rpx;
			box-sizing: border-box;
			view{
				display: flex;
				justify-content: space-between;
				align-items: center;
				image{
					width: 80rpx;
					height: 80rpx;
				}
			}
		}
		.lxtit{
			font-size: 32rpx;
			font-weight: 600;
			width: 100%;
			text-align: center;
			margin-top: 30rpx;
		}
		.kongzhi{
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
			.lxkai{
				width: 200rpx;
				height: 80rpx;
				background-color: #48893B;
				color: #fff;
				text-align: center;
				line-height: 80rpx;
				border-radius: 20rpx;
			}
		}
	}
	page {
		background: #fff;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}
	#active {
		border: 1px solid #226914 !important;
		background-color: #226914 !important;
		color: #fff !important;
	}
	.activetab {
		font-weight: 600 !important;
		font-size: 32rpx !important;
		color: #3D3D3D !important;
	}
	.cesuoul {
		width: 260rpx;
		max-height: 500rpx;
		background-color: #fff;
		border-radius: 30rpx;
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translateX(-50%);
		z-index: 99;
		padding: 30rpx;
		box-sizing: border-box;
		.anniuqx {
			width: 100%;
			height: 80rpx;
			border: 1px solid #226914;
			text-align: center;
			line-height: 80rpx;
			color: #226914;
			margin-top: 30rpx;
			border-radius: 20rpx;
			font-size: 32rpx;
			font-weight: 600;
		}
		.cesuoli {
			width: 100%;
			.caocuo {
				.name {
					width: 100%;
					text-align: center;
					margin-top: 20rpx;
					font-size: 32rpx;
				}
				.kaiguan {
					padding: 10rpx;
					background-color: #226914;
					color: #fff;
					border-radius: 10rpx;
					margin: auto;
					margin-top: 20rpx;
					width: 100rpx;
					text-align: center;
				}
			}
		}
	}
	.tabqh {
		display: flex;
		width: 100%;
		height: 84rpx;
		background-color: #DEF1DA;
		position: relative;
		border-radius: 30rpx 30rpx 0 0;
		view {
			font-weight: 600;
			font-size: 28rpx;
			color: #7C7C7C;
			text-align: center;
			width: 33%;
			height: 84rpx;
			line-height: 84rpx;
			position: relative;
			text {
				position: absolute;
				z-index: 1;
				left: 50%;
				transform: translateX(-50%);
			}
			image {
				height: 84rpx;
				position: absolute;
				top: 0;
				left: 0;
			}
		}
	}
	.jinxinx {
		width: 60rpx;
		height: 60rpx;
		position: fixed;
		top: 1216rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 2;
	}
	.karaokeroom {
		position: fixed;
		top: 332rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 2;
		width: 674rpx;
		height: 828rpx;
		background: linear-gradient(180deg, #DEF1DA 0%, #FFFFFF 100%, #FFFFFF 100%);
		border-radius: 36rpx 36rpx 36rpx 36rpx;
		.top {
			font-weight: 600;
			font-size: 44rpx;
			color: #48893B;
			width: 100%;
			text-align: center;
			margin-top: 52rpx;
			margin-bottom: 44rpx;
		}
		.anniu {
			width: 584rpx;
			height: 86rpx;
			background: #48893B;
			border-radius: 43rpx 43rpx 43rpx 43rpx;
			font-size: 36rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 86rpx;
			margin: auto;
			position: fixed;
			left: 50%;
			transform: translateX(-50%);
			bottom: 72rpx;
		}
		.roomlist {
			height: 480rpx;
			overflow: scroll;
			.room_item {
				width: 584rpx;
				height: 224rpx;
				border: 2rpx solid #969696;
				background-color: #fff;
				border-radius: 20rpx;
				margin: auto;
				display: flex;
				align-items: center;
				padding: 34rpx;
				box-sizing: border-box;
				margin-top: 26rpx;
				.lt {
					image {
						width: 154rpx;
						height: 154rpx;
						margin-right: 22rpx;
						border-radius: 20rpx;
					}
				}
			}
		}
	}
	.mask {
		width: 100%;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
		background-color: #000;
		opacity: .2;
	}
	.page {
		width: 750rpx;
		.xuanfu {
			position: fixed;
			top: 38%;
			right: 30rpx;
			animation: fadeInDowns .5s ease-out forwards;
			image {
				width: 90rpx;
				height: 90rpx;
				display: block;
				margin-bottom: 44rpx;
			}
		}
		@keyframes fadeInDowns {
			from {
				opacity: 0;
				transform: translateX(50px);
			}
			to {
				opacity: 1;
				transform: translateX(0);
			}
		}
		@keyframes fadeInDown {
			from {
				opacity: 0;
				transform: translateX(-50px);
			}
			to {
				opacity: 1;
				transform: translateX(0);
			}
		}
		.xuanfus {
			position: fixed;
			top: 34%;
			right: -80rpx;
			animation: fadeInDown .5s ease-out forwards;
			image {
				width: 90rpx;
				height: 90rpx;
				display: block;
				margin-top: 44rpx;
			}
		}
		.jiameng {
			width: 100%;
			text-align: center;
			background-color: #fff;
			margin: auto;
			position: fixed;
			bottom: 0;
			left: 0;
			.gb {
				width: 100rpx;
				height: 40rpx;
				background-color: rgba(0, 0, 0, 0.15);
				position: absolute;
				top: 0;
				right: 22rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 0 30rpx 0 0;
				text {
					font-size: 34rpx;
				}
			}
			image {
				width: 710rpx;
				height: 196rpx;
				border-radius: 30rpx;
			}
		}
		.shoplist {
			width: 750rpx;
			max-height: 1250rpx;
			background: #FFFFFF;
			box-sizing: border-box;
			overflow: scroll;
			padding-bottom: 240rpx !important;
			box-sizing: border-box;
			.shop_item {
				margin: auto;
				display: flex;
				margin-top: 30rpx;
				box-sizing: border-box;
				width: 674rpx;
				height: 330rpx;
				background: #FFFFFF;
				padding: 20rpx 18rpx;
				box-shadow: 0rpx 2rpx 12rpx 0rpx rgba(0, 0, 0, 0.15);
				border-radius: 18rpx 18rpx 18rpx 18rpx;
				.rt {
					.storebq {
						margin-top: 16rpx;
						display: flex;
						image {
							width: 100rpx;
							height: 40rpx;
							margin-right: 10rpx;
						}
					}
					.yuding {
						display: flex;
						align-items: center;
						font-size: 24rpx;
						color: #48893B;
						margin-top: 26rpx;
						margin-left: 248rpx;
						.one {
							width: 32rpx;
							height: 32rpx;
							margin-right: 10rpx;
						}
						.two {
							width: 14rpx;
							height: 36rpx;
							margin-left: 10rpx;
						}
					}
					.juli {
						display: flex;
						justify-content: space-between;
						margin-top: 16rpx;
						align-items: center;
						.kmjuli {
							width: 210rpx;
							height: 40rpx;
							border-radius: 4rpx 4rpx 4rpx 4rpx;
							border: 2rpx solid #48893B;
							text-align: center;
							line-height: 36rpx;
							font-size: 26rpx;
							color: #48893B;
						}
						.juliimg {
							image {
								width: 60rpx;
								height: 60rpx;
								margin-left: 26rpx;
							}
						}
					}
					.dizhi {
						font-size: 26rpx;
						color: #7C7C7C;
						margin-top: 16rpx;
					}
					.name {
						font-weight: 600;
						font-size: 36rpx;
						color: #3D3D3D;
					}
				}
				.lt {
					margin-right: 30rpx;
					image {
						width: 186rpx;
						height: 220rpx;
						border-radius: 10rpx;
					}
					.hour {
						font-size: 32rpx;
						color: #48893B;
						margin-top: 32rpx;
					}
				}
			}
		}
		.serchshop {
			display: flex;
			margin-top: 40rpx;
			align-items: center;
			justify-content: space-between;
			padding-left: 34rpx;
			padding-right: 34rpx;
			box-sizing: border-box;
			.search {
				width: 468rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: #FFFFFF;
				border-radius: 6rpx 6rpx 6rpx 6rpx;
				position: relative;
				input {
					margin-left: 94rpx;
					height: 72rpx;
					line-height: 72rpx;
				}
				/deep/ .uicon-search {
					position: absolute;
					top: 24rpx !important;
					left: 26rpx !important;
				}
			}
			.dingwei {
				display: flex;
				font-weight: 600;
				font-size: 32rpx;
				color: #48893B;
				align-items: center;
				.dwimg {
					width: 40rpx;
					height: 40rpx;
					margin-right: 12rpx;
				}
				.xlimg {
					width: 26rpx;
					height: 14rpx;
					margin-left: 12rpx;
				}
			}
		}
		.title {
			font-weight: 600;
			font-size: 36rpx;
			color: #226914;
			line-height: 50rpx;
			height: 50rpx;
			padding-left: 34rpx;
			box-sizing: border-box;
		}
		.imgbj {
			width: 750rpx;
			height: 610rpx;
			position: fixed;
			top: 0;
			left: 0;
			z-index: -1;
		}
	}
</style>