Ver código fonte

蓝牙连接

lilei 4 anos atrás
pai
commit
2bde17d5d6

+ 29 - 15
pages.json

@@ -10,13 +10,21 @@
 			}
 		},
 		{
-			"path": "pages/index/index", // 首页
+		    "path" : "pages/index/index", // 首页
+		    "style" :                                                                                    
+		    {
+		        "navigationBarTitleText": "垃圾投放",
+		        "enablePullDownRefresh": false
+		    }
+		    
+		},
+		{
+			"path": "pages/hexiao/index",
 			"style": {
-				"navigationBarTitleText": "乐色助手",
+				"navigationBarTitleText": "交易记录",
 				"enablePullDownRefresh": false
 			}
 		},
-		
 		{
 			"path": "pages/userCenter/index",
 			"style": {
@@ -85,17 +93,23 @@
 		"backgroundColor": "#ffffff",
 		"fontSize": "14px",
 		"list": [
-		{
-			"pagePath": "pages/index/index",
-			"iconPath": "static/tabbar/record.png",
-			"selectedIconPath": "static/tabbar/record-active.png",
-			"text": "交易记录"
-		},
-		{
-			"pagePath": "pages/userCenter/index",
-			"iconPath": "static/tabbar/user.png",
-			"selectedIconPath": "static/tabbar/user-active.png",
-			"text": "我的"
-		}]
+			{
+				"pagePath": "pages/index/index",
+				"iconPath": "static/tabbar/dzicheng.png",
+				"selectedIconPath": "static/tabbar/dzicheng-active.png",
+				"text": "垃圾投放"
+			},
+			{
+				"pagePath": "pages/hexiao/index",
+				"iconPath": "static/tabbar/record.png",
+				"selectedIconPath": "static/tabbar/record-active.png",
+				"text": "交易记录"
+			},
+			{
+				"pagePath": "pages/userCenter/index",
+				"iconPath": "static/tabbar/user.png",
+				"selectedIconPath": "static/tabbar/user-active.png",
+				"text": "我的"
+			}]
 	}
 }

+ 317 - 0
pages/bluetooth/bluetooth.vue

@@ -0,0 +1,317 @@
+<template>
+	<view style="width: 100%;">
+		<button @click="getBluetoothDevices">获取设备</button>
+		<view class="device-item" v-if="item.name=='FAYA'" @click="connectBlue(item)" v-for="item in deviceList" :key="item.name">
+			<text>{{item.name}}</text>
+			<text>点击连接</text>
+		</view>
+		<view>
+			状态:{{statusStr}}
+		</view>
+		<view>
+			结果:{{balanceData}}
+		</view>
+	</view>
+</template>
+
+<script>
+	export default {
+		data() {
+			return {
+				deviceList:[], // 设备列表
+				deviceId: '',
+				connectedDeviceId: '',
+				services: [],
+				notifyCharacteristicsId:'',
+				notifyServicweId: '',
+				writeDatas: '',
+				balanceData: '',
+				hexstr: '',
+				statusStr: ''
+			};
+		},
+		onShow() {
+			let _this = this
+			// this.closeConnect()
+			const res = uni.getSystemInfoSync();
+			// 初始化蓝牙模块
+			uni.openBluetoothAdapter({
+			  success(res) {
+			    console.log(res)
+				_this.statusStr = '初始化蓝牙模块成功'
+				/* 获取本机的蓝牙状态 */
+				setTimeout(() => {
+					_this.init()
+				}, 1000)
+			  }
+			})
+			// 监听蓝牙适配器状态变化事件
+			uni.onBluetoothAdapterStateChange(function (res) {
+			  console.log(res,'适配器变化')
+			})
+			
+			if(!res.bluetoothEnabled){
+				uni.showModal({
+					title:'提示',
+					content:'请开启手机蓝牙',
+					complete(e) {
+						console.log(e)
+						_this.statusStr = '请开启手机蓝牙'
+					}
+				})
+			} 
+		},
+		methods: {
+			// 初始化蓝牙
+			init(){
+				let _this = this
+				uni.getBluetoothAdapterState({
+				  success(res) {
+				    console.log(res)
+					_this.statusStr = 'adapterState changed, now is'
+					// 开始搜寻附近的蓝牙外围设备
+					_this.startBluetoothDevicesDiscovery()
+				  },
+				  fail(res) {
+					  console.log(res)
+				  }
+				})
+			},
+			startBluetoothDevicesDiscovery(){
+				let _this = this
+				uni.startBluetoothDevicesDiscovery({
+				  services: [],
+				  success(res) {
+				    console.log(res)
+					// 监听寻找到新设备的事件
+					uni.onBluetoothDeviceFound(function (res) {
+					  _this.statusStr = '发现新设备'
+					  console.dir(res.devices)
+					  if(res.devices[0].name == 'FAYA'){
+						  _this.deviceId = res.devices[0].deviceId
+						  _this.deviceList = res.devices
+						  // _this.createBLEConnection()
+					  }
+					})
+				  }
+				})
+			},
+			// 获取在蓝牙模块生效期间所有已发现的蓝牙设备
+			getBluetoothDevices() {
+				let _this = this
+				uni.getBluetoothDevices({
+				  success(res) {
+				    console.log(res)
+				    if (res.devices.length) {
+					  _this.deviceList = res.devices
+				    }
+				  }
+				})
+			},
+			connectBlue(item){
+				this.deviceId = item.deviceId
+				this.createBLEConnection()
+			},
+			// 连接指定设备
+			createBLEConnection(){
+				let _this = this
+				uni.createBLEConnection({
+				  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
+				  deviceId:this.deviceId,
+				  success(res) {
+				    console.log(res)
+					_this.statusStr = '连接设备成功'
+					_this.connectedDeviceId = _this.deviceId
+					// 获取连接设备的service服务
+					setTimeout(()=>{
+						_this.getBLEDeviceServices()						  
+					},1000)
+				  }
+				})
+			},
+			// 获取连接设备的service服务
+			getBLEDeviceServices(){
+				let _this = this
+				uni.getBLEDeviceServices({
+				  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
+				  deviceId: _this.deviceId,
+				  success(res) {
+				    console.log('device services:', res.services)
+					_this.statusStr = '获取连接设备的service服务成功'
+					_this.services = res.services
+					/* 获取连接设备的所有特征值 */
+					setTimeout(()=>{
+						_this.getBLEDeviceCharacteristics()
+					},500)
+					// 停止搜索
+					uni.stopBluetoothDevicesDiscovery({
+						success: function(res) {
+							console.log(res, '停止搜索')
+						},
+						fail(res) {
+						}
+					})
+				  }
+				})
+			},
+			// 获取连接设备的所有特征值
+			getBLEDeviceCharacteristics(){
+				let _this = this
+				this.notifyServicweId = this.services[1].uuid
+				uni.getBLEDeviceCharacteristics({
+					deviceId: this.connectedDeviceId,
+					serviceId: this.notifyServicweId,
+					success: function(res) {
+						console.log(res)
+						_this.statusStr = '获取连接设备的所有特征值成功'
+						for (let i = 0; i < res.characteristics.length; i++) {
+							if ((res.characteristics[i].properties.notify || res.characteristics[i].properties.indicate) &&
+								(res.characteristics[i].properties.read && res.characteristics[i].properties.write)) {
+								console.log(res.characteristics[i].uuid, '蓝牙特征值 ==========')
+								/* 获取蓝牙特征值 */
+								_this.notifyCharacteristicsId = res.characteristics[i].uuid
+								// 启用低功耗蓝牙设备特征值变化时的 notify 功能
+								setTimeout(()=>{
+									_this.notifyBLECharacteristicValueChange()
+								},500)
+							}
+						}
+					},
+					fail: function(res) {
+						console.log(res)
+					}
+				})
+			},
+			// 启用低功耗蓝牙设备特征值变化时的 notify 功能
+			 notifyBLECharacteristicValueChange() { 
+				let that = this;
+				console.log(that.connectedDeviceId,that.notifyServicweId,that.notifyCharacteristicsId)
+				uni.notifyBLECharacteristicValueChange({
+					state: true,
+					deviceId: that.connectedDeviceId,
+					serviceId: that.notifyServicweId,
+					// characteristicId: that.notifyCharacteristicsId,
+					characteristicId: '49535343-1E4D-4BD9-BA61-23C647249616',
+					success(res) {
+						console.log(res,'notifyBLECharacteristicValueChange')
+						that.statusStr = '启用低功耗蓝牙设备特征值变化成功'
+						/*用来监听手机蓝牙设备的数据变化*/
+						uni.onBLECharacteristicValueChange(function(res) {
+							console.log(res,'read data')
+							that.balanceData = that.buf2string(res.value)
+							// that.hexstr = that.receiveData(res.value)
+						})
+					},
+					fail(res) {
+						console.log(res, '启用低功耗蓝牙设备监听失败')
+					}
+				})
+			},
+			/*转换成需要的格式*/
+		    buf2string(buffer) {
+				let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
+				return arr.map((char, i) => {
+					return String.fromCharCode(char);
+				}).join('');
+			},
+			receiveData(buf) {
+					return this.hexCharCodeToStr(this.ab2hex(buf))
+			},
+			/*转成二进制*/
+			ab2hex (buffer) {
+			  let hexArr = Array.prototype.map.call(
+				  new Uint8Array(buffer), function (bit) {
+					  return ('00' + bit.toString(16)).slice(-2)
+				  }
+			  )
+			  return hexArr.join('')
+			},
+			/*转成可展会的文字*/
+			hexCharCodeToStr(hexCharCodeStr) {
+			  let trimedStr = hexCharCodeStr.trim();
+			  let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
+			  let len = rawStr.length;
+			  let curCharCode;
+			  let resultStr = [];
+			  for (let i = 0; i < len; i = i + 2) {
+				  curCharCode = parseInt(rawStr.substr(i, 2), 16);
+				  resultStr.push(String.fromCharCode(curCharCode));
+			  }
+			  return resultStr.join('');
+			},
+			// 发送数据
+			sendData(str) {
+			    let that = this;
+			    let dataBuffer = new ArrayBuffer(3)
+			    let dataView = new DataView(dataBuffer)
+			    // for (let i = 0; i < str.length; i++) {
+			    //     dataView.setUint8(i, str.charAt(i).charCodeAt())
+			    // }
+				dataView.setUint8(0, 0x02)
+				dataView.setUint8(1, 0x42)
+				dataView.setUint8(2, 0x03)
+			    let dataHex = that.ab2hex(dataBuffer);
+			    this.writeDatas = that.hexCharCodeToStr(dataHex);
+			    uni.writeBLECharacteristicValue({
+			        deviceId: that.connectedDeviceId,
+			        serviceId: that.notifyServicweId,
+			        characteristicId: that.notifyCharacteristicsId,
+					// characteristicId: '49535343-1E4D-4BD9-BA61-23C647249616',
+			        value: dataBuffer,
+			        success: function (res) {
+			            console.log('发送的数据:' + that.writeDatas)
+			            console.log('message发送成功')
+			        },
+			        fail: function (res) {
+						console.log('发送失败:' + res)
+			        },
+			        complete: function (res) {
+			        }
+			    })
+			},
+			 // 断开设备连接
+			closeConnect() {
+				let that = this;
+			    if (that.connectedDeviceId) {
+			        uni.closeBLEConnection({
+			            deviceId: that.connectedDeviceId,
+			            success: function(res) {
+			                that.closeBluetoothAdapter()
+			            },
+			            fail(res) {
+			            }
+			        })
+			    } else {
+			        that.closeBluetoothAdapter()
+			    }
+			},
+			// 关闭蓝牙模块
+			closeBluetoothAdapter() {
+				let that = this;
+			    uni.closeBluetoothAdapter({
+			        success: function(res) {
+						that.statusStr = '关闭蓝牙模块'
+						that.balanceData = ''
+						console.log(res,'closeBluetoothAdapter')
+			        },
+			        fail: function(err) {
+			        }
+			    })
+			},
+		},
+		onUnload() {
+			this.closeConnect()
+		}
+	}
+</script>
+
+<style lang="scss">
+	.device-item{
+		width: 100%;
+		padding: 20upx;
+		border-bottom: 2upx solid #eee;
+		display: flex;
+		align-items: center;
+		justify-content: space-between;
+	}
+</style>

+ 311 - 0
pages/hexiao/index.vue

@@ -0,0 +1,311 @@
+<template>
+	<view class="homePage-container">
+		<!-- 筛选弹框 -->
+		<search-modal v-if="openScreen" :visible="openScreen" :defaultParams="searchForm" @refresh="refresh" @close="openScreen=false"></search-modal>
+		<view class="homePage-head">
+			<view class="head-con">
+				<text class="homePage-tit">交易记录</text>
+				<view class="total-con">
+					总计:
+					<text class="goleNum">{{ldTotal}}</text>
+					<image src="/static/ledou.png" class="ld-icon"></image>
+				</view>
+			</view>
+			<image class="filter-icon" src="@/static/filter.png" @tap="openScreen=true"></image>
+		</view>
+		<scroll-view class="scroll-con" scroll-y :scroll-top="scrollTop" @scrolltolower="onreachBottom">
+			<!-- 列表数据 -->
+			<view class="cell-item-con">
+				<view class="cell-item" v-for="(item, index) in listdata" :key="index">
+					<view class="cell-item-c">
+						<view class="cell-item-label">交易时间</view>
+						<view class="cell-item-main">{{item.createDate}}</view>
+					</view>
+					<view class="cell-item-c">
+						<view class="cell-item-label">支付账户</view>
+						<view class="cell-item-main">{{item.customerMobile}}</view>
+					</view>
+					<view class="cell-item-c">
+						<view class="cell-item-label">交易数量(个)</view>
+						<view :class="['cell-item-main', item.changeType=='ADD' ? 'red' : 'blue']">{{item.changeType=='ADD' ? '+' : item.changeType=='SUB' ? '-' : ''}}{{ item.changeNum }}</view>
+					</view>
+					<view class="cell-item-c">
+						<view class="cell-item-label">备注</view>
+						<view class="cell-item-main">{{item.remarks ? item.remarks : '--'}}</view>
+					</view>
+					<view class="cell-item-c handle-con">
+						<view class="cell-item-label">操作</view>
+						<view class="cell-item-main">
+							<u-button size="mini" v-if="item.changeType=='ADD' && item.cancelFlag==0" class="handle-btn" hover-class="none" :custom-style="customBtnStyle" @click="revokeFun(item)">撤销单据</u-button>
+							<text v-if="item.changeType=='ADD' && item.cancelFlag==1" style="color: #909399;">单据已撤销</text>
+							<text v-if="item.changeType=='SUB'" style="color: #909399;">扫码支付撤销</text>
+						</view>
+					</view>
+				</view>
+			</view>
+			<u-empty class="nodata" :text="noDataText" v-if="listdata.length==0 && status!='loading'" mode="list"></u-empty>
+			<view class="loadmore">
+				<u-loadmore v-if="total>pageSize || status=='loading'" :status="status" />
+			</view>
+		</scroll-view>
+		
+		<view class="footer-bar">
+			<u-button shape="circle" @click="openQuery" size="medium" type="primary">用户乐豆查询/核销</u-button>
+		</view>
+	</view>
+</template>
+<script>
+	import searchModal from './searchModal.vue'
+	import { getUserInfo } from '@/api/user.js'
+	import { getGoldLogList, GoldLogCancel, GoldLogTotal } from '@/api/officialPartnerGoldLog.js'
+	export default {
+		components: { searchModal },
+		data() {
+			return {
+				listdata: [],
+				pageNo: 1,  //  当前页码
+				pageSize: 10,  //  每页条数
+				total: 0,  //  数据总条数
+				noDataText: '暂无数据',  //  列表请求状态提示语
+				status: 'loadmore',  //  加载中状态
+				openScreen: false,  //  是否打开筛选
+				searchForm: {  //  查询条件
+					beginDate: '',  //  创建时间默认筛选近7天
+					endDate: '',  //  创建时间默认筛选近7天
+					customerMobile: ''  //  支付账户
+				},
+				customBtnStyle: {  //  撤销单据  自定义按钮样式
+					borderColor: '#2ab4e5',
+					backgroundColor: '#2ab4e5',
+					color: '#fff'
+				},
+				scrollTop: 0,  //  滚动条位置
+				ldTotal: 0  //  乐豆总计数
+			}
+		},
+		onShow() {
+			this.openScreen = false  //  关闭筛选框
+			this.refresh({})
+			this.getUserInfo()
+		},
+		methods:{
+			//  获取用户信息
+			getUserInfo(){
+				getUserInfo().then(res => {
+					if(res.status == 200){
+						this.$u.vuex('vuex_userData', res.data);
+					}
+				})
+			},
+			openQuery(){
+				uni.navigateTo({
+					url: "/pages/userCenter/leDouQuery"
+				})
+			},
+			//  总计
+			getGoldLogTotal(){
+				GoldLogTotal({
+					beginDate: this.searchForm.beginDate,
+					endDate: this.searchForm.endDate,
+					customerMobile: this.searchForm.customerMobile
+				}).then(res => {
+					if(res.status == 200){
+						this.ldTotal = res.data ? res.data.changeNum : 0
+					}else{
+						this.ldTotal = 0
+					}
+				})
+			},
+			// 获取查询参数 刷新列表
+			refresh(params){
+				this.searchForm = params
+				this.listdata = []
+				this.total = 0
+				this.searchHandle(1)
+				this.getGoldLogTotal()
+			},
+			//  撤销单据
+			revokeFun(row){
+				const _this = this
+				uni.showModal({
+				  title: '提示',
+				  content: '撤销单据后乐豆将全部退回给原支付账号,确认撤销吗?',
+				  success(res) {
+				    if (res.confirm) {
+				      GoldLogCancel({ id: row.id }).then(ret => {
+						  if(ret.status == 200){
+							  uni.showToast({ title: ret.message, icon: 'none' })
+							  _this.searchHandle(1)
+						  }
+					  })
+				    }
+				  }
+				})
+			},
+			// 搜索查询
+			searchHandle (pageNo) {
+				this.status = "loading"
+				pageNo ? this.pageNo = pageNo : null
+				getGoldLogList({
+					pageNo: this.pageNo,
+					pageSize: this.pageSize,
+					beginDate: this.searchForm.beginDate,
+					endDate: this.searchForm.endDate,
+					customerMobile: this.searchForm.customerMobile
+				}).then(res => {
+					if (res.status == 200) {
+						if(this.pageNo>1){
+							this.listdata = this.listdata.concat(res.data.list || [])
+						}else{
+							this.listdata = res.data.list || []
+							this.scrollTop = 0
+						}
+						this.total = res.data.count || 0
+						this.noDataText = '暂无数据'
+					} else {
+						this.noDataText = res.message
+						this.listdata = []
+						this.total = 0
+					}
+					this.status = "loadmore"
+				})
+			},
+			// scroll-view到底部加载更多
+			onreachBottom() {
+				if(this.listdata.length < this.total){
+					this.pageNo += 1
+					this.searchHandle()
+				}else{
+					uni.showToast({ title: '已经到底了', icon: 'none' })
+					this.status = "nomore"
+				}
+			}
+		}
+	}
+</script>
+
+<style lang="scss">
+	page{
+		height: 100%;
+	}
+	.homePage-container {
+		width: 100%;
+		height: 100%;
+		display: flex;
+		flex-direction: column;
+		padding: 102rpx 0;
+		.footer-bar{
+			width: 100%;
+			display: flex;
+			align-items: center;
+			height: 102rpx;
+			justify-content: center;
+			padding: 0 10%;
+			position: fixed;
+			bottom: 0;
+			left: 0;
+		}
+		// 筛选菜单
+		.homePage-head{
+			font-size: 30rpx;
+			display: flex;
+			justify-content: space-between;
+			align-items: center;
+			position: fixed;
+			top: 0;
+			left: 0;
+			z-index: 9;
+			padding: 26rpx 30rpx;
+			background-color: #fff;
+			box-shadow: 0rpx 0rpx 14rpx rgba(0,0,0,.05);
+			width: 100%;
+			border-top: 10rpx solid #f8f8f8;
+			.head-con{
+				flex-grow: 1;
+				display: flex;
+				align-items: center;
+				justify-content: space-between;
+				.homePage-tit{
+					color: #040301;
+				}
+				.total-con{
+					color: #040301;
+					.goleNum{
+						color: #eb0000;
+						font-size: 30rpx;
+						font-weight: bold;
+					}
+					.ld-icon{
+						width: 30rpx;
+						height: 30rpx;
+						margin: 0 0 0 10rpx;
+						vertical-align: middle;
+					}
+				}
+			}
+			.filter-icon{
+				flex-shrink: 0;
+				width: 36rpx;
+				height: 36rpx;
+				padding-left: 20rpx;
+			}
+		}
+		//  列表
+		.scroll-con{
+			flex: 1;
+			// height: calc(100% - 40);
+			overflow: auto;
+		}
+		//  列表样式
+		.cell-item-con{
+			margin: 20rpx;
+			.cell-item{
+				background-color: #fff;
+				color: #606266;
+				padding: 0 30rpx;
+				margin-bottom: 20rpx;
+				border-radius: 16rpx;
+				font-size: 28rpx;
+				box-shadow: 3rpx 3rpx 5rpx #eee;
+				.cell-item-c{
+					display: flex;
+					justify-content: space-between;
+					padding: 18rpx 0;
+					border-bottom: 1rpx solid #f2f2f2;
+					.cell-item-label{
+						flex-shrink: 0;
+						width: 200rpx;
+						color: #303133;
+					}
+					.cell-item-main{
+						flex-grow: 1;
+						text-align: right;
+						.handle-btn{
+							border-color: #fcbd71!important;
+							background-color: #fcbd71!important;
+						}
+					}
+					.blue {
+						color: #1890FF;
+					}
+					.green {
+						color: #16b50e;
+					}
+					.red {
+						color: red;
+					}
+					&:last-child{
+						border: none;
+					}
+				}
+				.handle-con{
+					align-items: center;
+					// margin-top: 10rpx;
+					// padding-top: 18rpx;
+					// padding: 0;
+				}
+			}
+		}
+		
+	}
+</style>

+ 0 - 0
pages/index/searchModal.vue → pages/hexiao/searchModal.vue


+ 282 - 277
pages/index/index.vue

@@ -1,311 +1,316 @@
 <template>
-	<view class="homePage-container">
-		<!-- 筛选弹框 -->
-		<search-modal v-if="openScreen" :visible="openScreen" :defaultParams="searchForm" @refresh="refresh" @close="openScreen=false"></search-modal>
-		<view class="homePage-head">
-			<view class="head-con">
-				<text class="homePage-tit">交易记录</text>
-				<view class="total-con">
-					总计:
-					<text class="goleNum">{{ldTotal}}</text>
-					<image src="/static/ledou.png" class="ld-icon"></image>
-				</view>
-			</view>
-			<image class="filter-icon" src="@/static/filter.png" @tap="openScreen=true"></image>
-		</view>
-		<scroll-view class="scroll-con" scroll-y :scroll-top="scrollTop" @scrolltolower="onreachBottom">
-			<!-- 列表数据 -->
-			<view class="cell-item-con">
-				<view class="cell-item" v-for="(item, index) in listdata" :key="index">
-					<view class="cell-item-c">
-						<view class="cell-item-label">交易时间</view>
-						<view class="cell-item-main">{{item.createDate}}</view>
-					</view>
-					<view class="cell-item-c">
-						<view class="cell-item-label">支付账户</view>
-						<view class="cell-item-main">{{item.customerMobile}}</view>
-					</view>
-					<view class="cell-item-c">
-						<view class="cell-item-label">交易数量(个)</view>
-						<view :class="['cell-item-main', item.changeType=='ADD' ? 'red' : 'blue']">{{item.changeType=='ADD' ? '+' : item.changeType=='SUB' ? '-' : ''}}{{ item.changeNum }}</view>
-					</view>
-					<view class="cell-item-c">
-						<view class="cell-item-label">备注</view>
-						<view class="cell-item-main">{{item.remarks ? item.remarks : '--'}}</view>
-					</view>
-					<view class="cell-item-c handle-con">
-						<view class="cell-item-label">操作</view>
-						<view class="cell-item-main">
-							<u-button size="mini" v-if="item.changeType=='ADD' && item.cancelFlag==0" class="handle-btn" hover-class="none" :custom-style="customBtnStyle" @click="revokeFun(item)">撤销单据</u-button>
-							<text v-if="item.changeType=='ADD' && item.cancelFlag==1" style="color: #909399;">单据已撤销</text>
-							<text v-if="item.changeType=='SUB'" style="color: #909399;">扫码支付撤销</text>
-						</view>
-					</view>
-				</view>
-			</view>
-			<u-empty class="nodata" :text="noDataText" v-if="listdata.length==0 && status!='loading'" mode="list"></u-empty>
-			<view class="loadmore">
-				<u-loadmore v-if="total>pageSize || status=='loading'" :status="status" />
-			</view>
-		</scroll-view>
-		
-		<view class="footer-bar">
-			<u-button shape="circle" @click="openQuery" size="medium" type="primary">用户乐豆查询/核销</u-button>
+	<view class="content">
+		<view class="cz-box" @click="toFindDev">
+			<u-image width="150rpx" height="150rpx" src="/static/chengzhong.png"></u-image>
+			<view class="tits">{{statusStr}}</view>
 		</view>
 	</view>
 </template>
+
 <script>
-	import searchModal from './searchModal.vue'
-	import { getUserInfo } from '@/api/user.js'
-	import { getGoldLogList, GoldLogCancel, GoldLogTotal } from '@/api/officialPartnerGoldLog.js'
 	export default {
-		components: { searchModal },
 		data() {
 			return {
-				listdata: [],
-				pageNo: 1,  //  当前页码
-				pageSize: 10,  //  每页条数
-				total: 0,  //  数据总条数
-				noDataText: '暂无数据',  //  列表请求状态提示语
-				status: 'loadmore',  //  加载中状态
-				openScreen: false,  //  是否打开筛选
-				searchForm: {  //  查询条件
-					beginDate: '',  //  创建时间默认筛选近7天
-					endDate: '',  //  创建时间默认筛选近7天
-					customerMobile: ''  //  支付账户
-				},
-				customBtnStyle: {  //  撤销单据  自定义按钮样式
-					borderColor: '#2ab4e5',
-					backgroundColor: '#2ab4e5',
-					color: '#fff'
-				},
-				scrollTop: 0,  //  滚动条位置
-				ldTotal: 0  //  乐豆总计数
+				deviceList:[], // 设备列表
+				deviceId: '',
+				connectedDeviceId: '',
+				services: [],
+				notifyCharacteristicsId:'',
+				notifyServicweId: '',
+				writeDatas: '',
+				balanceData: '',
+				hexstr: '',
+				statusStr: '投放称重'
 			}
 		},
-		onShow() {
-			this.openScreen = false  //  关闭筛选框
-			this.refresh({})
-			this.getUserInfo()
-		},
-		methods:{
-			//  获取用户信息
-			getUserInfo(){
-				getUserInfo().then(res => {
-					if(res.status == 200){
-						this.$u.vuex('vuex_userData', res.data);
-					}
+		methods: {
+			toFindDev(){
+				let _this = this
+				const res = uni.getSystemInfoSync();
+				// 初始化蓝牙模块
+				uni.openBluetoothAdapter({
+				  success(res) {
+				    console.log(res)
+					_this.statusStr = '初始化蓝牙模块成功'
+					/* 获取本机的蓝牙状态 */
+					setTimeout(() => {
+						_this.init()
+					}, 1000)
+				  }
+				})
+				// 监听蓝牙适配器状态变化事件
+				uni.onBluetoothAdapterStateChange(function (res) {
+				  console.log(res,'适配器变化')
 				})
+				
+				if(!res.bluetoothEnabled){
+					uni.showModal({
+						title:'提示',
+						content:'请开启手机蓝牙',
+						complete(e) {
+							console.log(e)
+							_this.statusStr = '请开启手机蓝牙'
+						}
+					})
+				}
 			},
-			openQuery(){
-				uni.navigateTo({
-					url: "/pages/userCenter/leDouQuery"
+			// 初始化蓝牙
+			init(){
+				let _this = this
+				uni.getBluetoothAdapterState({
+				  success(res) {
+				    console.log(res)
+					_this.statusStr = 'adapterState changed, now is'
+					// 开始搜寻附近的蓝牙外围设备
+					_this.startBluetoothDevicesDiscovery()
+				  },
+				  fail(res) {
+					  console.log(res)
+				  }
 				})
 			},
-			//  总计
-			getGoldLogTotal(){
-				GoldLogTotal({
-					beginDate: this.searchForm.beginDate,
-					endDate: this.searchForm.endDate,
-					customerMobile: this.searchForm.customerMobile
-				}).then(res => {
-					if(res.status == 200){
-						this.ldTotal = res.data ? res.data.changeNum : 0
-					}else{
-						this.ldTotal = 0
-					}
+			startBluetoothDevicesDiscovery(){
+				let _this = this
+				uni.startBluetoothDevicesDiscovery({
+				  services: [],
+				  success(res) {
+				    console.log(res)
+					// 监听寻找到新设备的事件
+					uni.onBluetoothDeviceFound(function (res) {
+					  _this.deviceList = res.devices
+					  let devices = _this.deviceList.filter(item => item.name == 'FAYA')
+					  console.dir(devices,'devices list')
+					  if(devices.length == 1){
+						  _this.statusStr = '发现设备'
+						  _this.deviceId = devices[0].deviceId
+						  _this.createBLEConnection()
+					  }
+					  else if(devices.length > 1){
+						  
+					  }
+					  else{
+						  _this.statusStr = '没有找到电子秤'
+					  }
+					  
+					})
+				  }
 				})
 			},
-			// 获取查询参数 刷新列表
-			refresh(params){
-				this.searchForm = params
-				this.listdata = []
-				this.total = 0
-				this.searchHandle(1)
-				this.getGoldLogTotal()
+			// 开始连接指定设备
+			createBLEConnection(){
+				let _this = this
+				uni.createBLEConnection({
+				  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
+				  deviceId:this.deviceId,
+				  success(res) {
+				    console.log(res)
+					_this.statusStr = '连接设备成功'
+					_this.connectedDeviceId = _this.deviceId
+					// 获取连接设备的service服务
+					setTimeout(()=>{
+						_this.getBLEDeviceServices()						  
+					},1000)
+				  }
+				})
 			},
-			//  撤销单据
-			revokeFun(row){
-				const _this = this
-				uni.showModal({
-				  title: '提示',
-				  content: '撤销单据后乐豆将全部退回给原支付账号,确认撤销吗?',
+			// 获取连接设备的service服务
+			getBLEDeviceServices(){
+				let _this = this
+				uni.getBLEDeviceServices({
+				  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
+				  deviceId: _this.deviceId,
 				  success(res) {
-				    if (res.confirm) {
-				      GoldLogCancel({ id: row.id }).then(ret => {
-						  if(ret.status == 200){
-							  uni.showToast({ title: ret.message, icon: 'none' })
-							  _this.searchHandle(1)
-						  }
-					  })
-				    }
+				    console.log('device services:', res.services)
+					_this.statusStr = '获取连接设备的service服务成功'
+					_this.services = res.services
+					/* 获取连接设备的所有特征值 */
+					setTimeout(()=>{
+						_this.getBLEDeviceCharacteristics()
+					},500)
+					// 停止搜索
+					uni.stopBluetoothDevicesDiscovery({
+						success: function(res) {
+							console.log(res, '停止搜索')
+						},
+						fail(res) {
+						}
+					})
 				  }
 				})
 			},
-			// 搜索查询
-			searchHandle (pageNo) {
-				this.status = "loading"
-				pageNo ? this.pageNo = pageNo : null
-				getGoldLogList({
-					pageNo: this.pageNo,
-					pageSize: this.pageSize,
-					beginDate: this.searchForm.beginDate,
-					endDate: this.searchForm.endDate,
-					customerMobile: this.searchForm.customerMobile
-				}).then(res => {
-					if (res.status == 200) {
-						if(this.pageNo>1){
-							this.listdata = this.listdata.concat(res.data.list || [])
-						}else{
-							this.listdata = res.data.list || []
-							this.scrollTop = 0
+			// 获取连接设备的所有特征值
+			getBLEDeviceCharacteristics(){
+				let _this = this
+				this.notifyServicweId = this.services[1].uuid
+				uni.getBLEDeviceCharacteristics({
+					deviceId: this.connectedDeviceId,
+					serviceId: this.notifyServicweId,
+					success: function(res) {
+						console.log(res)
+						_this.statusStr = '获取连接设备的所有特征值成功'
+						for (let i = 0; i < res.characteristics.length; i++) {
+							if ((res.characteristics[i].properties.notify || res.characteristics[i].properties.indicate) &&
+								(res.characteristics[i].properties.read && res.characteristics[i].properties.write)) {
+								console.log(res.characteristics[i].uuid, '蓝牙特征值 ==========')
+								/* 获取蓝牙特征值 */
+								_this.notifyCharacteristicsId = res.characteristics[i].uuid
+								// 启用低功耗蓝牙设备特征值变化时的 notify 功能
+								setTimeout(()=>{
+									_this.notifyBLECharacteristicValueChange()
+								},500)
+							}
 						}
-						this.total = res.data.count || 0
-						this.noDataText = '暂无数据'
-					} else {
-						this.noDataText = res.message
-						this.listdata = []
-						this.total = 0
+					},
+					fail: function(res) {
+						console.log(res)
 					}
-					this.status = "loadmore"
 				})
 			},
-			// scroll-view到底部加载更多
-			onreachBottom() {
-				if(this.listdata.length < this.total){
-					this.pageNo += 1
-					this.searchHandle()
-				}else{
-					uni.showToast({ title: '已经到底了', icon: 'none' })
-					this.status = "nomore"
-				}
-			}
+			// 启用低功耗蓝牙设备特征值变化时的 notify 功能
+			 notifyBLECharacteristicValueChange() { 
+				let that = this;
+				console.log(that.connectedDeviceId,that.notifyServicweId,that.notifyCharacteristicsId)
+				uni.notifyBLECharacteristicValueChange({
+					state: true,
+					deviceId: that.connectedDeviceId,
+					serviceId: that.notifyServicweId,
+					// characteristicId: that.notifyCharacteristicsId,
+					characteristicId: '49535343-1E4D-4BD9-BA61-23C647249616',
+					success(res) {
+						console.log(res,'notifyBLECharacteristicValueChange')
+						that.statusStr = '启用低功耗蓝牙设备特征值变化成功'
+						/*用来监听手机蓝牙设备的数据变化*/
+						uni.onBLECharacteristicValueChange(function(res) {
+							console.log(res,'read data')
+							that.balanceData = that.buf2string(res.value)
+							// that.hexstr = that.receiveData(res.value)
+						})
+					},
+					fail(res) {
+						console.log(res, '启用低功耗蓝牙设备监听失败')
+					}
+				})
+			},
+			/*转换成需要的格式*/
+		    buf2string(buffer) {
+				let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
+				return arr.map((char, i) => {
+					return String.fromCharCode(char);
+				}).join('');
+			},
+			receiveData(buf) {
+					return this.hexCharCodeToStr(this.ab2hex(buf))
+			},
+			/*转成二进制*/
+			ab2hex (buffer) {
+			  let hexArr = Array.prototype.map.call(
+				  new Uint8Array(buffer), function (bit) {
+					  return ('00' + bit.toString(16)).slice(-2)
+				  }
+			  )
+			  return hexArr.join('')
+			},
+			/*转成可展会的文字*/
+			hexCharCodeToStr(hexCharCodeStr) {
+			  let trimedStr = hexCharCodeStr.trim();
+			  let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
+			  let len = rawStr.length;
+			  let curCharCode;
+			  let resultStr = [];
+			  for (let i = 0; i < len; i = i + 2) {
+				  curCharCode = parseInt(rawStr.substr(i, 2), 16);
+				  resultStr.push(String.fromCharCode(curCharCode));
+			  }
+			  return resultStr.join('');
+			},
+			// 发送数据
+			sendData(str) {
+			    let that = this;
+			    let dataBuffer = new ArrayBuffer(3)
+			    let dataView = new DataView(dataBuffer)
+			    // for (let i = 0; i < str.length; i++) {
+			    //     dataView.setUint8(i, str.charAt(i).charCodeAt())
+			    // }
+				dataView.setUint8(0, 0x02)
+				dataView.setUint8(1, 0x42)
+				dataView.setUint8(2, 0x03)
+			    let dataHex = that.ab2hex(dataBuffer);
+			    this.writeDatas = that.hexCharCodeToStr(dataHex);
+			    uni.writeBLECharacteristicValue({
+			        deviceId: that.connectedDeviceId,
+			        serviceId: that.notifyServicweId,
+			        characteristicId: that.notifyCharacteristicsId,
+					// characteristicId: '49535343-1E4D-4BD9-BA61-23C647249616',
+			        value: dataBuffer,
+			        success: function (res) {
+			            console.log('发送的数据:' + that.writeDatas)
+			            console.log('message发送成功')
+			        },
+			        fail: function (res) {
+						console.log('发送失败:' + res)
+			        },
+			        complete: function (res) {
+			        }
+			    })
+			},
+			 // 断开设备连接
+			closeConnect() {
+				let that = this;
+			    if (that.connectedDeviceId) {
+			        uni.closeBLEConnection({
+			            deviceId: that.connectedDeviceId,
+			            success: function(res) {
+			                that.closeBluetoothAdapter()
+			            },
+			            fail(res) {
+			            }
+			        })
+			    } else {
+			        that.closeBluetoothAdapter()
+			    }
+			},
+			// 关闭蓝牙模块
+			closeBluetoothAdapter() {
+				let that = this;
+			    uni.closeBluetoothAdapter({
+			        success: function(res) {
+						that.statusStr = '关闭蓝牙模块'
+						that.balanceData = ''
+						console.log(res,'closeBluetoothAdapter')
+			        },
+			        fail: function(err) {
+			        }
+			    })
+			},
+		},
+		onUnload() {
+			this.closeConnect()
 		}
 	}
 </script>
 
-<style lang="scss">
-	page{
-		height: 100%;
-	}
-	.homePage-container {
+<style>
+	.content{
+		display: flex;
+		align-items: center;
+		justify-content: center;
+		height: 100vh;
 		width: 100%;
-		height: 100%;
+	}
+	.cz-box{
+		text-align: center;
+		background-color: #FFFFFF;
+		padding: 50rpx 50rpx 30rpx;
+		border-radius: 300rpx;
+		box-shadow: 2rpx 2rpx 5rpx #eee;
+		width: 300rpx;
+		height: 300rpx;
 		display: flex;
+		align-items: center;
+		justify-content: center;
 		flex-direction: column;
-		padding: 102rpx 0;
-		.footer-bar{
-			width: 100%;
-			display: flex;
-			align-items: center;
-			height: 102rpx;
-			justify-content: center;
-			padding: 0 10%;
-			position: fixed;
-			bottom: 0;
-			left: 0;
-		}
-		// 筛选菜单
-		.homePage-head{
-			font-size: 30rpx;
-			display: flex;
-			justify-content: space-between;
-			align-items: center;
-			position: fixed;
-			top: 0;
-			left: 0;
-			z-index: 9;
-			padding: 26rpx 30rpx;
-			background-color: #fff;
-			box-shadow: 0rpx 0rpx 14rpx rgba(0,0,0,.05);
-			width: 100%;
-			border-top: 10rpx solid #f8f8f8;
-			.head-con{
-				flex-grow: 1;
-				display: flex;
-				align-items: center;
-				justify-content: space-between;
-				.homePage-tit{
-					color: #040301;
-				}
-				.total-con{
-					color: #040301;
-					.goleNum{
-						color: #eb0000;
-						font-size: 30rpx;
-						font-weight: bold;
-					}
-					.ld-icon{
-						width: 30rpx;
-						height: 30rpx;
-						margin: 0 0 0 10rpx;
-						vertical-align: middle;
-					}
-				}
-			}
-			.filter-icon{
-				flex-shrink: 0;
-				width: 36rpx;
-				height: 36rpx;
-				padding-left: 20rpx;
-			}
-		}
-		//  列表
-		.scroll-con{
-			flex: 1;
-			// height: calc(100% - 40);
-			overflow: auto;
-		}
-		//  列表样式
-		.cell-item-con{
-			margin: 20rpx;
-			.cell-item{
-				background-color: #fff;
-				color: #606266;
-				padding: 0 30rpx;
-				margin-bottom: 20rpx;
-				border-radius: 16rpx;
-				font-size: 28rpx;
-				box-shadow: 3rpx 3rpx 5rpx #eee;
-				.cell-item-c{
-					display: flex;
-					justify-content: space-between;
-					padding: 18rpx 0;
-					border-bottom: 1rpx solid #f2f2f2;
-					.cell-item-label{
-						flex-shrink: 0;
-						width: 200rpx;
-						color: #303133;
-					}
-					.cell-item-main{
-						flex-grow: 1;
-						text-align: right;
-						.handle-btn{
-							border-color: #fcbd71!important;
-							background-color: #fcbd71!important;
-						}
-					}
-					.blue {
-						color: #1890FF;
-					}
-					.green {
-						color: #16b50e;
-					}
-					.red {
-						color: red;
-					}
-					&:last-child{
-						border: none;
-					}
-				}
-				.handle-con{
-					align-items: center;
-					// margin-top: 10rpx;
-					// padding-top: 18rpx;
-					// padding: 0;
-				}
-			}
-		}
-		
+	}
+	.tits{
+		padding: 20rpx 0;
 	}
 </style>

+ 1 - 1
pages/ledouHexiao/hexiaoSuccess.vue

@@ -24,7 +24,7 @@
 		methods: {
 			toPage(){
 				uni.reLaunch({
-					url:'/pages/index/index'
+					url:'/pages/hexiao/index'
 				})
 			}
 		}

BIN
static/chengzhong.png


BIN
static/tabbar/dzicheng-active.png


BIN
static/tabbar/dzicheng.png


BIN
static/tabbar/user-active.png


BIN
static/tabbar/user.png