userInfo.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <!-- 个人信息页面 -->
  3. <view class="content">
  4. <view class="user-info">
  5. <!-- 头像 -->
  6. <view class="user-item">
  7. <view class="user-item-label">头像</view>
  8. <view class="user-item-value">
  9. <!-- #ifdef MP-WEIXIN -->
  10. <button class="avatar-btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" :disabled="uploadingAvatar">
  11. <view class="avatar-container">
  12. <image
  13. :src="formData.avatar || getFileUrl('img/personal/userDemo.png')"
  14. :mode="formData.avatar ? 'aspectFill' : 'widthFix'"
  15. class="avatar-img"
  16. />
  17. </view>
  18. </button>
  19. <!-- #endif -->
  20. <!-- #ifndef MP-WEIXIN -->
  21. <view class="avatar-container" @click="handleAvatarClick">
  22. <image
  23. :src="formData.avatar || getFileUrl('img/personal/userDemo.png')"
  24. :mode="formData.avatar ? 'aspectFill' : 'widthFix'"
  25. class="avatar-img"
  26. />
  27. </view>
  28. <!-- #endif -->
  29. </view>
  30. </view>
  31. <!-- 昵称:微信端 type="nickname" 可一键填入微信昵称 -->
  32. <view class="user-item">
  33. <view class="user-item-label">昵称</view>
  34. <view class="user-item-value">
  35. <input
  36. v-model="formData.nickname"
  37. type="nickname"
  38. class="user-input"
  39. placeholder="请输入昵称,可点击使用微信昵称"
  40. placeholder-style="color: #999;"
  41. />
  42. </view>
  43. </view>
  44. <!-- 性别 -->
  45. <view class="user-item">
  46. <view class="user-item-label">性别</view>
  47. <view class="user-item-value">
  48. <view class="gender-group">
  49. <view
  50. v-for="item in genderOptions"
  51. :key="item.value"
  52. class="gender-item"
  53. :class="{ active: formData.gender === item.value }"
  54. hover-class="none"
  55. @click="formData.gender = item.value"
  56. >
  57. <image
  58. :src="getFileUrl(formData.gender === item.value ? 'img/personal/sele2.png' : 'img/personal/sele1.png')"
  59. mode="widthFix"
  60. class="gender-img"
  61. />
  62. <text class="gender-text">{{ item.label }}</text>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. <!-- 生日 -->
  68. <view class="user-item">
  69. <view class="user-item-label">生日</view>
  70. <view class="user-item-value">
  71. <picker mode="date" :value="formData.birthday" @change="handleBirthdayChange"
  72. class="picker-wrapper">
  73. <view class="picker-content">
  74. <text :class="['picker-text', { placeholder: !formData.birthday }]">
  75. {{ formData.birthday || '请选择生日' }}
  76. </text>
  77. <view class="arrow-icon"></view>
  78. </view>
  79. </picker>
  80. </view>
  81. </view>
  82. <!-- 手机号码 -->
  83. <view class="user-item">
  84. <view class="user-item-label">手机号码</view>
  85. <view class="user-item-value">
  86. <view class="phone-wrapper">
  87. <text class="phone-number">{{ formData.phone || '1510987760' }}</text>
  88. <text class="change-btn" @click="handleChangePhone">更换</text>
  89. </view>
  90. </view>
  91. </view>
  92. </view>
  93. <view class="submit-wrap">
  94. <view class="submit-btn hover-active" @click="handleConfirm">确定</view>
  95. </view>
  96. </view>
  97. </template>
  98. <script setup>
  99. import { ref, onMounted } from 'vue';
  100. import { useUserStore } from '@/store/user.js';
  101. import { getFileUrl } from '@/utils/file.js';
  102. import { go } from '@/utils/utils.js';
  103. import { GetUserInfo, PostUpdateProfile, uploadFileToServer } from '@/api/dining.js';
  104. import { UPLOAD } from '@/settings/siteSetting.js';
  105. const userStore = useUserStore();
  106. const uploadingAvatar = ref(false);
  107. // 选择完头像后直接上传到 /file/upload,返回完整图片 URL
  108. async function doUploadAvatar(tempPath) {
  109. const res = await uploadFileToServer(tempPath);
  110. if (!res || typeof res !== 'string') return '';
  111. const p = res.trim();
  112. if (/^https?:\/\//i.test(p)) return p;
  113. const base = (UPLOAD || '').replace(/\/$/, '');
  114. return base ? base + p.replace(/^\//, '') : p;
  115. }
  116. function isTempAvatarPath(url) {
  117. if (!url || typeof url !== 'string') return false;
  118. const u = url.trim();
  119. return !/^https?:\/\//i.test(u) || /127\.0\.0\.1|localhost|\/tmp\/|wxfile:\/\//i.test(u);
  120. }
  121. // 性别选项
  122. const genderOptions = [
  123. { value: 'male', label: '男' },
  124. { value: 'female', label: '女' }
  125. ];
  126. // 表单数据(userId 用于更新资料接口必填)
  127. const formData = ref({
  128. userId: null,
  129. avatar: '',
  130. nickname: '',
  131. gender: 'male',
  132. birthday: '',
  133. phone: '1510987760'
  134. });
  135. // 从接口或本地缓存填充表单
  136. function fillFormFromUserInfo(userInfo) {
  137. if (!userInfo || typeof userInfo !== 'object') return;
  138. const g = userInfo.gender;
  139. const genderVal = g === '女' || g === 'female' ? 'female' : 'male';
  140. formData.value = {
  141. userId: userInfo.id ?? userInfo.userId ?? formData.value.userId ?? null,
  142. avatar: userInfo.avatarUrl ?? userInfo.avatar ?? formData.value.avatar ?? '',
  143. nickname: userInfo.nickName ?? userInfo.nickname ?? formData.value.nickname ?? '',
  144. gender: genderVal,
  145. birthday: userInfo.birthday ?? formData.value.birthday ?? '',
  146. phone: userInfo.phone ?? userInfo.mobile ?? userInfo.contactPhone ?? formData.value.phone ?? '1510987760'
  147. };
  148. }
  149. // 进入页面时调用获取用户信息接口,并填充表单;失败则用缓存
  150. onMounted(async () => {
  151. // 先用缓存快速展示
  152. fillFormFromUserInfo(userStore.getUserInfo || {});
  153. try {
  154. const res = await GetUserInfo();
  155. const data = res?.data ?? res ?? {};
  156. const userData = data?.data ?? data;
  157. if (userData && typeof userData === 'object') {
  158. fillFormFromUserInfo(userData);
  159. userStore.setUserInfo({ ...userStore.getUserInfo, ...userData });
  160. }
  161. } catch (e) {
  162. console.warn('获取用户信息失败,使用缓存:', e);
  163. }
  164. });
  165. // 处理头像点击
  166. const handleAvatarClick = () => {
  167. uni.showActionSheet({
  168. itemList: ['微信头像', '从相册选择', '拍照'],
  169. success: (res) => {
  170. if (res.tapIndex === 0) {
  171. // 选择微信头像
  172. handleWechatAvatar();
  173. } else if (res.tapIndex === 1) {
  174. // 选择相册
  175. handleChooseAlbum();
  176. } else if (res.tapIndex === 2) {
  177. // 选择拍照
  178. handleTakePhoto();
  179. }
  180. },
  181. fail: (err) => {
  182. console.log('取消选择', err);
  183. }
  184. });
  185. };
  186. // 选择头像后直接上传到服务器接口 /file/upload,把返回的 url 写入表单
  187. async function uploadAvatarAndSet(tempPath) {
  188. if (!tempPath) return;
  189. try {
  190. uploadingAvatar.value = true;
  191. const url = await doUploadAvatar(tempPath);
  192. formData.value.avatar = url || tempPath;
  193. uni.showToast({ title: url ? '头像上传成功' : '头像上传未返回地址', icon: url ? 'success' : 'none' });
  194. } catch (err) {
  195. uni.showToast({ title: err?.message || '头像上传失败', icon: 'none' });
  196. formData.value.avatar = tempPath;
  197. } finally {
  198. uploadingAvatar.value = false;
  199. }
  200. }
  201. // 微信小程序:选择头像后立即上传,防止重复点击导致 another chooseAvatar is in progress
  202. const onChooseAvatar = async (e) => {
  203. if (uploadingAvatar.value) return;
  204. const tempPath = e.detail?.avatarUrl;
  205. if (tempPath) await uploadAvatarAndSet(tempPath);
  206. };
  207. // 获取微信头像(仅非 MP-WEIXIN 时从 actionSheet 进入;MP-WEIXIN 已改用模板内 button chooseAvatar)
  208. const handleWechatAvatar = () => {
  209. // #ifdef MP-WEIXIN
  210. uni.showToast({
  211. title: '请点击上方头像区域选择',
  212. icon: 'none'
  213. });
  214. // #endif
  215. // #ifndef MP-WEIXIN
  216. uni.showToast({
  217. title: '当前环境不支持',
  218. icon: 'none'
  219. });
  220. // #endif
  221. };
  222. // 选择相册:选择后立即上传
  223. const handleChooseAlbum = () => {
  224. uni.chooseImage({
  225. count: 1,
  226. sizeType: ['original', 'compressed'],
  227. sourceType: ['album'],
  228. success: async (res) => {
  229. await uploadAvatarAndSet(res.tempFilePaths[0]);
  230. },
  231. fail: (err) => {
  232. console.log('选择相册失败', err);
  233. }
  234. });
  235. };
  236. // 拍照:选择后立即上传
  237. const handleTakePhoto = () => {
  238. uni.chooseImage({
  239. count: 1,
  240. sizeType: ['original', 'compressed'],
  241. sourceType: ['camera'],
  242. success: async (res) => {
  243. await uploadAvatarAndSet(res.tempFilePaths[0]);
  244. },
  245. fail: (err) => {
  246. console.log('拍照失败', err);
  247. }
  248. });
  249. };
  250. // 确定:先默认调用一次上传头像接口,等待返回后,再把返回的图片 url 传给 dining/user/updateProfile
  251. const handleConfirm = async () => {
  252. const uid = formData.value.userId;
  253. if (uid == null || uid === '') {
  254. uni.showToast({ title: '请先登录', icon: 'none' });
  255. return;
  256. }
  257. let avatarUrl = formData.value.avatar || '';
  258. // 有头像且为本地临时路径时,先调用自己的上传接口,等待返回后再走更新资料
  259. if (avatarUrl && isTempAvatarPath(avatarUrl)) {
  260. try {
  261. avatarUrl = await doUploadAvatar(avatarUrl);
  262. if (!avatarUrl) {
  263. uni.showToast({ title: '头像上传未返回地址', icon: 'none' });
  264. return;
  265. }
  266. } catch (err) {
  267. uni.showToast({ title: err?.message || '头像上传失败', icon: 'none' });
  268. return;
  269. }
  270. }
  271. const genderStr = formData.value.gender === 'female' ? '女' : '男';
  272. const dto = { userId: Number(uid) };
  273. if (avatarUrl) dto.avatarUrl = avatarUrl;
  274. if (formData.value.nickname) dto.nickName = formData.value.nickname;
  275. dto.gender = genderStr;
  276. if (formData.value.birthday) dto.birthday = formData.value.birthday;
  277. try {
  278. await PostUpdateProfile(dto);
  279. userStore.setUserInfo({
  280. ...userStore.getUserInfo,
  281. avatarUrl: avatarUrl || formData.value.avatar,
  282. nickName: formData.value.nickname
  283. });
  284. uni.showToast({ title: '保存成功', icon: 'success' });
  285. setTimeout(() => uni.navigateBack(), 1500);
  286. } catch (e) {
  287. uni.showToast({ title: e?.message || '保存失败', icon: 'none' });
  288. }
  289. };
  290. // 处理生日选择
  291. const handleBirthdayChange = (e) => {
  292. formData.value.birthday = e.detail.value;
  293. };
  294. // 处理更换手机号
  295. const handleChangePhone = () => {
  296. go('/pages/personal/setPhone');
  297. };
  298. </script>
  299. <style scoped lang="scss">
  300. .content {
  301. width: 100%;
  302. min-height: 100vh;
  303. background-color: #F5F5F5;
  304. box-sizing: border-box;
  305. padding: 20rpx;
  306. }
  307. .submit-wrap {
  308. margin-top: 60rpx;
  309. padding: 0 20rpx;
  310. }
  311. .submit-btn {
  312. width: 100%;
  313. height: 88rpx;
  314. line-height: 88rpx;
  315. text-align: center;
  316. font-size: 32rpx;
  317. color: #fff;
  318. background: linear-gradient(90deg, #FCB73F 0%, #FC743D 100%);
  319. border-radius: 44rpx;
  320. }
  321. .user-info {
  322. width: 100%;
  323. border-radius: 23rpx;
  324. background-color: #fff;
  325. box-sizing: border-box;
  326. padding: 0 30rpx;
  327. .user-item {
  328. display: flex;
  329. align-items: center;
  330. justify-content: space-between;
  331. padding: 30rpx 0;
  332. border-bottom: 1rpx solid #F5F5F5;
  333. &:last-child {
  334. border-bottom: none;
  335. }
  336. .user-item-label {
  337. font-size: 28rpx;
  338. color: #333;
  339. font-weight: 400;
  340. }
  341. .user-item-value {
  342. display: flex;
  343. align-items: center;
  344. justify-content: flex-end;
  345. flex: 1;
  346. }
  347. }
  348. }
  349. // 头像样式
  350. .avatar-btn {
  351. padding: 0;
  352. margin: 0;
  353. background: transparent;
  354. border: none;
  355. line-height: 1;
  356. &::after {
  357. border: none;
  358. }
  359. &[disabled] {
  360. opacity: 0.7;
  361. }
  362. }
  363. .avatar-container {
  364. width: 76rpx;
  365. height: 76rpx;
  366. border-radius: 8rpx;
  367. display: flex;
  368. align-items: center;
  369. justify-content: center;
  370. position: relative;
  371. overflow: hidden;
  372. box-sizing: border-box;
  373. .avatar-img {
  374. width: 100%;
  375. height: 100%;
  376. border-radius: 50%;
  377. }
  378. }
  379. // 输入框样式
  380. .user-input {
  381. flex: 1;
  382. text-align: right;
  383. font-size: 28rpx;
  384. color: #333;
  385. }
  386. // 性别选择样式
  387. .gender-group {
  388. display: flex;
  389. align-items: center;
  390. gap: 40rpx;
  391. .gender-item {
  392. display: flex;
  393. align-items: center;
  394. gap: 12rpx;
  395. cursor: pointer;
  396. .gender-text {
  397. font-size: 28rpx;
  398. color: #333;
  399. }
  400. &.active {
  401. .gender-text {
  402. color: #FF6B35;
  403. }
  404. }
  405. }
  406. }
  407. // 日期选择器样式
  408. .picker-wrapper {
  409. width: 100%;
  410. display: flex;
  411. justify-content: flex-end;
  412. }
  413. .picker-content {
  414. display: flex;
  415. align-items: center;
  416. gap: 12rpx;
  417. .picker-text {
  418. font-size: 28rpx;
  419. color: #333;
  420. &.placeholder {
  421. color: #999;
  422. }
  423. }
  424. .arrow-icon {
  425. width: 12rpx;
  426. height: 12rpx;
  427. border-right: 2rpx solid #999;
  428. border-top: 2rpx solid #999;
  429. transform: rotate(45deg);
  430. margin-right: 4rpx;
  431. }
  432. }
  433. // 手机号样式
  434. .phone-wrapper {
  435. display: flex;
  436. align-items: center;
  437. gap: 20rpx;
  438. .phone-number {
  439. font-size: 28rpx;
  440. color: #333;
  441. }
  442. .change-btn {
  443. font-size: 28rpx;
  444. color: #F47D1F;
  445. cursor: pointer;
  446. }
  447. }
  448. .gender-img {
  449. width: 26rpx;
  450. height: 26rpx;
  451. }
  452. </style>