userInfo.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 { 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. // 初始化用户信息(头像用 avatarUrl/avatar,与缓存字段一致)
  136. onMounted(() => {
  137. const userInfo = userStore.getUserInfo || {};
  138. const g = userInfo.gender;
  139. const genderVal = g === '女' || g === 'female' ? 'female' : 'male';
  140. formData.value = {
  141. userId: userInfo.id ?? userInfo.userId ?? null,
  142. avatar: userInfo.avatarUrl ?? userInfo.avatar ?? '',
  143. nickname: userInfo.nickName ?? userInfo.nickname ?? '',
  144. gender: genderVal,
  145. birthday: userInfo.birthday ?? '',
  146. phone: userInfo.phone ?? userInfo.mobile ?? '1510987760'
  147. };
  148. });
  149. // 处理头像点击
  150. const handleAvatarClick = () => {
  151. uni.showActionSheet({
  152. itemList: ['微信头像', '从相册选择', '拍照'],
  153. success: (res) => {
  154. if (res.tapIndex === 0) {
  155. // 选择微信头像
  156. handleWechatAvatar();
  157. } else if (res.tapIndex === 1) {
  158. // 选择相册
  159. handleChooseAlbum();
  160. } else if (res.tapIndex === 2) {
  161. // 选择拍照
  162. handleTakePhoto();
  163. }
  164. },
  165. fail: (err) => {
  166. console.log('取消选择', err);
  167. }
  168. });
  169. };
  170. // 选择头像后直接上传到服务器接口 /file/upload,把返回的 url 写入表单
  171. async function uploadAvatarAndSet(tempPath) {
  172. if (!tempPath) return;
  173. try {
  174. uploadingAvatar.value = true;
  175. const url = await doUploadAvatar(tempPath);
  176. formData.value.avatar = url || tempPath;
  177. uni.showToast({ title: url ? '头像上传成功' : '头像上传未返回地址', icon: url ? 'success' : 'none' });
  178. } catch (err) {
  179. uni.showToast({ title: err?.message || '头像上传失败', icon: 'none' });
  180. formData.value.avatar = tempPath;
  181. } finally {
  182. uploadingAvatar.value = false;
  183. }
  184. }
  185. // 微信小程序:选择头像后立即上传,防止重复点击导致 another chooseAvatar is in progress
  186. const onChooseAvatar = async (e) => {
  187. if (uploadingAvatar.value) return;
  188. const tempPath = e.detail?.avatarUrl;
  189. if (tempPath) await uploadAvatarAndSet(tempPath);
  190. };
  191. // 获取微信头像(仅非 MP-WEIXIN 时从 actionSheet 进入;MP-WEIXIN 已改用模板内 button chooseAvatar)
  192. const handleWechatAvatar = () => {
  193. // #ifdef MP-WEIXIN
  194. uni.showToast({
  195. title: '请点击上方头像区域选择',
  196. icon: 'none'
  197. });
  198. // #endif
  199. // #ifndef MP-WEIXIN
  200. uni.showToast({
  201. title: '当前环境不支持',
  202. icon: 'none'
  203. });
  204. // #endif
  205. };
  206. // 选择相册:选择后立即上传
  207. const handleChooseAlbum = () => {
  208. uni.chooseImage({
  209. count: 1,
  210. sizeType: ['original', 'compressed'],
  211. sourceType: ['album'],
  212. success: async (res) => {
  213. await uploadAvatarAndSet(res.tempFilePaths[0]);
  214. },
  215. fail: (err) => {
  216. console.log('选择相册失败', err);
  217. }
  218. });
  219. };
  220. // 拍照:选择后立即上传
  221. const handleTakePhoto = () => {
  222. uni.chooseImage({
  223. count: 1,
  224. sizeType: ['original', 'compressed'],
  225. sourceType: ['camera'],
  226. success: async (res) => {
  227. await uploadAvatarAndSet(res.tempFilePaths[0]);
  228. },
  229. fail: (err) => {
  230. console.log('拍照失败', err);
  231. }
  232. });
  233. };
  234. // 确定:先默认调用一次上传头像接口,等待返回后,再把返回的图片 url 传给 dining/user/updateProfile
  235. const handleConfirm = async () => {
  236. const uid = formData.value.userId;
  237. if (uid == null || uid === '') {
  238. uni.showToast({ title: '请先登录', icon: 'none' });
  239. return;
  240. }
  241. let avatarUrl = formData.value.avatar || '';
  242. // 有头像且为本地临时路径时,先调用自己的上传接口,等待返回后再走更新资料
  243. if (avatarUrl && isTempAvatarPath(avatarUrl)) {
  244. try {
  245. avatarUrl = await doUploadAvatar(avatarUrl);
  246. if (!avatarUrl) {
  247. uni.showToast({ title: '头像上传未返回地址', icon: 'none' });
  248. return;
  249. }
  250. } catch (err) {
  251. uni.showToast({ title: err?.message || '头像上传失败', icon: 'none' });
  252. return;
  253. }
  254. }
  255. const genderStr = formData.value.gender === 'female' ? '女' : '男';
  256. const dto = { userId: Number(uid) };
  257. if (avatarUrl) dto.avatarUrl = avatarUrl;
  258. if (formData.value.nickname) dto.nickName = formData.value.nickname;
  259. dto.gender = genderStr;
  260. if (formData.value.birthday) dto.birthday = formData.value.birthday;
  261. try {
  262. await PostUpdateProfile(dto);
  263. userStore.setUserInfo({
  264. ...userStore.getUserInfo,
  265. avatarUrl: avatarUrl || formData.value.avatar,
  266. nickName: formData.value.nickname
  267. });
  268. uni.showToast({ title: '保存成功', icon: 'success' });
  269. setTimeout(() => uni.navigateBack(), 1500);
  270. } catch (e) {
  271. uni.showToast({ title: e?.message || '保存失败', icon: 'none' });
  272. }
  273. };
  274. // 处理生日选择
  275. const handleBirthdayChange = (e) => {
  276. formData.value.birthday = e.detail.value;
  277. };
  278. // 处理更换手机号
  279. const handleChangePhone = () => {
  280. go('/pages/personal/setPhone');
  281. };
  282. </script>
  283. <style scoped lang="scss">
  284. .content {
  285. width: 100%;
  286. min-height: 100vh;
  287. background-color: #F5F5F5;
  288. box-sizing: border-box;
  289. padding: 20rpx;
  290. }
  291. .submit-wrap {
  292. margin-top: 60rpx;
  293. padding: 0 20rpx;
  294. }
  295. .submit-btn {
  296. width: 100%;
  297. height: 88rpx;
  298. line-height: 88rpx;
  299. text-align: center;
  300. font-size: 32rpx;
  301. color: #fff;
  302. background: linear-gradient(90deg, #FCB73F 0%, #FC743D 100%);
  303. border-radius: 44rpx;
  304. }
  305. .user-info {
  306. width: 100%;
  307. border-radius: 23rpx;
  308. background-color: #fff;
  309. box-sizing: border-box;
  310. padding: 0 30rpx;
  311. .user-item {
  312. display: flex;
  313. align-items: center;
  314. justify-content: space-between;
  315. padding: 30rpx 0;
  316. border-bottom: 1rpx solid #F5F5F5;
  317. &:last-child {
  318. border-bottom: none;
  319. }
  320. .user-item-label {
  321. font-size: 28rpx;
  322. color: #333;
  323. font-weight: 400;
  324. }
  325. .user-item-value {
  326. display: flex;
  327. align-items: center;
  328. justify-content: flex-end;
  329. flex: 1;
  330. }
  331. }
  332. }
  333. // 头像样式
  334. .avatar-btn {
  335. padding: 0;
  336. margin: 0;
  337. background: transparent;
  338. border: none;
  339. line-height: 1;
  340. &::after {
  341. border: none;
  342. }
  343. &[disabled] {
  344. opacity: 0.7;
  345. }
  346. }
  347. .avatar-container {
  348. width: 76rpx;
  349. height: 76rpx;
  350. border-radius: 8rpx;
  351. display: flex;
  352. align-items: center;
  353. justify-content: center;
  354. position: relative;
  355. overflow: hidden;
  356. box-sizing: border-box;
  357. .avatar-img {
  358. width: 100%;
  359. height: 100%;
  360. border-radius: 50%;
  361. }
  362. }
  363. // 输入框样式
  364. .user-input {
  365. flex: 1;
  366. text-align: right;
  367. font-size: 28rpx;
  368. color: #333;
  369. }
  370. // 性别选择样式
  371. .gender-group {
  372. display: flex;
  373. align-items: center;
  374. gap: 40rpx;
  375. .gender-item {
  376. display: flex;
  377. align-items: center;
  378. gap: 12rpx;
  379. cursor: pointer;
  380. .gender-text {
  381. font-size: 28rpx;
  382. color: #333;
  383. }
  384. &.active {
  385. .gender-text {
  386. color: #FF6B35;
  387. }
  388. }
  389. }
  390. }
  391. // 日期选择器样式
  392. .picker-wrapper {
  393. width: 100%;
  394. display: flex;
  395. justify-content: flex-end;
  396. }
  397. .picker-content {
  398. display: flex;
  399. align-items: center;
  400. gap: 12rpx;
  401. .picker-text {
  402. font-size: 28rpx;
  403. color: #333;
  404. &.placeholder {
  405. color: #999;
  406. }
  407. }
  408. .arrow-icon {
  409. width: 12rpx;
  410. height: 12rpx;
  411. border-right: 2rpx solid #999;
  412. border-top: 2rpx solid #999;
  413. transform: rotate(45deg);
  414. margin-right: 4rpx;
  415. }
  416. }
  417. // 手机号样式
  418. .phone-wrapper {
  419. display: flex;
  420. align-items: center;
  421. gap: 20rpx;
  422. .phone-number {
  423. font-size: 28rpx;
  424. color: #333;
  425. }
  426. .change-btn {
  427. font-size: 28rpx;
  428. color: #F47D1F;
  429. cursor: pointer;
  430. }
  431. }
  432. .gender-img {
  433. width: 26rpx;
  434. height: 26rpx;
  435. }
  436. </style>