search.nvue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <view class="page">
  3. <image class="content-bg" src="/static/device/devicebg.png" mode="scaleToFill"></image>
  4. <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view>
  5. <!-- 顶部导航 -->
  6. <view class="custom-head">
  7. <view class="head-back" @click="goBack">
  8. <image class="back-icon" src="/static/public/back-arrow.png" mode="aspectFit"></image>
  9. </view>
  10. <text class="head-title">添加设备</text>
  11. <view class="head-placeholder"></view>
  12. </view>
  13. <!-- 扫描状态提示 -->
  14. <view class="search-tip">
  15. <text class="search-tip-text">{{searching ? '正在搜索附近设备...' : '搜索完成'}}</text>
  16. <text class="search-btn" @click="onRefresh">{{searching ? '停止' : '重新搜索'}}</text>
  17. </view>
  18. <!-- 扫描限流警告 -->
  19. <view class="throttle-warning" v-if="scanThrottled">
  20. <text class="throttle-text">扫描过于频繁,可能无法搜索到设备,请稍等片刻再试</text>
  21. </view>
  22. <!-- 设备列表 -->
  23. <scroll-view class="device-scroll" scroll-y="true">
  24. <view class="device-item" v-for="(item, index) in deviceList" :key="index" @click="onSelectDevice(item)">
  25. <view class="device-info">
  26. <text class="device-name">{{item.name || '未知设备'}}</text>
  27. <text class="device-id">{{item.deviceId}}</text>
  28. </view>
  29. <view class="device-right">
  30. <!-- 信号强度图标 -->
  31. <view class="signal-wrap">
  32. <view class="signal-bar signal-bar1" :class="{'signal-active': getSignalLevel(item.RSSI) >= 1}"></view>
  33. <view class="signal-bar signal-bar2" :class="{'signal-active': getSignalLevel(item.RSSI) >= 2}"></view>
  34. <view class="signal-bar signal-bar3" :class="{'signal-active': getSignalLevel(item.RSSI) >= 3}"></view>
  35. <view class="signal-bar signal-bar4" :class="{'signal-active': getSignalLevel(item.RSSI) >= 4}"></view>
  36. </view>
  37. <text class="connect-btn">添加</text>
  38. </view>
  39. </view>
  40. <view class="empty-tip" v-if="!searching && deviceList.length === 0">
  41. <text class="empty-text">未发现设备,请确认设备已开启</text>
  42. </view>
  43. </scroll-view>
  44. <ay-toast ref="ayToast" />
  45. </view>
  46. </template>
  47. <script>
  48. import { useBleStore } from '@/stores/ble'
  49. import { ensureBlePrerequisite, openLocationSettings, openBluetoothSettings } from '@/utils/ble/permission.js'
  50. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  51. export default {
  52. components: {
  53. ayToast
  54. },
  55. data() {
  56. return {
  57. statusBarHeight: 44
  58. }
  59. },
  60. computed: {
  61. bleStore() {
  62. return useBleStore()
  63. },
  64. searching() {
  65. return this.bleStore.searching
  66. },
  67. deviceList() {
  68. return this.bleStore.scannedDevices
  69. },
  70. scanThrottled() {
  71. return this.bleStore.scanThrottled
  72. }
  73. },
  74. onLoad() {
  75. const sysInfo = uni.getSystemInfoSync()
  76. this.statusBarHeight = sysInfo.statusBarHeight || 44
  77. // 配置并开始扫描
  78. this.bleStore.configure({ debug: true })
  79. this.startScan()
  80. },
  81. onUnload() {
  82. // 离开页面才真正停止底层BLE发现
  83. this.bleStore.stopScan()
  84. },
  85. methods: {
  86. goBack() {
  87. uni.navigateBack()
  88. },
  89. async startScan() {
  90. try {
  91. await ensureBlePrerequisite()
  92. // startScan 内部自动初始化适配器,扫描结果自动更新 store.scannedDevices
  93. await this.bleStore.startScan({ timeout: 15000, returnAll: true })
  94. } catch (e) {
  95. const msg = e && (e.message || e.code || '')
  96. if (msg === 'BLE_LOCATION_OFF') {
  97. uni.showModal({
  98. title: '提示',
  99. content: '请先开启位置服务以搜索蓝牙设备',
  100. success: r => {
  101. if (r.confirm) openLocationSettings()
  102. }
  103. })
  104. } else if (msg === 'BLE_ADAPTER_OFF' || msg === 'BLE_ADAPTER_OFF') {
  105. uni.showModal({
  106. title: '蓝牙未开启',
  107. content: '请先开启手机蓝牙,才能搜索和连接艾灸椅设备',
  108. confirmText: '去设置',
  109. cancelText: '取消',
  110. success: r => {
  111. if (r.confirm) openBluetoothSettings()
  112. }
  113. })
  114. } else if (msg === 'BLE_PERMISSION_DENIED') {
  115. this.$refs.ayToast.error('请授权蓝牙权限')
  116. } else {
  117. console.error('扫描异常', e)
  118. this.$refs.ayToast.error('扫描失败: ' + (msg || ''))
  119. }
  120. }
  121. },
  122. onRefresh() {
  123. if (this.searching) {
  124. this.bleStore.stopScan()
  125. } else {
  126. this.startScan()
  127. }
  128. },
  129. getSignalLevel(rssi) {
  130. console.log("信号",rssi)
  131. if (!rssi) return 0
  132. const val = Number(rssi)
  133. if (val >= -50) return 4
  134. if (val >= -65) return 3
  135. if (val >= -80) return 2
  136. if (val >= -95) return 1
  137. return 1
  138. },
  139. onSelectDevice(device) {
  140. let deviceList = []
  141. try {
  142. const data = uni.getStorageSync('deviceList')
  143. if (data && data.length > 0) {
  144. deviceList = data
  145. }
  146. } catch (e) {}
  147. const exists = deviceList.find(item => item.deviceId === device.deviceId)
  148. if (exists) {
  149. this.$refs.ayToast.show('该设备已添加')
  150. return
  151. }
  152. deviceList.push({
  153. deviceId: device.deviceId,
  154. name: device.name || '艾灸椅',
  155. RSSI: device.RSSI
  156. })
  157. uni.setStorageSync('deviceList', deviceList)
  158. this.$refs.ayToast.success('设备添加成功')
  159. setTimeout(() => {
  160. uni.redirectTo({
  161. url: `/pages/device/deviceInfo/deviceInfo?deviceId=${encodeURIComponent(device.deviceId)}&name=${encodeURIComponent(device.name || '艾灸椅')}&rssi=${device.RSSI || ''}`
  162. })
  163. }, 1000)
  164. }
  165. }
  166. }
  167. </script>
  168. <style>
  169. .page {
  170. flex: 1;
  171. position: relative;
  172. }
  173. .content-bg {
  174. position: absolute;
  175. left: 0;
  176. top: 0;
  177. width: 750rpx;
  178. height: 2000px;
  179. }
  180. .status-bar {
  181. background-color: rgba(0,0,0,0);
  182. }
  183. .custom-head {
  184. flex-direction: row;
  185. justify-content: space-between;
  186. align-items: center;
  187. height: 88rpx;
  188. padding-left: 30rpx;
  189. padding-right: 54rpx;
  190. }
  191. .head-back {
  192. width: 60rpx;
  193. height: 60rpx;
  194. justify-content: center;
  195. align-items: center;
  196. }
  197. .back-icon {
  198. width: 60rpx;
  199. height: 60rpx;
  200. }
  201. .head-title {
  202. font-weight: bold;
  203. font-size: 32rpx;
  204. color: #545F71;
  205. }
  206. .head-placeholder {
  207. width: 60rpx;
  208. height: 60rpx;
  209. }
  210. .search-tip {
  211. flex-direction: row;
  212. justify-content: space-between;
  213. align-items: center;
  214. padding-left: 38rpx;
  215. padding-right: 38rpx;
  216. padding-top: 20rpx;
  217. padding-bottom: 20rpx;
  218. }
  219. .search-tip-text {
  220. font-size: 26rpx;
  221. color: #999;
  222. }
  223. .search-btn {
  224. font-size: 26rpx;
  225. color: #389588;
  226. padding: 10rpx 24rpx;
  227. border-width: 1px;
  228. border-style: solid;
  229. border-color: #389588;
  230. border-radius: 24rpx;
  231. }
  232. .device-scroll {
  233. flex: 1;
  234. padding-left: 38rpx;
  235. padding-right: 38rpx;
  236. }
  237. .device-item {
  238. flex-direction: row;
  239. justify-content: space-between;
  240. align-items: center;
  241. background-color: #FFFFFF;
  242. border-radius: 16rpx;
  243. padding: 30rpx;
  244. margin-bottom: 20rpx;
  245. }
  246. .device-info {
  247. flex: 1;
  248. }
  249. .device-name {
  250. font-size: 30rpx;
  251. color: #333;
  252. font-weight: bold;
  253. }
  254. .device-id {
  255. font-size: 22rpx;
  256. color: #999;
  257. margin-top: 8rpx;
  258. }
  259. .device-right {
  260. align-items: center;
  261. }
  262. .signal-wrap {
  263. flex-direction: row;
  264. align-items: flex-end;
  265. margin-bottom: 12rpx;
  266. }
  267. .signal-bar {
  268. width: 8rpx;
  269. margin-left: 4rpx;
  270. border-radius: 4rpx;
  271. background-color: #ddd;
  272. }
  273. .signal-bar1 {
  274. height: 14rpx;
  275. }
  276. .signal-bar2 {
  277. height: 22rpx;
  278. }
  279. .signal-bar3 {
  280. height: 30rpx;
  281. }
  282. .signal-bar4 {
  283. height: 38rpx;
  284. }
  285. .signal-active {
  286. background-color: #389588;
  287. }
  288. .connect-btn {
  289. font-size: 26rpx;
  290. color: #FFFFFF;
  291. background-color: #389588;
  292. padding-left: 24rpx;
  293. padding-right: 24rpx;
  294. padding-top: 12rpx;
  295. padding-bottom: 12rpx;
  296. border-radius: 20rpx;
  297. }
  298. .empty-tip {
  299. align-items: center;
  300. padding-top: 100rpx;
  301. }
  302. .empty-text {
  303. font-size: 28rpx;
  304. color: #999;
  305. }
  306. .throttle-warning {
  307. background-color: #FFF3E0;
  308. padding: 16rpx 38rpx;
  309. margin-left: 38rpx;
  310. margin-right: 38rpx;
  311. border-radius: 12rpx;
  312. margin-bottom: 16rpx;
  313. }
  314. .throttle-text {
  315. font-size: 24rpx;
  316. color: #E65100;
  317. }
  318. </style>