index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <template>
  2. <CustomNav title="家属首页" leftType="scan" @scan="handleScan" :opacity="0" />
  3. <view class="page-container">
  4. <view class="content">
  5. <view class="user-info">
  6. <view class="avatar-section">
  7. <view class="avatar">
  8. <view class="avatar-frame">
  9. <image class="avatar-img" :src="avatarSrc" mode="aspectFill" />
  10. </view>
  11. </view>
  12. <view class="user-details">
  13. <text class="username">{{ user.nickname || '未登录' }}</text>
  14. <text class="user-role">家属</text>
  15. </view>
  16. <view class="qr-button" @click="onQrClick">
  17. <image src="/static/icons/remixicon/qr-code-line.svg" class="qr-icon" />
  18. </view>
  19. </view>
  20. </view>
  21. <view class="function-container">
  22. <view class="function-row">
  23. <view class="function-item blue" @click="onItemClick('家人管理')">
  24. <view class="item-content">
  25. <view class="title-row">
  26. <view class="item-line"></view>
  27. <text class="item-title">家人管理</text>
  28. </view>
  29. <text class="item-desc">管理被监护的家人</text>
  30. </view>
  31. </view>
  32. <view class="function-item green" @click="onItemClick('健康监控')">
  33. <view class="item-content">
  34. <view class="title-row">
  35. <view class="item-line"></view>
  36. <text class="item-title">健康监控</text>
  37. </view>
  38. <text class="item-desc">查看家人健康数据</text>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="function-row">
  43. <view class="function-item orange" @click="onItemClick('联系医生')">
  44. <view class="item-content">
  45. <view class="title-row">
  46. <view class="item-line"></view>
  47. <text class="item-title">联系医生</text>
  48. </view>
  49. <text class="item-desc">与医生沟通交流</text>
  50. </view>
  51. </view>
  52. <view class="function-item purple" @click="onItemClick('疑问解答')">
  53. <view class="item-content">
  54. <view class="title-row">
  55. <view class="item-line"></view>
  56. <text class="item-title">疑问解答</text>
  57. </view>
  58. <text class="item-desc">咨询健康相关问题</text>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <view class="today-reminder-card">
  64. <view class="card-header">
  65. <text class="card-title">今日提醒</text>
  66. </view>
  67. <view class="card-content">
  68. <view class="reminder-item" @click="onItemClick('健康监控')">
  69. <view class="reminder-icon">
  70. <image src="/static/icons/remixicon/time-line.svg" class="icon" />
  71. </view>
  72. <view class="reminder-text">
  73. <text class="reminder-number">{{ todayReminders.medicationCount }}</text>
  74. <text class="reminder-label">用药提醒</text>
  75. </view>
  76. </view>
  77. <view class="reminder-item" @click="onItemClick('健康监控')">
  78. <view class="reminder-icon">
  79. <image src="/static/icons/remixicon/alert-line.svg" class="icon" />
  80. </view>
  81. <view class="reminder-text">
  82. <text class="reminder-number">{{ todayReminders.abnormalCount }}</text>
  83. <text class="reminder-label">异常数据</text>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. <view class="family-activity-card">
  89. <view class="card-header">
  90. <text class="card-title">家人动态</text>
  91. </view>
  92. <view class="activity-card-content">
  93. <view class="activity-item" v-for="(activity, index) in familyActivities" :key="index">
  94. <view class="activity-avatar">
  95. <image :src="activity.familyAvatar" class="avatar-img" mode="aspectFill" />
  96. </view>
  97. <view class="activity-text">
  98. <text class="activity-desc">{{ activity.desc }}</text>
  99. <text class="activity-time">{{ activity.time }}</text>
  100. </view>
  101. </view>
  102. <view v-if="familyActivities.length === 0" class="no-activity">
  103. <text>暂无家人动态</text>
  104. </view>
  105. </view>
  106. </view>
  107. </view>
  108. </view>
  109. <TabBar />
  110. </template>
  111. <script setup lang="ts">
  112. import { ref, computed } from 'vue'
  113. import { onShow } from '@dcloudio/uni-app'
  114. import CustomNav from '@/components/custom-nav.vue'
  115. import { fetchUserInfo as fetchUserInfoApi } from '@/api/user'
  116. import TabBar from '@/components/tab-bar.vue'
  117. const user = ref<{ avatar?: string; nickname?: string }>({})
  118. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  119. const avatarSrc = computed(() => {
  120. const a = user.value?.avatar
  121. if (!a) return defaultAvatarUrl
  122. try {
  123. const s = String(a)
  124. if (/^(https?:\/\/|data:|wxfile:\/\/|file:\/\/|\/static\/)/i.test(s)) {
  125. return s
  126. }
  127. if (/^(\.|\/|temp)/i.test(s)) return s
  128. } catch (e) {
  129. // fallback
  130. }
  131. return defaultAvatarUrl
  132. })
  133. const todayReminders = ref({
  134. medicationCount: 0,
  135. abnormalCount: 0
  136. })
  137. const familyActivities = ref([
  138. {
  139. desc: '家人张三完成了今日用药',
  140. time: '10分钟前',
  141. familyAvatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  142. },
  143. {
  144. desc: '家人李四血糖数据已更新',
  145. time: '1小时前',
  146. familyAvatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  147. },
  148. {
  149. desc: '家人王五预约了下周复诊',
  150. time: '2小时前',
  151. familyAvatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  152. }
  153. ])
  154. const loadUser = () => {
  155. try {
  156. const u = uni.getStorageSync('user_info')
  157. if (u) {
  158. user.value = u
  159. }
  160. } catch (e) {
  161. // ignore
  162. }
  163. }
  164. const fetchUserInfo = async () => {
  165. try {
  166. const token = uni.getStorageSync('token')
  167. if (!token) return
  168. uni.showLoading({ title: '加载中...' })
  169. const response = await fetchUserInfoApi()
  170. uni.hideLoading()
  171. console.log('User info response:', response)
  172. const resp = response.data as any
  173. if (response.statusCode === 401) {
  174. // Token 无效,清除并跳转登录
  175. uni.removeStorageSync('token')
  176. uni.removeStorageSync('role')
  177. user.value = {}
  178. uni.reLaunch({ url: '/pages/public/login/index' })
  179. return
  180. }
  181. if (resp && resp.code === 200 && resp.data) {
  182. // 如果头像无效(不是有效的 http URL),则下载头像
  183. if (!resp.data.avatar || !resp.data.avatar.startsWith('http')) {
  184. const userId = resp.data.id || resp.data.userId
  185. if (userId) {
  186. try {
  187. const downloadRes = await uni.downloadFile({
  188. url: `https://wx.baiyun.work/user/avatar/${userId}`,
  189. header: {
  190. Authorization: `Bearer ${token}`
  191. }
  192. })
  193. if (downloadRes.statusCode === 200 && downloadRes.tempFilePath) {
  194. resp.data.avatar = downloadRes.tempFilePath
  195. }
  196. } catch (e) {
  197. console.error('Download avatar error:', e)
  198. }
  199. }
  200. }
  201. user.value = resp.data
  202. uni.setStorageSync('user_info', resp.data)
  203. if (!resp.data.nickname || !resp.data.avatar) {
  204. uni.navigateTo({ url: '/pages/patient-family/profile/index' })
  205. }
  206. }
  207. } catch (err) {
  208. uni.hideLoading()
  209. console.error('Fetch user info error:', err)
  210. }
  211. }
  212. const fetchTodayReminders = async () => {
  213. try {
  214. const token = uni.getStorageSync('token')
  215. if (!token) return
  216. const response = await uni.request({
  217. url: 'https://wx.baiyun.work/family/today_reminders',
  218. method: 'GET',
  219. header: {
  220. 'Authorization': `Bearer ${token}`
  221. }
  222. })
  223. const resp = response.data as any
  224. if (resp && resp.code === 200 && resp.data) {
  225. todayReminders.value = resp.data
  226. }
  227. } catch (err) {
  228. console.error('Fetch today reminders error:', err)
  229. }
  230. }
  231. const fetchFamilyActivities = async () => {
  232. try {
  233. const token = uni.getStorageSync('token')
  234. if (!token) return
  235. const response = await uni.request({
  236. url: 'https://wx.baiyun.work/family/activities',
  237. method: 'GET',
  238. header: {
  239. 'Authorization': `Bearer ${token}`
  240. }
  241. })
  242. const resp = response.data as any
  243. if (resp && resp.code === 200 && resp.data) {
  244. familyActivities.value = resp.data
  245. }
  246. } catch (err) {
  247. console.error('Fetch family activities error:', err)
  248. }
  249. }
  250. // 如果在微信小程序端且未登录,自动跳转到登录页
  251. onShow(() => {
  252. const token = uni.getStorageSync('token')
  253. if (!token) {
  254. // 使用 uni.reLaunch 替代 navigateTo,确保页面栈被清空
  255. uni.reLaunch({ url: '/pages/public/login/index' })
  256. } else {
  257. fetchUserInfo()
  258. fetchTodayReminders()
  259. fetchFamilyActivities()
  260. }
  261. })
  262. function handleScan(res: any) {
  263. console.log('[family-index] scan result', res)
  264. const resultText = res?.result || ''
  265. if (resultText) {
  266. uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
  267. } else {
  268. uni.showToast({ title: '未识别到有效内容', icon: 'none' })
  269. }
  270. }
  271. function onItemClick(type: string) {
  272. if (type === '家人管理') {
  273. uni.navigateTo({ url: '/pages/patient-family/family/index' })
  274. } else if (type === '健康监控') {
  275. uni.navigateTo({ url: '/pages/patient-family/health/index' })
  276. } else if (type === '联系医生') {
  277. uni.navigateTo({ url: '/pages/patient-family/doctor/index' })
  278. } else if (type === '疑问解答') {
  279. uni.navigateTo({ url: '/pages/patient-family/qa/index' })
  280. } else {
  281. uni.showToast({ title: '功能正在开发中', icon: 'none' })
  282. }
  283. }
  284. function onQrClick() {
  285. uni.navigateTo({ url: '/pages/public/profile/qr/index' })
  286. }
  287. </script>
  288. <style>
  289. .page-container {
  290. min-height: 100vh;
  291. padding-top: calc(var(--status-bar-height) + 44px);
  292. padding-bottom: 100rpx;
  293. box-sizing: border-box;
  294. justify-content: center;
  295. align-items: center;
  296. background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  297. }
  298. .content {
  299. width: 100%;
  300. }
  301. .user-info {
  302. /* background-color: #fff; */
  303. padding: 40rpx;
  304. margin-top: 0rpx;
  305. }
  306. .avatar-section {
  307. display: flex;
  308. align-items: center;
  309. }
  310. .avatar {
  311. width: 120rpx;
  312. height: 120rpx;
  313. border-radius: 50%;
  314. margin-right: 30rpx;
  315. position: relative;
  316. }
  317. .avatar-frame {
  318. width: 100%;
  319. height: 100%;
  320. border-radius: 50%;
  321. overflow: hidden;
  322. border: 4rpx solid #fff;
  323. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
  324. }
  325. .avatar-img {
  326. width: 100%;
  327. height: 100%;
  328. }
  329. .user-details {
  330. flex: 1;
  331. }
  332. .username {
  333. font-size: 36rpx;
  334. font-weight: bold;
  335. color: #333;
  336. display: block;
  337. margin-bottom: 10rpx;
  338. }
  339. .user-role {
  340. font-size: 28rpx;
  341. color: #666;
  342. display: block;
  343. }
  344. .qr-button {
  345. width: 100rpx;
  346. height: 100rpx;
  347. border-radius: 50%;
  348. background-color: rgba(255, 255, 255, 0.5);
  349. display: flex;
  350. align-items: center;
  351. justify-content: center;
  352. margin-left: 20rpx;
  353. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  354. }
  355. .qr-icon {
  356. width: 60rpx;
  357. height: 60rpx;
  358. }
  359. .function-container {
  360. padding-inline: 20rpx;
  361. margin-top: 40rpx;
  362. }
  363. .function-row {
  364. display: flex;
  365. justify-content: space-between;
  366. margin-bottom: 20rpx;
  367. }
  368. .function-row:last-child {
  369. margin-bottom: 0;
  370. }
  371. .function-item {
  372. flex: 1;
  373. height: 160rpx;
  374. background-color: #fff;
  375. border-radius: 20rpx;
  376. margin: 0 10rpx;
  377. position: relative;
  378. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  379. overflow: hidden;
  380. }
  381. .item-content {
  382. position: absolute;
  383. top: 20rpx;
  384. left: 20rpx;
  385. display: flex;
  386. flex-direction: column;
  387. }
  388. .title-row {
  389. display: flex;
  390. align-items: center;
  391. margin-bottom: 10rpx;
  392. }
  393. .item-line {
  394. width: 8rpx;
  395. height: 48rpx;
  396. margin-right: 15rpx;
  397. border-radius: 10rpx;
  398. }
  399. .blue .item-line {
  400. background-color: #3742fa;
  401. }
  402. .green .item-line {
  403. background-color: #2ed573;
  404. }
  405. .orange .item-line {
  406. background-color: #ffa502;
  407. }
  408. .purple .item-line {
  409. background-color: #9c88ff;
  410. }
  411. .item-title {
  412. font-size: 36rpx;
  413. font-weight: bold;
  414. color: #333;
  415. }
  416. .item-desc {
  417. font-size: 28rpx;
  418. color: #666;
  419. }
  420. .today-reminder-card {
  421. margin: 40rpx 20rpx 0;
  422. background-color: #fff;
  423. border-radius: 20rpx;
  424. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  425. overflow: hidden;
  426. }
  427. .card-header {
  428. padding: 30rpx 40rpx 20rpx;
  429. border-bottom: 1rpx solid #f0f0f0;
  430. }
  431. .card-title {
  432. font-size: 32rpx;
  433. font-weight: bold;
  434. color: #333;
  435. }
  436. .card-content {
  437. padding: 20rpx 40rpx 30rpx;
  438. display: flex;
  439. justify-content: space-around;
  440. }
  441. .reminder-item {
  442. display: flex;
  443. align-items: center;
  444. flex: 1;
  445. padding: 20rpx;
  446. border-radius: 12rpx;
  447. background-color: #f8f9fa;
  448. margin: 0 10rpx;
  449. }
  450. .reminder-icon {
  451. width: 60rpx;
  452. height: 60rpx;
  453. border-radius: 50%;
  454. display: flex;
  455. align-items: center;
  456. justify-content: center;
  457. margin-right: 20rpx;
  458. }
  459. .icon {
  460. width: 30rpx;
  461. height: 30rpx;
  462. }
  463. .reminder-text {
  464. flex: 1;
  465. }
  466. .reminder-number {
  467. font-size: 36rpx;
  468. font-weight: bold;
  469. display: block;
  470. }
  471. .reminder-label {
  472. font-size: 28rpx;
  473. color: #666;
  474. display: block;
  475. }
  476. .family-activity-card {
  477. margin: 40rpx 20rpx 0;
  478. background-color: #fff;
  479. border-radius: 20rpx;
  480. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  481. overflow: hidden;
  482. }
  483. .activity-card-content {
  484. padding: 20rpx 40rpx 30rpx;
  485. }
  486. .activity-item {
  487. display: flex;
  488. align-items: center;
  489. padding: 20rpx 0;
  490. border-bottom: 1rpx solid #f0f0f0;
  491. }
  492. .activity-item:last-child {
  493. border-bottom: none;
  494. }
  495. .activity-avatar {
  496. width: 80rpx;
  497. height: 80rpx;
  498. border-radius: 50%;
  499. overflow: hidden;
  500. margin-right: 20rpx;
  501. border: 2rpx solid #f0f0f0;
  502. }
  503. .activity-text {
  504. flex: 1;
  505. }
  506. .activity-desc {
  507. font-size: 30rpx;
  508. color: #333;
  509. display: block;
  510. margin-bottom: 8rpx;
  511. line-height: 1.4;
  512. }
  513. .activity-time {
  514. font-size: 24rpx;
  515. color: #999;
  516. display: block;
  517. }
  518. .no-activity {
  519. text-align: center;
  520. padding: 60rpx 0;
  521. color: #999;
  522. font-size: 28rpx;
  523. }
  524. </style>