search.nvue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. <text class="device-id">设备编码:{{item.advertisData}}</text>
  29. </view>
  30. <view class="device-right">
  31. <!-- 信号强度图标 -->
  32. <view class="signal-wrap">
  33. <view class="signal-bar signal-bar1" :class="{'signal-active': getSignalLevel(item.RSSI) >= 1}"></view>
  34. <view class="signal-bar signal-bar2" :class="{'signal-active': getSignalLevel(item.RSSI) >= 2}"></view>
  35. <view class="signal-bar signal-bar3" :class="{'signal-active': getSignalLevel(item.RSSI) >= 3}"></view>
  36. <view class="signal-bar signal-bar4" :class="{'signal-active': getSignalLevel(item.RSSI) >= 4}"></view>
  37. </view>
  38. <text class="connect-btn">添加</text>
  39. </view>
  40. </view>
  41. <view class="empty-tip" v-if="!searching && deviceList.length === 0">
  42. <text class="empty-text">未发现设备,请确认设备已开启</text>
  43. </view>
  44. </scroll-view>
  45. <ay-toast ref="ayToast" />
  46. </view>
  47. </template>
  48. <script>
  49. import { useBleStore } from '@/stores/ble'
  50. import { ensureBlePrerequisite, openLocationSettings, openBluetoothSettings } from '@/utils/ble/permission.js'
  51. import { bindDeviceByCode, getDeviceDetail } from '@/utils/api/device.js'
  52. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  53. export default {
  54. components: {
  55. ayToast
  56. },
  57. data() {
  58. return {
  59. statusBarHeight: 44,
  60. btModalShowing: false
  61. }
  62. },
  63. computed: {
  64. bleStore() {
  65. return useBleStore()
  66. },
  67. searching() {
  68. return this.bleStore.searching
  69. },
  70. deviceList() {
  71. console.log("scannedDevices ",this.bleStore.scannedDevices)
  72. return this.bleStore.scannedDevices
  73. },
  74. scanThrottled() {
  75. return this.bleStore.scanThrottled
  76. }
  77. },
  78. onLoad() {
  79. const sysInfo = uni.getSystemInfoSync()
  80. this.statusBarHeight = sysInfo.statusBarHeight || 44
  81. // 配置并开始扫描
  82. this.bleStore.configure({ debug: true })
  83. this.startScan()
  84. },
  85. onUnload() {
  86. // 离开页面才真正停止底层BLE发现
  87. this.bleStore.stopScan()
  88. },
  89. methods: {
  90. goBack() {
  91. uni.navigateBack()
  92. },
  93. async startScan() {
  94. try {
  95. console.log("重新扫描")
  96. await ensureBlePrerequisite()
  97. // startScan 内部自动初始化适配器,扫描结果自动更新 store.scannedDevices
  98. await this.bleStore.startScan({ timeout: 15000, returnAll: true })
  99. } catch (e) {
  100. const msg = e && (e.message || e.code || '')
  101. if (msg === 'BLE_LOCATION_OFF') {
  102. uni.showModal({
  103. title: '提示',
  104. content: '请先开启位置服务以搜索蓝牙设备',
  105. success: r => {
  106. if (r.confirm) openLocationSettings()
  107. }
  108. })
  109. } else if (msg === 'BLE_ADAPTER_OFF' || msg === 'BLE_ADAPTER_OFF') {
  110. if (this.btModalShowing) return
  111. this.btModalShowing = true
  112. uni.showModal({
  113. title: '蓝牙未开启',
  114. content: '请先开启手机蓝牙,才能搜索和连接艾灸椅设备',
  115. confirmText: '去设置',
  116. cancelText: '取消',
  117. success: r => {
  118. this.btModalShowing = false
  119. if (r.confirm) openBluetoothSettings()
  120. }
  121. })
  122. } else if (msg === 'BLE_PERMISSION_DENIED') {
  123. this.$refs.ayToast.error('请授权蓝牙权限')
  124. } else {
  125. console.error('扫描异常', e)
  126. this.$refs.ayToast.error('扫描失败: ' + (msg || ''))
  127. }
  128. }
  129. },
  130. onRefresh() {
  131. if (this.searching) {
  132. this.bleStore.stopScan()
  133. } else {
  134. this.startScan()
  135. }
  136. },
  137. getSignalLevel(rssi) {
  138. console.log("信号",rssi)
  139. if (!rssi) return 0
  140. const val = Number(rssi)
  141. if (val >= -50) return 4
  142. if (val >= -65) return 3
  143. if (val >= -80) return 2
  144. if (val >= -95) return 1
  145. return 1
  146. },
  147. async onSelectDevice(device) {
  148. console.log("要操作的设备",device)
  149. let deviceList = []
  150. try {
  151. // uni.removeStorageSync('deviceList')
  152. const data = uni.getStorageSync('deviceList')
  153. console.log("deviceList",data)
  154. if (data && data.length > 0) {
  155. deviceList = data
  156. }
  157. } catch (e) {}
  158. // 使用 advertisData/deviceCode 做重复判断(跨平台唯一标识)
  159. // const exists = deviceList.find(item => {
  160. // const itemCode = item.advertisData || item.deviceCode || ''
  161. // return itemCode && itemCode === device.advertisData
  162. // })
  163. // if (exists) {
  164. // this.$refs.ayToast.show('该设备已添加')
  165. // return
  166. // }
  167. try {
  168. // 调用后端接口绑定设备,advertisData作为deviceCode传递
  169. const res = await bindDeviceByCode(device.advertisData)
  170. console.log('绑定设备成功', res)
  171. // 调用设备详情接口获取deviceType
  172. let deviceType = ''
  173. try {
  174. const detail = await getDeviceDetail(res.deviceId)
  175. deviceType = detail.deviceType || ''
  176. console.log('获取设备类型', deviceType)
  177. } catch (detailErr) {
  178. console.error('获取设备详情失败', detailErr)
  179. }
  180. // 后端绑定成功后,保存到本地
  181. deviceList.push({
  182. deviceId: res.deviceId,
  183. name: device.name || '艾灸椅',
  184. RSSI: device.RSSI,
  185. id: res.deviceId,
  186. deviceType: deviceType,
  187. advertisData: device.advertisData
  188. })
  189. uni.setStorageSync('deviceList', deviceList)
  190. this.$refs.ayToast.success('设备添加成功')
  191. setTimeout(() => {
  192. uni.redirectTo({
  193. url: `/pages/device/deviceInfo/deviceInfo?deviceId=${encodeURIComponent(res.deviceId)}&advertisData=${encodeURIComponent(device.advertisData)}&name=${encodeURIComponent(device.name || '艾灸椅')}&rssi=${device.RSSI || ''}&deviceType=${deviceType}&id=${encodeURIComponent(res.deviceId)}`
  194. })
  195. }, 1000)
  196. } catch (e) {
  197. console.error('绑定设备失败', e)
  198. // 错误提示由http模块统一处理,这里不再重复提示
  199. }
  200. }
  201. }
  202. }
  203. </script>
  204. <style>
  205. .page {
  206. flex: 1;
  207. position: relative;
  208. }
  209. .content-bg {
  210. position: absolute;
  211. left: 0;
  212. top: 0;
  213. width: 750rpx;
  214. height: 2000px;
  215. }
  216. .status-bar {
  217. background-color: rgba(0,0,0,0);
  218. }
  219. .custom-head {
  220. flex-direction: row;
  221. justify-content: space-between;
  222. align-items: center;
  223. height: 88rpx;
  224. padding-left: 30rpx;
  225. padding-right: 54rpx;
  226. }
  227. .head-back {
  228. width: 60rpx;
  229. height: 60rpx;
  230. justify-content: center;
  231. align-items: center;
  232. }
  233. .back-icon {
  234. width: 60rpx;
  235. height: 60rpx;
  236. }
  237. .head-title {
  238. font-weight: bold;
  239. font-size: 32rpx;
  240. color: #545F71;
  241. }
  242. .head-placeholder {
  243. width: 60rpx;
  244. height: 60rpx;
  245. }
  246. .search-tip {
  247. flex-direction: row;
  248. justify-content: space-between;
  249. align-items: center;
  250. padding-left: 38rpx;
  251. padding-right: 38rpx;
  252. padding-top: 20rpx;
  253. padding-bottom: 20rpx;
  254. }
  255. .search-tip-text {
  256. font-size: 26rpx;
  257. color: #999;
  258. }
  259. .search-btn {
  260. font-size: 26rpx;
  261. color: #389588;
  262. padding: 10rpx 24rpx;
  263. border-width: 1px;
  264. border-style: solid;
  265. border-color: #389588;
  266. border-radius: 24rpx;
  267. }
  268. .device-scroll {
  269. flex: 1;
  270. padding-left: 38rpx;
  271. padding-right: 38rpx;
  272. }
  273. .device-item {
  274. flex-direction: row;
  275. justify-content: space-between;
  276. align-items: center;
  277. background-color: #FFFFFF;
  278. border-radius: 16rpx;
  279. padding: 30rpx;
  280. margin-bottom: 20rpx;
  281. }
  282. .device-info {
  283. flex: 1;
  284. }
  285. .device-name {
  286. font-size: 30rpx;
  287. color: #333;
  288. font-weight: bold;
  289. }
  290. .device-id {
  291. font-size: 22rpx;
  292. color: #999;
  293. margin-top: 8rpx;
  294. }
  295. .device-right {
  296. align-items: center;
  297. }
  298. .signal-wrap {
  299. flex-direction: row;
  300. align-items: flex-end;
  301. margin-bottom: 12rpx;
  302. }
  303. .signal-bar {
  304. width: 8rpx;
  305. margin-left: 4rpx;
  306. border-radius: 4rpx;
  307. background-color: #ddd;
  308. }
  309. .signal-bar1 {
  310. height: 14rpx;
  311. }
  312. .signal-bar2 {
  313. height: 22rpx;
  314. }
  315. .signal-bar3 {
  316. height: 30rpx;
  317. }
  318. .signal-bar4 {
  319. height: 38rpx;
  320. }
  321. .signal-active {
  322. background-color: #389588;
  323. }
  324. .connect-btn {
  325. font-size: 26rpx;
  326. color: #FFFFFF;
  327. background-color: #389588;
  328. padding-left: 24rpx;
  329. padding-right: 24rpx;
  330. padding-top: 12rpx;
  331. padding-bottom: 12rpx;
  332. border-radius: 20rpx;
  333. }
  334. .empty-tip {
  335. align-items: center;
  336. padding-top: 100rpx;
  337. }
  338. .empty-text {
  339. font-size: 28rpx;
  340. color: #999;
  341. }
  342. .throttle-warning {
  343. background-color: #FFF3E0;
  344. padding: 16rpx 38rpx;
  345. margin-left: 38rpx;
  346. margin-right: 38rpx;
  347. border-radius: 12rpx;
  348. margin-bottom: 16rpx;
  349. }
  350. .throttle-text {
  351. font-size: 24rpx;
  352. color: #E65100;
  353. }
  354. </style>