index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. <text v-if="appVersionLabel" class="version-footer">当前版本 {{ appVersionLabel }}</text>
  52. </view>
  53. </template>
  54. <script>
  55. import UserAvatar from '../../components/UserAvatar.vue'
  56. import { getToken, getCurrentUserInfo, normalizeUserPayload, setToken } from '../../utils/api'
  57. // #ifdef APP-PLUS
  58. import { manualCheckAndroidApkUpdate } from '../../utils/appUpgrade'
  59. // #endif
  60. const USER_KEY = 'current_user'
  61. const DEFAULT_ORG = '韫珠科技'
  62. export default {
  63. components: { UserAvatar },
  64. data() {
  65. return {
  66. isAndroid: false,
  67. loading: false,
  68. appVersionLabel: '',
  69. user: {
  70. name: '',
  71. id: '',
  72. avatar: '',
  73. alias: '',
  74. orgName: ''
  75. }
  76. }
  77. },
  78. computed: {
  79. displayOrg() {
  80. return this.user.orgName || DEFAULT_ORG
  81. }
  82. },
  83. onLoad() {
  84. this.applyFromStorage()
  85. this.refreshAndroidFlag()
  86. this.refreshAppVersion()
  87. },
  88. onShow() {
  89. this.applyFromStorage()
  90. this.refreshAndroidFlag()
  91. this.refreshAppVersion()
  92. this.refreshFromServer()
  93. },
  94. methods: {
  95. refreshAppVersion() {
  96. let v = ''
  97. // #ifdef APP-PLUS
  98. try {
  99. if (typeof plus !== 'undefined' && plus.runtime) {
  100. const name = plus.runtime.version
  101. if (name) v = String(name)
  102. }
  103. } catch (e) {}
  104. // #endif
  105. if (!v) {
  106. try {
  107. const si = uni.getSystemInfoSync()
  108. v = String(si.appVersion || si.appWgtVersion || '').trim()
  109. } catch (e) {}
  110. }
  111. this.appVersionLabel = v
  112. },
  113. refreshAndroidFlag() {
  114. // #ifdef APP-PLUS
  115. try {
  116. this.isAndroid = uni.getSystemInfoSync().platform === 'android'
  117. } catch (e) {
  118. this.isAndroid = false
  119. }
  120. // #endif
  121. },
  122. applyFromStorage() {
  123. try {
  124. const raw = uni.getStorageSync(USER_KEY)
  125. const u = normalizeUserPayload(raw)
  126. if (u) {
  127. this.user = {
  128. name: u.name,
  129. id: u.id,
  130. avatar: u.avatar,
  131. alias: u.alias,
  132. orgName: u.orgName || u.org_name || ''
  133. }
  134. }
  135. } catch (e) {}
  136. },
  137. async refreshFromServer() {
  138. const token = getToken()
  139. if (!token) return
  140. this.loading = true
  141. try {
  142. const me = await getCurrentUserInfo(token)
  143. const u = normalizeUserPayload(me)
  144. if (!u) return
  145. const orgName = u.orgName || u.org_name || DEFAULT_ORG
  146. this.user = {
  147. name: u.name,
  148. id: u.id,
  149. avatar: u.avatar,
  150. alias: u.alias,
  151. orgName
  152. }
  153. try {
  154. const prev = uni.getStorageSync(USER_KEY)
  155. const base = prev && typeof prev === 'object' ? prev : {}
  156. uni.setStorageSync(USER_KEY, {
  157. ...base,
  158. ...u,
  159. orgName,
  160. org_name: orgName
  161. })
  162. } catch (e) {}
  163. } catch (e) {
  164. // 网络失败时保留本地/缓存展示
  165. } finally {
  166. this.loading = false
  167. }
  168. },
  169. onCheckUpdate() {
  170. // #ifdef APP-PLUS
  171. manualCheckAndroidApkUpdate()
  172. // #endif
  173. },
  174. onChangePassword() {
  175. uni.navigateTo({ url: '/pages/change-password/index' })
  176. },
  177. onLogout() {
  178. uni.showModal({
  179. title: '提示',
  180. content: '确定要退出登录吗?',
  181. success: (res) => {
  182. if (!res.confirm) return
  183. setToken('')
  184. try {
  185. uni.removeStorageSync(USER_KEY)
  186. } catch (e) {}
  187. this.user = {
  188. name: '',
  189. id: '',
  190. avatar: '',
  191. alias: '',
  192. orgName: ''
  193. }
  194. uni.reLaunch({ url: '/pages/login/index' })
  195. }
  196. })
  197. }
  198. }
  199. }
  200. </script>
  201. <style scoped>
  202. .profile-page {
  203. min-height: 100vh;
  204. background-color: #f5f5f5;
  205. padding: 24rpx;
  206. padding-bottom: calc(120rpx + constant(safe-area-inset-bottom));
  207. padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
  208. box-sizing: border-box;
  209. }
  210. .loading-bar {
  211. font-size: 24rpx;
  212. color: #999;
  213. margin-bottom: 16rpx;
  214. padding-left: 8rpx;
  215. }
  216. .card {
  217. background: #fff;
  218. border-radius: 16rpx;
  219. overflow: hidden;
  220. margin-bottom: 24rpx;
  221. }
  222. .row {
  223. display: flex;
  224. align-items: center;
  225. justify-content: space-between;
  226. min-height: 112rpx;
  227. padding: 0 32rpx;
  228. border-bottom: 1rpx solid #f0f0f0;
  229. }
  230. .row:last-child {
  231. border-bottom: none;
  232. }
  233. .row.no-arrow {
  234. border-bottom: none;
  235. }
  236. .label {
  237. font-size: 30rpx;
  238. color: #333;
  239. flex-shrink: 0;
  240. }
  241. .row-right {
  242. display: flex;
  243. align-items: center;
  244. gap: 16rpx;
  245. min-width: 0;
  246. }
  247. .value {
  248. font-size: 28rpx;
  249. color: #333;
  250. overflow: hidden;
  251. text-overflow: ellipsis;
  252. white-space: nowrap;
  253. max-width: 400rpx;
  254. }
  255. .value.placeholder {
  256. color: #999;
  257. }
  258. .avatar-wrap {
  259. display: flex;
  260. align-items: center;
  261. justify-content: center;
  262. }
  263. .hint {
  264. display: block;
  265. font-size: 24rpx;
  266. color: #999;
  267. line-height: 1.5;
  268. padding: 0 16rpx;
  269. }
  270. .logout-section {
  271. margin-top: 48rpx;
  272. padding: 0 0 24rpx;
  273. display: flex;
  274. flex-direction: column;
  275. gap: 24rpx;
  276. }
  277. .action-btn {
  278. background: #fff;
  279. border-radius: 16rpx;
  280. min-height: 96rpx;
  281. line-height: 96rpx;
  282. text-align: center;
  283. font-size: 32rpx;
  284. }
  285. .check-update-btn {
  286. color: #259653;
  287. }
  288. .change-pwd-btn {
  289. color: #333;
  290. }
  291. .logout-btn {
  292. background: #fff;
  293. border-radius: 16rpx;
  294. min-height: 96rpx;
  295. line-height: 96rpx;
  296. text-align: center;
  297. font-size: 32rpx;
  298. color: #e54d42;
  299. }
  300. .version-footer {
  301. display: block;
  302. text-align: center;
  303. margin-top: 40rpx;
  304. font-size: 24rpx;
  305. color: #999;
  306. line-height: 1.5;
  307. padding: 0 16rpx;
  308. }
  309. </style>