index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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="card qr-entry-card">
  41. <view class="row qr-row" @click="onOpenIdentityQr">
  42. <text class="label">我的二维码</text>
  43. <view class="row-right">
  44. <text class="value sub">身份核验</text>
  45. <text class="chevron">›</text>
  46. </view>
  47. </view>
  48. </view>
  49. <view class="logout-section">
  50. <view
  51. v-if="isAndroid"
  52. class="action-btn check-update-btn"
  53. @click="onCheckUpdate"
  54. >
  55. 检测更新
  56. </view>
  57. <view class="action-btn app-settings-btn" @click="onAppSettings">应用设置</view>
  58. <view class="action-btn change-pwd-btn" @click="onChangePassword">修改密码</view>
  59. <view class="action-btn switch-account-btn" @click="onOpenSwitchAccount">切换账号</view>
  60. <view class="logout-btn" @click="onLogout">退出登录</view>
  61. </view>
  62. <text v-if="appVersionLabel" class="version-footer">当前版本 {{ appVersionLabel }}</text>
  63. <view v-if="accountPickerVisible" class="account-picker-mask" @click="closeAccountPicker">
  64. <view class="account-picker-sheet" @click.stop>
  65. <text class="account-picker-title">选择要切换的账号</text>
  66. <scroll-view scroll-y class="account-picker-list" :show-scrollbar="false">
  67. <view
  68. v-for="(item, idx) in accountPickerList"
  69. :key="idx"
  70. class="account-picker-item"
  71. @click="onPickSwitchAccount(item)"
  72. >
  73. <text class="account-picker-item-text">{{ savedAccountLabel(item) }}</text>
  74. </view>
  75. </scroll-view>
  76. <view class="account-picker-cancel" @click="closeAccountPicker">
  77. <text>取消</text>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import UserAvatar from '../../components/UserAvatar.vue'
  85. import { getToken, getCurrentUserInfo, normalizeUserPayload, setToken } from '../../utils/api'
  86. import {
  87. loadSavedAccounts,
  88. formatSavedAccountLabel,
  89. setPendingSwitchAccount
  90. } from '../../utils/loginAccounts'
  91. // #ifdef APP-PLUS
  92. import { manualCheckAndroidApkUpdate } from '../../utils/appUpgrade'
  93. // #endif
  94. const USER_KEY = 'current_user'
  95. const DEFAULT_ORG = '韫珠科技'
  96. export default {
  97. components: { UserAvatar },
  98. data() {
  99. return {
  100. isAndroid: false,
  101. loading: false,
  102. appVersionLabel: '',
  103. user: {
  104. name: '',
  105. id: '',
  106. avatar: '',
  107. alias: '',
  108. orgName: ''
  109. },
  110. accountPickerVisible: false,
  111. accountPickerList: []
  112. }
  113. },
  114. computed: {
  115. displayOrg() {
  116. return this.user.orgName || DEFAULT_ORG
  117. }
  118. },
  119. onLoad() {
  120. this.applyFromStorage()
  121. this.refreshAndroidFlag()
  122. this.refreshAppVersion()
  123. },
  124. onShow() {
  125. this.applyFromStorage()
  126. this.refreshAndroidFlag()
  127. this.refreshAppVersion()
  128. this.refreshFromServer()
  129. },
  130. methods: {
  131. refreshAppVersion() {
  132. let v = ''
  133. // #ifdef APP-PLUS
  134. try {
  135. if (typeof plus !== 'undefined' && plus.runtime) {
  136. const name = plus.runtime.version
  137. if (name) v = String(name)
  138. }
  139. } catch (e) {}
  140. // #endif
  141. if (!v) {
  142. try {
  143. const si = uni.getSystemInfoSync()
  144. v = String(si.appVersion || si.appWgtVersion || '').trim()
  145. } catch (e) {}
  146. }
  147. this.appVersionLabel = v
  148. },
  149. refreshAndroidFlag() {
  150. // #ifdef APP-PLUS
  151. try {
  152. this.isAndroid = uni.getSystemInfoSync().platform === 'android'
  153. } catch (e) {
  154. this.isAndroid = false
  155. }
  156. // #endif
  157. },
  158. applyFromStorage() {
  159. try {
  160. const raw = uni.getStorageSync(USER_KEY)
  161. const u = normalizeUserPayload(raw)
  162. if (u) {
  163. this.user = {
  164. name: u.name,
  165. id: u.id,
  166. avatar: u.avatar,
  167. alias: u.alias,
  168. orgName: u.orgName || u.org_name || ''
  169. }
  170. }
  171. } catch (e) {}
  172. },
  173. async refreshFromServer() {
  174. const token = getToken()
  175. if (!token) return
  176. this.loading = true
  177. try {
  178. const me = await getCurrentUserInfo(token)
  179. const u = normalizeUserPayload(me)
  180. if (!u) return
  181. const orgName = u.orgName || u.org_name || DEFAULT_ORG
  182. this.user = {
  183. name: u.name,
  184. id: u.id,
  185. avatar: u.avatar,
  186. alias: u.alias,
  187. orgName
  188. }
  189. try {
  190. const prev = uni.getStorageSync(USER_KEY)
  191. const base = prev && typeof prev === 'object' ? prev : {}
  192. uni.setStorageSync(USER_KEY, {
  193. ...base,
  194. ...u,
  195. orgName,
  196. org_name: orgName
  197. })
  198. } catch (e) {}
  199. } catch (e) {
  200. // 网络失败时保留本地/缓存展示
  201. } finally {
  202. this.loading = false
  203. }
  204. },
  205. onCheckUpdate() {
  206. // #ifdef APP-PLUS
  207. manualCheckAndroidApkUpdate()
  208. // #endif
  209. },
  210. onAppSettings() {
  211. uni.navigateTo({ url: '/pages/app-settings/index' })
  212. },
  213. onChangePassword() {
  214. uni.navigateTo({ url: '/pages/change-password/index' })
  215. },
  216. onOpenIdentityQr() {
  217. uni.navigateTo({ url: '/pages/identity-qr/index' })
  218. },
  219. savedAccountLabel(acc) {
  220. return formatSavedAccountLabel(acc)
  221. },
  222. closeAccountPicker() {
  223. this.accountPickerVisible = false
  224. },
  225. onOpenSwitchAccount() {
  226. let accounts = loadSavedAccounts().filter((a) => a && a.password)
  227. if (!accounts.length) {
  228. uni.showToast({ title: '暂无可切换的已记住密码账号', icon: 'none' })
  229. return
  230. }
  231. try {
  232. const raw = uni.getStorageSync(USER_KEY)
  233. const u = normalizeUserPayload(raw)
  234. const curMobile =
  235. raw && raw.mobile != null ? String(raw.mobile).trim() : ''
  236. if (u && u.name && curMobile) {
  237. accounts = accounts.map((a) =>
  238. String(a.mobile) === curMobile && !(a.name && String(a.name).trim())
  239. ? { ...a, name: u.name }
  240. : a
  241. )
  242. }
  243. if (curMobile) {
  244. accounts = accounts.filter((a) => String(a.mobile).trim() !== curMobile)
  245. }
  246. } catch (e) {}
  247. if (!accounts.length) {
  248. uni.showToast({ title: '暂无其他已记住密码账号', icon: 'none' })
  249. return
  250. }
  251. this.accountPickerList = accounts
  252. this.accountPickerVisible = true
  253. },
  254. onPickSwitchAccount(acc) {
  255. this.closeAccountPicker()
  256. if (!acc || !acc.mobile || !acc.password) return
  257. setToken('')
  258. try {
  259. uni.removeStorageSync(USER_KEY)
  260. } catch (e) {}
  261. setPendingSwitchAccount({ mobile: acc.mobile, password: acc.password })
  262. uni.reLaunch({ url: '/pages/login/index' })
  263. },
  264. onLogout() {
  265. uni.showModal({
  266. title: '提示',
  267. content: '确定要退出登录吗?',
  268. success: (res) => {
  269. if (!res.confirm) return
  270. setToken('')
  271. try {
  272. uni.removeStorageSync(USER_KEY)
  273. } catch (e) {}
  274. this.user = {
  275. name: '',
  276. id: '',
  277. avatar: '',
  278. alias: '',
  279. orgName: ''
  280. }
  281. uni.reLaunch({ url: '/pages/login/index' })
  282. }
  283. })
  284. }
  285. }
  286. }
  287. </script>
  288. <style scoped>
  289. .profile-page {
  290. min-height: 100vh;
  291. background-color: #f5f5f5;
  292. padding: 24rpx;
  293. padding-bottom: calc(120rpx + constant(safe-area-inset-bottom));
  294. padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
  295. box-sizing: border-box;
  296. }
  297. .loading-bar {
  298. font-size: 24rpx;
  299. color: #999;
  300. margin-bottom: 16rpx;
  301. padding-left: 8rpx;
  302. }
  303. .card {
  304. background: #fff;
  305. border-radius: 16rpx;
  306. overflow: hidden;
  307. margin-bottom: 24rpx;
  308. }
  309. .row {
  310. display: flex;
  311. align-items: center;
  312. justify-content: space-between;
  313. min-height: 112rpx;
  314. padding: 0 32rpx;
  315. border-bottom: 1rpx solid #f0f0f0;
  316. }
  317. .row:last-child {
  318. border-bottom: none;
  319. }
  320. .row.no-arrow {
  321. border-bottom: none;
  322. }
  323. .qr-entry-card {
  324. margin-top: 8rpx;
  325. }
  326. .qr-row {
  327. border-bottom: none;
  328. }
  329. .value.sub {
  330. color: #999;
  331. max-width: 280rpx;
  332. }
  333. .chevron {
  334. font-size: 36rpx;
  335. color: #ccc;
  336. line-height: 1;
  337. margin-left: 8rpx;
  338. font-weight: 300;
  339. }
  340. .label {
  341. font-size: 30rpx;
  342. color: #333;
  343. flex-shrink: 0;
  344. }
  345. .row-right {
  346. display: flex;
  347. align-items: center;
  348. gap: 16rpx;
  349. min-width: 0;
  350. }
  351. .value {
  352. font-size: 28rpx;
  353. color: #333;
  354. overflow: hidden;
  355. text-overflow: ellipsis;
  356. white-space: nowrap;
  357. max-width: 400rpx;
  358. }
  359. .value.placeholder {
  360. color: #999;
  361. }
  362. .avatar-wrap {
  363. display: flex;
  364. align-items: center;
  365. justify-content: center;
  366. }
  367. .hint {
  368. display: block;
  369. font-size: 24rpx;
  370. color: #999;
  371. line-height: 1.5;
  372. padding: 0 16rpx;
  373. }
  374. .logout-section {
  375. margin-top: 48rpx;
  376. padding: 0 0 24rpx;
  377. display: flex;
  378. flex-direction: column;
  379. gap: 24rpx;
  380. }
  381. .action-btn {
  382. background: #fff;
  383. border-radius: 16rpx;
  384. min-height: 96rpx;
  385. line-height: 96rpx;
  386. text-align: center;
  387. font-size: 32rpx;
  388. }
  389. .check-update-btn {
  390. color: #259653;
  391. }
  392. .app-settings-btn {
  393. color: #333;
  394. }
  395. .change-pwd-btn {
  396. color: #333;
  397. }
  398. .switch-account-btn {
  399. color: #259653;
  400. }
  401. .logout-btn {
  402. background: #fff;
  403. border-radius: 16rpx;
  404. min-height: 96rpx;
  405. line-height: 96rpx;
  406. text-align: center;
  407. font-size: 32rpx;
  408. color: #e54d42;
  409. }
  410. .version-footer {
  411. display: block;
  412. text-align: center;
  413. margin-top: 40rpx;
  414. font-size: 24rpx;
  415. color: #999;
  416. line-height: 1.5;
  417. padding: 0 16rpx;
  418. }
  419. .account-picker-mask {
  420. position: fixed;
  421. left: 0;
  422. right: 0;
  423. top: 0;
  424. bottom: 0;
  425. background: rgba(0, 0, 0, 0.45);
  426. z-index: 999;
  427. display: flex;
  428. align-items: flex-end;
  429. justify-content: center;
  430. }
  431. .account-picker-sheet {
  432. width: 100%;
  433. max-height: 70vh;
  434. background: #fff;
  435. border-radius: 24rpx 24rpx 0 0;
  436. padding: 24rpx 0 env(safe-area-inset-bottom);
  437. box-sizing: border-box;
  438. }
  439. .account-picker-title {
  440. display: block;
  441. text-align: center;
  442. font-size: 30rpx;
  443. font-weight: 600;
  444. color: #222;
  445. padding: 16rpx 32rpx 24rpx;
  446. }
  447. .account-picker-list {
  448. max-height: 50vh;
  449. padding: 0 24rpx;
  450. box-sizing: border-box;
  451. }
  452. .account-picker-item {
  453. padding: 28rpx 24rpx;
  454. border-bottom: 1rpx solid #eee;
  455. }
  456. .account-picker-item:active {
  457. background: #f7f7f7;
  458. }
  459. .account-picker-item-text {
  460. font-size: 30rpx;
  461. color: #222;
  462. line-height: 1.4;
  463. word-break: break-all;
  464. }
  465. .account-picker-cancel {
  466. margin-top: 16rpx;
  467. padding: 28rpx;
  468. text-align: center;
  469. font-size: 30rpx;
  470. color: #666;
  471. }
  472. .account-picker-cancel:active {
  473. opacity: 0.7;
  474. }
  475. </style>