index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <CustomNav title="个人中心" leftType="none" />
  3. <view class="profile-container">
  4. <view class="header">
  5. <view class="avatar-section">
  6. <view class="avatar">
  7. <view class="avatar-frame">
  8. <image class="avatar-img" :src="avatarSrc" mode="aspectFill" />
  9. </view>
  10. </view>
  11. <view class="user-info">
  12. <text class="username">{{ user.nickname || '慢病患者' }}</text>
  13. <text class="user-id" v-if="user.openid">ID: {{ user.openid }}</text>
  14. </view>
  15. </view>
  16. </view>
  17. <view class="menu-list">
  18. <view class="menu-item" @click="onMenuClick('base-info')">
  19. <text class="menu-text">基本信息设定</text>
  20. <text class="menu-arrow"></text>
  21. </view>
  22. <view class="menu-item" @click="onMenuClick('settings')">
  23. <text class="menu-text">设置</text>
  24. <text class="menu-arrow"></text>
  25. </view>
  26. <view class="menu-item" @click="onMenuClick('about')">
  27. <text class="menu-text">关于我们</text>
  28. <text class="menu-arrow"></text>
  29. </view>
  30. </view>
  31. <view class="logout-section">
  32. <button class="logout-btn" @click="onLogout">退出登录</button>
  33. </view>
  34. </view>
  35. <TabBar />
  36. </template>
  37. <script setup lang="ts">
  38. import { ref } from 'vue'
  39. import { computed } from 'vue'
  40. import { onShow } from '@dcloudio/uni-app'
  41. import CustomNav from '@/components/custom-nav.vue'
  42. import TabBar from '@/components/tab-bar.vue'
  43. import { logout } from '@/composables/useAuth'
  44. import { fetchUserInfo as fetchUserInfoApi, downloadAvatar as downloadAvatarApi } from '@/api/user'
  45. import { avatarCache } from '@/utils/avatarCache'
  46. const title = ref('个人中心')
  47. const user = ref<{ avatar?: string; nickname?: string; openid?: string }>({})
  48. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  49. // 计算一个安全的 avatar src:优先使用有效的 user.avatar,否则使用默认头像
  50. const avatarSrc = computed(() => {
  51. const a = user.value?.avatar
  52. if (!a) return defaultAvatarUrl
  53. try {
  54. const s = String(a)
  55. // 常见有效前缀:http(s), data:, wxfile://, file://, /static
  56. if (/^(https?:\/\/|data:|wxfile:\/\/|file:\/\/|\/static\/)/i.test(s)) {
  57. return s
  58. }
  59. // 有时候小程序临时路径也以 / 开头或以 temp 开头,尽量允许以 ./ 或 / 开头
  60. if (/^(\.|\/|temp)/i.test(s)) return s
  61. } catch (e) {
  62. // fallback
  63. }
  64. return defaultAvatarUrl
  65. })
  66. const loadUser = () => {
  67. try {
  68. const u = uni.getStorageSync('user_info')
  69. if (u) {
  70. user.value = u
  71. }
  72. } catch (e) {
  73. // ignore
  74. }
  75. }
  76. const fetchUserInfo = async () => {
  77. try {
  78. const token = uni.getStorageSync('token')
  79. if (!token) return
  80. uni.showLoading({ title: '加载中...' })
  81. const response = await fetchUserInfoApi()
  82. uni.hideLoading()
  83. console.log('User info response:', response)
  84. const resp = response.data as any
  85. if (response.statusCode === 401) {
  86. // Token 无效,清除并跳转登录
  87. uni.removeStorageSync('token')
  88. uni.removeStorageSync('role')
  89. user.value = {}
  90. uni.reLaunch({ url: '/pages/public/login/index' })
  91. return
  92. }
  93. if (resp && resp.code === 200 && resp.data) {
  94. // 如果头像无效(不是有效的 http URL),则下载头像
  95. if (!resp.data.avatar || !resp.data.avatar.startsWith('http')) {
  96. const userIdRaw = resp.data.id || resp.data.userId
  97. const userId = userIdRaw ? String(userIdRaw) : ''
  98. if (userId) {
  99. // 检查是否有缓存的头像
  100. if (avatarCache.has(String(userId))) {
  101. resp.data.avatar = avatarCache.get(String(userId))
  102. } else {
  103. try {
  104. const downloadRes = await downloadAvatarApi(userId)
  105. if (downloadRes.statusCode === 200 && downloadRes.tempFilePath) {
  106. resp.data.avatar = downloadRes.tempFilePath
  107. // 缓存头像路径
  108. avatarCache.set(String(userId), downloadRes.tempFilePath)
  109. }
  110. } catch (e) {
  111. console.error('Download avatar error:', e)
  112. }
  113. }
  114. }
  115. }
  116. user.value = resp.data
  117. // 保存到本地存储
  118. uni.setStorageSync('user_info', resp.data)
  119. // 检查 nickname 和 avatar
  120. if (!resp.data.nickname || !resp.data.avatar) {
  121. uni.navigateTo({ url: '/pages/patient/profile/infos/base-info' })
  122. }
  123. }
  124. } catch (err) {
  125. uni.hideLoading()
  126. console.error('Fetch user info error:', err)
  127. }
  128. }
  129. // 如果在微信小程序端且未登录,自动跳转到登录页
  130. onShow(() => {
  131. const token = uni.getStorageSync('token')
  132. if (!token) {
  133. // 使用 uni.reLaunch 替代 navigateTo,确保页面栈被清空
  134. uni.reLaunch({ url: '/pages/public/login/index' })
  135. } else {
  136. fetchUserInfo()
  137. }
  138. })
  139. const onMenuClick = (type: string) => {
  140. console.log('Menu clicked:', type)
  141. if (type === 'base-info') {
  142. // 跳转到修改个人信息页面
  143. uni.navigateTo({ url: '/pages/patient/profile/infos/base-info' })
  144. return
  145. }
  146. if (type === 'patient-filing') {
  147. // 跳转到建档信息设定页面
  148. uni.navigateTo({ url: '/pages/patient/profile/infos/patient-filing' })
  149. return
  150. }
  151. uni.showToast({
  152. title: `${type} 功能开发中`,
  153. icon: 'none'
  154. })
  155. }
  156. const onLogout = async () => {
  157. // 清空头像缓存
  158. avatarCache.clear()
  159. await logout({ after: () => { user.value = {} } })
  160. }
  161. </script>
  162. <style>
  163. .profile-container {
  164. min-height: 100vh;
  165. background-color: #f5f5f5;
  166. padding-top: calc(var(--status-bar-height) + 44px);
  167. }
  168. .header {
  169. background-color: #fff;
  170. padding: 40rpx;
  171. margin-bottom: 20rpx;
  172. }
  173. .avatar-section {
  174. display: flex;
  175. align-items: center;
  176. }
  177. .avatar {
  178. width: 120rpx;
  179. height: 120rpx;
  180. border-radius: 50%;
  181. /* background-color: #007aff; */
  182. border: 1px solid rgba(128, 128, 128, 0.5);
  183. display: flex;
  184. align-items: center;
  185. justify-content: center;
  186. margin-right: 30rpx;
  187. }
  188. .avatar-frame {
  189. width: 100%;
  190. height: 100%;
  191. border-radius: 50%;
  192. overflow: hidden; /* 裁切超出部分,避免溢出 */
  193. display: flex;
  194. align-items: center;
  195. justify-content: center;
  196. }
  197. .avatar-img {
  198. width: 100%;
  199. height: 100%;
  200. object-fit: cover; /* 在部分平台下生效,保证图片填充同时裁切 */
  201. }
  202. .avatar-text {
  203. color: #fff;
  204. font-size: 32rpx;
  205. font-weight: bold;
  206. }
  207. .user-info {
  208. flex: 1;
  209. }
  210. .username {
  211. font-size: 36rpx;
  212. font-weight: bold;
  213. color: #333;
  214. display: block;
  215. margin-bottom: 10rpx;
  216. }
  217. .user-id {
  218. font-size: 28rpx;
  219. color: #666;
  220. }
  221. .menu-list {
  222. background-color: #fff;
  223. margin-bottom: 20rpx;
  224. }
  225. .menu-item {
  226. display: flex;
  227. justify-content: space-between;
  228. align-items: center;
  229. padding: 30rpx 40rpx;
  230. border-bottom: 1rpx solid #eee;
  231. }
  232. .menu-item:last-child {
  233. border-bottom: none;
  234. }
  235. .menu-text {
  236. font-size: 32rpx;
  237. color: #333;
  238. }
  239. .menu-arrow {
  240. font-size: 28rpx;
  241. color: #ccc;
  242. }
  243. .logout-section {
  244. padding: 40rpx;
  245. }
  246. .logout-btn {
  247. width: 100%;
  248. background-color: #ff4757;
  249. color: #fff;
  250. border-radius: 8rpx;
  251. font-size: 32rpx;
  252. line-height: 80rpx;
  253. }
  254. </style>