search.nvue 7.7 KB

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