fitWatchAll.vue 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. <template>
  2. <view class="page">
  3. <view class="nav-bar" :style="{ paddingTop: statusBarHeight}">
  4. <view class="nav-back" @tap="goBack">
  5. <!-- <text class="back-text">‹ 返回</text> -->
  6. <image class="back-text" src="@/static/images/pages_watch/icons/back_arrow_icon24.png"></image>
  7. </view>
  8. <view class="nav-title">FitCloud智能手表</view>
  9. <image class="next-text" src="@/static/images/pages_watch/icons/more_icon24.png"></image>
  10. </view>
  11. <view class="content">
  12. <view class="section-header">我的设备</view>
  13. <view class="empty-container" v-if="!loading&&xhsDeviceList&&xhsDeviceList.length==0">
  14. <image class="empty-img" src="/static/images/empty.png" mode="aspectFit"></image>
  15. <view class="es-fs-28 es-c-99 es-mt-20">暂无设备</view>
  16. <button class="btn btn-primary es-mt-40" @click="goScan" style="width: 280rpx;">添加设备</button>
  17. </view>
  18. <view v-show="loading">
  19. <u-loading-icon text="加载中" textSize="16" :vertical="true"></u-loading-icon>
  20. </view>
  21. <view class="status-card" :class="{ connected: item.newXhsDeviceId == connectedDeviceMac }"
  22. v-for="(item,index) in xhsDeviceList" :key="index">
  23. <image class="deviceimg" src="/static/images/pages_watch/icons/smartwatch_img.png" mode="aspectFit">
  24. </image>
  25. <view class="x-f flex">
  26. <view class="flex">
  27. <view class="device-name">{{item.newXhsDeviceId}}</view>
  28. <view class="status-row">
  29. <!-- <text class="status-label ">连接状态</text> -->
  30. <text class="status-value"
  31. :class="item.newXhsDeviceId === connectedDeviceMac ? (connectionState || '').toLowerCase() : (item.connectedDeviceMac || '').toLowerCase()">
  32. {{ connectionText(item) }}
  33. </text>
  34. </view>
  35. </view>
  36. <button class="device-connect" :disabled="disconnecting" :loading="disconnecting" @tap="unbindDevice(item)">解绑</button>
  37. <button class="device-connect" @tap="connectDevice(item)">连接</button>
  38. </view>
  39. </view>
  40. <!-- connectionState === 'CONNECTED' -->
  41. <view class="panel" v-if="connectedDeviceMac" style="margin-top: 24rpx;">
  42. <view class="panel-title x-bc">
  43. <text>实时健康数据</text>
  44. <text style="color: #07c160;" v-show="dataSync=='success'">同步成功</text>
  45. <button class="device-item-btn" v-show="dataSync!='success'" :disabled="dataSync=== 'loading'"
  46. :loading="dataSync=== 'loading'" @click="xhsDataAdd(connectedDeviceMac,1)">
  47. {{ dataSync=== 'loading' ? '同步中' : dataSync === 'error'? '重新同步': '同步成功'}}
  48. </button>
  49. </view>
  50. <view class="health-monitoring">
  51. <view class="health-monitoring-item">
  52. <view class="health-monitoring-title">
  53. <view>
  54. <view class="health-monitoring-maintitle">心率</view>
  55. <view>心脏健康管理</view>
  56. </view>
  57. <image src="/static/images/pages_watch/icons/heart_rate_icon.png" mode="aspectFill"></image>
  58. </view>
  59. <view class="health-monitoring-res resnum">{{ healthData.heartRate}}</view>
  60. </view>
  61. <view class="health-monitoring-item">
  62. <view class="health-monitoring-title">
  63. <view>
  64. <view class="health-monitoring-maintitle">血氧</view>
  65. <view>血氧风险管控</view>
  66. </view>
  67. <image src="/static/images/pages_watch/icons/blood_oxygen_icon.png" mode="aspectFill">
  68. </image>
  69. </view>
  70. <view class="health-monitoring-res resnum">{{ healthData.oxygen}}</view>
  71. </view>
  72. <view class="health-monitoring-item">
  73. <view class="health-monitoring-title">
  74. <view>
  75. <view class="health-monitoring-maintitle">血压</view>
  76. <view>血压健康监测</view>
  77. </view>
  78. <image src="/static/images/pages_watch/icons/blood_pressure_icon.png" mode="aspectFill"></image>
  79. </view>
  80. <view class="health-monitoring-res resnum">
  81. {{ healthData.systolicPressure }}/{{ healthData.diastolicPressure }}</view>
  82. </view>
  83. </view>
  84. <template v-if="connectionState == 'CONNECTED'">
  85. <view class="btn-row">
  86. <button class="btn btn-primary" @click="startRealTimeData" :disabled="isMeasuring">开始测量</button>
  87. <button class="btn btn-danger" @click="stopRealTimeData" :disabled="!isMeasuring">停止测量</button>
  88. </view>
  89. <view class="btn-row">
  90. <button class="btn btn-default" @click="disconnect">断开连接</button>
  91. <!-- <button class="btn btn-danger-outline" @click="unbindDevice">解绑设备</button> -->
  92. </view>
  93. </template>
  94. </view>
  95. <!-- connectionState === 'CONNECTED' v-if="connectedDeviceMac"-->
  96. <view class="panel es-mt-20" v-if="connectedDeviceMac">
  97. <view class="panel-title x-bc">
  98. <text>其他数据</text>
  99. <text style="color: #07c160;" v-show="dataOtherSync=='success'&&!isSyncing">同步成功</text>
  100. <button class="device-item-btn" v-show="dataOtherSync!='success'" :disabled="dataOtherSync=== 'loading'"
  101. :loading="dataOtherSync=== 'loading'" @click="syncData()">
  102. {{ dataOtherSync=== 'loading' ? '同步中' : dataOtherSync === 'error'||dataOtherSync === 'errorSync'? '重新同步': '同步成功'}}
  103. </button>
  104. </view>
  105. <view class="history-section" v-if="sleepDataList && sleepDataList.length > 0">
  106. <view class="section-title">睡眠记录 (最新{{ sleepDataList.length }}条)</view>
  107. <scroll-view scroll-y class="history-list">
  108. <view class="history-item" v-for="(item, index) in sleepDataList" :key="index">
  109. <view class="history-header">
  110. <text class="date">{{ formatTime(item.timestamp) }}</text>
  111. <text class="count">{{ item.items ? item.items.length : 0 }}段</text>
  112. </view>
  113. <view class="history-detail" v-if="item.items && item.items.length > 0">
  114. <view class="detail-row" v-for="(detail, dIndex) in item.items" :key="dIndex">
  115. <text class="time">{{ formatTimeOnly(detail.startTime) }} - {{ formatTimeOnly(detail.endTime) }}</text>
  116. <text class="status" :class="'status-' + detail.status">
  117. {{ getSleepStatus(detail.status) }}
  118. </text>
  119. </view>
  120. </view>
  121. </view>
  122. </scroll-view>
  123. </view>
  124. <view class="empty-tip" v-else>暂无睡眠数据</view>
  125. <!-- 运动数据展示 -->
  126. <view class="history-section" v-if="sportDataList && sportDataList.length > 0">
  127. <view class="section-title">锻炼记录 (最新{{ sportDataList.length }}条)</view>
  128. <scroll-view scroll-y class="history-list">
  129. <view class="history-item" v-for="(item, index) in sportDataList" :key="index">
  130. <view class="history-header">
  131. <text class="date">{{ formatTime(item.timestamp) }}</text>
  132. <text class="count">类型: {{ item.sportType }}</text>
  133. </view>
  134. <view class="history-detail">
  135. <view class="detail-row">
  136. <text class="time">时长: {{ item.duration }}秒</text>
  137. <text class="status">消耗: {{ item.calories }}千卡</text>
  138. </view>
  139. <view class="detail-row">
  140. <text class="time">步数: {{ item.steps }}步</text>
  141. <text class="status">距离: {{ item.distance }}公里</text>
  142. </view>
  143. </view>
  144. </view>
  145. </scroll-view>
  146. </view>
  147. <view class="empty-tip" v-else>暂无锻炼记录</view>
  148. <!-- 计步数据展示 -->
  149. <view class="history-section" v-if="stepDataList && stepDataList.length > 0">
  150. <view class="section-title">计步记录 (最新{{ stepDataList.length }}条)</view>
  151. <scroll-view scroll-y class="history-list">
  152. <view class="history-item" v-for="(item, index) in stepDataList" :key="index">
  153. <view class="history-header">
  154. <text class="date">{{ formatTime(item.timestamp) }}</text>
  155. </view>
  156. <view class="history-detail">
  157. <view class="detail-row">
  158. <text class="time">步数: {{ item.steps }}步</text>
  159. <text class="status">消耗: {{ item.calories }}千卡</text>
  160. </view>
  161. <view class="detail-row">
  162. <text class="time">距离: {{ item.distance }}公里</text>
  163. </view>
  164. </view>
  165. </view>
  166. </scroll-view>
  167. </view>
  168. <view class="empty-tip" v-else>暂无计步记录</view>
  169. </view>
  170. <!-- v-if="!connectionState || connectionState == 'DISCONNECTED'" -->
  171. <template v-if="false">
  172. <!-- <view class="section-header">设备扫描</view> -->
  173. <button class="es-w-200 es-h-88" v-show="!isFamily"
  174. @click="editXHSDevice('EF:00:11:11:8F:05')">绑定</button>
  175. <button class="es-w-200 es-h-88" v-show="isFamily"
  176. @click="editMyfamily('EF:00:11:11:8F:05')">家人绑定</button>
  177. <button class="es-w-200 es-h-88" @click="xhsDataAddTest('EF:00:11:11:8F:05')">记录</button>
  178. <!-- <view class="btn-row">
  179. <button class="btn btn-primary" @click="startScan" :loading="isScanning">扫描设备</button>
  180. <button class="btn btn-default" @click="stopScan" :disabled="!isScanning">停止扫描</button>
  181. </view> -->
  182. <view class="panel" v-if="deviceList&&deviceList.length>0">
  183. <view class="list-title">发现的设备({{ deviceList.length }})</view>
  184. <view class="device-list">
  185. <scroll-view scroll-y="true" class="scroll-y">
  186. <view class="device-item" v-for="(item, index) in deviceList" :key="index">
  187. <view class="device-info">
  188. <text class="device-name">{{ item.name || '未知设备' }}</text>
  189. <view>
  190. <text class="device-mac">{{ item.mac }}</text>
  191. </view>
  192. </view>
  193. <!-- 带连接状态的按钮 -->
  194. <view
  195. @click="connectDevice(item)"
  196. class="device-connectbox"
  197. :style="{opacity: item.mac === connectedDeviceMac && connectionState === 'CONNECTING' ? 0.6 : 1}"
  198. >
  199. <!-- 状态判断 -->
  200. <text
  201. class="device-connect"
  202. :class="{
  203. connected: item.mac === connectedDeviceMac && connectionState === 'CONNECTED',
  204. connecting: item.mac === connectedDeviceMac && connectionState === 'CONNECTING'
  205. }"
  206. >
  207. {{
  208. item.mac === connectedDeviceMac
  209. ? (connectionState === 'CONNECTED' ? '已连接' : connectionState === 'CONNECTING' ? '连接中...' : '连接')
  210. : '连接'
  211. }}
  212. </text>
  213. </view>
  214. </view>
  215. </scroll-view>
  216. <view class="empty-tip" v-if="deviceList.length === 0">暂无设备</view>
  217. </view>
  218. </view>
  219. </template>
  220. </view>
  221. </view>
  222. </template>
  223. <script>
  224. import {
  225. getWatchUserInfo,
  226. editMyfamily
  227. } from "@/api/pages_watch/user.js";
  228. import {
  229. editXHSDevice,
  230. xhsDataAddList,
  231. removeXHSDevice,
  232. getLastData
  233. } from "@/api/pages_watch/device.js"
  234. // 引入原生插件
  235. const fitCloudWatch = uni.requireNativePlugin('fitCloudWatch');
  236. export default {
  237. data() {
  238. return {
  239. disconnecting: false,
  240. isScanning: false,
  241. deviceList: [],
  242. connectionState: 'DISCONNECTED', // CONNECTED, DISCONNECTED, CONNECTING
  243. connectedDeviceMac: '',
  244. isMeasuring: false,
  245. isAutoConnecting: false, // 避免收到原生旧状态干扰的标识
  246. healthData: {
  247. heartRate: 0,
  248. oxygen: 0,
  249. diastolicPressure: 0,
  250. systolicPressure: 0,
  251. respiratoryRate: 0
  252. },
  253. isSyncing: false,
  254. sleepDataList: [],
  255. sportDataList: [],
  256. stepDataList: [],
  257. statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
  258. xhsDeviceList: [],
  259. loading: true,
  260. fitCloudWatchUser: {},
  261. deviceType: 2, //0腕表1小护士2新表
  262. isFamily: false,
  263. selectUser: 0,
  264. boundDevice: null,
  265. dataSync: 'success',
  266. dataSyncFunInfo: "",
  267. dataOtherSync: 'success',
  268. dataOtherSyncFunInfo: "",
  269. otherDevice: []
  270. }
  271. },
  272. computed: {
  273. connectionText() {
  274. const map = {
  275. 'CONNECTED': '已连接',
  276. 'DISCONNECTED': '未连接',
  277. 'CONNECTING': '连接中...'
  278. };
  279. return (item) => {
  280. return item.newXhsDeviceId == this.connectedDeviceMac ? map[item.connectionState] : map[this.connectionState];
  281. }
  282. }
  283. },
  284. watch: {
  285. connectionState(newVal) {
  286. this.updateXhsDeviceStatus();
  287. },
  288. connectedDeviceMac: {
  289. handler(newVal) {
  290. // if (newVal) {
  291. // getLastData(newVal)
  292. // }
  293. this.updateXhsDeviceStatus();
  294. },
  295. immediate: true
  296. },
  297. },
  298. onLoad(option) {
  299. this.selectUser = Number(option.selectUser || 0)
  300. this.isFamily = option.selectUser != '0';
  301. this.getUser()
  302. const sys = uni.getSystemInfoSync();
  303. },
  304. onShow() {
  305. // 页面显示时注册事件和检查状态,保证从后台切回或从其他页面返回时都能自动重连
  306. this.registerGlobalEvents();
  307. if(this.xhsDeviceList&&this.xhsDeviceList.length>0) {
  308. this.checkCurrentConnectionState();
  309. }
  310. // this.checkCurrentConnectionState();
  311. },
  312. onHide() {
  313. // 页面隐藏时清理事件,避免多实例问题
  314. this.removeGlobalEvents();
  315. },
  316. onUnload() {
  317. // 页面卸载时停止扫描和测量,防止内存泄漏
  318. if (this.isScanning) {
  319. this.stopScan();
  320. }
  321. if (this.isMeasuring) {
  322. this.stopRealTimeData();
  323. }
  324. this.removeGlobalEvents();
  325. },
  326. methods: {
  327. goScan() {
  328. uni.navigateTo({
  329. url: '/pages_bluetooth/scanFitWatch?selectUser='+this.selectUser
  330. })
  331. },
  332. // 检查当前连接状态
  333. checkCurrentConnectionState() {
  334. if (!fitCloudWatch) return;
  335. try {
  336. fitCloudWatch.getConnectionState({}, (res) => {
  337. console.log(type,'当前连接状态:', res);
  338. if (res.state === 'CONNECTED' && res.mac) {
  339. // 原生层已经连接,直接恢复 UI 状态
  340. // 校验:缓存设备是否在当前列表中
  341. const isDeviceValid = res.mac && this.xhsDeviceList.some(
  342. item => item.newXhsDeviceId == res.mac
  343. );
  344. if(isDeviceValid) {
  345. this.connectionState = 'CONNECTED';
  346. this.connectedDeviceMac = res.mac;
  347. // uni.showToast({ title: '已恢复连接状态', icon: 'none' });
  348. } else {
  349. // // 校验:缓存设备是否在当前列表中
  350. // this.disconnect()
  351. this.checkAutoConnect();
  352. }
  353. } else {
  354. // 原生层未连接,尝试自动连接已绑定的设备
  355. this.checkAutoConnect();
  356. }
  357. });
  358. } catch (e) {
  359. console.error('调用 getConnectionState 失败:', e);
  360. // 降级处理,直接尝试自动连接
  361. this.checkAutoConnect();
  362. }
  363. },
  364. updateXhsDeviceStatus() {
  365. this.xhsDeviceList.forEach((item, index) => {
  366. if (item.newXhsDeviceId === this.connectedDeviceMac) {
  367. this.$set(this.xhsDeviceList[index], 'connectionState', this.connectionState);
  368. } else {
  369. this.$set(this.xhsDeviceList[index], 'connectionState', '');
  370. }
  371. });
  372. },
  373. goBack() {
  374. uni.navigateBack({
  375. delta: 1
  376. });
  377. },
  378. // 注册插件的全局事件监听
  379. registerGlobalEvents() {
  380. // 先移除旧的监听,防止重复注册
  381. this.removeGlobalEvents();
  382. // 监听扫描到的设备
  383. plus.globalEvent.addEventListener('onFitCloudDeviceFound', this.onDeviceFound);
  384. // 监听连接状态变化
  385. plus.globalEvent.addEventListener('onFitCloudConnectionStateChanged', this.onConnectionStateChanged);
  386. // 监听连接错误
  387. plus.globalEvent.addEventListener('onFitCloudConnectionError', this.onConnectionError);
  388. // 监听实时健康数据
  389. plus.globalEvent.addEventListener('onFitCloudRealTimeData', this.onRealTimeData);
  390. // 监听实时测量错误
  391. plus.globalEvent.addEventListener('onFitCloudRealTimeDataError', this.onRealTimeDataError);
  392. // 监听睡眠数据同步
  393. plus.globalEvent.addEventListener('onFitCloudSleepData', this.onSleepData);
  394. // 监听运动数据同步
  395. plus.globalEvent.addEventListener('onFitCloudSportData', this.onSportData);
  396. // 监听计步数据同步
  397. plus.globalEvent.addEventListener('onFitCloudStepData', this.onStepData);
  398. },
  399. removeGlobalEvents() {
  400. plus.globalEvent.removeEventListener('onFitCloudDeviceFound', this.onDeviceFound);
  401. plus.globalEvent.removeEventListener('onFitCloudConnectionStateChanged', this.onConnectionStateChanged);
  402. plus.globalEvent.removeEventListener('onFitCloudConnectionError', this.onConnectionError);
  403. plus.globalEvent.removeEventListener('onFitCloudRealTimeData', this.onRealTimeData);
  404. plus.globalEvent.removeEventListener('onFitCloudRealTimeDataError', this.onRealTimeDataError);
  405. plus.globalEvent.removeEventListener('onFitCloudSleepData', this.onSleepData);
  406. plus.globalEvent.removeEventListener('onFitCloudSportData', this.onSportData);
  407. plus.globalEvent.removeEventListener('onFitCloudStepData', this.onStepData);
  408. },
  409. onDeviceFound(device) {
  410. console.log('扫描到设备:', JSON.stringify(device));
  411. // 原生层已经做好了 MAC 和 FLink 的强过滤,前端直接展示即可
  412. const index = this.deviceList.findIndex(item => item.mac === device.mac);
  413. if (index === -1) {
  414. this.deviceList.push(device);
  415. } else {
  416. // 更新信号强度
  417. this.$set(this.deviceList, index, device);
  418. }
  419. },
  420. onConnectionStateChanged(res) {
  421. console.log('状态改变:', res.state);
  422. if (res.state === 'DISCONNECTED') {
  423. if (this.isAutoConnecting) {
  424. // 自动连接中收到的断开状态很可能是原生的旧状态,忽略它
  425. console.log('忽略自动连接中的断开状态事件');
  426. return;
  427. }
  428. this.connectionState = res.state;
  429. uni.showToast({ title: '已断开连接', icon: 'none' });
  430. this.isMeasuring = false;
  431. } else if (res.state === 'CONNECTED') {
  432. this.connectionState = res.state;
  433. this.isAutoConnecting = false; // 连接成功,立即重置
  434. uni.showToast({ title: '连接成功', icon: 'success' });
  435. // 如果 res 里面带了 mac,更新一下
  436. if (res.mac) {
  437. this.connectedDeviceMac = res.mac;
  438. }
  439. // 连接成功后,缓存 MAC 地址以便下次自动连接
  440. if (this.connectedDeviceMac) {
  441. uni.setStorageSync('fitCloudWatch_mac', this.connectedDeviceMac);
  442. if (this.isFamily) {
  443. this.editMyfamily(this.connectedDeviceMac)
  444. } else {
  445. this.editXHSDevice(this.connectedDeviceMac)
  446. }
  447. }
  448. } else {
  449. this.connectionState = res.state;
  450. }
  451. },
  452. onConnectionError(res) {
  453. console.log('连接错误:', res);
  454. // 如果是 SDK 内部在重试,可以不弹窗打扰用户
  455. if (res.isRetry) {
  456. console.log('正在重试连接...');
  457. return;
  458. }
  459. // 只有当前不在已连接状态,或者不是重试时才提示
  460. if (this.connectionState !== 'CONNECTED') {
  461. uni.showToast({
  462. title: '连接异常: ' + (res.error || '未知错误'),
  463. icon: 'none',
  464. duration: 3000
  465. });
  466. this.connectionState = 'DISCONNECTED';
  467. this.isAutoConnecting = false;
  468. }
  469. },
  470. onRealTimeData(data) {
  471. this.healthData = {
  472. ...this.healthData,
  473. ...data
  474. };
  475. },
  476. onRealTimeDataError(error) {
  477. uni.showToast({ title: '测量异常: ' + error.error, icon: 'none' });
  478. this.isMeasuring = false;
  479. },
  480. onSleepData(res) {
  481. console.log('接收睡眠数据:', res);
  482. if (res.sleepData && res.sleepData.length > 0) {
  483. this.sleepDataList = res.sleepData;
  484. this.xhsOtherDataAdd(this.connectedDeviceMac)
  485. }
  486. },
  487. onSportData(res) {
  488. console.log('接收到运动(锻炼)数据:', res);
  489. if (res.sportData && res.sportData.length > 0) {
  490. this.sportDataList = res.sportData;
  491. this.xhsOtherDataAdd(this.connectedDeviceMac)
  492. }
  493. },
  494. onStepData(res) {
  495. console.log('接收到计步数据:', res);
  496. if (res.stepData && res.stepData.length > 0) {
  497. this.stepDataList = res.stepData;
  498. this.xhsOtherDataAdd(this.connectedDeviceMac)
  499. }
  500. },
  501. // 自动连接
  502. checkAutoConnect() {
  503. if (!fitCloudWatch) return;
  504. const savedMac = uni.getStorageSync('fitCloudWatch_mac');
  505. if (savedMac) {
  506. this.connectionState = 'CONNECTING';
  507. this.connectedDeviceMac = savedMac;
  508. // 标记正在自动连接中,避免被原生的初始状态事件(DISCONNECTED)干扰
  509. this.isAutoConnecting = true;
  510. uni.showLoading({ title: '自动连接中...' });
  511. const userId = this.fitCloudWatchUser.userId + '_' + savedMac
  512. fitCloudWatch.connect({
  513. mac: savedMac,
  514. userId: userId, // 测试用户ID
  515. isBind: true, // 测试绑定模式
  516. sex: this.fitCloudWatchUser.sex || true, // 男
  517. age: this.fitCloudWatchUser.age || 25,
  518. height: this.fitCloudWatchUser.height || 175,
  519. weight: this.fitCloudWatchUser.weight || 70
  520. }, (res) => {
  521. uni.hideLoading();
  522. if (res.code !== 0) {
  523. this.connectionState = 'DISCONNECTED';
  524. this.isAutoConnecting = false;
  525. uni.showToast({ title: '自动连接指令发送失败', icon: 'none' });
  526. } else {
  527. // 连接指令发送成功,重置标记,让状态监听正常接管
  528. setTimeout(() => {
  529. this.isAutoConnecting = false;
  530. }, 1500); // 延迟 1.5 秒重置,跳过旧状态的干扰
  531. }
  532. });
  533. }
  534. },
  535. // 1. 开始扫描
  536. async startScan() {
  537. if (!fitCloudWatch) {
  538. uni.showToast({ title: '原生插件未加载,请在自定义基座或原生App中运行', icon: 'none' });
  539. return;
  540. }
  541. // Android 平台必须动态申请定位和蓝牙权限
  542. if (plus.os.name === 'Android') {
  543. const permissions = [
  544. 'android.permission.ACCESS_FINE_LOCATION',
  545. 'android.permission.ACCESS_COARSE_LOCATION'
  546. ];
  547. // 适配 Android 12+ 蓝牙权限
  548. if (parseInt(plus.os.version) >= 12) {
  549. permissions.push('android.permission.BLUETOOTH_SCAN');
  550. permissions.push('android.permission.BLUETOOTH_CONNECT');
  551. }
  552. for (let i = 0; i < permissions.length; i++) {
  553. const result = await new Promise(resolve => {
  554. plus.android.requestPermissions([permissions[i]], (res) => {
  555. resolve(res.granted.length > 0);
  556. }, (e) => {
  557. resolve(false);
  558. });
  559. });
  560. if (!result) {
  561. uni.showToast({ title: '需要定位和蓝牙权限才能扫描设备', icon: 'none' });
  562. return;
  563. }
  564. }
  565. // 检查蓝牙是否开启
  566. const main = plus.android.runtimeMainActivity();
  567. const BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
  568. const BAdapter = BluetoothAdapter.getDefaultAdapter();
  569. if(!BAdapter.isEnabled()){
  570. uni.showModal({
  571. title: '提示',
  572. content: '请先打开蓝牙',
  573. showCancel: false
  574. });
  575. return;
  576. }
  577. }
  578. this.deviceList = [];
  579. this.isScanning = true;
  580. // 将保存的 MAC 传给原生,让原生直接过滤
  581. const savedMac = uni.getStorageSync('fitCloudWatch_mac');
  582. fitCloudWatch.startScan({ mac: savedMac || '' }, (res) => {
  583. if (res.code !== 0) {
  584. this.isScanning = false;
  585. uni.showToast({ title: '启动扫描失败: ' + res.message, icon: 'none' });
  586. }
  587. });
  588. },
  589. // 2. 停止扫描
  590. stopScan() {
  591. if (!fitCloudWatch) return;
  592. fitCloudWatch.stopScan({}, (res) => {
  593. this.isScanning = false;
  594. });
  595. },
  596. // 3. 连接设备
  597. connectDevice(device) {
  598. // 连接状态检测
  599. if (this.connectionState === 'CONNECTED') {
  600. if (this.connectedDeviceMac === device.mac) {
  601. uni.showToast({ title: '该设备已连接,无需重复连接', icon: 'none' });
  602. return;
  603. } else {
  604. uni.showToast({ title: '请先断开当前设备后再连接新设备', icon: 'none' });
  605. return;
  606. }
  607. }
  608. if (this.isScanning) {
  609. this.stopScan();
  610. }
  611. this.connectionState = 'CONNECTING';
  612. this.connectedDeviceMac = device.mac;
  613. // 判断 xhsDeviceList 中是否已存在当前设备
  614. const existIndex = this.xhsDeviceList.findIndex(item => item.newXhsDeviceId === device.mac);
  615. if (existIndex !== -1) {
  616. // 已存在 → 更新连接状态
  617. this.$set(this.xhsDeviceList[existIndex], 'connectionState', 'CONNECTING');
  618. } else {
  619. // // 不存在 → 添加新设备
  620. // this.xhsDeviceList.push({
  621. // newXhsDeviceId: device.mac,
  622. // connectionState: ''
  623. // });
  624. }
  625. uni.showLoading({
  626. title: '连接中...'
  627. });
  628. const userId = this.fitCloudWatchUser.userId + '_' + device.mac
  629. fitCloudWatch.connect({
  630. mac: device.mac,
  631. userId: userId, // 测试用户ID
  632. isBind: true, // 测试绑定模式
  633. sex: this.fitCloudWatchUser.sex || true, // 男
  634. age: this.fitCloudWatchUser.age || 25,
  635. height: this.fitCloudWatchUser.height || 175,
  636. weight: this.fitCloudWatchUser.weight || 70
  637. }, (res) => {
  638. uni.hideLoading();
  639. if (res.code !== 0) {
  640. this.connectionState = 'DISCONNECTED';
  641. uni.showToast({
  642. title: '连接指令发送失败,请检查蓝牙和定位是否打开',
  643. icon: 'none'
  644. });
  645. } else {
  646. // setTimeout(()=>{
  647. // const fitWatchDevice = uni.getStorageSync('fitCloudWatch_mac');
  648. // if(fitWatchDevice == this.connectedDeviceMac) {
  649. // this.connectionState = 'CONNECTED';
  650. // }
  651. // },1000)
  652. }
  653. });
  654. },
  655. // 4. 断开连接
  656. disconnect() {
  657. fitCloudWatch.disconnect({}, (res) => {
  658. console.log('断开指令发送', res);
  659. });
  660. },
  661. editXHSDevice(selectedDeviceId) {
  662. if (!selectedDeviceId || this.xhsDeviceList.some(item => item.newXhsDeviceId == selectedDeviceId)) return;
  663. const newXhsDeviceId = this.xhsDeviceList.map(item => item.newXhsDeviceId);
  664. newXhsDeviceId.push(selectedDeviceId);
  665. console.log("newXhsDeviceId==", newXhsDeviceId)
  666. const param = {
  667. newXhsDeviceId: newXhsDeviceId.join(','), // 设备唯一MAC
  668. deviceType: this.deviceType
  669. }
  670. uni.showLoading({ title: '绑定中'});
  671. editXHSDevice(param).then(res => {
  672. uni.hideLoading();
  673. if (res.code == 200) {
  674. this.xhsDeviceList.push({
  675. newXhsDeviceId: selectedDeviceId,
  676. connectionState: this.connectionState
  677. })
  678. this.boundDevice = selectedDeviceId
  679. this.deviceList = this.deviceList.filter(it=>it.mac!=selectedDeviceId)
  680. uni.showToast({ title: '绑定成功'});
  681. if (this.isScanning) {
  682. this.stopScan();
  683. }
  684. uni.setStorageSync('fitCloudWatch_mac', this.boundDevice);
  685. } else {
  686. uni.showToast({
  687. title: res.msg,
  688. icon: 'none'
  689. })
  690. }
  691. })
  692. },
  693. editMyfamily(selectedDeviceId) {
  694. if (!selectedDeviceId || this.xhsDeviceList.some(item => item.newXhsDeviceId == selectedDeviceId)) return;
  695. let newXhsDeviceId = this.xhsDeviceList.map(item => item.newXhsDeviceId);
  696. newXhsDeviceId.push(selectedDeviceId);
  697. this.otherDevice[this.selectUser - 1].newXhsDeviceId = newXhsDeviceId.join(',')
  698. uni.showLoading({ title: '绑定中'});
  699. editMyfamily({
  700. otherDevice: JSON.stringify(this.otherDevice)
  701. }).then(async res => {
  702. uni.hideLoading();
  703. if (res.code == 200) {
  704. this.xhsDeviceList.push({
  705. newXhsDeviceId: selectedDeviceId,
  706. connectionState: this.connectionState
  707. })
  708. this.boundDevice = selectedDeviceId
  709. this.deviceList = this.deviceList.filter(it=>it.mac!=selectedDeviceId)
  710. uni.showToast({ title: '绑定成功'});
  711. if (this.isScanning) {
  712. this.stopScan();
  713. }
  714. uni.setStorageSync('fitCloudWatch_mac', this.boundDevice);
  715. } else {
  716. uni.showToast({
  717. title: res.msg,
  718. icon: 'none'
  719. })
  720. // if (this.connected) {
  721. // await this.disconnect();
  722. // }
  723. // uni.removeStorageSync('fitCloudWatch_mac');
  724. }
  725. })
  726. },
  727. getUser() {
  728. this.loading = true
  729. getWatchUserInfo({
  730. isFamily: false
  731. }).then(res => {
  732. if (res.code == 200) {
  733. this.fitCloudWatchUser = res.user
  734. uni.setStorageSync("userWatchInfo", JSON.stringify(res.user))
  735. if (this.isFamily) {
  736. // 安全解析家庭成员设备
  737. this.otherDevice = this.fitCloudWatchUser.otherDevice ? JSON.parse(this.fitCloudWatchUser.otherDevice) : [];
  738. console.log("otherDevice===",this.otherDevice)
  739. // 格式化设备ID为对象数组
  740. const formattedDevices = this.otherDevice.map(item => ({
  741. ...item,
  742. newXhsDeviceId: item.newXhsDeviceId ?
  743. item.newXhsDeviceId.split(',').map(id => ({
  744. newXhsDeviceId: id,
  745. connectionState: ''
  746. })) : []
  747. }));
  748. // 安全取当前选中成员的设备(防越界)
  749. const index = this.selectUser - 1;
  750. this.loading = false
  751. this.xhsDeviceList = formattedDevices[index]?.newXhsDeviceId || [];
  752. console.log("newXhsDeviceId===",this.xhsDeviceList)
  753. } else {
  754. // 个人设备格式化
  755. const deviceIds = this.fitCloudWatchUser.newXhsDeviceId ? this.fitCloudWatchUser.newXhsDeviceId.split(',') : [];
  756. this.loading = false
  757. this.xhsDeviceList = deviceIds.map(id => ({
  758. newXhsDeviceId: id,
  759. connectionState: ''
  760. }));
  761. }
  762. this.checkConnectedDeviceMac()
  763. } else {
  764. this.loading = false
  765. uni.showToast({
  766. title:res.msg,
  767. icon: 'none'
  768. })
  769. }
  770. }).catch(()=>{
  771. this.loading = false
  772. uni.showToast({
  773. title: "请求接口失败",
  774. icon: 'none'
  775. })
  776. })
  777. },
  778. checkConnectedDeviceMac() {
  779. // 2. 设备绑定 & 自动连接逻辑(统一处理,无重复代码)
  780. if (!this.xhsDeviceList || this.xhsDeviceList.length === 0) {
  781. // 无设备:清空绑定缓存
  782. uni.removeStorageSync('fitCloudWatch_mac');
  783. this.boundDevice = null;
  784. } else {
  785. // 有设备:安全获取绑定设备
  786. let boundDevice = uni.getStorageSync('fitCloudWatch_mac');
  787. try {
  788. // 校验:缓存设备是否在当前列表中
  789. const isDeviceValid = boundDevice && this.xhsDeviceList.some(
  790. item => item.newXhsDeviceId == boundDevice
  791. );
  792. // 确定最终绑定设备
  793. this.boundDevice = isDeviceValid ? boundDevice : this.xhsDeviceList[0]
  794. .newXhsDeviceId;
  795. // 持久化保存
  796. uni.setStorageSync('fitCloudWatch_mac', this.boundDevice);
  797. // 赋值选中设备信息
  798. // this.selectedDeviceId = this.boundDevice.deviceId;
  799. // this.selectedDeviceName = this.boundDevice.name;
  800. // this.deviceId = this.selectedDeviceId;
  801. // 自动连接 & 获取数据
  802. this.connectedDeviceMac = this.boundDevice
  803. this.checkCurrentConnectionState()
  804. } catch (e) {
  805. this.connectedDeviceMac = this.boundDevice
  806. // 异常兜底:绑定第一个设备
  807. this.boundDevice = this.xhsDeviceList[0].newXhsDeviceId;
  808. uni.setStorageSync('fitCloudWatch_mac', this.boundDevice);
  809. // 赋值选中设备信息
  810. // this.selectedDeviceId = this.boundDevice.deviceId;
  811. // this.selectedDeviceName = this.boundDevice.name;
  812. // this.deviceId = this.selectedDeviceId;
  813. // 自动连接 & 获取数据
  814. this.checkCurrentConnectionState()
  815. console.error('设备绑定异常:', e);
  816. }
  817. }
  818. },
  819. // 解绑设备
  820. unbindDevice(item) {
  821. uni.showModal({
  822. title: '解绑确认',
  823. content: '确定要解绑当前手表吗?',
  824. success: (res) => {
  825. if (res.confirm) {
  826. // 处理缓存设备,解绑的当前连接设备需要从列表连接一个新设备
  827. let boundDevice = uni.getStorageSync('fitCloudWatch_mac');
  828. if (boundDevice && boundDevice === deviceId) {
  829. uni.removeStorageSync('fitCloudWatch_mac');
  830. }
  831. // 统一删除设备方法
  832. const removeDevice = () => {
  833. this.isFamily
  834. ? this.removeXHSDevicefamily(item.newXhsDeviceId)
  835. : this.removeXHSDevice(item.newXhsDeviceId);
  836. };
  837. // 判断是否是当前连接设备
  838. if (item.newXhsDeviceId === this.connectedDeviceMac) {
  839. try {
  840. fitCloudWatch.unbind({}, (unbindRes) => {
  841. // 无论解绑成功/失败,都执行删除
  842. if (unbindRes.code === 0) {
  843. this.disconnect();
  844. }
  845. removeDevice();
  846. });
  847. } catch (error) {
  848. // 出错了也强制删除设备
  849. removeDevice();
  850. console.error('解绑异常:', error);
  851. }
  852. } else {
  853. removeDevice();
  854. }
  855. }
  856. }
  857. });
  858. },
  859. // 5. 开始测量实时数据
  860. startRealTimeData() {
  861. this.isMeasuring = true;
  862. // 重置数据展示
  863. this.healthData = {
  864. heartRate: 0,
  865. oxygen: 0,
  866. diastolicPressure: 0,
  867. systolicPressure: 0,
  868. respiratoryRate: 0
  869. };
  870. fitCloudWatch.startRealTimeData({
  871. heartRate: true,
  872. oxygen: true,
  873. bloodPressure: true,
  874. respiratoryRate: true
  875. }, (res) => {
  876. if (res.code !== 0) {
  877. this.isMeasuring = false;
  878. uni.showToast({
  879. title: '开启测量失败',
  880. icon: 'none'
  881. });
  882. }
  883. });
  884. },
  885. // 6. 停止测量实时数据
  886. stopRealTimeData() {
  887. this.xhsDataAdd(this.connectedDeviceMac)
  888. fitCloudWatch.stopRealTimeData({}, (res) => {
  889. this.isMeasuring = false;
  890. });
  891. },
  892. xhsDataAdd(deviceId, type) {
  893. // 无设备ID直接返回
  894. if (!deviceId) return;
  895. // 条件一致时不重新构造数据
  896. if (type === 1 && this.dataSyncFunInfo?.[0]?.deviceId === deviceId) {
  897. // 不处理,直接走接口
  898. } else {
  899. // 解构防止 healthData 不存在报错
  900. const { systolicPressure, diastolicPressure, heartRate, oxygen } = this.healthData || {};
  901. // 构造参数(0:血压 1:血糖 2:心率 3尿酸 4血氧 5步数 6运动)
  902. this.dataSyncFunInfo = [{
  903. recordType: 0, // 血压
  904. recordValue: JSON.stringify({
  905. sdb: systolicPressure,
  906. dbp: diastolicPressure
  907. }),
  908. deviceId: deviceId,
  909. deviceType: this.deviceType
  910. }, {
  911. recordType: 2, // 心率
  912. recordValue: heartRate,
  913. deviceId: deviceId,
  914. deviceType: this.deviceType
  915. }, {
  916. recordType: 4, // 血氧
  917. recordValue: oxygen,
  918. deviceId: deviceId,
  919. deviceType: this.deviceType
  920. }];
  921. }
  922. this.dataSync = 'loading'
  923. xhsDataAddList(this.dataSyncFunInfo).then(res => {
  924. if (res.code == 200) {
  925. this.dataSync = 'success'
  926. this.dataSyncFunInfo = []
  927. } else {
  928. this.dataSync = 'error'
  929. uni.showToast({
  930. title: '数据同步失败' + JSON.stringify(res.msg),
  931. icon: 'none'
  932. })
  933. }
  934. }).catch(() => {
  935. this.dataSync = 'error'
  936. uni.showToast({
  937. title: '数据同步失败',
  938. icon: 'none'
  939. })
  940. })
  941. },
  942. // 同步睡眠、步数,运动
  943. xhsOtherDataAdd(deviceId, type) {
  944. // 无设备ID直接返回
  945. if (!deviceId) return;
  946. // 条件一致时不重新构造数据
  947. if (type === 1 && this.dataOtherSyncFunInfo?.[0]?.deviceId === deviceId) {
  948. // 不处理,直接走接口
  949. } else {
  950. // 构造参数(0:血压 1:血糖 2:心率 3尿酸 4血氧 5步数 6运动 7睡眠)
  951. this.dataOtherSyncFunInfo = [{
  952. recordType: 5, // 步数
  953. recordValue: JSON.stringify(this.stepDataList),
  954. deviceId: deviceId,
  955. deviceType: this.deviceType
  956. }, {
  957. recordType: 6, // 运动
  958. recordValue: JSON.stringify(this.sportDataList),
  959. deviceId: deviceId,
  960. deviceType: this.deviceType
  961. }, {
  962. recordType: 7, // 睡眠
  963. recordValue: JSON.stringify(this.sleepDataList),
  964. deviceId: deviceId,
  965. deviceType: this.deviceType
  966. }];
  967. }
  968. this.dataOtherSync = 'loading'
  969. xhsDataAddList(this.dataOtherSyncFunInfo).then(res => {
  970. if (res.code == 200) {
  971. this.dataOtherSync = 'success'
  972. this.dataOtherSyncFunInfo = []
  973. } else {
  974. this.dataOtherSync = 'error'
  975. uni.showToast({
  976. title: '数据同步失败' + JSON.stringify(res.msg),
  977. icon: 'none'
  978. })
  979. }
  980. }).catch(() => {
  981. this.dataOtherSync = 'error'
  982. uni.showToast({
  983. title: '数据同步失败',
  984. icon: 'none'
  985. })
  986. })
  987. },
  988. // 7. 同步历史数据 (包含睡眠、运动、计步等)
  989. syncData() {
  990. if(this.dataOtherSync == 'error'){
  991. this.xhsOtherDataAdd(this.connectedDeviceMac, 1)
  992. return
  993. }
  994. console.log("同步历史数据this.isSyncing",this.isSyncing)
  995. if (this.isSyncing) return;
  996. this.isSyncing = true;
  997. // 清理旧数据
  998. this.sleepDataList = [];
  999. this.sportDataList = [];
  1000. this.stepDataList = [];
  1001. uni.showLoading({ title: '同步中...' });
  1002. this.dataOtherSync = 'loading'
  1003. try {
  1004. fitCloudWatch.syncData({}, (res) => {
  1005. this.isSyncing = false;
  1006. uni.hideLoading();
  1007. if (res.code === 0) {
  1008. uni.showToast({ title: '同步指令已发送', icon: 'success' });
  1009. } else {
  1010. this.dataOtherSync = 'errorSync'
  1011. uni.showToast({ title: '同步失败: ' + (res.message || '未知错误'), icon: 'none' });
  1012. }
  1013. });
  1014. } catch (err) {
  1015. console.error("同步失败:", err);
  1016. this.isSyncing = false;
  1017. this.dataOtherSync = 'errorSync';
  1018. uni.hideLoading();
  1019. uni.showToast({ title: "同步异常,请重试", icon: "none" });
  1020. }
  1021. },
  1022. formatTime(timestamp) {
  1023. if (!timestamp) return '--';
  1024. const date = new Date(timestamp);
  1025. const y = date.getFullYear();
  1026. const m = (date.getMonth() + 1).toString().padStart(2, '0');
  1027. const d = date.getDate().toString().padStart(2, '0');
  1028. return `${y}-${m}-${d}`;
  1029. },
  1030. // 辅助方法:状态转换
  1031. getSleepStatus(status) {
  1032. const statusMap = {
  1033. 1: '深睡',
  1034. 2: '浅睡',
  1035. 3: '清醒'
  1036. };
  1037. return statusMap[status] || '未知';
  1038. },
  1039. formatTimeOnly(timestamp) {
  1040. if (!timestamp) return '';
  1041. const date = new Date(timestamp);
  1042. const h = String(date.getHours()).padStart(2, '0');
  1043. const m = String(date.getMinutes()).padStart(2, '0');
  1044. return `${h}:${m}`;
  1045. },
  1046. xhsDataAddTest(deviceId) {
  1047. this.healthData = {
  1048. heartRate: 80,
  1049. oxygen: 99,
  1050. diastolicPressure: 114,
  1051. systolicPressure: 80,
  1052. respiratoryRate: 0
  1053. }
  1054. const recordValue = {
  1055. sdb: this.healthData.systolicPressure,
  1056. dbp: this.healthData.diastolicPressure,
  1057. }
  1058. let param = [{
  1059. recordType: 0, // 0:血压 1:血糖 2:心率 3尿酸 4血氧
  1060. recordValue: JSON.stringify(recordValue),
  1061. deviceId: deviceId,
  1062. deviceType: this.deviceType
  1063. }, {
  1064. recordType: 2, // 0:血压 1:血糖 2:心率 3尿酸 4血氧
  1065. recordValue: this.healthData.heartRate,
  1066. deviceId: deviceId,
  1067. deviceType: this.deviceType
  1068. }, {
  1069. recordType: 4, // 0:血压 1:血糖 2:心率 3尿酸 4血氧
  1070. recordValue: this.healthData.oxygen,
  1071. deviceId: deviceId,
  1072. deviceType: this.deviceType
  1073. }]
  1074. this.dataSync = 'loading'
  1075. this.dataSyncFunInfo = param
  1076. uni.showLoading({
  1077. title: '数据同步中'
  1078. })
  1079. xhsDataAddList(this.dataSyncFunInfo).then(res => {
  1080. uni.hideLoading()
  1081. if (res.code == 200) {
  1082. this.dataSync = 'success'
  1083. this.dataSyncFunInfo = []
  1084. } else {
  1085. this.dataSync = 'error'
  1086. uni.showToast({
  1087. title: '数据同步失败' + JSON.stringify(res.msg),
  1088. icon: 'none'
  1089. })
  1090. }
  1091. }).catch(() => {
  1092. uni.hideLoading()
  1093. this.dataSync = 'error'
  1094. uni.showToast({
  1095. title: '数据同步失败',
  1096. icon: 'none'
  1097. })
  1098. })
  1099. },
  1100. // 小护士解除绑定设备(自己)
  1101. removeXHSDevice(newXhsDeviceId) {
  1102. uni.showLoading({
  1103. title: '解绑中'
  1104. })
  1105. removeXHSDevice({
  1106. newXhsDeviceId: newXhsDeviceId,
  1107. deviceType: this.deviceType
  1108. }).then(async res => {
  1109. uni.hideLoading()
  1110. if (res.code == 200) {
  1111. // this.disconnect();
  1112. this.connectionState = ''
  1113. this.connectedDeviceMac = ''
  1114. this.xhsDeviceList = this.xhsDeviceList.filter(item => item.newXhsDeviceId !=
  1115. newXhsDeviceId)
  1116. uni.showToast({
  1117. title: '解绑成功',
  1118. icon: 'success'
  1119. });
  1120. } else {
  1121. uni.showToast({
  1122. title: res.msg,
  1123. icon: 'none'
  1124. })
  1125. }
  1126. }).catch(err => {
  1127. uni.hideLoading()
  1128. uni.showToast({
  1129. title: '设备解绑失败',
  1130. icon: 'none'
  1131. })
  1132. })
  1133. },
  1134. removeXHSDevicefamily(deviceId) {
  1135. let newXhsDeviceId = this.xhsDeviceList.map(item => item.newXhsDeviceId);
  1136. newXhsDeviceId = newXhsDeviceId.filter(item => item != deviceId);
  1137. this.otherDevice[this.selectUser - 1].newXhsDeviceId = newXhsDeviceId.join(',')
  1138. console.log("==this.otherDevice=", this.otherDevice)
  1139. uni.showLoading({
  1140. title: '解绑中'
  1141. })
  1142. editMyfamily({
  1143. otherDevice: JSON.stringify(this.otherDevice)
  1144. }).then(async res => {
  1145. uni.hideLoading()
  1146. if (res.code == 200) {
  1147. this.xhsDeviceList = this.xhsDeviceList.filter(item => item.newXhsDeviceId != deviceId)
  1148. this.connectionState = ''
  1149. this.connectedDeviceMac = ''
  1150. uni.showToast({
  1151. title: '解绑成功',
  1152. icon: 'success'
  1153. });
  1154. } else {
  1155. uni.showToast({
  1156. title: res.msg,
  1157. icon: 'none'
  1158. })
  1159. // if (this.connected) {
  1160. // await this.disconnect();
  1161. // }
  1162. uni.removeStorageSync('fitCloudWatch_mac');
  1163. }
  1164. }).catch(async err => {
  1165. uni.hideLoading()
  1166. uni.showToast({
  1167. title: '设备解绑失败',
  1168. icon: 'none'
  1169. })
  1170. });
  1171. },
  1172. // 获取接口数据
  1173. getLastData(selectedDeviceId) {
  1174. const param = {
  1175. deviceId: selectedDeviceId,
  1176. deviceType: this.deviceType
  1177. }
  1178. getLastData(param).then(res => {
  1179. if (res.code == 200) {
  1180. // // 0:血压 1:血糖 2:心率 3尿酸 4血氧
  1181. // // 状态标记
  1182. // this.dataSync = 'success';
  1183. // 安全获取数据列表
  1184. const {
  1185. data = []
  1186. } = res;
  1187. // 一次性提取所有需要的数据(只遍历一次数组,性能更好)
  1188. const bloodPressure = data.find(item => item.recordType === 0);
  1189. const pulse = data.find(item => item.recordType === 2);
  1190. const oxygen = data.find(item => item.recordType === 4);
  1191. const glucose = data.find(item => item.recordType === 1);
  1192. const uricAcid = data.find(item => item.recordType === 3);
  1193. // 血压数据解析(安全处理)
  1194. let bpData = {};
  1195. try {
  1196. bpData = bloodPressure?.recordValue ? JSON.parse(bloodPressure.recordValue) : {};
  1197. } catch (e) {
  1198. bpData = {};
  1199. }
  1200. // 统一赋值(带默认值,防止 undefined 报错)
  1201. this.cardGlucose = {
  1202. value: glucose?.recordValue || '',
  1203. unit: 'mmol/L',
  1204. time: glucose?.createTime || ''
  1205. };
  1206. this.healthData = {
  1207. heartRate: pulse?.recordValue,
  1208. oxygen: oxygen?.recordValue,
  1209. diastolicPressure: bpData.dbp,
  1210. systolicPressure: bpData.sdb,
  1211. respiratoryRate: null
  1212. }
  1213. } else {
  1214. uni.showToast({
  1215. title: res.msg,
  1216. icon: 'none'
  1217. })
  1218. }
  1219. })
  1220. },
  1221. }
  1222. }
  1223. </script>
  1224. <style lang="scss" scoped>
  1225. .page {
  1226. min-height: 100vh;
  1227. background: #ffffff;
  1228. }
  1229. .empty-container {
  1230. display: flex;
  1231. flex-direction: column;
  1232. align-items: center;
  1233. .empty-img {
  1234. width: 200rpx;
  1235. height: 200rpx;
  1236. }
  1237. .es-c-99 {
  1238. color: #999999;
  1239. font-size: 28rpx;
  1240. margin-top: 20rpx;
  1241. }
  1242. }
  1243. .deviceimg {
  1244. width: 120rpx;
  1245. height: 120rpx;
  1246. margin-right: 24rpx;
  1247. }
  1248. .nav-bar {
  1249. position: sticky;
  1250. top: 0;
  1251. z-index: 10;
  1252. display: flex;
  1253. align-items: center;
  1254. justify-content: space-between;
  1255. margin: 0 24rpx;
  1256. background: #fff;
  1257. }
  1258. .back-text {
  1259. width: 64rpx;
  1260. height: 64rpx;
  1261. }
  1262. .next-text {
  1263. width: 48rpx;
  1264. height: 48rpx;
  1265. }
  1266. .nav-back,
  1267. .nav-placeholder {
  1268. width: 84rpx;
  1269. height: 64rpx;
  1270. display: flex;
  1271. align-items: center;
  1272. }
  1273. .nav-back {
  1274. justify-content: flex-start;
  1275. }
  1276. .nav-placeholder {
  1277. justify-content: flex-end;
  1278. }
  1279. .back-text {
  1280. font-size: 30rpx;
  1281. color: #ff5c03;
  1282. }
  1283. .nav-title {
  1284. font-size: 34rpx;
  1285. font-weight: 600;
  1286. color: #333;
  1287. }
  1288. .content {
  1289. padding: 32rpx 24rpx 48rpx;
  1290. }
  1291. .section-header {
  1292. font-size: 32rpx;
  1293. font-weight: 700;
  1294. color: #333;
  1295. margin-bottom: 24rpx;
  1296. padding-left: 8rpx;
  1297. }
  1298. .status-card {
  1299. background: #fff;
  1300. border-radius: 24rpx;
  1301. padding: 32rpx;
  1302. border: 2rpx solid #f0f0f0;
  1303. margin-bottom: 24rpx;
  1304. display: flex;
  1305. align-items: center;
  1306. }
  1307. .status-info {
  1308. flex: 1;
  1309. }
  1310. .device-name {
  1311. font-size: 30rpx;
  1312. font-weight: 600;
  1313. color: #222;
  1314. margin-bottom: 16rpx;
  1315. }
  1316. .status-card.connected {
  1317. background: #fffaf7;
  1318. border-color: rgba(255, 92, 3, 0.15);
  1319. box-shadow: 0 4rpx 12rpx rgba(255, 92, 3, 0.05);
  1320. }
  1321. .status-row {
  1322. display: flex;
  1323. align-items: center;
  1324. justify-content: space-between;
  1325. }
  1326. .status-row:not(:last-child) {
  1327. margin-bottom: 12rpx;
  1328. }
  1329. .status-label {
  1330. font-size: 26rpx;
  1331. color: #999;
  1332. }
  1333. .status-value {
  1334. font-size: 28rpx;
  1335. font-weight: 600;
  1336. color: #333;
  1337. }
  1338. .status-value.connected {
  1339. color: #07c160;
  1340. }
  1341. .status-value.disconnected {
  1342. color: #999;
  1343. }
  1344. .status-value.connecting {
  1345. color: #ff9501;
  1346. }
  1347. .status-value.mac {
  1348. font-family: monospace;
  1349. font-weight: normal;
  1350. color: #666;
  1351. font-size: 24rpx;
  1352. }
  1353. .success {
  1354. color: #07c160;
  1355. }
  1356. .error {
  1357. color: #e64340;
  1358. }
  1359. .panel {
  1360. background: #fff;
  1361. border-radius: 24rpx;
  1362. padding: 24rpx;
  1363. margin-bottom: 24rpx;
  1364. border: 2rpx solid #f5f5f5;
  1365. }
  1366. .panel-head {
  1367. display: flex;
  1368. align-items: center;
  1369. justify-content: space-between;
  1370. margin-bottom: 20rpx;
  1371. }
  1372. .panel-title {
  1373. font-size: 30rpx;
  1374. font-weight: 600;
  1375. color: #333;
  1376. margin-bottom: 20rpx;
  1377. }
  1378. .panel-head .panel-title {
  1379. margin-bottom: 0;
  1380. }
  1381. .health-monitoring {
  1382. display: flex;
  1383. align-items: center;
  1384. justify-content: flex-start;
  1385. flex-wrap: wrap;
  1386. margin-right: -16rpx;
  1387. &-item {
  1388. width: calc(50% - 16rpx);
  1389. min-height: 264rpx;
  1390. margin: 0 16rpx 16rpx 0;
  1391. overflow: hidden;
  1392. background: #F5f7fa;
  1393. border-radius: 16rpx 16rpx 16rpx 16rpx;
  1394. padding: 24rpx 34rpx 24rpx 24rpx;
  1395. box-sizing: border-box;
  1396. }
  1397. &-maintitle {
  1398. margin-bottom: 4rpx;
  1399. font-weight: 500;
  1400. font-size: 30rpx;
  1401. color: #333333;
  1402. }
  1403. &-title {
  1404. display: flex;
  1405. align-items: center;
  1406. justify-content: space-between;
  1407. font-weight: 400;
  1408. font-size: 24rpx;
  1409. color: #999999;
  1410. image {
  1411. width: 72rpx;
  1412. height: 72rpx;
  1413. flex-shrink: 0;
  1414. }
  1415. }
  1416. .resnum {
  1417. font-family: DIN, DIN;
  1418. font-weight: 500;
  1419. font-size: 64rpx;
  1420. }
  1421. &-res {
  1422. height: 78rpx;
  1423. margin: 20rpx 0 6rpx;
  1424. font-family: PingFang SC, PingFang SC;
  1425. font-weight: 600;
  1426. font-size: 48rpx;
  1427. color: #333333;
  1428. }
  1429. &-time {
  1430. font-weight: 400;
  1431. font-size: 22rpx;
  1432. color: #999999;
  1433. }
  1434. }
  1435. .unit {
  1436. font-size: 22rpx;
  1437. color: #999;
  1438. font-weight: 500;
  1439. }
  1440. .btn-row {
  1441. display: flex;
  1442. gap: 16rpx;
  1443. margin-bottom: 16rpx;
  1444. }
  1445. .btn-row:last-child {
  1446. margin-bottom: 0;
  1447. }
  1448. .btn {
  1449. flex: 1;
  1450. height: 84rpx;
  1451. line-height: 84rpx;
  1452. border-radius: 42rpx;
  1453. font-size: 28rpx;
  1454. border: none;
  1455. }
  1456. .btn-primary {
  1457. background: linear-gradient(90deg, #f8551f 0%, #ff9501 100%);
  1458. color: #fff;
  1459. }
  1460. .btn-default {
  1461. background: #f5f7fa;
  1462. color: #67686f;
  1463. }
  1464. .btn-danger {
  1465. background: #e64340;
  1466. color: #fff;
  1467. }
  1468. .btn-danger-outline {
  1469. background: #fff;
  1470. color: #e64340;
  1471. border: 2rpx solid #e64340;
  1472. }
  1473. .btn-mini-primary {
  1474. width: 160rpx;
  1475. height: 64rpx;
  1476. line-height: 64rpx;
  1477. padding: 0 20rpx;
  1478. border-radius: 32rpx;
  1479. font-size: 24rpx;
  1480. background: #ff7700;
  1481. color: #fff;
  1482. }
  1483. .device-list {
  1484. margin-top: 8rpx;
  1485. background: #f5f7fa;
  1486. border-radius: 16rpx;
  1487. padding: 16rpx;
  1488. }
  1489. .list-title {
  1490. font-size: 24rpx;
  1491. color: #666;
  1492. margin-bottom: 16rpx;
  1493. }
  1494. .scroll-y {
  1495. max-height: 520rpx;
  1496. }
  1497. .device-item {
  1498. display: flex;
  1499. align-items: center;
  1500. justify-content: space-between;
  1501. padding: 18rpx 8rpx;
  1502. border-bottom: 1rpx solid #ebedf0;
  1503. }
  1504. .device-item:last-child {
  1505. border-bottom: none;
  1506. }
  1507. .device-info {
  1508. display: flex;
  1509. flex-direction: column;
  1510. }
  1511. .device-mac {
  1512. margin-top: 6rpx;
  1513. font-size: 22rpx;
  1514. color: #999;
  1515. }
  1516. .device-rssi {
  1517. font-size: 24rpx;
  1518. color: #ff7700;
  1519. }
  1520. .device-connectbox {
  1521. display: flex;
  1522. flex-direction: column;
  1523. align-items: center;
  1524. }
  1525. .device-connect {
  1526. font-family: PingFang SC, PingFang SC;
  1527. font-weight: 500;
  1528. font-size: 26rpx;
  1529. color: #fff;
  1530. padding: 16rpx 32rpx;
  1531. background: #FF7700;
  1532. border-radius: 26rpx 26rpx 26rpx 26rpx;
  1533. // 已连接
  1534. &.connected {
  1535. background: #07c160;
  1536. }
  1537. // 连接中
  1538. &.connecting {
  1539. background: #ff9501;
  1540. }
  1541. }
  1542. .empty-tip {
  1543. text-align: center;
  1544. font-size: 24rpx;
  1545. color: #999;
  1546. padding: 28rpx 0 18rpx;
  1547. }
  1548. .sleep-list {
  1549. margin-top: 8rpx;
  1550. }
  1551. .sleep-item {
  1552. background: #f5f7fa;
  1553. border-radius: 14rpx;
  1554. padding: 18rpx 16rpx;
  1555. margin-bottom: 12rpx;
  1556. }
  1557. .sleep-date {
  1558. display: block;
  1559. font-size: 24rpx;
  1560. color: #333;
  1561. }
  1562. .sleep-detail {
  1563. display: block;
  1564. margin-top: 8rpx;
  1565. font-size: 22rpx;
  1566. color: #999;
  1567. }
  1568. .device-item-btn {
  1569. background: #07c160;
  1570. color: #fff;
  1571. padding: 16rpx 24rpx;
  1572. border-radius: 44rpx;
  1573. font-size: 28rpx;
  1574. margin: 0;
  1575. min-width: 160rpx;
  1576. }
  1577. .history-section {
  1578. margin-top: 30rpx;
  1579. background: #fff;
  1580. border-radius: 12rpx;
  1581. padding: 16rpx;
  1582. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  1583. .section-title {
  1584. font-size: 28rpx;
  1585. font-weight: bold;
  1586. color: #333;
  1587. margin-bottom: 16rpx;
  1588. border-left: 6rpx solid #007aff;
  1589. padding-left: 12rpx;
  1590. }
  1591. .history-list {
  1592. max-height: 360rpx;
  1593. }
  1594. .history-item {
  1595. background: #f9f9f9;
  1596. border-radius: 8rpx;
  1597. padding: 16rpx;
  1598. margin-bottom: 16rpx;
  1599. }
  1600. .history-header {
  1601. display: flex;
  1602. justify-content: space-between;
  1603. align-items: center;
  1604. margin-bottom: 12rpx;
  1605. border-bottom: 2rpx dashed #eee;
  1606. padding-bottom: 8rpx;
  1607. }
  1608. .history-header .date {
  1609. font-size: 26rpx;
  1610. color: #333;
  1611. font-weight: bold;
  1612. }
  1613. .history-header .count {
  1614. font-size: 24rpx;
  1615. color: #007aff;
  1616. background: rgba(0, 122, 255, 0.1);
  1617. padding: 4rpx 12rpx;
  1618. border-radius: 20rpx;
  1619. }
  1620. .history-detail {
  1621. display: flex;
  1622. flex-direction: column;
  1623. gap: 8rpx;
  1624. }
  1625. .detail-row {
  1626. display: flex;
  1627. justify-content: space-between;
  1628. font-size: 24rpx;
  1629. color: #666;
  1630. }
  1631. .status-1 { color: #5B8FF9; } /* 深睡 */
  1632. .status-2 { color: #5AD8A6; } /* 浅睡 */
  1633. .status-3 { color: #F6BD16; } /* 清醒 */
  1634. }
  1635. </style>