edit-profile.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <template>
  2. <view class="edit-profile">
  3. <view class="status-bar-placeholder" :style="{height: statusBarHeight + 'px', backgroundColor: '#FFF9F9'}"></view>
  4. <!-- 顶部导航栏 -->
  5. <view class="header">
  6. <view class="back-btn" @click="handleBack">
  7. <text class="back-icon">←</text>
  8. </view>
  9. <text class="header-title">编辑资料</text>
  10. <view class="right-empty"></view>
  11. </view>
  12. <scroll-view scroll-y class="content">
  13. <!-- 头像上传区域 -->
  14. <view class="avatar-section">
  15. <view class="avatar-container" @click="chooseAvatar">
  16. <image class="avatar" :src="userInfo.avatar || defaultAvatar" mode="aspectFill"></image>
  17. <view class="avatar-upload-btn">
  18. <text class="upload-icon">📷</text>
  19. </view>
  20. </view>
  21. <text class="avatar-text">点击更换头像</text>
  22. </view>
  23. <!-- 表单区域 -->
  24. <view class="form-section">
  25. <!-- 真实姓名 -->
  26. <view class="form-item">
  27. <view class="form-label">
  28. <text class="label-icon">👤</text>
  29. <text class="label-text">真实姓名</text>
  30. </view>
  31. <input
  32. class="form-input"
  33. v-model="userInfo.real_name"
  34. placeholder="请输入真实姓名"
  35. placeholder-style="color: #999"
  36. >
  37. </view>
  38. <!-- 性别 -->
  39. <view class="form-item">
  40. <view class="form-label">
  41. <text class="label-icon">⚧️</text>
  42. <text class="label-text">性别</text>
  43. </view>
  44. <picker class="form-picker gender-picker" @change="handleGenderChange" :value="genderIndex"
  45. :range="genderOptions">
  46. <view class="picker-content">
  47. {{ genderLabel }}
  48. </view>
  49. </picker>
  50. </view>
  51. <!-- 出生日期 -->
  52. <view class="form-item">
  53. <view class="form-label">
  54. <text class="label-icon">📅</text>
  55. <text class="label-text">出生日期</text>
  56. </view>
  57. <picker class="form-picker" mode="date" :value="userInfo.birth_date" @change="handleBirthChange">
  58. <view class="picker-content">
  59. {{ userInfo.birth_date || '请选择出生日期' }}
  60. </view>
  61. </picker>
  62. </view>
  63. <!-- 邮箱 -->
  64. <view class="form-item">
  65. <view class="form-label">
  66. <text class="label-icon">📧</text>
  67. <text class="label-text">邮箱</text>
  68. </view>
  69. <input
  70. class="form-input"
  71. v-model="userInfo.email"
  72. placeholder="请输入邮箱"
  73. placeholder-style="color: #999"
  74. type="email"
  75. >
  76. </view>
  77. <!-- 所在地区 -->
  78. <view class="form-item">
  79. <view class="form-label">
  80. <text class="label-icon">📍</text>
  81. <text class="label-text">所在地区</text>
  82. </view>
  83. <picker
  84. class="form-picker"
  85. mode="multiSelector"
  86. :value="multiIndex"
  87. :range="multiArray"
  88. @columnchange="handleColumnChange"
  89. @change="handleMultiPickerChange"
  90. >
  91. <view class="picker-content">{{ selectedArea || '请选择地区' }}</view>
  92. </picker>
  93. </view>
  94. <!-- 详细地址 -->
  95. <view class="form-item">
  96. <view class="form-label">
  97. <text class="label-icon">🏠</text>
  98. <text class="label-text">详细地址</text>
  99. </view>
  100. <textarea
  101. class="form-textarea"
  102. v-model="userInfo.address_detail"
  103. placeholder="请输入详细地址"
  104. placeholder-style="color: #999"
  105. maxlength="255"
  106. auto-height
  107. ></textarea>
  108. </view>
  109. <!-- 个人简介 -->
  110. <view class="form-item">
  111. <view class="form-label">
  112. <text class="label-icon">📝</text>
  113. <text class="label-text">个人简介</text>
  114. </view>
  115. <textarea
  116. class="form-textarea"
  117. v-model="userInfo.profile"
  118. placeholder="请输入个人简介"
  119. placeholder-style="color: #999"
  120. maxlength="200"
  121. auto-height
  122. ></textarea>
  123. </view>
  124. </view>
  125. <!-- 保存按钮 -->
  126. <view class="save-btn-section">
  127. <button class="save-btn" @click="handleSave">保存</button>
  128. </view>
  129. </scroll-view>
  130. </view>
  131. </template>
  132. <script>
  133. import api from '../../utils/api.js'
  134. export default {
  135. data() {
  136. return {
  137. statusBarHeight: 0,
  138. // 默认头像
  139. defaultAvatar: 'https://q.qlogo.cn/qqapp/1105591438/05E13358B43B3D39D6AC2D6888828201/100',
  140. // 用户信息(与后端字段基本对应)
  141. userInfo: {
  142. avatar: '',
  143. real_name: '',
  144. gender: null, // 1: 男, 2: 女
  145. birth_date: '',
  146. email: '',
  147. province_id: null,
  148. city_id: null,
  149. area_id: null,
  150. address_detail: '',
  151. province_name: '',
  152. city_name: '',
  153. area_name: '',
  154. profile: ''
  155. },
  156. // 当前红娘ID(用于更新)
  157. matchmakerId: null,
  158. // 省市区多列选择器数据(名称数组)
  159. multiArray: [
  160. [], // 省
  161. [], // 市
  162. [] // 区
  163. ],
  164. multiIndex: [0, 0, 0],
  165. selectedArea: '',
  166. // 后端返回的省市区原始列表(包含id和name)
  167. provinces: [],
  168. cities: [],
  169. areas: [],
  170. // 性别选项
  171. genderOptions: ['男', '女'],
  172. genderIndex: -1
  173. }
  174. },
  175. computed: {
  176. genderLabel() {
  177. if (this.userInfo.gender === 1) return '男'
  178. if (this.userInfo.gender === 2) return '女'
  179. return '请选择性别'
  180. }
  181. },
  182. onLoad() {
  183. this.initPage()
  184. //获取状态栏高度
  185. const systemInfo = uni.getSystemInfoSync()
  186. this.statusBarHeight = systemInfo.statusBarHeight
  187. },
  188. methods: {
  189. async initPage() {
  190. await this.loadUserInfo()
  191. await this.loadProvincesAndInit()
  192. },
  193. // 加载红娘资料
  194. async loadUserInfo() {
  195. try {
  196. const basicUser = uni.getStorageSync('userInfo')
  197. const userId = basicUser && basicUser.userId ? basicUser.userId : null
  198. if (!userId) {
  199. uni.showToast({ title: '未登录,无法加载资料', icon: 'none' })
  200. return
  201. }
  202. const matchmakerInfo = await api.matchmaker.getByUserId(userId)
  203. if (matchmakerInfo) {
  204. // 映射后端字段到本地表单
  205. this.matchmakerId = matchmakerInfo.matchmakerId || matchmakerInfo.matchmaker_id || null
  206. this.userInfo.avatar = matchmakerInfo.avatar_url || ''
  207. this.userInfo.real_name = matchmakerInfo.real_name || ''
  208. this.userInfo.gender = typeof matchmakerInfo.gender === 'number' ? matchmakerInfo.gender : null
  209. this.userInfo.birth_date = matchmakerInfo.birth_date || ''
  210. this.userInfo.email = matchmakerInfo.email || ''
  211. this.userInfo.address_detail = matchmakerInfo.address_detail || ''
  212. this.userInfo.province_id = matchmakerInfo.province_id || null
  213. this.userInfo.city_id = matchmakerInfo.city_id || null
  214. this.userInfo.area_id = matchmakerInfo.area_id || null
  215. this.userInfo.province_name = matchmakerInfo.province_name || ''
  216. this.userInfo.city_name = matchmakerInfo.city_name || ''
  217. this.userInfo.area_name = matchmakerInfo.area_name || ''
  218. this.userInfo.profile = matchmakerInfo.profile || ''
  219. // 性别索引
  220. if (this.userInfo.gender === 1) this.genderIndex = 0
  221. else if (this.userInfo.gender === 2) this.genderIndex = 1
  222. // 如果后端返回了省市区名称,直接使用
  223. if (matchmakerInfo.province_name && matchmakerInfo.city_name && matchmakerInfo.area_name) {
  224. this.selectedArea = `${matchmakerInfo.province_name} ${matchmakerInfo.city_name} ${matchmakerInfo.area_name}`
  225. }
  226. }
  227. } catch (e) {
  228. uni.showToast({ title: '加载资料失败', icon: 'none' })
  229. }
  230. },
  231. // 从后端加载省列表并根据已有省市区初始化
  232. async loadProvincesAndInit() {
  233. try {
  234. const list = await api.area.getProvinces()
  235. this.provinces = list || []
  236. this.multiArray[0] = this.provinces.map(p => p.name)
  237. if (this.userInfo.province_id) {
  238. const pIndex = this.provinces.findIndex(p => p.id === this.userInfo.province_id)
  239. if (pIndex >= 0) {
  240. this.multiIndex[0] = pIndex
  241. await this.updateCityList(pIndex, true)
  242. }
  243. }
  244. } catch (e) {
  245. }
  246. },
  247. // 更新城市列表(根据省索引),init 表示是否按已有 city_id 初始化
  248. async updateCityList(provinceIndex, init = false) {
  249. const province = this.provinces[provinceIndex]
  250. if (!province) return
  251. try {
  252. const list = await api.area.getCities(province.id)
  253. this.cities = list || []
  254. this.multiArray[1] = this.cities.map(c => c.name)
  255. this.multiArray[2] = []
  256. this.multiIndex[1] = 0
  257. this.multiIndex[2] = 0
  258. if (init && this.userInfo.city_id) {
  259. const cIndex = this.cities.findIndex(c => c.id === this.userInfo.city_id)
  260. if (cIndex >= 0) {
  261. this.multiIndex[1] = cIndex
  262. await this.updateAreaList(provinceIndex, cIndex, true)
  263. }
  264. }
  265. } catch (e) {
  266. }
  267. },
  268. // 更新区县列表
  269. async updateAreaList(provinceIndex, cityIndex, init = false) {
  270. const city = this.cities[cityIndex]
  271. if (!city) return
  272. try {
  273. const list = await api.area.getAreas(city.id)
  274. this.areas = list || []
  275. this.multiArray[2] = this.areas.map(a => a.name)
  276. this.multiIndex[2] = 0
  277. if (init && this.userInfo.area_id) {
  278. const aIndex = this.areas.findIndex(a => a.id === this.userInfo.area_id)
  279. if (aIndex >= 0) {
  280. this.multiIndex[2] = aIndex
  281. }
  282. }
  283. } catch (e) {
  284. }
  285. },
  286. // 处理列变化
  287. handleColumnChange(e) {
  288. const column = e.detail.column
  289. const value = e.detail.value
  290. this.multiIndex[column] = value
  291. // 根据列索引更新对应的数据
  292. if (column === 0) {
  293. // 省份变化,更新城市和区县
  294. this.updateCityList(value, false)
  295. } else if (column === 1) {
  296. // 城市变化,更新区县
  297. this.updateAreaList(this.multiIndex[0], value, false)
  298. }
  299. },
  300. // 处理多列选择器确认
  301. handleMultiPickerChange(e) {
  302. this.multiIndex = e.detail.value
  303. const p = this.provinces[this.multiIndex[0]]
  304. const c = this.cities[this.multiIndex[1]]
  305. const a = this.areas[this.multiIndex[2]]
  306. const provinceName = p ? p.name : ''
  307. const cityName = c ? c.name : ''
  308. const areaName = a ? a.name : ''
  309. this.selectedArea = provinceName && cityName && areaName
  310. ? `${provinceName} ${cityName} ${areaName}`
  311. : ''
  312. // 更新用户信息中的 id 和名称
  313. this.userInfo.province_id = p ? p.id : null
  314. this.userInfo.city_id = c ? c.id : null
  315. this.userInfo.area_id = a ? a.id : null
  316. this.userInfo.province_name = provinceName
  317. this.userInfo.city_name = cityName
  318. this.userInfo.area_name = areaName
  319. },
  320. // 返回上一页
  321. async handleBack() {
  322. uni.navigateBack({
  323. delta: 1
  324. })
  325. },
  326. // 性别选择变化
  327. handleGenderChange(e) {
  328. const index = Number(e.detail.value)
  329. this.genderIndex = index
  330. this.userInfo.gender = index >= 0 ? index + 1 : null
  331. },
  332. // 出生日期选择
  333. handleBirthChange(e) {
  334. this.userInfo.birth_date = e.detail.value
  335. },
  336. // 选择头像
  337. chooseAvatar() {
  338. uni.chooseImage({
  339. count: 1,
  340. sizeType: ['compressed'],
  341. sourceType: ['album', 'camera'],
  342. success: (res) => {
  343. const tempFilePath = res.tempFilePaths[0]
  344. this.uploadAvatar(tempFilePath)
  345. },
  346. fail: (err) => {
  347. if (err.errMsg && !err.errMsg.includes('cancel')) {
  348. uni.showToast({ title: '选择图片失败', icon: 'none' })
  349. }
  350. }
  351. })
  352. },
  353. // 上传头像
  354. async uploadAvatar(filePath) {
  355. uni.showLoading({ title: '上传中...' })
  356. try {
  357. const uploadRes = await api.matchmaker.uploadAvatar(filePath)
  358. if (uploadRes) {
  359. this.userInfo.avatar = uploadRes
  360. uni.showToast({ title: '头像上传成功', icon: 'success' })
  361. } else {
  362. uni.showToast({ title: '上传失败', icon: 'none' })
  363. }
  364. } catch (e) {
  365. console.error('上传头像失败:', e)
  366. const errMsg = e.message || (typeof e === 'string' ? e : '上传失败,请重试')
  367. uni.showToast({ title: errMsg, icon: 'none', duration: 3000 })
  368. } finally {
  369. uni.hideLoading()
  370. }
  371. },
  372. // 保存资料
  373. async handleSave() {
  374. // 验证表单
  375. if (!this.userInfo.real_name) {
  376. uni.showToast({
  377. title: '请输入真实姓名',
  378. icon: 'none'
  379. })
  380. return
  381. }
  382. if (!this.userInfo.gender) {
  383. uni.showToast({
  384. title: '请选择性别',
  385. icon: 'none'
  386. })
  387. return
  388. }
  389. // 简单邮箱格式校验(若填写)
  390. if (this.userInfo.email) {
  391. const emailRegex = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
  392. if (!emailRegex.test(this.userInfo.email)) {
  393. uni.showToast({ title: '邮箱格式不正确', icon: 'none' })
  394. return
  395. }
  396. }
  397. try {
  398. if (!this.matchmakerId) {
  399. uni.showToast({ title: '红娘信息缺失,无法保存', icon: 'none' })
  400. return
  401. }
  402. const payload = {
  403. // 注意:后端 Jackson 使用了 SNAKE_CASE 策略
  404. // email、gender、profile 名字不变,其他字段用下划线命名
  405. avatar_url: this.userInfo.avatar,
  406. email: this.userInfo.email,
  407. real_name: this.userInfo.real_name,
  408. gender: this.userInfo.gender,
  409. birth_date: this.userInfo.birth_date,
  410. address_detail: this.userInfo.address_detail,
  411. province_id: this.userInfo.province_id,
  412. city_id: this.userInfo.city_id,
  413. area_id: this.userInfo.area_id,
  414. profile: this.userInfo.profile
  415. }
  416. await api.matchmaker.updateProfile(this.matchmakerId, payload)
  417. uni.showToast({
  418. title: '资料保存成功',
  419. icon: 'success'
  420. })
  421. setTimeout(() => {
  422. this.handleBack()
  423. }, 1200)
  424. } catch (e) {
  425. uni.showToast({ title: '保存失败,请稍后重试', icon: 'none' })
  426. }
  427. }
  428. }
  429. }
  430. </script>
  431. <style lang="scss" scoped>
  432. .edit-profile {
  433. min-height: 100vh;
  434. background: #FFFFFF;
  435. display: flex;
  436. flex-direction: column;
  437. }
  438. /* 顶部导航栏 */
  439. .header {
  440. display: flex;
  441. align-items: center;
  442. justify-content: space-between;
  443. padding: 25rpx 30rpx;
  444. padding-top: calc(25rpx + env(safe-area-inset-top));
  445. background: #FFFFFF;
  446. border-bottom: 1rpx solid #F0F0F0;
  447. .back-btn {
  448. width: 44rpx;
  449. height: 44rpx;
  450. display: flex;
  451. align-items: center;
  452. justify-content: center;
  453. .back-icon {
  454. font-size: 40rpx;
  455. color: #333;
  456. }
  457. }
  458. .header-title {
  459. font-size: 38rpx;
  460. font-weight: bold;
  461. color: #333;
  462. }
  463. .right-empty {
  464. width: 44rpx;
  465. }
  466. }
  467. .content {
  468. flex: 1;
  469. padding-bottom: 40rpx;
  470. }
  471. /* 头像上传区域 */
  472. .avatar-section {
  473. display: flex;
  474. flex-direction: column;
  475. align-items: center;
  476. padding: 40rpx 0;
  477. border-bottom: 1rpx solid #F0F0F0;
  478. .avatar-container {
  479. position: relative;
  480. width: 160rpx;
  481. height: 160rpx;
  482. margin-bottom: 20rpx;
  483. .avatar {
  484. width: 100%;
  485. height: 100%;
  486. border-radius: 50%;
  487. background: #F5F5F5;
  488. }
  489. .avatar-upload-btn {
  490. position: absolute;
  491. bottom: 0;
  492. right: 0;
  493. width: 48rpx;
  494. height: 48rpx;
  495. display: flex;
  496. align-items: center;
  497. justify-content: center;
  498. background: #E91E63;
  499. border-radius: 50%;
  500. box-shadow: 0 2rpx 8rpx rgba(233, 30, 99, 0.3);
  501. .upload-icon {
  502. font-size: 24rpx;
  503. }
  504. }
  505. }
  506. .avatar-text {
  507. font-size: 28rpx;
  508. color: #E91E63;
  509. font-weight: 500;
  510. }
  511. }
  512. /* 表单区域 */
  513. .form-section {
  514. padding: 0 30rpx;
  515. .form-item {
  516. display: flex;
  517. flex-direction: column;
  518. padding: 25rpx 0;
  519. border-bottom: 1rpx solid #F5F5F5;
  520. &:last-child {
  521. border-bottom: none;
  522. }
  523. .form-label {
  524. display: flex;
  525. align-items: center;
  526. margin-bottom: 15rpx;
  527. .label-icon {
  528. font-size: 28rpx;
  529. margin-right: 12rpx;
  530. }
  531. .label-text {
  532. font-size: 28rpx;
  533. color: #333;
  534. font-weight: 500;
  535. }
  536. }
  537. .form-input,
  538. .form-picker {
  539. width: 90%;
  540. font-size: 28rpx;
  541. color: #333;
  542. padding: 18rpx 20rpx;
  543. background: #F9F9F9;
  544. border-radius: 12rpx;
  545. border: 1rpx solid #E0E0E0;
  546. &.readonly {
  547. background: #F5F5F5;
  548. color: #999;
  549. }
  550. }
  551. .form-picker {
  552. display: flex;
  553. align-items: center;
  554. .picker-content {
  555. flex: 1;
  556. }
  557. }
  558. /* 性别选择器特定样式 */
  559. .gender-picker {
  560. width: 25%;
  561. }
  562. .form-textarea {
  563. width: 90%;
  564. font-size: 28rpx;
  565. color: #333;
  566. padding: 20rpx;
  567. background: #F9F9F9;
  568. border-radius: 12rpx;
  569. border: 1rpx solid #E0E0E0;
  570. min-height: 160rpx;
  571. line-height: 1.5;
  572. }
  573. }
  574. }
  575. /* 保存按钮 */
  576. .save-btn-section {
  577. padding: 40rpx 30rpx;
  578. .save-btn {
  579. width: 100%;
  580. height: 90rpx;
  581. background: linear-gradient(135deg, #E91E63 0%, #FF6B8A 100%);
  582. color: #FFFFFF;
  583. font-size: 32rpx;
  584. font-weight: bold;
  585. border-radius: 45rpx;
  586. border: none;
  587. box-shadow: 0 6rpx 20rpx rgba(233, 30, 99, 0.3);
  588. transition: all 0.3s ease;
  589. &:active {
  590. transform: scale(0.98);
  591. box-shadow: 0 3rpx 10rpx rgba(233, 30, 99, 0.2);
  592. }
  593. }
  594. }
  595. </style>