index.vue 6.2 KB

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