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. console.error('加载红娘资料失败', e)
  221. uni.showToast({ title: '加载资料失败', icon: 'none' })
  222. }
  223. },
  224. // 从后端加载省列表并根据已有省市区初始化
  225. async loadProvincesAndInit() {
  226. try {
  227. const list = await api.area.getProvinces()
  228. this.provinces = list || []
  229. this.multiArray[0] = this.provinces.map(p => p.name)
  230. if (this.userInfo.province_id) {
  231. const pIndex = this.provinces.findIndex(p => p.id === this.userInfo.province_id)
  232. if (pIndex >= 0) {
  233. this.multiIndex[0] = pIndex
  234. await this.updateCityList(pIndex, true)
  235. }
  236. }
  237. } catch (e) {
  238. console.error('加载省份列表失败', e)
  239. }
  240. },
  241. // 更新城市列表(根据省索引),init 表示是否按已有 city_id 初始化
  242. async updateCityList(provinceIndex, init = false) {
  243. const province = this.provinces[provinceIndex]
  244. if (!province) return
  245. try {
  246. const list = await api.area.getCities(province.id)
  247. this.cities = list || []
  248. this.multiArray[1] = this.cities.map(c => c.name)
  249. this.multiArray[2] = []
  250. this.multiIndex[1] = 0
  251. this.multiIndex[2] = 0
  252. if (init && this.userInfo.city_id) {
  253. const cIndex = this.cities.findIndex(c => c.id === this.userInfo.city_id)
  254. if (cIndex >= 0) {
  255. this.multiIndex[1] = cIndex
  256. await this.updateAreaList(provinceIndex, cIndex, true)
  257. }
  258. }
  259. } catch (e) {
  260. console.error('加载城市列表失败', e)
  261. }
  262. },
  263. // 更新区县列表
  264. async updateAreaList(provinceIndex, cityIndex, init = false) {
  265. const city = this.cities[cityIndex]
  266. if (!city) return
  267. try {
  268. const list = await api.area.getAreas(city.id)
  269. this.areas = list || []
  270. this.multiArray[2] = this.areas.map(a => a.name)
  271. this.multiIndex[2] = 0
  272. if (init && this.userInfo.area_id) {
  273. const aIndex = this.areas.findIndex(a => a.id === this.userInfo.area_id)
  274. if (aIndex >= 0) {
  275. this.multiIndex[2] = aIndex
  276. }
  277. }
  278. } catch (e) {
  279. console.error('加载区县列表失败', e)
  280. }
  281. },
  282. // 处理列变化
  283. handleColumnChange(e) {
  284. const column = e.detail.column
  285. const value = e.detail.value
  286. this.multiIndex[column] = value
  287. // 根据列索引更新对应的数据
  288. if (column === 0) {
  289. // 省份变化,更新城市和区县
  290. this.updateCityList(value, false)
  291. } else if (column === 1) {
  292. // 城市变化,更新区县
  293. this.updateAreaList(this.multiIndex[0], value, false)
  294. }
  295. },
  296. // 处理多列选择器确认
  297. handleMultiPickerChange(e) {
  298. this.multiIndex = e.detail.value
  299. const p = this.provinces[this.multiIndex[0]]
  300. const c = this.cities[this.multiIndex[1]]
  301. const a = this.areas[this.multiIndex[2]]
  302. const provinceName = p ? p.name : ''
  303. const cityName = c ? c.name : ''
  304. const areaName = a ? a.name : ''
  305. this.selectedArea = provinceName && cityName && areaName
  306. ? `${provinceName} ${cityName} ${areaName}`
  307. : ''
  308. // 更新用户信息中的 id 和名称
  309. this.userInfo.province_id = p ? p.id : null
  310. this.userInfo.city_id = c ? c.id : null
  311. this.userInfo.area_id = a ? a.id : null
  312. this.userInfo.province_name = provinceName
  313. this.userInfo.city_name = cityName
  314. this.userInfo.area_name = areaName
  315. },
  316. // 返回上一页
  317. async handleBack() {
  318. uni.navigateBack({
  319. delta: 1
  320. })
  321. },
  322. // 性别选择变化
  323. handleGenderChange(e) {
  324. const index = Number(e.detail.value)
  325. this.genderIndex = index
  326. this.userInfo.gender = index >= 0 ? index + 1 : null
  327. },
  328. // 出生日期选择
  329. handleBirthChange(e) {
  330. this.userInfo.birth_date = e.detail.value
  331. },
  332. // 保存资料
  333. async handleSave() {
  334. // 验证表单
  335. if (!this.userInfo.real_name) {
  336. uni.showToast({
  337. title: '请输入真实姓名',
  338. icon: 'none'
  339. })
  340. return
  341. }
  342. if (!this.userInfo.gender) {
  343. uni.showToast({
  344. title: '请选择性别',
  345. icon: 'none'
  346. })
  347. return
  348. }
  349. // 简单邮箱格式校验(若填写)
  350. if (this.userInfo.email) {
  351. const emailRegex = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
  352. if (!emailRegex.test(this.userInfo.email)) {
  353. uni.showToast({ title: '邮箱格式不正确', icon: 'none' })
  354. return
  355. }
  356. }
  357. try {
  358. if (!this.matchmakerId) {
  359. uni.showToast({ title: '红娘信息缺失,无法保存', icon: 'none' })
  360. return
  361. }
  362. const payload = {
  363. // 注意:后端 Jackson 使用了 SNAKE_CASE 策略
  364. // email、gender、profile 名字不变,其他字段用下划线命名
  365. email: this.userInfo.email,
  366. real_name: this.userInfo.real_name,
  367. gender: this.userInfo.gender,
  368. birth_date: this.userInfo.birth_date,
  369. address_detail: this.userInfo.address_detail,
  370. province_id: this.userInfo.province_id,
  371. city_id: this.userInfo.city_id,
  372. area_id: this.userInfo.area_id,
  373. profile: this.userInfo.profile
  374. }
  375. await api.matchmaker.updateProfile(this.matchmakerId, payload)
  376. uni.showToast({
  377. title: '资料保存成功',
  378. icon: 'success'
  379. })
  380. setTimeout(() => {
  381. this.handleBack()
  382. }, 1200)
  383. } catch (e) {
  384. console.error('保存红娘资料失败', e)
  385. uni.showToast({ title: '保存失败,请稍后重试', icon: 'none' })
  386. }
  387. }
  388. }
  389. }
  390. </script>
  391. <style lang="scss" scoped>
  392. .edit-profile {
  393. min-height: 100vh;
  394. background: #FFFFFF;
  395. display: flex;
  396. flex-direction: column;
  397. }
  398. /* 顶部导航栏 */
  399. .header {
  400. display: flex;
  401. align-items: center;
  402. justify-content: space-between;
  403. padding: 25rpx 30rpx;
  404. padding-top: calc(25rpx + env(safe-area-inset-top));
  405. background: #FFFFFF;
  406. border-bottom: 1rpx solid #F0F0F0;
  407. .back-btn {
  408. width: 44rpx;
  409. height: 44rpx;
  410. display: flex;
  411. align-items: center;
  412. justify-content: center;
  413. .back-icon {
  414. font-size: 40rpx;
  415. color: #333;
  416. }
  417. }
  418. .header-title {
  419. font-size: 38rpx;
  420. font-weight: bold;
  421. color: #333;
  422. }
  423. .right-empty {
  424. width: 44rpx;
  425. }
  426. }
  427. .content {
  428. flex: 1;
  429. padding-bottom: 40rpx;
  430. }
  431. /* 头像上传区域 */
  432. .avatar-section {
  433. display: flex;
  434. flex-direction: column;
  435. align-items: center;
  436. padding: 40rpx 0;
  437. border-bottom: 1rpx solid #F0F0F0;
  438. .avatar-container {
  439. position: relative;
  440. width: 160rpx;
  441. height: 160rpx;
  442. margin-bottom: 20rpx;
  443. .avatar {
  444. width: 100%;
  445. height: 100%;
  446. border-radius: 50%;
  447. background: #F5F5F5;
  448. }
  449. .avatar-upload-btn {
  450. position: absolute;
  451. bottom: 0;
  452. right: 0;
  453. width: 48rpx;
  454. height: 48rpx;
  455. display: flex;
  456. align-items: center;
  457. justify-content: center;
  458. background: #FFFFFF;
  459. border: 2rpx solid #E91E63;
  460. border-radius: 50%;
  461. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  462. .upload-icon {
  463. font-size: 28rpx;
  464. color: #E91E63;
  465. }
  466. }
  467. }
  468. .avatar-text {
  469. font-size: 28rpx;
  470. color: #E91E63;
  471. font-weight: 500;
  472. }
  473. }
  474. /* 表单区域 */
  475. .form-section {
  476. padding: 0 30rpx;
  477. .form-item {
  478. display: flex;
  479. flex-direction: column;
  480. padding: 25rpx 0;
  481. border-bottom: 1rpx solid #F5F5F5;
  482. &:last-child {
  483. border-bottom: none;
  484. }
  485. .form-label {
  486. display: flex;
  487. align-items: center;
  488. margin-bottom: 15rpx;
  489. .label-icon {
  490. font-size: 28rpx;
  491. margin-right: 12rpx;
  492. }
  493. .label-text {
  494. font-size: 28rpx;
  495. color: #333;
  496. font-weight: 500;
  497. }
  498. }
  499. .form-input,
  500. .form-picker {
  501. width: 90%;
  502. font-size: 28rpx;
  503. color: #333;
  504. padding: 18rpx 20rpx;
  505. background: #F9F9F9;
  506. border-radius: 12rpx;
  507. border: 1rpx solid #E0E0E0;
  508. &.readonly {
  509. background: #F5F5F5;
  510. color: #999;
  511. }
  512. }
  513. .form-picker {
  514. display: flex;
  515. align-items: center;
  516. .picker-content {
  517. flex: 1;
  518. }
  519. }
  520. /* 性别选择器特定样式 */
  521. .gender-picker {
  522. width: 25%;
  523. }
  524. .form-textarea {
  525. width: 90%;
  526. font-size: 28rpx;
  527. color: #333;
  528. padding: 20rpx;
  529. background: #F9F9F9;
  530. border-radius: 12rpx;
  531. border: 1rpx solid #E0E0E0;
  532. min-height: 160rpx;
  533. line-height: 1.5;
  534. }
  535. }
  536. }
  537. /* 保存按钮 */
  538. .save-btn-section {
  539. padding: 40rpx 30rpx;
  540. .save-btn {
  541. width: 100%;
  542. height: 90rpx;
  543. background: linear-gradient(135deg, #E91E63 0%, #FF6B8A 100%);
  544. color: #FFFFFF;
  545. font-size: 32rpx;
  546. font-weight: bold;
  547. border-radius: 45rpx;
  548. border: none;
  549. box-shadow: 0 6rpx 20rpx rgba(233, 30, 99, 0.3);
  550. transition: all 0.3s ease;
  551. &:active {
  552. transform: scale(0.98);
  553. box-shadow: 0 3rpx 10rpx rgba(233, 30, 99, 0.2);
  554. }
  555. }
  556. }
  557. </style>