index.vue 11 KB

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