index.vue 7.4 KB

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