edit-profile.vue 17 KB

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