search.nvue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. <scroll-view class="device-scroll" scroll-y="true">
  20. <view class="device-item" v-for="(item, index) in deviceList" :key="index" @click="onSelectDevice(item)">
  21. <view class="device-info">
  22. <text class="device-name">{{item.name || '未知设备'}}</text>
  23. <text class="device-id">{{item.deviceId}}</text>
  24. </view>
  25. <view class="device-right">
  26. <!-- 信号强度图标 -->
  27. <view class="signal-wrap">
  28. <view class="signal-bar signal-bar1" :class="{'signal-active': getSignalLevel(item.RSSI) >= 1}"></view>
  29. <view class="signal-bar signal-bar2" :class="{'signal-active': getSignalLevel(item.RSSI) >= 2}"></view>
  30. <view class="signal-bar signal-bar3" :class="{'signal-active': getSignalLevel(item.RSSI) >= 3}"></view>
  31. <view class="signal-bar signal-bar4" :class="{'signal-active': getSignalLevel(item.RSSI) >= 4}"></view>
  32. </view>
  33. <text class="connect-btn">添加</text>
  34. </view>
  35. </view>
  36. <view class="empty-tip" v-if="!searching && deviceList.length === 0">
  37. <text class="empty-text">未发现设备,请确认设备已开启</text>
  38. </view>
  39. </scroll-view>
  40. <ay-toast ref="ayToast" />
  41. </view>
  42. </template>
  43. <script>
  44. import bleManager, {
  45. BLE_STATE, ensureBlePrerequisite, openLocationSettings
  46. } from '@/utils/ble'
  47. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  48. import { bindDevice } from '@/utils/api/device'
  49. export default {
  50. components: {
  51. ayToast
  52. },
  53. data() {
  54. return {
  55. statusBarHeight: 44,
  56. searching: false,
  57. deviceList: [],
  58. _unbinders: []
  59. }
  60. },
  61. onLoad() {
  62. const sysInfo = uni.getSystemInfoSync()
  63. this.statusBarHeight = sysInfo.statusBarHeight || 44
  64. // 配置并监听事件
  65. bleManager.configure({ debug: true })
  66. this._unbinders = [
  67. bleManager.on('state', s => {
  68. this.searching = (s === BLE_STATE.SCANNING)
  69. }),
  70. bleManager.on('deviceFound', d => {
  71. // 避免重复
  72. const exists = this.deviceList.find(item => item.deviceId === d.deviceId)
  73. if (!exists) {
  74. this.deviceList.push({
  75. deviceId: d.deviceId,
  76. name: d.name || d.localName || '',
  77. RSSI: d.RSSI || ''
  78. })
  79. }
  80. })
  81. ]
  82. this.startScan()
  83. },
  84. onUnload() {
  85. // 离开页面停止扫描
  86. this.stopScan()
  87. if (this._unbinders && this._unbinders.length) {
  88. this._unbinders.forEach(fn => fn && fn())
  89. }
  90. },
  91. methods: {
  92. goBack() {
  93. uni.navigateBack()
  94. },
  95. async startScan() {
  96. this.deviceList = []
  97. try {
  98. await ensureBlePrerequisite()
  99. await bleManager.init()
  100. // 使用returnAll模式,超时后resolve所有设备
  101. bleManager.scan({ timeout: 15000, returnAll: true }).then(devices => {
  102. // 扫描结束
  103. this.searching = false
  104. }).catch(e => {
  105. this.searching = false
  106. console.error('扫描异常', e)
  107. })
  108. } catch (e) {
  109. this.searching = false
  110. if (e.message === 'BLE_LOCATION_OFF') {
  111. uni.showModal({
  112. title: '提示',
  113. content: '请先开启位置服务以搜索蓝牙设备',
  114. success: r => {
  115. if (r.confirm) openLocationSettings()
  116. }
  117. })
  118. } else if (e.message === 'BLE_ADAPTER_OFF') {
  119. this.$refs.ayToast.error('请先开启蓝牙')
  120. } else if (e.message === 'BLE_PERMISSION_DENIED') {
  121. this.$refs.ayToast.error('请授权蓝牙权限')
  122. } else {
  123. this.$refs.ayToast.error('扫描失败: ' + (e.message || ''))
  124. }
  125. }
  126. },
  127. stopScan() {
  128. try {
  129. uni.stopBluetoothDevicesDiscovery({
  130. success() {},
  131. fail() {}
  132. })
  133. } catch (e) {}
  134. },
  135. onRefresh() {
  136. if (this.searching) {
  137. this.stopScan()
  138. this.searching = false
  139. } else {
  140. this.startScan()
  141. }
  142. },
  143. getSignalLevel(rssi) {
  144. // RSSI转信号格数: 4格(强) / 3格 / 2格 / 1格(弱)
  145. if (!rssi) return 0
  146. const val = Number(rssi)
  147. if (val >= -50) return 4 // 非常强
  148. if (val >= -65) return 3 // 强
  149. if (val >= -80) return 2 // 中
  150. if (val >= -95) return 1 // 弱
  151. return 1
  152. },
  153. async onSelectDevice(device) {
  154. const deviceCode = device.name || device.localName || device.deviceId
  155. if (!deviceCode) {
  156. this.$refs.ayToast.error('未识别到设备编号')
  157. return
  158. }
  159. let bindResult
  160. try {
  161. bindResult = await bindDevice({ deviceCode })
  162. } catch (e) {
  163. console.error('绑定设备失败', e)
  164. return
  165. }
  166. let deviceList = []
  167. try {
  168. const data = uni.getStorageSync('deviceList')
  169. if (data && data.length > 0) {
  170. deviceList = data
  171. }
  172. } catch (e) {}
  173. // 检查是否已添加
  174. const exists = deviceList.find(item =>
  175. item.deviceId === device.deviceId || item.deviceCode === bindResult.deviceCode
  176. )
  177. if (exists) {
  178. this.$refs.ayToast.show('该设备已添加')
  179. return
  180. }
  181. const newDevice = {
  182. deviceId: device.deviceId,
  183. bleDeviceName: device.name || '',
  184. name: bindResult.deviceName || device.name || '艾灸椅',
  185. deviceCode: bindResult.deviceCode || deviceCode,
  186. deviceType: bindResult.deviceType || 1,
  187. groupId: bindResult.groupId || null,
  188. profileId: bindResult.profileId || null,
  189. profileComplete: !!bindResult.profileComplete,
  190. profile: bindResult.profile || null,
  191. RSSI: device.RSSI
  192. }
  193. deviceList.push(newDevice)
  194. uni.setStorageSync('deviceList', deviceList)
  195. uni.setStorageSync('current_device', newDevice)
  196. if (bindResult.profile) {
  197. uni.setStorageSync('current_manage_user_id', bindResult.profile.profileId || bindResult.profile.id)
  198. uni.setStorageSync('current_manage_user', bindResult.profile)
  199. }
  200. this.$refs.ayToast.success('设备添加成功')
  201. // 延迟返回
  202. setTimeout(() => {
  203. uni.navigateBack()
  204. }, 1000)
  205. }
  206. }
  207. }
  208. </script>
  209. <style>
  210. .page {
  211. flex: 1;
  212. position: relative;
  213. }
  214. .content-bg {
  215. position: absolute;
  216. left: 0;
  217. top: 0;
  218. width: 750rpx;
  219. height: 2000px;
  220. }
  221. .status-bar {
  222. background-color: rgba(0,0,0,0);
  223. }
  224. .custom-head {
  225. flex-direction: row;
  226. justify-content: space-between;
  227. align-items: center;
  228. height: 88rpx;
  229. padding-left: 30rpx;
  230. padding-right: 54rpx;
  231. }
  232. .head-back {
  233. width: 60rpx;
  234. height: 60rpx;
  235. justify-content: center;
  236. align-items: center;
  237. }
  238. .back-icon {
  239. width: 60rpx;
  240. height: 60rpx;
  241. }
  242. .head-title {
  243. font-weight: bold;
  244. font-size: 32rpx;
  245. color: #545F71;
  246. }
  247. .head-placeholder {
  248. width: 60rpx;
  249. height: 60rpx;
  250. }
  251. .search-tip {
  252. flex-direction: row;
  253. justify-content: space-between;
  254. align-items: center;
  255. padding-left: 38rpx;
  256. padding-right: 38rpx;
  257. padding-top: 20rpx;
  258. padding-bottom: 20rpx;
  259. }
  260. .search-tip-text {
  261. font-size: 26rpx;
  262. color: #999;
  263. }
  264. .search-btn {
  265. font-size: 26rpx;
  266. color: #389588;
  267. padding: 10rpx 24rpx;
  268. border-width: 1px;
  269. border-style: solid;
  270. border-color: #389588;
  271. border-radius: 24rpx;
  272. }
  273. .device-scroll {
  274. flex: 1;
  275. padding-left: 38rpx;
  276. padding-right: 38rpx;
  277. }
  278. .device-item {
  279. flex-direction: row;
  280. justify-content: space-between;
  281. align-items: center;
  282. background-color: #FFFFFF;
  283. border-radius: 16rpx;
  284. padding: 30rpx;
  285. margin-bottom: 20rpx;
  286. }
  287. .device-info {
  288. flex: 1;
  289. }
  290. .device-name {
  291. font-size: 30rpx;
  292. color: #333;
  293. font-weight: bold;
  294. }
  295. .device-id {
  296. font-size: 22rpx;
  297. color: #999;
  298. margin-top: 8rpx;
  299. }
  300. .device-right {
  301. align-items: center;
  302. }
  303. .signal-wrap {
  304. flex-direction: row;
  305. align-items: flex-end;
  306. margin-bottom: 12rpx;
  307. }
  308. .signal-bar {
  309. width: 8rpx;
  310. margin-left: 4rpx;
  311. border-radius: 4rpx;
  312. background-color: #ddd;
  313. }
  314. .signal-bar1 {
  315. height: 14rpx;
  316. }
  317. .signal-bar2 {
  318. height: 22rpx;
  319. }
  320. .signal-bar3 {
  321. height: 30rpx;
  322. }
  323. .signal-bar4 {
  324. height: 38rpx;
  325. }
  326. .signal-active {
  327. background-color: #389588;
  328. }
  329. .connect-btn {
  330. font-size: 26rpx;
  331. color: #FFFFFF;
  332. background-color: #389588;
  333. padding-left: 24rpx;
  334. padding-right: 24rpx;
  335. padding-top: 12rpx;
  336. padding-bottom: 12rpx;
  337. border-radius: 20rpx;
  338. }
  339. .empty-tip {
  340. align-items: center;
  341. padding-top: 100rpx;
  342. }
  343. .empty-text {
  344. font-size: 28rpx;
  345. color: #999;
  346. }
  347. </style>