edit-profile.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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">
  16. <image class="avatar" :src="userInfo.avatar || defaultAvatar" mode="aspectFill"></image>
  17. </view>
  18. <text class="avatar-text">头像由平台统一设置,暂不支持修改</text>
  19. </view>
  20. <!-- 表单区域 -->
  21. <view class="form-section">
  22. <!-- 真实姓名 -->
  23. <view class="form-item">
  24. <view class="form-label">
  25. <text class="label-icon">👤</text>
  26. <text class="label-text">真实姓名</text>
  27. </view>
  28. <input
  29. class="form-input"
  30. v-model="userInfo.real_name"
  31. placeholder="请输入真实姓名"
  32. placeholder-style="color: #999"
  33. >
  34. </view>
  35. <!-- 性别 -->
  36. <view class="form-item">
  37. <view class="form-label">
  38. <text class="label-icon">⚧️</text>
  39. <text class="label-text">性别</text>
  40. </view>
  41. <picker class="form-picker gender-picker" @change="handleGenderChange" :value="genderIndex"
  42. :range="genderOptions">
  43. <view class="picker-content">
  44. {{ genderLabel }}
  45. </view>
  46. </picker>
  47. </view>
  48. <!-- 出生日期 -->
  49. <view class="form-item">
  50. <view class="form-label">
  51. <text class="label-icon">📅</text>
  52. <text class="label-text">出生日期</text>
  53. </view>
  54. <picker class="form-picker" mode="date" :value="userInfo.birth_date" @change="handleBirthChange">
  55. <view class="picker-content">
  56. {{ userInfo.birth_date || '请选择出生日期' }}
  57. </view>
  58. </picker>
  59. </view>
  60. <!-- 邮箱 -->
  61. <view class="form-item">
  62. <view class="form-label">
  63. <text class="label-icon">📧</text>
  64. <text class="label-text">邮箱</text>
  65. </view>
  66. <input
  67. class="form-input"
  68. v-model="userInfo.email"
  69. placeholder="请输入邮箱"
  70. placeholder-style="color: #999"
  71. type="email"
  72. >
  73. </view>
  74. <!-- 所在地区 -->
  75. <view class="form-item">
  76. <view class="form-label">
  77. <text class="label-icon">📍</text>
  78. <text class="label-text">所在地区</text>
  79. </view>
  80. <picker
  81. class="form-picker"
  82. mode="multiSelector"
  83. :value="multiIndex"
  84. :range="multiArray"
  85. @columnchange="handleColumnChange"
  86. @change="handleMultiPickerChange"
  87. >
  88. <view class="picker-content">{{ selectedArea || '请选择地区' }}</view>
  89. </picker>
  90. </view>
  91. <!-- 详细地址 -->
  92. <view class="form-item">
  93. <view class="form-label">
  94. <text class="label-icon">🏠</text>
  95. <text class="label-text">详细地址</text>
  96. </view>
  97. <textarea
  98. class="form-textarea"
  99. v-model="userInfo.address_detail"
  100. placeholder="请输入详细地址"
  101. placeholder-style="color: #999"
  102. maxlength="255"
  103. auto-height
  104. ></textarea>
  105. </view>
  106. <!-- 个人简介 -->
  107. <view class="form-item">
  108. <view class="form-label">
  109. <text class="label-icon">📝</text>
  110. <text class="label-text">个人简介</text>
  111. </view>
  112. <textarea
  113. class="form-textarea"
  114. v-model="userInfo.profile"
  115. placeholder="请输入个人简介"
  116. placeholder-style="color: #999"
  117. maxlength="200"
  118. auto-height
  119. ></textarea>
  120. </view>
  121. </view>
  122. <!-- 保存按钮 -->
  123. <view class="save-btn-section">
  124. <button class="save-btn" @click="handleSave">保存</button>
  125. </view>
  126. </scroll-view>
  127. </view>
  128. </template>
  129. <script>
  130. import api from '../../utils/api.js'
  131. export default {
  132. data() {
  133. return {
  134. statusBarHeight: 0,
  135. // 默认头像
  136. defaultAvatar: 'https://q.qlogo.cn/qqapp/1105591438/05E13358B43B3D39D6AC2D6888828201/100',
  137. // 用户信息(与后端字段基本对应)
  138. userInfo: {
  139. avatar: '',
  140. real_name: '',
  141. gender: null, // 1: 男, 2: 女
  142. birth_date: '',
  143. email: '',
  144. province_id: null,
  145. city_id: null,
  146. area_id: null,
  147. address_detail: '',
  148. province_name: '',
  149. city_name: '',
  150. area_name: '',
  151. profile: ''
  152. },
  153. // 当前红娘ID(用于更新)
  154. matchmakerId: null,
  155. // 省市区多列选择器数据(名称数组)
  156. multiArray: [
  157. [], // 省
  158. [], // 市
  159. [] // 区
  160. ],
  161. multiIndex: [0, 0, 0],
  162. selectedArea: '',
  163. // 后端返回的省市区原始列表(包含id和name)
  164. provinces: [],
  165. cities: [],
  166. areas: [],
  167. // 性别选项
  168. genderOptions: ['男', '女'],
  169. genderIndex: -1
  170. }
  171. },
  172. computed: {
  173. genderLabel() {
  174. if (this.userInfo.gender === 1) return '男'
  175. if (this.userInfo.gender === 2) return '女'
  176. return '请选择性别'
  177. }
  178. },
  179. onLoad() {
  180. this.initPage()
  181. //获取状态栏高度
  182. const systemInfo = uni.getSystemInfoSync()
  183. this.statusBarHeight = systemInfo.statusBarHeight
  184. },
  185. methods: {
  186. async initPage() {
  187. await this.loadUserInfo()
  188. await this.loadProvincesAndInit()
  189. },
  190. // 加载红娘资料
  191. async loadUserInfo() {
  192. try {
  193. const basicUser = uni.getStorageSync('userInfo')
  194. const userId = basicUser && basicUser.userId ? basicUser.userId : null
  195. if (!userId) {
  196. uni.showToast({ title: '未登录,无法加载资料', icon: 'none' })
  197. return
  198. }
  199. const matchmakerInfo = await api.matchmaker.getByUserId(userId)
  200. if (matchmakerInfo) {
  201. // 映射后端字段到本地表单
  202. this.matchmakerId = matchmakerInfo.matchmakerId || matchmakerInfo.matchmaker_id || null
  203. this.userInfo.avatar = matchmakerInfo.avatar_url || ''
  204. this.userInfo.real_name = matchmakerInfo.real_name || ''
  205. this.userInfo.gender = typeof matchmakerInfo.gender === 'number' ? matchmakerInfo.gender : null
  206. this.userInfo.birth_date = matchmakerInfo.birth_date || ''
  207. this.userInfo.email = matchmakerInfo.email || ''
  208. this.userInfo.address_detail = matchmakerInfo.address_detail || ''
  209. this.userInfo.province_id = matchmakerInfo.province_id || null
  210. this.userInfo.city_id = matchmakerInfo.city_id || null
  211. this.userInfo.area_id = matchmakerInfo.area_id || null
  212. this.userInfo.province_name = matchmakerInfo.province_name || ''
  213. this.userInfo.city_name = matchmakerInfo.city_name || ''
  214. this.userInfo.area_name = matchmakerInfo.area_name || ''
  215. this.userInfo.profile = matchmakerInfo.profile || ''
  216. // 性别索引
  217. if (this.userInfo.gender === 1) this.genderIndex = 0
  218. else if (this.userInfo.gender === 2) this.genderIndex = 1
  219. // 如果后端返回了省市区名称,直接使用
  220. if (matchmakerInfo.province_name && matchmakerInfo.city_name && matchmakerInfo.area_name) {
  221. this.selectedArea = `${matchmakerInfo.province_name} ${matchmakerInfo.city_name} ${matchmakerInfo.area_name}`
  222. }
  223. }
  224. } catch (e) {
  225. uni.showToast({ title: '加载资料失败', icon: 'none' })
  226. }
  227. },
  228. // 从后端加载省列表并根据已有省市区初始化
  229. async loadProvincesAndInit() {
  230. try {
  231. const list = await api.area.getProvinces()
  232. this.provinces = list || []
  233. this.multiArray[0] = this.provinces.map(p => p.name)
  234. if (this.userInfo.province_id) {
  235. const pIndex = this.provinces.findIndex(p => p.id === this.userInfo.province_id)
  236. if (pIndex >= 0) {
  237. this.multiIndex[0] = pIndex
  238. await this.updateCityList(pIndex, true)
  239. }
  240. }
  241. } catch (e) {
  242. }
  243. },
  244. // 更新城市列表(根据省索引),init 表示是否按已有 city_id 初始化
  245. async updateCityList(provinceIndex, init = false) {
  246. const province = this.provinces[provinceIndex]
  247. if (!province) return
  248. try {
  249. const list = await api.area.getCities(province.id)
  250. this.cities = list || []
  251. this.multiArray[1] = this.cities.map(c => c.name)
  252. this.multiArray[2] = []
  253. this.multiIndex[1] = 0
  254. this.multiIndex[2] = 0
  255. if (init && this.userInfo.city_id) {
  256. const cIndex = this.cities.findIndex(c => c.id === this.userInfo.city_id)
  257. if (cIndex >= 0) {
  258. this.multiIndex[1] = cIndex
  259. await this.updateAreaList(provinceIndex, cIndex, true)
  260. }
  261. }
  262. } catch (e) {
  263. }
  264. },
  265. // 更新区县列表
  266. async updateAreaList(provinceIndex, cityIndex, init = false) {
  267. const city = this.cities[cityIndex]
  268. if (!city) return
  269. try {
  270. const list = await api.area.getAreas(city.id)
  271. this.areas = list || []
  272. this.multiArray[2] = this.areas.map(a => a.name)
  273. this.multiIndex[2] = 0
  274. if (init && this.userInfo.area_id) {
  275. const aIndex = this.areas.findIndex(a => a.id === this.userInfo.area_id)
  276. if (aIndex >= 0) {
  277. this.multiIndex[2] = aIndex
  278. }
  279. }
  280. } catch (e) {
  281. }
  282. },
  283. // 处理列变化
  284. handleColumnChange(e) {
  285. const column = e.detail.column
  286. const value = e.detail.value
  287. this.multiIndex[column] = value
  288. // 根据列索引更新对应的数据
  289. if (column === 0) {
  290. // 省份变化,更新城市和区县
  291. this.updateCityList(value, false)
  292. } else if (column === 1) {
  293. // 城市变化,更新区县
  294. this.updateAreaList(this.multiIndex[0], value, false)
  295. }
  296. },
  297. // 处理多列选择器确认
  298. handleMultiPickerChange(e) {
  299. this.multiIndex = e.detail.value
  300. const p = this.provinces[this.multiIndex[0]]
  301. const c = this.cities[this.multiIndex[1]]
  302. const a = this.areas[this.multiIndex[2]]
  303. const provinceName = p ? p.name : ''
  304. const cityName = c ? c.name : ''
  305. const areaName = a ? a.name : ''
  306. this.selectedArea = provinceName && cityName && areaName
  307. ? `${provinceName} ${cityName} ${areaName}`
  308. : ''
  309. // 更新用户信息中的 id 和名称
  310. this.userInfo.province_id = p ? p.id : null
  311. this.userInfo.city_id = c ? c.id : null
  312. this.userInfo.area_id = a ? a.id : null
  313. this.userInfo.province_name = provinceName
  314. this.userInfo.city_name = cityName
  315. this.userInfo.area_name = areaName
  316. },
  317. // 返回上一页
  318. async handleBack() {
  319. uni.navigateBack({
  320. delta: 1
  321. })
  322. },
  323. // 性别选择变化
  324. handleGenderChange(e) {
  325. const index = Number(e.detail.value)
  326. this.genderIndex = index
  327. this.userInfo.gender = index >= 0 ? index + 1 : null
  328. },
  329. // 出生日期选择
  330. handleBirthChange(e) {
  331. this.userInfo.birth_date = e.detail.value
  332. },
  333. // 保存资料
  334. async handleSave() {
  335. // 验证表单
  336. if (!this.userInfo.real_name) {
  337. uni.showToast({
  338. title: '请输入真实姓名',
  339. icon: 'none'
  340. })
  341. return
  342. }
  343. if (!this.userInfo.gender) {
  344. uni.showToast({
  345. title: '请选择性别',
  346. icon: 'none'
  347. })
  348. return
  349. }
  350. // 简单邮箱格式校验(若填写)
  351. if (this.userInfo.email) {
  352. const emailRegex = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
  353. if (!emailRegex.test(this.userInfo.email)) {
  354. uni.showToast({ title: '邮箱格式不正确', icon: 'none' })
  355. return
  356. }
  357. }
  358. try {
  359. if (!this.matchmakerId) {
  360. uni.showToast({ title: '红娘信息缺失,无法保存', icon: 'none' })
  361. return
  362. }
  363. const payload = {
  364. // 注意:后端 Jackson 使用了 SNAKE_CASE 策略
  365. // email、gender、profile 名字不变,其他字段用下划线命名
  366. email: this.userInfo.email,
  367. real_name: this.userInfo.real_name,
  368. gender: this.userInfo.gender,
  369. birth_date: this.userInfo.birth_date,
  370. address_detail: this.userInfo.address_detail,
  371. province_id: this.userInfo.province_id,
  372. city_id: this.userInfo.city_id,
  373. area_id: this.userInfo.area_id,
  374. profile: this.userInfo.profile
  375. }
  376. await api.matchmaker.updateProfile(this.matchmakerId, payload)
  377. uni.showToast({
  378. title: '资料保存成功',
  379. icon: 'success'
  380. })
  381. setTimeout(() => {
  382. this.handleBack()
  383. }, 1200)
  384. } catch (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>