search.nvue 8.4 KB

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