index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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-age" v-if="user.age">年龄: {{ user.age }}</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 green" @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 blue" @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="health-news-card">
  64. <view class="card-header">
  65. <text class="card-title">健康资讯</text>
  66. </view>
  67. <view class="card-content">
  68. <view class="news-item" v-for="(news, index) in newsList" :key="index">
  69. <view v-if="news.image" class="news-image-container">
  70. <image class="news-image" :src="news.image" mode="aspectFill" />
  71. </view>
  72. <view v-else class="news-placeholder">
  73. <image class="placeholder-icon" src="/static/icons/remixicon/image-line.svg" />
  74. </view>
  75. <view class="news-text">
  76. <text class="news-title">{{ news.title }}</text>
  77. <text class="news-desc">{{ news.desc }}</text>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. </view>
  84. <TabBar />
  85. </template>
  86. <script setup lang="ts">
  87. import { ref, computed } from 'vue'
  88. import { onShow } from '@dcloudio/uni-app'
  89. import CustomNav from '@/components/custom-nav.vue'
  90. import TabBar from '@/components/tab-bar.vue'
  91. import { fetchUserInfo as fetchUserInfoApi, downloadAvatar as downloadAvatarApi } from '@/api/user'
  92. const user = ref<{ avatar?: string; nickname?: string; age?: number }>({})
  93. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  94. const avatarSrc = computed(() => {
  95. const a = user.value?.avatar
  96. if (!a) return defaultAvatarUrl
  97. try {
  98. const s = String(a)
  99. if (/^(https?:\/\/|data:|wxfile:\/\/|file:\/\/|\/static\/)/i.test(s)) {
  100. return s
  101. }
  102. if (/^(\.|\/|temp)/i.test(s)) return s
  103. } catch (e) {
  104. // fallback
  105. }
  106. return defaultAvatarUrl
  107. })
  108. const newsList = ref([
  109. {
  110. title: '健康饮食指南',
  111. desc: '了解均衡饮食的重要性,掌握健康饮食的基本原则。',
  112. image: '/static/carousel/BHFIIABBCDJII-5kCEkD6zh9.jpg'
  113. },
  114. {
  115. title: '运动与健康',
  116. desc: '定期运动对身体的好处,以及如何制定适合自己的运动计划。'
  117. },
  118. {
  119. title: '心理健康维护',
  120. desc: '保持良好的心理状态,应对日常生活中的压力和挑战。'
  121. },
  122. {
  123. title: '运动与健康',
  124. desc: '定期运动对身体的好处,以及如何制定适合自己的运动计划。'
  125. },
  126. {
  127. title: '心理健康维护',
  128. desc: '保持良好的心理状态,应对日常生活中的压力和挑战。'
  129. }
  130. ])
  131. const loadUser = () => {
  132. try {
  133. const u = uni.getStorageSync('user_info')
  134. if (u) {
  135. user.value = u
  136. }
  137. } catch (e) {
  138. // ignore
  139. }
  140. }
  141. const fetchUserInfo = async () => {
  142. try {
  143. const token = uni.getStorageSync('token')
  144. if (!token) return
  145. uni.showLoading({ title: '加载中...' })
  146. const response = await fetchUserInfoApi()
  147. uni.hideLoading()
  148. console.log('User info response:', response)
  149. const resp = response.data as any
  150. if (response.statusCode === 401) {
  151. // Token 无效,清除并跳转登录
  152. uni.removeStorageSync('token')
  153. uni.removeStorageSync('role')
  154. user.value = {}
  155. uni.reLaunch({ url: '/pages/public/login/index' })
  156. return
  157. }
  158. if (resp && resp.code === 200 && resp.data) {
  159. // 如果头像无效(不是有效的 http URL),则下载头像
  160. if (!resp.data.avatar || !resp.data.avatar.startsWith('http')) {
  161. const userId = resp.data.id || resp.data.userId
  162. if (userId) {
  163. try {
  164. const downloadRes = await downloadAvatarApi(userId)
  165. if (downloadRes.statusCode === 200 && downloadRes.tempFilePath) {
  166. resp.data.avatar = downloadRes.tempFilePath
  167. }
  168. } catch (e) {
  169. console.error('Download avatar error:', e)
  170. }
  171. }
  172. }
  173. user.value = resp.data
  174. uni.setStorageSync('user_info', resp.data)
  175. if (!resp.data.nickname || !resp.data.avatar) {
  176. uni.navigateTo({ url: '/pages/patient/profile/infos/base-info' })
  177. }
  178. }
  179. } catch (err) {
  180. uni.hideLoading()
  181. console.error('Fetch user info error:', err)
  182. }
  183. }
  184. // 如果在微信小程序端且未登录,自动跳转到登录页
  185. onShow(() => {
  186. const token = uni.getStorageSync('token')
  187. if (!token) {
  188. // 使用 uni.reLaunch 替代 navigateTo,确保页面栈被清空
  189. uni.reLaunch({ url: '/pages/public/login/index' })
  190. } else {
  191. fetchUserInfo()
  192. }
  193. })
  194. function handleScan(res: any) {
  195. console.log('[index] scan result', res)
  196. const resultText = res?.result || ''
  197. if (resultText) {
  198. uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
  199. } else {
  200. uni.showToast({ title: '未识别到有效内容', icon: 'none' })
  201. }
  202. }
  203. function onItemClick(type: string) {
  204. if (type === '个人中心') {
  205. uni.switchTab({ url: '/pages/patient/profile/index' })
  206. } else if (type === '提醒管理') {
  207. uni.navigateTo({ url: '/pages/patient/health/reminder' })
  208. } else if (type === '我的医生') {
  209. uni.navigateTo({ url: '/pages/patient/profile/infos/my-doctor' })
  210. } else {
  211. uni.showToast({ title: '功能正在开发中', icon: 'none' })
  212. }
  213. }
  214. function onQrClick() {
  215. uni.navigateTo({ url: '/pages/public/profile/qr/index' })
  216. }</script>
  217. <style>
  218. .page-container {
  219. min-height: 100vh;
  220. padding-top: calc(var(--status-bar-height) + 44px);
  221. padding-bottom: 100rpx;
  222. box-sizing: border-box;
  223. justify-content: center;
  224. align-items: center;
  225. background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  226. }
  227. .content {
  228. width: 100%;
  229. }
  230. .user-info {
  231. /* background-color: #fff; */
  232. padding: 40rpx;
  233. margin-top: 0rpx;
  234. }
  235. .avatar-section {
  236. display: flex;
  237. align-items: center;
  238. }
  239. .avatar {
  240. width: 120rpx;
  241. height: 120rpx;
  242. border-radius: 50%;
  243. border: 1px solid rgba(128, 128, 128, 0.5);
  244. display: flex;
  245. align-items: center;
  246. justify-content: center;
  247. margin-right: 30rpx;
  248. }
  249. .avatar-frame {
  250. width: 100%;
  251. height: 100%;
  252. border-radius: 50%;
  253. overflow: hidden;
  254. display: flex;
  255. align-items: center;
  256. justify-content: center;
  257. }
  258. .avatar-img {
  259. width: 100%;
  260. height: 100%;
  261. object-fit: cover;
  262. }
  263. .user-details {
  264. flex: 1;
  265. }
  266. .username {
  267. font-size: 36rpx;
  268. font-weight: bold;
  269. color: #333;
  270. display: block;
  271. margin-bottom: 10rpx;
  272. }
  273. .user-age {
  274. font-size: 28rpx;
  275. color: #666;
  276. display: block;
  277. margin-bottom: 10rpx;
  278. }
  279. .user-id {
  280. font-size: 28rpx;
  281. color: #666;
  282. }
  283. .qr-button {
  284. width: 100rpx;
  285. height: 100rpx;
  286. border-radius: 50%;
  287. background-color: rgba(255, 255, 255, 0.5);
  288. display: flex;
  289. align-items: center;
  290. justify-content: center;
  291. margin-left: 20rpx;
  292. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  293. }
  294. .qr-icon {
  295. width: 60rpx;
  296. height: 60rpx;
  297. }
  298. .function-container {
  299. padding-inline: 20rpx;
  300. }
  301. .function-row {
  302. display: flex;
  303. justify-content: space-between;
  304. margin-bottom: 20rpx;
  305. }
  306. .function-row:last-child {
  307. margin-bottom: 0;
  308. }
  309. .function-item {
  310. flex: 1;
  311. height: 160rpx;
  312. background-color: #fff;
  313. border-radius: 20rpx;
  314. margin: 0 10rpx;
  315. position: relative;
  316. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  317. overflow: hidden;
  318. }
  319. .item-content {
  320. position: absolute;
  321. top: 20rpx;
  322. left: 20rpx;
  323. display: flex;
  324. flex-direction: column;
  325. }
  326. .title-row {
  327. display: flex;
  328. align-items: center;
  329. margin-bottom: 10rpx;
  330. }
  331. .item-line {
  332. width: 8rpx;
  333. height: 48rpx;
  334. margin-right: 15rpx;
  335. border-radius: 10rpx;
  336. }
  337. .green .item-line {
  338. background-color: #2ed573;
  339. }
  340. .blue .item-line {
  341. background-color: #3742fa;
  342. }
  343. .orange .item-line {
  344. background-color: #ffa502;
  345. }
  346. .purple .item-line {
  347. background-color: #9c88ff;
  348. }
  349. .item-title {
  350. font-size: 36rpx;
  351. font-weight: bold;
  352. color: #333;
  353. }
  354. .item-desc {
  355. font-size: 28rpx;
  356. color: #666;
  357. }
  358. .health-news-card {
  359. background-color: #fff;
  360. border-radius: 20rpx;
  361. margin: 20rpx;
  362. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  363. overflow: hidden;
  364. }
  365. .card-header {
  366. padding: 20rpx;
  367. border-bottom: 1rpx solid #eee;
  368. }
  369. .card-title {
  370. font-size: 36rpx;
  371. font-weight: bold;
  372. color: #333;
  373. }
  374. .card-content {
  375. padding: 20rpx;
  376. display: flex;
  377. flex-direction: column;
  378. }
  379. .news-item {
  380. display: flex;
  381. align-items: center;
  382. margin-bottom: 20rpx;
  383. padding-bottom: 20rpx;
  384. border-bottom: 1rpx solid #eee;
  385. }
  386. .news-item:last-child {
  387. margin-bottom: 0;
  388. padding-bottom: 0;
  389. border-bottom: none;
  390. }
  391. .news-image-container {
  392. width: 180rpx;
  393. height: 130rpx;
  394. border-radius: 10rpx;
  395. margin-right: 20rpx;
  396. overflow: hidden;
  397. }
  398. .news-image {
  399. width: 100%;
  400. height: 100%;
  401. object-fit: cover;
  402. }
  403. .news-placeholder {
  404. width: 180rpx;
  405. height: 130rpx;
  406. background-color: #f0f0f0;
  407. border-radius: 10rpx;
  408. margin-right: 20rpx;
  409. display: flex;
  410. align-items: center;
  411. justify-content: center;
  412. }
  413. .placeholder-icon {
  414. width: 40rpx;
  415. height: 40rpx;
  416. opacity: 0.5;
  417. }
  418. .news-text {
  419. flex: 1;
  420. }
  421. .news-title {
  422. font-size: 32rpx;
  423. font-weight: bold;
  424. color: #333;
  425. display: block;
  426. margin-bottom: 10rpx;
  427. }
  428. .news-desc {
  429. font-size: 28rpx;
  430. color: #666;
  431. line-height: 1.4;
  432. }
  433. </style>