search.nvue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 } 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. const exists = deviceList.find(item => item.deviceId === device.deviceId)
  158. if (exists) {
  159. this.$refs.ayToast.show('该设备已添加')
  160. return
  161. }
  162. try {
  163. // 调用后端接口绑定设备,deviceId作为deviceCode传递
  164. const res = await bindDeviceByCode(device.deviceId)
  165. console.log('绑定设备成功', res)
  166. // 后端绑定成功后,保存到本地
  167. deviceList.push({
  168. deviceId: device.deviceId,
  169. name: device.name || '艾灸椅',
  170. RSSI: device.RSSI,
  171. id:res.id
  172. })
  173. uni.setStorageSync('deviceList', deviceList)
  174. this.$refs.ayToast.success('设备添加成功')
  175. setTimeout(() => {
  176. uni.redirectTo({
  177. url: `/pages/device/deviceInfo/deviceInfo?deviceId=${encodeURIComponent(device.deviceId)}&name=${encodeURIComponent(device.name || '艾灸椅')}&rssi=${device.RSSI || ''}`
  178. })
  179. }, 1000)
  180. } catch (e) {
  181. console.error('绑定设备失败', e)
  182. // 错误提示由http模块统一处理,这里不再重复提示
  183. }
  184. }
  185. }
  186. }
  187. </script>
  188. <style>
  189. .page {
  190. flex: 1;
  191. position: relative;
  192. }
  193. .content-bg {
  194. position: absolute;
  195. left: 0;
  196. top: 0;
  197. width: 750rpx;
  198. height: 2000px;
  199. }
  200. .status-bar {
  201. background-color: rgba(0,0,0,0);
  202. }
  203. .custom-head {
  204. flex-direction: row;
  205. justify-content: space-between;
  206. align-items: center;
  207. height: 88rpx;
  208. padding-left: 30rpx;
  209. padding-right: 54rpx;
  210. }
  211. .head-back {
  212. width: 60rpx;
  213. height: 60rpx;
  214. justify-content: center;
  215. align-items: center;
  216. }
  217. .back-icon {
  218. width: 60rpx;
  219. height: 60rpx;
  220. }
  221. .head-title {
  222. font-weight: bold;
  223. font-size: 32rpx;
  224. color: #545F71;
  225. }
  226. .head-placeholder {
  227. width: 60rpx;
  228. height: 60rpx;
  229. }
  230. .search-tip {
  231. flex-direction: row;
  232. justify-content: space-between;
  233. align-items: center;
  234. padding-left: 38rpx;
  235. padding-right: 38rpx;
  236. padding-top: 20rpx;
  237. padding-bottom: 20rpx;
  238. }
  239. .search-tip-text {
  240. font-size: 26rpx;
  241. color: #999;
  242. }
  243. .search-btn {
  244. font-size: 26rpx;
  245. color: #389588;
  246. padding: 10rpx 24rpx;
  247. border-width: 1px;
  248. border-style: solid;
  249. border-color: #389588;
  250. border-radius: 24rpx;
  251. }
  252. .device-scroll {
  253. flex: 1;
  254. padding-left: 38rpx;
  255. padding-right: 38rpx;
  256. }
  257. .device-item {
  258. flex-direction: row;
  259. justify-content: space-between;
  260. align-items: center;
  261. background-color: #FFFFFF;
  262. border-radius: 16rpx;
  263. padding: 30rpx;
  264. margin-bottom: 20rpx;
  265. }
  266. .device-info {
  267. flex: 1;
  268. }
  269. .device-name {
  270. font-size: 30rpx;
  271. color: #333;
  272. font-weight: bold;
  273. }
  274. .device-id {
  275. font-size: 22rpx;
  276. color: #999;
  277. margin-top: 8rpx;
  278. }
  279. .device-right {
  280. align-items: center;
  281. }
  282. .signal-wrap {
  283. flex-direction: row;
  284. align-items: flex-end;
  285. margin-bottom: 12rpx;
  286. }
  287. .signal-bar {
  288. width: 8rpx;
  289. margin-left: 4rpx;
  290. border-radius: 4rpx;
  291. background-color: #ddd;
  292. }
  293. .signal-bar1 {
  294. height: 14rpx;
  295. }
  296. .signal-bar2 {
  297. height: 22rpx;
  298. }
  299. .signal-bar3 {
  300. height: 30rpx;
  301. }
  302. .signal-bar4 {
  303. height: 38rpx;
  304. }
  305. .signal-active {
  306. background-color: #389588;
  307. }
  308. .connect-btn {
  309. font-size: 26rpx;
  310. color: #FFFFFF;
  311. background-color: #389588;
  312. padding-left: 24rpx;
  313. padding-right: 24rpx;
  314. padding-top: 12rpx;
  315. padding-bottom: 12rpx;
  316. border-radius: 20rpx;
  317. }
  318. .empty-tip {
  319. align-items: center;
  320. padding-top: 100rpx;
  321. }
  322. .empty-text {
  323. font-size: 28rpx;
  324. color: #999;
  325. }
  326. .throttle-warning {
  327. background-color: #FFF3E0;
  328. padding: 16rpx 38rpx;
  329. margin-left: 38rpx;
  330. margin-right: 38rpx;
  331. border-radius: 12rpx;
  332. margin-bottom: 16rpx;
  333. }
  334. .throttle-text {
  335. font-size: 24rpx;
  336. color: #E65100;
  337. }
  338. </style>