index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. user.value = resp.data
  104. // 保存到本地存储
  105. uni.setStorageSync('user_info', resp.data)
  106. // 检查 nickname 和 avatar
  107. if (!resp.data.nickname || !resp.data.avatar) {
  108. uni.navigateTo({ url: '/pages/public/profile/infos/base-info' })
  109. }
  110. }
  111. } catch (err) {
  112. uni.hideLoading()
  113. console.error('Fetch user info error:', err)
  114. }
  115. }
  116. // 如果在微信小程序端且未登录,自动跳转到登录页
  117. onShow(() => {
  118. const token = uni.getStorageSync('token')
  119. if (!token) {
  120. // 使用 uni.reLaunch 替代 navigateTo,确保页面栈被清空
  121. uni.reLaunch({ url: '/pages/public/login/index' })
  122. } else {
  123. fetchUserInfo()
  124. }
  125. })
  126. const onMenuClick = (type: string) => {
  127. console.log('Menu clicked:', type)
  128. if (type === 'base-info') {
  129. // 跳转到修改个人信息页面
  130. uni.navigateTo({ url: '/pages/public/profile/infos/base-info' })
  131. return
  132. }
  133. if (type === 'patient-filing') {
  134. // 跳转到建档信息设定页面
  135. uni.navigateTo({ url: '/pages/public/profile/infos/patient-filing' })
  136. return
  137. }
  138. uni.showToast({
  139. title: `${type} 功能开发中`,
  140. icon: 'none'
  141. })
  142. }
  143. const onLogout = () => {
  144. uni.showModal({
  145. title: '提示',
  146. content: '确定要退出登录吗?',
  147. success: (res) => {
  148. if (res.confirm) {
  149. uni.removeStorageSync('token')
  150. uni.removeStorageSync('role')
  151. user.value = {}
  152. uni.showToast({ title: '已退出登录', icon: 'none' })
  153. }
  154. }
  155. })
  156. }
  157. </script>
  158. <style>
  159. .profile-container {
  160. min-height: 100vh;
  161. background-color: #f5f5f5;
  162. padding-top: calc(var(--status-bar-height) + 44px);
  163. }
  164. .header {
  165. background-color: #fff;
  166. padding: 40rpx;
  167. margin-bottom: 20rpx;
  168. }
  169. .avatar-section {
  170. display: flex;
  171. align-items: center;
  172. }
  173. .avatar {
  174. width: 120rpx;
  175. height: 120rpx;
  176. border-radius: 50%;
  177. /* background-color: #007aff; */
  178. border: 1px solid rgba(128, 128, 128, 0.5);
  179. display: flex;
  180. align-items: center;
  181. justify-content: center;
  182. margin-right: 30rpx;
  183. }
  184. .avatar-frame {
  185. width: 100%;
  186. height: 100%;
  187. border-radius: 50%;
  188. overflow: hidden; /* 裁切超出部分,避免溢出 */
  189. display: flex;
  190. align-items: center;
  191. justify-content: center;
  192. }
  193. .avatar-img {
  194. width: 100%;
  195. height: 100%;
  196. object-fit: cover; /* 在部分平台下生效,保证图片填充同时裁切 */
  197. }
  198. .avatar-text {
  199. color: #fff;
  200. font-size: 32rpx;
  201. font-weight: bold;
  202. }
  203. .user-info {
  204. flex: 1;
  205. }
  206. .username {
  207. font-size: 36rpx;
  208. font-weight: bold;
  209. color: #333;
  210. display: block;
  211. margin-bottom: 10rpx;
  212. }
  213. .user-id {
  214. font-size: 28rpx;
  215. color: #666;
  216. }
  217. .menu-list {
  218. background-color: #fff;
  219. margin-bottom: 20rpx;
  220. }
  221. .menu-item {
  222. display: flex;
  223. justify-content: space-between;
  224. align-items: center;
  225. padding: 30rpx 40rpx;
  226. border-bottom: 1rpx solid #eee;
  227. }
  228. .menu-item:last-child {
  229. border-bottom: none;
  230. }
  231. .menu-text {
  232. font-size: 32rpx;
  233. color: #333;
  234. }
  235. .menu-arrow {
  236. font-size: 28rpx;
  237. color: #ccc;
  238. }
  239. .logout-section {
  240. padding: 40rpx;
  241. }
  242. .logout-btn {
  243. width: 100%;
  244. background-color: #ff4757;
  245. color: #fff;
  246. border-radius: 8rpx;
  247. font-size: 32rpx;
  248. line-height: 80rpx;
  249. }
  250. </style>