detail.nvue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <template>
  2. <view class="container">
  3. <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view>
  4. <view class="customHead">
  5. <view class="back-wrap" @click="goBack">
  6. <image src="/static/public/backBlack.png" mode="aspectFill" class="backimg"></image>
  7. </view>
  8. <text class="head-title">设备详情</text>
  9. <view class="plus"></view>
  10. </view>
  11. <scroll-view class="list" scroll-y="true">
  12. <!-- 连接状态 -->
  13. <view class="connect-status">
  14. <view class="status-dot" :style="{backgroundColor: connected ? '#389588' : '#CCCCCC'}"></view>
  15. <text class="status-text">{{connected ? '已连接' : '未连接'}}</text>
  16. <text class="connect-btn" v-if="!connected && !connecting" @click="connectDevice">连接设备</text>
  17. <text class="connect-btn" v-if="connecting">连接中...</text>
  18. </view>
  19. <text class="txt">通用设置</text>
  20. <view class="menuitem">
  21. <text>设备名称</text>
  22. <text class="gray">{{name}}</text>
  23. </view>
  24. <view class="menuitem">
  25. <text>设备ID</text>
  26. <text class="gray device-id-text">{{deviceId}}</text>
  27. </view>
  28. <view class="menuitem">
  29. <text>信号强度</text>
  30. <view class="signal-value-wrap">
  31. <view class="signal-wrap">
  32. <view class="signal-bar signal-bar1" :class="{'signal-active': signalLevel >= 1}"></view>
  33. <view class="signal-bar signal-bar2" :class="{'signal-active': signalLevel >= 2}"></view>
  34. <view class="signal-bar signal-bar3" :class="{'signal-active': signalLevel >= 3}"></view>
  35. <view class="signal-bar signal-bar4" :class="{'signal-active': signalLevel >= 4}"></view>
  36. </view>
  37. <text class="gray" style="padding-right: 0;">{{rssiText}}</text>
  38. </view>
  39. </view>
  40. <view class="menuitem">
  41. <text>固件版本</text>
  42. <text class="gray">{{firmwareVersion || '未获取'}}</text>
  43. </view>
  44. <view class="menuitem lastItem">
  45. <text>连接状态</text>
  46. <text class="gray">{{connected ? '已连接' : '未连接'}}</text>
  47. </view>
  48. <text class="txt">设备操作</text>
  49. <view class="menuitem" @click="disconnectDevice" v-if="connected">
  50. <text>断开连接</text>
  51. <image class="arrowright" src="/static/my/arrowright.png" mode="aspectFill"></image>
  52. </view>
  53. <view class="menuitem lastItem">
  54. <text>帮助与反馈</text>
  55. <image class="arrowright" src="/static/my/arrowright.png" mode="aspectFill"></image>
  56. </view>
  57. <view class="delbtn" @click="delDevice">
  58. <text class="delbtn-text">删除设备</text>
  59. </view>
  60. </scroll-view>
  61. <ay-toast ref="ayToast" />
  62. </view>
  63. </template>
  64. <script>
  65. import { useBleStore } from '@/stores/ble'
  66. import { BLE_STATE } from '@/utils/ble/constants.js'
  67. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  68. export default {
  69. components: {
  70. ayToast
  71. },
  72. data() {
  73. return {
  74. statusBarHeight: 44,
  75. name: '',
  76. deviceId: '',
  77. rssi: '',
  78. connected: false,
  79. connecting: false,
  80. firmwareVersion: '',
  81. _unbinders: []
  82. }
  83. },
  84. computed: {
  85. rssiText() {
  86. console.log("信号强度",this.rssi)
  87. if (!this.rssi) return '未知'
  88. const val = Number(this.rssi)
  89. if (val >= -50) return '非常强'
  90. if (val >= -65) return '强'
  91. if (val >= -80) return '中'
  92. return '弱'
  93. },
  94. signalLevel() {
  95. if (!this.rssi) return 0
  96. const val = Number(this.rssi)
  97. if (val >= -50) return 4
  98. if (val >= -65) return 3
  99. if (val >= -80) return 2
  100. if (val >= -95) return 1
  101. return 1
  102. }
  103. },
  104. onLoad(options) {
  105. const sysInfo = uni.getSystemInfoSync()
  106. this.statusBarHeight = sysInfo.statusBarHeight || 44
  107. this.name = decodeURIComponent(options.name || '')
  108. this.deviceId = decodeURIComponent(options.deviceId || '')
  109. this.rssi = decodeURIComponent(options.rssi || '')
  110. },
  111. async onShow() {
  112. const bleStore = useBleStore()
  113. // 用bleState精确判断连接状态
  114. this.connected = (bleStore.bleState === BLE_STATE.READY_COMM)
  115. console.log("连接状态", this.connected, "bleState:", bleStore.bleState)
  116. // 已连接时获取实时RSSI(使用store中实际连接的deviceId)
  117. const connectedId = bleStore.device && bleStore.device.deviceId
  118. if (this.connected && connectedId) {
  119. console.log("获取RSSI使用deviceId:", connectedId, "页面deviceId:", this.deviceId)
  120. uni.getBLEDeviceRSSI({
  121. deviceId: connectedId,
  122. success: (res) => {
  123. console.log("实时信号强度", res.RSSI)
  124. this.rssi = res.RSSI
  125. },
  126. fail: (err) => {
  127. console.log("获取RSSI失败", err)
  128. // 连接实际已断开,同步状态
  129. if (err.code === 10004) {
  130. this.connected = false
  131. }
  132. }
  133. })
  134. }
  135. // 监听store状态变化(使用$watch)
  136. this._stopWatch = this.$watch(
  137. () => bleStore.bleState,
  138. (s) => {
  139. this.connected = (s === BLE_STATE.READY_COMM)
  140. if (s === BLE_STATE.DISCONNECTED) {
  141. this.connecting = false
  142. }
  143. }
  144. )
  145. },
  146. onHide() {
  147. this._cleanListeners()
  148. },
  149. onUnload() {
  150. this._cleanListeners()
  151. },
  152. methods: {
  153. goBack() {
  154. uni.navigateBack()
  155. },
  156. _cleanListeners() {
  157. if (this._stopWatch) {
  158. this._stopWatch()
  159. this._stopWatch = null
  160. }
  161. },
  162. async connectDevice() {
  163. if (this.connecting || this.connected) return
  164. this.connecting = true
  165. const bleStore = useBleStore()
  166. try {
  167. await bleStore.init()
  168. await bleStore.connectDevice(this.deviceId)
  169. this.connected = true
  170. this.$refs.ayToast.success('连接成功')
  171. } catch (e) {
  172. this.$refs.ayToast.error('连接失败: ' + (e.message || ''))
  173. } finally {
  174. this.connecting = false
  175. }
  176. },
  177. async disconnectDevice() {
  178. const bleStore = useBleStore()
  179. try {
  180. await bleStore.disconnect()
  181. this.connected = false
  182. this.$refs.ayToast.success('已断开连接')
  183. } catch (e) {
  184. this.$refs.ayToast.error('断开失败')
  185. }
  186. },
  187. delDevice() {
  188. const bleStore = useBleStore()
  189. uni.showModal({
  190. title: '提示',
  191. content: '此操作将删除该设备,是否继续?',
  192. cancelText: '取消',
  193. confirmText: '继续',
  194. success: (res) => {
  195. if (res.confirm) {
  196. if (this.connected) {
  197. bleStore.disconnect().catch(() => {})
  198. }
  199. // 从本地存储中移除
  200. try {
  201. let deviceList = uni.getStorageSync('deviceList') || []
  202. deviceList = deviceList.filter(item => item.deviceId !== this.deviceId)
  203. uni.setStorageSync('deviceList', deviceList)
  204. } catch (e) {}
  205. this.$refs.ayToast.success('设备删除成功')
  206. setTimeout(() => {
  207. uni.navigateBack()
  208. }, 1500)
  209. }
  210. }
  211. })
  212. }
  213. }
  214. }
  215. </script>
  216. <style>
  217. .container {
  218. flex: 1;
  219. background-color: #FFFFFF;
  220. }
  221. .status-bar {
  222. background-color: #FFFFFF;
  223. }
  224. .customHead {
  225. width: 750rpx;
  226. background-color: #FFFFFF;
  227. flex-direction: row;
  228. justify-content: space-between;
  229. height: 88rpx;
  230. align-items: center;
  231. padding-left: 54rpx;
  232. padding-right: 54rpx;
  233. }
  234. .back-wrap {
  235. width: 50rpx;
  236. height: 50rpx;
  237. justify-content: center;
  238. align-items: center;
  239. }
  240. .backimg {
  241. width: 50rpx;
  242. height: 50rpx;
  243. }
  244. .head-title {
  245. font-weight: bold;
  246. font-size: 32rpx;
  247. color: #545F71;
  248. }
  249. .plus {
  250. width: 50rpx;
  251. height: 50rpx;
  252. }
  253. .list {
  254. flex: 1;
  255. }
  256. .connect-status {
  257. flex-direction: row;
  258. align-items: center;
  259. margin-left: 38rpx;
  260. margin-right: 38rpx;
  261. margin-top: 30rpx;
  262. margin-bottom: 10rpx;
  263. padding-top: 24rpx;
  264. padding-bottom: 24rpx;
  265. padding-left: 30rpx;
  266. padding-right: 30rpx;
  267. background-color: #F8F9FB;
  268. border-radius: 12rpx;
  269. }
  270. .status-dot {
  271. width: 16rpx;
  272. height: 16rpx;
  273. border-radius: 16rpx;
  274. background-color: #CCCCCC;
  275. margin-right: 12rpx;
  276. }
  277. .status-text {
  278. font-size: 28rpx;
  279. color: #545F71;
  280. flex: 1;
  281. }
  282. .connect-btn {
  283. font-size: 26rpx;
  284. color: #389588;
  285. padding-top: 8rpx;
  286. padding-bottom: 8rpx;
  287. padding-left: 24rpx;
  288. padding-right: 24rpx;
  289. border-width: 1px;
  290. border-style: solid;
  291. border-color: #389588;
  292. border-radius: 24rpx;
  293. }
  294. .txt {
  295. margin-top: 50rpx;
  296. margin-left: 38rpx;
  297. margin-right: 38rpx;
  298. font-size: 28rpx;
  299. color: #9BA5B7;
  300. }
  301. .lastItem {
  302. border-bottom-width: 0;
  303. }
  304. .menuitem {
  305. margin-left: 38rpx;
  306. margin-right: 38rpx;
  307. flex-direction: row;
  308. align-items: center;
  309. justify-content: space-between;
  310. height: 104rpx;
  311. font-size: 32rpx;
  312. color: #545F71;
  313. border-bottom-width: 1px;
  314. border-bottom-style: solid;
  315. border-bottom-color: #EEF1F4;
  316. }
  317. .gray {
  318. font-size: 28rpx;
  319. color: #9BA5B7;
  320. padding-right: 20rpx;
  321. }
  322. .device-id-text {
  323. font-size: 22rpx;
  324. }
  325. .signal-value-wrap {
  326. flex-direction: row;
  327. align-items: center;
  328. padding-right: 20rpx;
  329. }
  330. .signal-wrap {
  331. flex-direction: row;
  332. align-items: flex-end;
  333. margin-right: 12rpx;
  334. height: 38rpx;
  335. padding-bottom: 4rpx;
  336. }
  337. .signal-bar {
  338. width: 8rpx;
  339. margin-left: 4rpx;
  340. border-radius: 4rpx;
  341. background-color: #ddd;
  342. }
  343. .signal-bar1 {
  344. height: 14rpx;
  345. }
  346. .signal-bar2 {
  347. height: 22rpx;
  348. }
  349. .signal-bar3 {
  350. height: 30rpx;
  351. }
  352. .signal-bar4 {
  353. height: 38rpx;
  354. }
  355. .signal-active {
  356. background-color: #389588;
  357. }
  358. .arrowright {
  359. width: 40rpx;
  360. height: 40rpx;
  361. }
  362. .delbtn {
  363. width: 654rpx;
  364. height: 96rpx;
  365. background-color: #F3F3F3;
  366. border-radius: 12rpx;
  367. margin-top: 60rpx;
  368. margin-left: 48rpx;
  369. margin-right: 48rpx;
  370. justify-content: center;
  371. align-items: center;
  372. }
  373. .delbtn-text {
  374. font-size: 32rpx;
  375. color: #F65252;
  376. }
  377. </style>