search.nvue 9.0 KB

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