index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="profile-page">
  3. <view v-if="loading" class="loading-bar">同步中…</view>
  4. <!-- 个人信息卡片(只读,数据来自 GET /users/me) -->
  5. <view class="card">
  6. <view class="row">
  7. <text class="label">头像</text>
  8. <view class="row-right">
  9. <view class="avatar-wrap">
  10. <UserAvatar
  11. :name="user.name"
  12. :id="user.id"
  13. :src="user.avatar"
  14. :size="80"
  15. unit="rpx"
  16. />
  17. </view>
  18. </view>
  19. </view>
  20. <view class="row">
  21. <text class="label">姓名</text>
  22. <view class="row-right">
  23. <text class="value">{{ user.name || '—' }}</text>
  24. </view>
  25. </view>
  26. <view class="row">
  27. <text class="label">账号名</text>
  28. <view class="row-right">
  29. <text class="value" :class="{ placeholder: !user.alias }">{{ user.alias || '—' }}</text>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="card">
  34. <view class="row no-arrow">
  35. <text class="label">企业</text>
  36. <text class="value">{{ displayOrg }}</text>
  37. </view>
  38. </view>
  39. <text class="hint">信息由服务端维护,暂不支持在应用内修改。</text>
  40. <view class="logout-section">
  41. <view
  42. v-if="isAndroid"
  43. class="action-btn check-update-btn"
  44. @click="onCheckUpdate"
  45. >
  46. 检测更新
  47. </view>
  48. <view class="action-btn change-pwd-btn" @click="onChangePassword">修改密码</view>
  49. <view class="logout-btn" @click="onLogout">退出登录</view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import UserAvatar from '../../components/UserAvatar.vue'
  55. import { getToken, getCurrentUserInfo, normalizeUserPayload, setToken } from '../../utils/api'
  56. // #ifdef APP-PLUS
  57. import { manualCheckAndroidApkUpdate } from '../../utils/appUpgrade'
  58. // #endif
  59. const USER_KEY = 'current_user'
  60. const DEFAULT_ORG = '韫珠科技'
  61. export default {
  62. components: { UserAvatar },
  63. data() {
  64. return {
  65. isAndroid: false,
  66. loading: false,
  67. user: {
  68. name: '',
  69. id: '',
  70. avatar: '',
  71. alias: '',
  72. orgName: ''
  73. }
  74. }
  75. },
  76. computed: {
  77. displayOrg() {
  78. return this.user.orgName || DEFAULT_ORG
  79. }
  80. },
  81. onLoad() {
  82. this.applyFromStorage()
  83. this.refreshAndroidFlag()
  84. },
  85. onShow() {
  86. this.applyFromStorage()
  87. this.refreshAndroidFlag()
  88. this.refreshFromServer()
  89. },
  90. methods: {
  91. refreshAndroidFlag() {
  92. // #ifdef APP-PLUS
  93. try {
  94. this.isAndroid = uni.getSystemInfoSync().platform === 'android'
  95. } catch (e) {
  96. this.isAndroid = false
  97. }
  98. // #endif
  99. },
  100. applyFromStorage() {
  101. try {
  102. const raw = uni.getStorageSync(USER_KEY)
  103. const u = normalizeUserPayload(raw)
  104. if (u) {
  105. this.user = {
  106. name: u.name,
  107. id: u.id,
  108. avatar: u.avatar,
  109. alias: u.alias,
  110. orgName: u.orgName || u.org_name || ''
  111. }
  112. }
  113. } catch (e) {}
  114. },
  115. async refreshFromServer() {
  116. const token = getToken()
  117. if (!token) return
  118. this.loading = true
  119. try {
  120. const me = await getCurrentUserInfo(token)
  121. const u = normalizeUserPayload(me)
  122. if (!u) return
  123. const orgName = u.orgName || u.org_name || DEFAULT_ORG
  124. this.user = {
  125. name: u.name,
  126. id: u.id,
  127. avatar: u.avatar,
  128. alias: u.alias,
  129. orgName
  130. }
  131. try {
  132. const prev = uni.getStorageSync(USER_KEY)
  133. const base = prev && typeof prev === 'object' ? prev : {}
  134. uni.setStorageSync(USER_KEY, {
  135. ...base,
  136. ...u,
  137. orgName,
  138. org_name: orgName
  139. })
  140. } catch (e) {}
  141. } catch (e) {
  142. // 网络失败时保留本地/缓存展示
  143. } finally {
  144. this.loading = false
  145. }
  146. },
  147. onCheckUpdate() {
  148. // #ifdef APP-PLUS
  149. manualCheckAndroidApkUpdate()
  150. // #endif
  151. },
  152. onChangePassword() {
  153. uni.navigateTo({ url: '/pages/change-password/index' })
  154. },
  155. onLogout() {
  156. uni.showModal({
  157. title: '提示',
  158. content: '确定要退出登录吗?',
  159. success: (res) => {
  160. if (!res.confirm) return
  161. setToken('')
  162. try {
  163. uni.removeStorageSync(USER_KEY)
  164. } catch (e) {}
  165. this.user = {
  166. name: '',
  167. id: '',
  168. avatar: '',
  169. alias: '',
  170. orgName: ''
  171. }
  172. uni.reLaunch({ url: '/pages/login/index' })
  173. }
  174. })
  175. }
  176. }
  177. }
  178. </script>
  179. <style scoped>
  180. .profile-page {
  181. min-height: 100vh;
  182. background-color: #f5f5f5;
  183. padding: 24rpx;
  184. padding-bottom: calc(120rpx + constant(safe-area-inset-bottom));
  185. padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
  186. box-sizing: border-box;
  187. }
  188. .loading-bar {
  189. font-size: 24rpx;
  190. color: #999;
  191. margin-bottom: 16rpx;
  192. padding-left: 8rpx;
  193. }
  194. .card {
  195. background: #fff;
  196. border-radius: 16rpx;
  197. overflow: hidden;
  198. margin-bottom: 24rpx;
  199. }
  200. .row {
  201. display: flex;
  202. align-items: center;
  203. justify-content: space-between;
  204. min-height: 112rpx;
  205. padding: 0 32rpx;
  206. border-bottom: 1rpx solid #f0f0f0;
  207. }
  208. .row:last-child {
  209. border-bottom: none;
  210. }
  211. .row.no-arrow {
  212. border-bottom: none;
  213. }
  214. .label {
  215. font-size: 30rpx;
  216. color: #333;
  217. flex-shrink: 0;
  218. }
  219. .row-right {
  220. display: flex;
  221. align-items: center;
  222. gap: 16rpx;
  223. min-width: 0;
  224. }
  225. .value {
  226. font-size: 28rpx;
  227. color: #333;
  228. overflow: hidden;
  229. text-overflow: ellipsis;
  230. white-space: nowrap;
  231. max-width: 400rpx;
  232. }
  233. .value.placeholder {
  234. color: #999;
  235. }
  236. .avatar-wrap {
  237. display: flex;
  238. align-items: center;
  239. justify-content: center;
  240. }
  241. .hint {
  242. display: block;
  243. font-size: 24rpx;
  244. color: #999;
  245. line-height: 1.5;
  246. padding: 0 16rpx;
  247. }
  248. .logout-section {
  249. margin-top: 48rpx;
  250. padding: 0 0 24rpx;
  251. display: flex;
  252. flex-direction: column;
  253. gap: 24rpx;
  254. }
  255. .action-btn {
  256. background: #fff;
  257. border-radius: 16rpx;
  258. min-height: 96rpx;
  259. line-height: 96rpx;
  260. text-align: center;
  261. font-size: 32rpx;
  262. }
  263. .check-update-btn {
  264. color: #259653;
  265. }
  266. .change-pwd-btn {
  267. color: #333;
  268. }
  269. .logout-btn {
  270. background: #fff;
  271. border-radius: 16rpx;
  272. min-height: 96rpx;
  273. line-height: 96rpx;
  274. text-align: center;
  275. font-size: 32rpx;
  276. color: #e54d42;
  277. }
  278. </style>