index.vue 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. <template>
  2. <view class="profile-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="header">
  5. <view class="back-btn" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <text class="header-title">基本资料</text>
  9. <view class="edit-btn" @click="enableEdit">
  10. <text class="edit-text">{{ isEditing ? '保存' : '编辑' }}</text>
  11. </view>
  12. </view>
  13. <!-- 头像和基本信息 -->
  14. <view class="avatar-section">
  15. <view class="avatar-wrapper" @click="chooseAvatar">
  16. <image class="avatar" :src="profile.avatar || defaultAvatar" mode="aspectFill"></image>
  17. <view class="avatar-badge" v-if="profile.isVip">
  18. <text class="vip-icon">👑</text>
  19. </view>
  20. <view class="avatar-edit-hint">
  21. <text class="edit-hint-icon">📷</text>
  22. </view>
  23. </view>
  24. <text class="nickname">{{ profile.realName || profile.nickname || '未设置' }}</text>
  25. </view>
  26. <!-- 编辑提示 -->
  27. <view class="edit-tip" v-if="!isEditing">
  28. <text class="tip-text">💡 点击任意字段即可编辑</text>
  29. </view>
  30. <scroll-view scroll-y class="content-scroll">
  31. <!-- 个人信息 -->
  32. <view class="section">
  33. <view class="section-title">个人信息</view>
  34. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  35. <text class="label">昵称</text>
  36. <input v-if="isEditing" class="input" v-model="profile.nickname" placeholder="请输入昵称" />
  37. <text v-else class="value">{{ profile.nickname || '未设置' }}</text>
  38. </view>
  39. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  40. <text class="label">真实姓名</text>
  41. <input v-if="isEditing" class="input" v-model="profile.realName" placeholder="请输入真实姓名" />
  42. <text v-else class="value">{{ profile.realName || '未设置' }}</text>
  43. </view>
  44. <!-- 身份证号输入项改造 -->
  45. <view class="info-item" :class="{ editing: isEditing, error: idCardError }" @click="startEdit">
  46. <text class="label">身份证号</text>
  47. <input
  48. v-if="isEditing"
  49. class="input"
  50. v-model="profile.idCard"
  51. placeholder="请输入18位身份证号"
  52. maxlength="18"
  53. @input="checkIdCardLength"
  54. @blur="checkIdCardValid"
  55. />
  56. <text v-else class="value">{{ formatIdCard(profile.idCard) }}</text>
  57. <text class="verify-status verified" v-if="profile.idCard && profile.isRealNameVerified">✓已认证</text>
  58. <text class="verify-status error" v-else-if="isEditing && idCardError">{{ idCardErrorText }}</text>
  59. <text class="verify-status unverified" v-else-if="!profile.idCard">未认证</text>
  60. <text class="verify-status warning" v-else-if="profile.idCard && profile.idCard.length !== 18">长度有误</text>
  61. </view>
  62. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  63. <text class="label">性别</text>
  64. <picker v-if="isEditing" :range="genderOptions" range-key="label" :value="getGenderIndex()" @change="onGenderChange">
  65. <view class="picker-value">{{ getGenderText() }}</view>
  66. </picker>
  67. <text v-else class="value">{{ getGenderText() }}</text>
  68. </view>
  69. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  70. <text class="label">出生日期</text>
  71. <picker v-if="isEditing" mode="date" :value="profile.birthDate" @change="onBirthDateChange">
  72. <view class="picker-value">{{ profile.birthDate || '请选择' }}</view>
  73. </picker>
  74. <text v-else class="value">{{ profile.birthDate || '未设置' }}</text>
  75. </view>
  76. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  77. <text class="label">身高</text>
  78. <input v-if="isEditing" class="input" type="number" v-model="profile.height" placeholder="请输入身高(cm)" />
  79. <text v-else class="value">{{ profile.height ? profile.height + ' cm' : '未设置' }}</text>
  80. </view>
  81. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  82. <text class="label">体重</text>
  83. <input v-if="isEditing" class="input" type="number" v-model="profile.weight" placeholder="请输入体重(kg)" />
  84. <text v-else class="value">{{ profile.weight ? profile.weight + ' kg' : '未设置' }}</text>
  85. </view>
  86. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  87. <text class="label">星座</text>
  88. <picker v-if="isEditing" :range="starOptions" @change="onStarChange">
  89. <view class="picker-value">{{ profile.star || '请选择' }}</view>
  90. </picker>
  91. <text v-else class="value">{{ profile.star || '未设置' }}</text>
  92. </view>
  93. <view class="info-item" :class="{ editing: isEditing }">
  94. <text class="label">生肖</text>
  95. <text class="value">{{ profile.animal || '未设置' }}</text>
  96. </view>
  97. <view class="info-item" :class="{ editing: isEditing }" @click="handleAreaClick">
  98. <text class="label">地区</text>
  99. <picker v-if="isEditing" mode="multiSelector" :range="multiAreaData" range-key="name" :value="multiAreaIndex" @change="onAreaChange" @columnchange="onAreaColumnChange">
  100. <view class="picker-value">{{ getAreaText() }}</view>
  101. </picker>
  102. <text v-else class="value">{{ getAreaText() }}</text>
  103. </view>
  104. </view>
  105. <!-- 学业与工作 -->
  106. <view class="section">
  107. <view class="section-title">学业与工作</view>
  108. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  109. <text class="label">学历</text>
  110. <picker v-if="isEditing" :range="educationOptions" :value="getEducationIndex()" @change="onEducationChange">
  111. <view class="picker-value">{{ getEducationText() }}</view>
  112. </picker>
  113. <text v-else class="value">{{ getEducationText() }}</text>
  114. <text class="verify-status verified" v-if="profile.educationLevel && profile.isEducationVerified">✓已认证</text>
  115. <text class="verify-status unverified" v-else-if="!profile.educationLevel">未认证</text>
  116. </view>
  117. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  118. <text class="label">学校名称</text>
  119. <input v-if="isEditing" class="input" v-model="profile.schoolName" placeholder="请输入学校名称" />
  120. <text v-else class="value">{{ profile.schoolName || '未设置' }}</text>
  121. </view>
  122. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  123. <text class="label">工作单位</text>
  124. <input v-if="isEditing" class="input" v-model="profile.company" placeholder="请输入工作单位" />
  125. <text v-else class="value">{{ profile.company || '未设置' }}</text>
  126. <text class="verify-status verified" v-if="profile.company && profile.isWorkVerified">✓已认证</text>
  127. <text class="verify-status unverified" v-else-if="!profile.company">未认证</text>
  128. </view>
  129. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  130. <text class="label">职位</text>
  131. <input v-if="isEditing" class="input" v-model="profile.jobTitle" placeholder="请输入职位" />
  132. <text v-else class="value">{{ profile.jobTitle || '未设置' }}</text>
  133. </view>
  134. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  135. <text class="label">薪资范围</text>
  136. <picker v-if="isEditing" :range="salaryOptions" :value="getSalaryIndex()" @change="onSalaryChange">
  137. <view class="picker-value">{{ getSalaryText() }}</view>
  138. </picker>
  139. <text v-else class="value">{{ getSalaryText() }}</text>
  140. </view>
  141. </view>
  142. <!-- 婚姻与住房 -->
  143. <view class="section">
  144. <view class="section-title">婚姻与住房</view>
  145. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  146. <text class="label">婚姻状况</text>
  147. <picker v-if="isEditing" :range="maritalOptions" :value="getMaritalIndex()" @change="onMaritalChange">
  148. <view class="picker-value">{{ getMaritalText() }}</view>
  149. </picker>
  150. <text v-else class="value">{{ getMaritalText() }}</text>
  151. <text class="verify-status verified" v-if="profile.maritalStatus !== null && profile.maritalStatus !== undefined && profile.isMaritalVerified">✓已认证</text>
  152. <text class="verify-status unverified" v-else-if="profile.maritalStatus === null || profile.maritalStatus === undefined">未认证</text>
  153. </view>
  154. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  155. <text class="label">房产</text>
  156. <picker v-if="isEditing" :range="houseOptions" range-key="label" :value="getHouseIndex()" @change="onHouseChange">
  157. <view class="picker-value">{{ getHouseText() }}</view>
  158. </picker>
  159. <text v-else class="value">{{ getHouseText() }}</text>
  160. </view>
  161. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  162. <text class="label">车辆</text>
  163. <picker v-if="isEditing" :range="carOptions" range-key="label" :value="getCarIndex()" @change="onCarChange">
  164. <view class="picker-value">{{ getCarText() }}</view>
  165. </picker>
  166. <text v-else class="value">{{ getCarText() }}</text>
  167. </view>
  168. </view>
  169. <!-- 兴趣爱好 -->
  170. <view class="section">
  171. <view class="section-title">兴趣爱好</view>
  172. <view class="info-item column" @click="startEdit">
  173. <text class="label">兴趣爱好标签</text>
  174. <view class="hobby-tags">
  175. <view class="hobby-tag"
  176. v-for="(hobby, index) in hobbyList"
  177. :key="index"
  178. @click.stop="isEditing ? removeHobbyTag(index) : null"
  179. :class="{ deletable: isEditing }"
  180. >
  181. {{ hobby }}
  182. <text class="delete-icon" v-if="isEditing">×</text>
  183. </view>
  184. <view class="hobby-tag add" v-if="isEditing" @click.stop="addHobby">
  185. + 添加
  186. </view>
  187. <view class="hobby-empty" v-if="hobbyList.length === 0 && !isEditing">
  188. <text class="empty-text">暂无标签</text>
  189. </view>
  190. </view>
  191. </view>
  192. </view>
  193. <!-- 隐私设置 -->
  194. <view class="section">
  195. <view class="section-title">隐私设置</view>
  196. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  197. <text class="label">薪资公开</text>
  198. <switch :checked="profile.privacySalary === 1" @change="onPrivacySalaryChange" :disabled="!isEditing" />
  199. </view>
  200. <view class="info-item" :class="{ editing: isEditing }" @click="startEdit">
  201. <text class="label">手机号公开</text>
  202. <switch :checked="profile.privacyPhone === 1" @change="onPrivacyPhoneChange" :disabled="!isEditing" />
  203. </view>
  204. </view>
  205. <!-- 真实度评分 -->
  206. <view class="section">
  207. <view class="section-title">真实度评分</view>
  208. <view class="authenticity-score">
  209. <view class="score-circle">
  210. <text class="score-number">{{ profile.authenticityScore || 0 }}</text>
  211. <text class="score-total">/100</text>
  212. </view>
  213. <view class="score-tips">
  214. <text class="tip-text">完善资料并通过认证可提升真实度评分</text>
  215. </view>
  216. </view>
  217. </view>
  218. <!-- 底部占位 -->
  219. <view class="bottom-placeholder"></view>
  220. </scroll-view>
  221. <!-- 标签选择弹窗 -->
  222. <view class="hobby-modal" v-if="showHobbyModal" @click="closeHobbyModal">
  223. <view class="modal-content" @click.stop>
  224. <view class="modal-header">
  225. <text class="modal-title">选择兴趣爱好标签</text>
  226. <text class="close-btn" @click="closeHobbyModal">×</text>
  227. </view>
  228. <scroll-view scroll-y class="modal-body">
  229. <!-- 预设标签分类 -->
  230. <view class="hobby-category" v-for="(category, index) in hobbyCategories" :key="index">
  231. <view class="category-title">
  232. <text class="category-icon">{{ category.icon }}</text>
  233. <text class="category-name">{{ category.name }}</text>
  234. </view>
  235. <view class="category-tags">
  236. <view class="preset-tag"
  237. v-for="(tag, tagIndex) in category.tags"
  238. :key="tagIndex"
  239. @click="selectPresetTag(tag)"
  240. :class="{ selected: hobbyList.includes(tag) }"
  241. >
  242. {{ tag }}
  243. <text class="check-icon" v-if="hobbyList.includes(tag)">✓</text>
  244. </view>
  245. </view>
  246. </view>
  247. <!-- 自定义输入 -->
  248. <view class="custom-input-section">
  249. <view class="category-title">
  250. <text class="category-icon">✏️</text>
  251. <text class="category-name">自定义标签</text>
  252. </view>
  253. <view class="input-wrapper">
  254. <input
  255. class="custom-input"
  256. v-model="customHobbyInput"
  257. placeholder="输入自定义标签"
  258. @confirm="addCustomTag"
  259. />
  260. <view class="add-custom-btn" @click="addCustomTag">
  261. <text class="add-text">添加</text>
  262. </view>
  263. </view>
  264. </view>
  265. </scroll-view>
  266. <view class="modal-footer">
  267. <text class="selected-count">已选择 {{ hobbyList.length }}/10 个标签</text>
  268. <view class="done-btn" @click="closeHobbyModal">
  269. <text class="done-text">完成</text>
  270. </view>
  271. </view>
  272. </view>
  273. </view>
  274. </view>
  275. </template>
  276. <script>
  277. import userAuth from '@/utils/userAuth.js'
  278. import { getZodiacByBirthday } from '@/utils/zodiac.js'
  279. import api from '@/utils/api.js'
  280. export default {
  281. data() {
  282. return {
  283. gatewayURL: 'https://api.zhongruanke.cn',
  284. currentUserId: null,
  285. isEditing: false,
  286. defaultAvatar: 'https://via.placeholder.com/150',
  287. // 用户资料数据
  288. profile: {
  289. userId: null,
  290. avatar: uni.getStorageSync("userInfo").avatarUrl,
  291. nickname: '',
  292. realName: '',
  293. idCard: '',
  294. gender: null,
  295. birthDate: '',
  296. height: null,
  297. weight: null,
  298. star: '',
  299. animal: '',
  300. educationLevel: null,
  301. schoolName: '',
  302. company: '',
  303. jobTitle: '',
  304. salaryRange: null,
  305. maritalStatus: null,
  306. house: null,
  307. car: null,
  308. hobby: '',
  309. privacySalary: 0,
  310. privacyPhone: 0,
  311. authenticityScore: 0,
  312. isVip: false,
  313. isRealNameVerified: false,
  314. isEducationVerified: false,
  315. isWorkVerified: false,
  316. isMaritalVerified: false,
  317. provinceId: null,
  318. cityId: null,
  319. areaId: null
  320. },
  321. // 选项列表
  322. genderOptions: [
  323. { value: 1, label: '男' },
  324. { value: 2, label: '女' }
  325. ],
  326. starOptions: ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座',
  327. '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'],
  328. animalOptions: ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'],
  329. educationOptions: ['高中', '专科', '本科', '硕士', '博士', '无'],
  330. salaryOptions: ['1-5k', '5-10k', '10-20k', '20-50k', '50k+'],
  331. maritalOptions: ['未婚', '离异', '丧偶'],
  332. houseOptions: [
  333. { value: 0, label: '无' },
  334. { value: 1, label: '有' }
  335. ],
  336. carOptions: [
  337. { value: 0, label: '无' },
  338. { value: 1, label: '有' }
  339. ],
  340. // 标签选择弹窗
  341. showHobbyModal: false,
  342. hobbyCategories: [
  343. {
  344. name: '运动健身',
  345. icon: '🏃',
  346. tags: ['运动', '健身', '瑜伽', '爬山', '游泳', '篮球', '足球', '网球']
  347. },
  348. {
  349. name: '文艺休闲',
  350. icon: '🎨',
  351. tags: ['旅游', '阅读', '音乐', '电影', '摄影', '绘画', '舞蹈', '唱歌']
  352. },
  353. {
  354. name: '技能爱好',
  355. icon: '🎹',
  356. tags: ['钢琴', '吉他', '书法', '茶艺', '烹饪', '园艺', '手工', '收藏']
  357. },
  358. {
  359. name: '生活方式',
  360. icon: '🌟',
  361. tags: ['美食', '游戏', '宠物', '时尚', '购物', '化妆']
  362. }
  363. ],
  364. customHobbyInput: '',
  365. // 省市区数据
  366. provinceList: [],
  367. cityList: [],
  368. areaList: [],
  369. multiAreaData: [[], [], []], // 三级联动数据
  370. multiAreaIndex: [0, 0, 0], // 三级联动索引
  371. idCardError: false,
  372. idCardErrorText: ''
  373. }
  374. },
  375. computed: {
  376. hobbyList() {
  377. if (!this.profile.hobby) return []
  378. try {
  379. return JSON.parse(this.profile.hobby)
  380. } catch (e) {
  381. return this.profile.hobby.split(',').filter(item => item)
  382. }
  383. }
  384. },
  385. onLoad() {
  386. // 获取登录用户ID - 使用userAuth工具类
  387. this.currentUserId = userAuth.getUserId()
  388. // 检查是否已登录(userId为1也是合法的测试用户)
  389. if (this.currentUserId === null || this.currentUserId === undefined || this.currentUserId === 0) {
  390. this.checkLoginStatus()
  391. } else {
  392. // 加载省市区数据
  393. this.loadAreaData()
  394. // 加载用户资料
  395. this.loadProfile()
  396. }
  397. },
  398. methods: {
  399. // 实时校验身份证长度
  400. checkIdCardLength() {
  401. const idCard = this.profile.idCard.trim();
  402. this.idCardError = false;
  403. this.idCardErrorText = '';
  404. if (idCard) {
  405. if (idCard.length !== 18 && idCard.length !== 15) {
  406. this.idCardError = true;
  407. this.idCardErrorText = idCard.length < 18 ? '身份证号不足18位' : '身份证号超过18位';
  408. }
  409. }
  410. },
  411. // 失焦校验身份证格式
  412. checkIdCardValid() {
  413. const idCard = this.profile.idCard.trim();
  414. if (!idCard) return;
  415. // 长度校验
  416. if (idCard.length !== 18 && idCard.length !== 15) {
  417. this.idCardError = true;
  418. this.idCardErrorText = '请输入18位有效身份证号';
  419. return;
  420. }
  421. // 格式正则校验(支持18位含X/x,15位纯数字)
  422. const reg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)|(^\d{15}$)/;
  423. if (!reg.test(idCard)) {
  424. this.idCardError = true;
  425. this.idCardErrorText = '身份证号格式有误';
  426. }
  427. },
  428. // 保存时校验身份证
  429. validateIdCard() {
  430. const idCard = this.profile.idCard.trim();
  431. if (!idCard) return true; // 非必填
  432. // 长度校验
  433. if (idCard.length !== 18 && idCard.length !== 15) {
  434. uni.showToast({ title: '请输入18位有效身份证号', icon: 'none' });
  435. return false;
  436. }
  437. // 格式校验
  438. const reg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)|(^\d{15}$)/;
  439. if (!reg.test(idCard)) {
  440. uni.showToast({ title: '身份证号格式有误', icon: 'none' });
  441. return false;
  442. }
  443. return true;
  444. },
  445. // 选择并上传头像
  446. chooseAvatar() {
  447. uni.showActionSheet({
  448. itemList: ['从相册选择', '拍照'],
  449. success: (res) => {
  450. if (res.tapIndex === 0) {
  451. // 从相册选择
  452. this.selectImage('album')
  453. } else if (res.tapIndex === 1) {
  454. // 拍照
  455. this.selectImage('camera')
  456. }
  457. }
  458. })
  459. },
  460. // 选择图片
  461. selectImage(sourceType) {
  462. uni.chooseImage({
  463. count: 1,
  464. sizeType: ['compressed'], // 使用压缩图
  465. sourceType: [sourceType],
  466. success: (res) => {
  467. const tempFilePath = res.tempFilePaths[0]
  468. // 上传图片
  469. this.uploadAvatar(tempFilePath)
  470. },
  471. fail: (err) => {
  472. console.error('选择图片失败:', err)
  473. uni.showToast({
  474. title: '选择图片失败',
  475. icon: 'none'
  476. })
  477. }
  478. })
  479. },
  480. // 上传头像
  481. uploadAvatar(filePath) {
  482. uni.showLoading({
  483. title: '上传中...',
  484. mask: true
  485. })
  486. // 使用 uni.uploadFile 上传到服务器
  487. const uploadTask = uni.uploadFile({
  488. url: this.gatewayURL + '/api/avatar/upload',
  489. filePath: filePath,
  490. name: 'file',
  491. timeout: 60000, // 设置超时时间为60秒
  492. formData: {
  493. userId: this.currentUserId
  494. },
  495. success: (uploadRes) => {
  496. uni.hideLoading()
  497. // 检查HTTP状态码
  498. if (uploadRes.statusCode !== 200) {
  499. console.error('HTTP错误:', uploadRes.statusCode)
  500. uni.showToast({
  501. title: `服务器错误: ${uploadRes.statusCode}`,
  502. icon: 'none',
  503. duration: 3000
  504. })
  505. return
  506. }
  507. try {
  508. const result = JSON.parse(uploadRes.data)
  509. if (result.code === 200) {
  510. // 更新头像URL
  511. this.profile.avatar = result.data
  512. // 更新本地存储的用户信息
  513. const userInfo = uni.getStorageSync('userInfo')
  514. if (userInfo) {
  515. userInfo.avatarUrl = result.data
  516. uni.setStorageSync('userInfo', userInfo)
  517. }
  518. uni.showToast({
  519. title: '头像上传成功',
  520. icon: 'success',
  521. duration: 2000
  522. })
  523. // 刷新页面数据
  524. setTimeout(() => {
  525. this.loadProfile()
  526. }, 500)
  527. } else {
  528. console.error('❌ 业务错误:', result.message)
  529. uni.showToast({
  530. title: result.message || '上传失败',
  531. icon: 'none',
  532. duration: 3000
  533. })
  534. }
  535. } catch (e) {
  536. console.error('❌ 解析响应失败:', e)
  537. console.error('原始响应数据:', uploadRes.data)
  538. uni.showToast({
  539. title: '服务器响应格式错误',
  540. icon: 'none',
  541. duration: 3000
  542. })
  543. }
  544. },
  545. fail: (err) => {
  546. uni.hideLoading()
  547. console.error('❌ 上传失败:', err)
  548. let errorMsg = '上传失败'
  549. if (err.errMsg) {
  550. if (err.errMsg.includes('timeout')) {
  551. errorMsg = '上传超时,请检查网络连接'
  552. } else if (err.errMsg.includes('fail')) {
  553. errorMsg = '网络连接失败'
  554. } else {
  555. errorMsg = err.errMsg
  556. }
  557. }
  558. uni.showToast({
  559. title: errorMsg,
  560. icon: 'none',
  561. duration: 3000
  562. })
  563. }
  564. })
  565. // 监听上传进度
  566. uploadTask.onProgressUpdate((res) => {
  567. if (res.progress % 20 === 0 || res.progress > 90) {
  568. }
  569. })
  570. },
  571. // 检查登录状态
  572. checkLoginStatus() {
  573. const token = uni.getStorageSync('token')
  574. const userInfo = uni.getStorageSync('userInfo')
  575. if (!token || !userInfo) {
  576. // 没有登录信息,跳转到登录页
  577. uni.showModal({
  578. title: '需要登录',
  579. content: '请先登录后查看个人资料',
  580. showCancel: false,
  581. confirmText: '去登录',
  582. success: () => {
  583. uni.navigateTo({
  584. url: '/pages/page3/page3?redirect=' + encodeURIComponent('/pages/profile/index')
  585. })
  586. }
  587. })
  588. return false
  589. }
  590. if (this.currentUserId === null || this.currentUserId === undefined || this.currentUserId === 0) {
  591. // 有登录信息但无法获取用户ID - 登录信息异常
  592. uni.showModal({
  593. title: '登录信息异常',
  594. content: '登录信息不完整,请重新登录',
  595. showCancel: false,
  596. confirmText: '重新登录',
  597. success: () => {
  598. // 清除异常的登录信息
  599. uni.clearStorageSync()
  600. uni.navigateTo({
  601. url: '/pages/page3/page3?redirect=' + encodeURIComponent('/pages/profile/index')
  602. })
  603. }
  604. })
  605. return false
  606. }
  607. return true
  608. },
  609. // 加载用户资料
  610. loadProfile() {
  611. uni.showLoading({
  612. title: '加载中...'
  613. })
  614. uni.request({
  615. url: this.gatewayURL + '/api/user/profile?userId=' + this.currentUserId,
  616. method: 'GET',
  617. success: async (res) => {
  618. uni.hideLoading()
  619. if (res.data && res.data.code === 200) {
  620. // 确保省市区ID正确赋值(处理可能的字段名不匹配)
  621. const profileData = { ...res.data.data }
  622. // 如果字段名是下划线格式,转换为驼峰格式
  623. if (profileData.province_id !== undefined && profileData.provinceId === undefined) {
  624. profileData.provinceId = profileData.province_id
  625. }
  626. if (profileData.city_id !== undefined && profileData.cityId === undefined) {
  627. profileData.cityId = profileData.city_id
  628. }
  629. if (profileData.area_id !== undefined && profileData.areaId === undefined) {
  630. profileData.areaId = profileData.area_id
  631. }
  632. this.profile = { ...this.profile, ...profileData }
  633. // 先加载省市区数据,然后再初始化索引和显示
  634. await this.loadAreaData()
  635. // 如果有省市区ID,加载对应的城市和区域数据,并初始化选择器索引
  636. if (this.profile.provinceId || this.profile.cityId || this.profile.areaId) {
  637. // 先加载省份对应的城市
  638. if (this.profile.provinceId) {
  639. await this.loadCitiesForProvince(this.profile.provinceId)
  640. }
  641. // 再加载城市对应的区域
  642. if (this.profile.cityId) {
  643. await this.loadAreasForCity(this.profile.cityId)
  644. }
  645. // 最后初始化选择器索引
  646. setTimeout(() => {
  647. this.initAreaIndex()
  648. }, 300)
  649. }
  650. } else {
  651. uni.showToast({
  652. title: res.data?.message || '加载失败',
  653. icon: 'none'
  654. })
  655. }
  656. },
  657. fail: (err) => {
  658. uni.hideLoading()
  659. console.error('加载用户资料失败:', err)
  660. uni.showToast({
  661. title: '网络请求失败',
  662. icon: 'none'
  663. })
  664. }
  665. })
  666. },
  667. // 格式化身份证号(隐藏中间部分)
  668. formatIdCard(idCard) {
  669. if (!idCard) return '未设置'
  670. if (idCard.length < 8) return idCard
  671. return idCard.substring(0, 6) + '********' + idCard.substring(idCard.length - 4)
  672. },
  673. // 获取性别文本
  674. getGenderText() {
  675. const option = this.genderOptions.find(item => item.value === this.profile.gender)
  676. return option ? option.label : '未设置'
  677. },
  678. // 获取学历文本
  679. getEducationText() {
  680. if (this.profile.educationLevel === null || this.profile.educationLevel === undefined) return '未设置'
  681. const index = this.profile.educationLevel - 1
  682. return this.educationOptions[index] || '未设置'
  683. },
  684. // 获取薪资文本
  685. getSalaryText() {
  686. if (this.profile.salaryRange === null || this.profile.salaryRange === undefined) return '未设置'
  687. const index = this.profile.salaryRange - 1
  688. return this.salaryOptions[index] || '未设置'
  689. },
  690. // 获取薪资选择器的索引
  691. getSalaryIndex() {
  692. if (this.profile.salaryRange === null || this.profile.salaryRange === undefined) {
  693. return 0 // 默认选择第一项
  694. }
  695. const index = this.profile.salaryRange - 1
  696. // 确保索引在有效范围内
  697. return Math.max(0, Math.min(index, this.salaryOptions.length - 1))
  698. },
  699. // 获取性别选择器的索引
  700. getGenderIndex() {
  701. const option = this.genderOptions.findIndex(item => item.value === this.profile.gender)
  702. return Math.max(0, option)
  703. },
  704. // 获取学历选择器的索引
  705. getEducationIndex() {
  706. if (this.profile.educationLevel === null || this.profile.educationLevel === undefined) {
  707. return 0
  708. }
  709. const index = this.profile.educationLevel - 1
  710. return Math.max(0, Math.min(index, this.educationOptions.length - 1))
  711. },
  712. // 获取婚姻状况选择器的索引
  713. getMaritalIndex() {
  714. if (this.profile.maritalStatus === null || this.profile.maritalStatus === undefined) {
  715. return 0
  716. }
  717. return Math.max(0, Math.min(this.profile.maritalStatus, this.maritalOptions.length - 1))
  718. },
  719. // 获取房产选择器的索引
  720. getHouseIndex() {
  721. const option = this.houseOptions.findIndex(item => item.value === this.profile.house)
  722. return Math.max(0, option)
  723. },
  724. // 获取车辆选择器的索引
  725. getCarIndex() {
  726. const option = this.carOptions.findIndex(item => item.value === this.profile.car)
  727. return Math.max(0, option)
  728. },
  729. // 获取婚姻状况文本
  730. getMaritalText() {
  731. if (this.profile.maritalStatus === null || this.profile.maritalStatus === undefined) return '未设置'
  732. const index = this.profile.maritalStatus
  733. return this.maritalOptions[index] || '未设置'
  734. },
  735. // 获取房产文本
  736. getHouseText() {
  737. const option = this.houseOptions.find(item => item.value === this.profile.house)
  738. return option ? option.label : '未设置'
  739. },
  740. // 获取车辆文本
  741. getCarText() {
  742. const option = this.carOptions.find(item => item.value === this.profile.car)
  743. return option ? option.label : '未设置'
  744. },
  745. // 选择器改变事件
  746. onGenderChange(e) {
  747. this.profile.gender = this.genderOptions[parseInt(e.detail.value)].value
  748. },
  749. onBirthDateChange(e) {
  750. this.profile.birthDate = e.detail.value
  751. // 自动计算并设置生肖
  752. if (this.profile.birthDate) {
  753. const zodiac = getZodiacByBirthday(this.profile.birthDate)
  754. this.profile.animal = zodiac
  755. }
  756. },
  757. onStarChange(e) {
  758. this.profile.star = this.starOptions[parseInt(e.detail.value)]
  759. },
  760. onAnimalChange(e) {
  761. this.profile.animal = this.animalOptions[parseInt(e.detail.value)]
  762. },
  763. onEducationChange(e) {
  764. // 将字符串转换为数字
  765. this.profile.educationLevel = parseInt(e.detail.value) + 1
  766. },
  767. onSalaryChange(e) {
  768. // 将字符串转换为数字,避免字符串拼接问题
  769. this.profile.salaryRange = parseInt(e.detail.value) + 1
  770. },
  771. onMaritalChange(e) {
  772. // 将字符串转换为数字
  773. this.profile.maritalStatus = parseInt(e.detail.value)
  774. },
  775. onHouseChange(e) {
  776. this.profile.house = this.houseOptions[parseInt(e.detail.value)].value
  777. },
  778. onCarChange(e) {
  779. this.profile.car = this.carOptions[parseInt(e.detail.value)].value
  780. },
  781. onPrivacySalaryChange(e) {
  782. this.profile.privacySalary = e.detail.value ? 1 : 0
  783. },
  784. onPrivacyPhoneChange(e) {
  785. this.profile.privacyPhone = e.detail.value ? 1 : 0
  786. },
  787. // 省市区相关方法
  788. // 加载省市区数据
  789. async loadAreaData() {
  790. try {
  791. // 加载省份列表
  792. const provinceRes = await api.area.getProvinces()
  793. // api.js 已经处理了数据格式,直接使用返回的数组
  794. if (Array.isArray(provinceRes)) {
  795. this.provinceList = provinceRes
  796. } else if (provinceRes && provinceRes.data) {
  797. this.provinceList = provinceRes.data
  798. } else if (provinceRes && provinceRes.code === 200 && provinceRes.data) {
  799. this.provinceList = provinceRes.data
  800. } else {
  801. this.provinceList = []
  802. }
  803. this.multiAreaData[0] = this.provinceList
  804. // 如果有已选择的省份,加载对应的城市
  805. if (this.profile.provinceId) {
  806. await this.loadCitiesForProvince(this.profile.provinceId)
  807. } else if (this.provinceList.length > 0) {
  808. // 默认加载第一个省份的城市
  809. await this.loadCitiesForProvince(this.provinceList[0].id)
  810. }
  811. } catch (e) {
  812. console.error('加载省份失败:', e)
  813. uni.showToast({
  814. title: '加载省份数据失败',
  815. icon: 'none'
  816. })
  817. }
  818. },
  819. // 根据省份ID加载城市
  820. async loadCitiesForProvince(provinceId) {
  821. if (!provinceId) return
  822. try {
  823. const cityRes = await api.area.getCities(provinceId)
  824. // api.js 已经处理了数据格式,直接使用返回的数组
  825. if (Array.isArray(cityRes)) {
  826. this.cityList = cityRes
  827. } else if (cityRes && cityRes.data) {
  828. this.cityList = cityRes.data
  829. } else if (cityRes && cityRes.code === 200 && cityRes.data) {
  830. this.cityList = cityRes.data
  831. } else {
  832. this.cityList = []
  833. }
  834. this.multiAreaData[1] = this.cityList
  835. // 如果有已选择的城市,加载对应的区域
  836. if (this.profile.cityId) {
  837. await this.loadAreasForCity(this.profile.cityId)
  838. } else if (this.cityList.length > 0) {
  839. // 默认加载第一个城市的区域
  840. await this.loadAreasForCity(this.cityList[0].id)
  841. } else {
  842. this.multiAreaData[2] = []
  843. this.areaList = []
  844. }
  845. } catch (e) {
  846. console.error('加载城市失败:', e)
  847. this.cityList = []
  848. this.multiAreaData[1] = []
  849. this.multiAreaData[2] = []
  850. this.areaList = []
  851. }
  852. },
  853. // 根据城市ID加载区域
  854. async loadAreasForCity(cityId) {
  855. if (!cityId) return
  856. try {
  857. const areaRes = await api.area.getAreas(cityId)
  858. // api.js 已经处理了数据格式,直接使用返回的数组
  859. if (Array.isArray(areaRes)) {
  860. this.areaList = areaRes
  861. } else if (areaRes && areaRes.data) {
  862. this.areaList = areaRes.data
  863. } else if (areaRes && areaRes.code === 200 && areaRes.data) {
  864. this.areaList = areaRes.data
  865. } else {
  866. this.areaList = []
  867. }
  868. this.multiAreaData[2] = this.areaList
  869. } catch (e) {
  870. console.error('加载区域失败:', e)
  871. this.areaList = []
  872. this.multiAreaData[2] = []
  873. }
  874. },
  875. // 省市区选择器列变化事件
  876. async onAreaColumnChange(e) {
  877. const column = e.detail.column // 列索引:0-省,1-市,2-区
  878. const row = e.detail.value // 选中的行索引
  879. if (column === 0) {
  880. // 选择了省份
  881. const province = this.provinceList[row]
  882. if (province && province.id) {
  883. this.multiAreaIndex[0] = row
  884. this.multiAreaIndex[1] = 0
  885. this.multiAreaIndex[2] = 0
  886. // 加载该省份的城市
  887. await this.loadCitiesForProvince(province.id)
  888. }
  889. } else if (column === 1) {
  890. // 选择了城市
  891. const city = this.cityList[row]
  892. if (city && city.id) {
  893. this.multiAreaIndex[1] = row
  894. this.multiAreaIndex[2] = 0
  895. // 加载该城市的区域
  896. await this.loadAreasForCity(city.id)
  897. }
  898. } else if (column === 2) {
  899. // 选择了区域
  900. this.multiAreaIndex[2] = row
  901. }
  902. },
  903. // 省市区选择器确认事件
  904. onAreaChange(e) {
  905. const values = e.detail.value
  906. this.multiAreaIndex = values
  907. // 设置选中的省市区ID
  908. if (this.provinceList[values[0]]) {
  909. this.profile.provinceId = this.provinceList[values[0]].id
  910. }
  911. if (this.cityList[values[1]]) {
  912. this.profile.cityId = this.cityList[values[1]].id
  913. }
  914. if (this.areaList[values[2]]) {
  915. this.profile.areaId = this.areaList[values[2]].id
  916. }
  917. },
  918. // 获取地区显示文本
  919. getAreaText() {
  920. // 如果没有省市区ID,返回未设置
  921. if (!this.profile.provinceId && !this.profile.cityId && !this.profile.areaId) {
  922. return '未设置'
  923. }
  924. let text = ''
  925. let hasText = false
  926. // 查找省份名称
  927. if (this.profile.provinceId) {
  928. if (this.provinceList.length > 0) {
  929. const province = this.provinceList.find(p => p.id === this.profile.provinceId)
  930. if (province) {
  931. text += province.name
  932. hasText = true
  933. }
  934. } else {
  935. // 如果省份列表还没加载,触发加载
  936. if (this.provinceList.length === 0) {
  937. this.loadAreaData()
  938. }
  939. return '加载中...'
  940. }
  941. }
  942. // 查找城市名称
  943. if (this.profile.cityId) {
  944. if (this.cityList.length > 0) {
  945. const city = this.cityList.find(c => c.id === this.profile.cityId)
  946. if (city) {
  947. text += (hasText ? ' ' : '') + city.name
  948. hasText = true
  949. }
  950. } else if (this.profile.provinceId) {
  951. // 如果城市列表还没加载但有省份ID,加载城市数据
  952. this.loadCitiesForProvince(this.profile.provinceId)
  953. return hasText ? text + ' 加载中...' : '加载中...'
  954. }
  955. }
  956. // 查找区域名称
  957. if (this.profile.areaId) {
  958. if (this.areaList.length > 0) {
  959. const area = this.areaList.find(a => a.id === this.profile.areaId)
  960. if (area) {
  961. text += (hasText ? ' ' : '') + area.name
  962. hasText = true
  963. }
  964. } else if (this.profile.cityId) {
  965. // 如果区域列表还没加载但有城市ID,加载区域数据
  966. this.loadAreasForCity(this.profile.cityId)
  967. return hasText ? text + ' 加载中...' : '加载中...'
  968. }
  969. }
  970. return text || '未设置'
  971. },
  972. // 初始化省市区选择器索引(根据已保存的ID)
  973. async initAreaIndex() {
  974. if (!this.profile.provinceId) {
  975. return
  976. }
  977. // 如果省份列表还没加载,先加载
  978. if (this.provinceList.length === 0) {
  979. await this.loadAreaData()
  980. }
  981. // 查找省份索引
  982. const provinceIndex = this.provinceList.findIndex(p => p.id === this.profile.provinceId)
  983. if (provinceIndex >= 0) {
  984. this.multiAreaIndex[0] = provinceIndex
  985. // 加载该省份的城市
  986. await this.loadCitiesForProvince(this.profile.provinceId)
  987. // 等待城市加载完成后再查找城市索引
  988. await new Promise(resolve => setTimeout(resolve, 300))
  989. if (this.profile.cityId) {
  990. const cityIndex = this.cityList.findIndex(c => c.id === this.profile.cityId)
  991. if (cityIndex >= 0) {
  992. this.multiAreaIndex[1] = cityIndex
  993. // 加载该城市的区域
  994. await this.loadAreasForCity(this.profile.cityId)
  995. // 等待区域加载完成后再查找区域索引
  996. await new Promise(resolve => setTimeout(resolve, 300))
  997. if (this.profile.areaId) {
  998. const areaIndex = this.areaList.findIndex(a => a.id === this.profile.areaId)
  999. if (areaIndex >= 0) {
  1000. this.multiAreaIndex[2] = areaIndex
  1001. }
  1002. }
  1003. }
  1004. }
  1005. // 强制更新视图
  1006. this.$forceUpdate()
  1007. } else {
  1008. console.warn('未找到省份索引,省份ID:', this.profile.provinceId)
  1009. }
  1010. },
  1011. // 显示标签选择弹窗
  1012. addHobby() {
  1013. this.showHobbyModal = true
  1014. },
  1015. // 关闭标签选择弹窗
  1016. closeHobbyModal() {
  1017. this.showHobbyModal = false
  1018. this.customHobbyInput = ''
  1019. },
  1020. // 选择预设标签
  1021. selectPresetTag(tag) {
  1022. if (this.hobbyList.includes(tag)) {
  1023. // 如果已选中,则移除
  1024. this.removeHobbyTag(this.hobbyList.indexOf(tag))
  1025. } else {
  1026. // 如果未选中,则添加
  1027. this.addHobbyTag(tag)
  1028. }
  1029. },
  1030. // 添加自定义标签
  1031. addCustomTag() {
  1032. const customTag = this.customHobbyInput.trim()
  1033. if (customTag) {
  1034. this.addHobbyTag(customTag)
  1035. this.customHobbyInput = ''
  1036. } else {
  1037. uni.showToast({
  1038. title: '请输入标签内容',
  1039. icon: 'none'
  1040. })
  1041. }
  1042. },
  1043. // 添加标签到列表
  1044. addHobbyTag(hobbyText) {
  1045. let currentHobbies = this.hobbyList.slice()
  1046. // 检查是否已存在
  1047. if (currentHobbies.includes(hobbyText)) {
  1048. uni.showToast({
  1049. title: '标签已存在',
  1050. icon: 'none'
  1051. })
  1052. return
  1053. }
  1054. // 限制标签数量
  1055. if (currentHobbies.length >= 10) {
  1056. uni.showToast({
  1057. title: '最多只能添加10个标签',
  1058. icon: 'none'
  1059. })
  1060. return
  1061. }
  1062. // 添加新标签
  1063. currentHobbies.push(hobbyText)
  1064. // 更新hobby字段(转为JSON格式)
  1065. this.profile.hobby = JSON.stringify(currentHobbies)
  1066. uni.showToast({
  1067. title: '标签添加成功',
  1068. icon: 'success'
  1069. })
  1070. },
  1071. // 移除标签
  1072. removeHobbyTag(index) {
  1073. uni.showModal({
  1074. title: '确认删除',
  1075. content: '确定要删除这个标签吗?',
  1076. success: (res) => {
  1077. if (res.confirm) {
  1078. let currentHobbies = this.hobbyList.slice()
  1079. currentHobbies.splice(index, 1)
  1080. // 更新hobby字段
  1081. this.profile.hobby = currentHobbies.length > 0 ? JSON.stringify(currentHobbies) : ''
  1082. uni.showToast({
  1083. title: '标签删除成功',
  1084. icon: 'success'
  1085. })
  1086. }
  1087. }
  1088. })
  1089. },
  1090. // 点击字段进入编辑模式
  1091. startEdit() {
  1092. if (!this.isEditing) {
  1093. this.isEditing = true
  1094. // 提示用户已进入编辑模式
  1095. uni.showToast({
  1096. title: '已进入编辑模式',
  1097. icon: 'none',
  1098. duration: 1000
  1099. })
  1100. }
  1101. },
  1102. // 处理地区点击事件
  1103. handleAreaClick() {
  1104. if (!this.isEditing) {
  1105. this.startEdit()
  1106. }
  1107. // 如果数据还没加载,立即加载
  1108. if (this.provinceList.length === 0) {
  1109. this.loadAreaData()
  1110. }
  1111. },
  1112. // 启用编辑/保存
  1113. enableEdit() {
  1114. if (this.isEditing) {
  1115. // 保存
  1116. this.saveProfile()
  1117. } else {
  1118. // 进入编辑模式
  1119. this.isEditing = true
  1120. }
  1121. },
  1122. // 保存资料
  1123. saveProfile(needGoBack = false) {
  1124. if (!this.validateIdCard()) {
  1125. return;
  1126. }
  1127. uni.showLoading({
  1128. title: '保存中...'
  1129. })
  1130. // 准备提交的数据(只包含扩展信息字段)
  1131. const submitData = {
  1132. userId: this.currentUserId,
  1133. nickname: this.profile.nickname,
  1134. realName: this.profile.realName,
  1135. idCard: this.profile.idCard,
  1136. height: this.profile.height,
  1137. weight: this.profile.weight,
  1138. gender: this.profile.gender,
  1139. birthDate: this.profile.birthDate,
  1140. star: this.profile.star,
  1141. animal: this.profile.animal,
  1142. educationLevel: this.profile.educationLevel,
  1143. schoolName: this.profile.schoolName,
  1144. company: this.profile.company,
  1145. jobTitle: this.profile.jobTitle,
  1146. salaryRange: this.profile.salaryRange,
  1147. maritalStatus: this.profile.maritalStatus,
  1148. house: this.profile.house,
  1149. car: this.profile.car,
  1150. hobby: this.profile.hobby,
  1151. privacySalary: this.profile.privacySalary,
  1152. privacyPhone: this.profile.privacyPhone,
  1153. provinceId: this.profile.provinceId || null,
  1154. cityId: this.profile.cityId || null,
  1155. areaId: this.profile.areaId || null
  1156. }
  1157. uni.request({
  1158. url: this.gatewayURL + '/api/user/profile',
  1159. method: 'PUT',
  1160. data: submitData,
  1161. success: (res) => {
  1162. uni.hideLoading()
  1163. if (res.data && res.data.code === 200) {
  1164. uni.showToast({
  1165. title: '保存成功',
  1166. icon: 'success',
  1167. duration: 1500
  1168. })
  1169. this.isEditing = false
  1170. // 更新 storage 中的 userInfo,特别是性别信息
  1171. const storedUserInfo = uni.getStorageSync('userInfo');
  1172. if (storedUserInfo) {
  1173. // 更新性别信息
  1174. if (submitData.gender !== undefined && submitData.gender !== null) {
  1175. storedUserInfo.gender = submitData.gender;
  1176. }
  1177. // 更新其他可能变化的字段
  1178. if (submitData.birthDate) {
  1179. storedUserInfo.birthDate = submitData.birthDate;
  1180. }
  1181. uni.setStorageSync('userInfo', storedUserInfo);
  1182. }
  1183. // 重新加载资料
  1184. this.loadProfile()
  1185. // 如果需要返回,延迟执行返回操作
  1186. if (needGoBack) {
  1187. setTimeout(() => {
  1188. this.performGoBack()
  1189. }, 1000)
  1190. }
  1191. } else {
  1192. uni.showToast({
  1193. title: res.data?.message || '保存失败',
  1194. icon: 'none'
  1195. })
  1196. }
  1197. },
  1198. fail: (err) => {
  1199. uni.hideLoading()
  1200. console.error('保存失败:', err)
  1201. uni.showToast({
  1202. title: '网络请求失败',
  1203. icon: 'none'
  1204. })
  1205. }
  1206. })
  1207. },
  1208. // 返回上一页
  1209. goBack() {
  1210. try {
  1211. // 如果正在编辑,先提醒保存
  1212. if (this.isEditing) {
  1213. uni.showModal({
  1214. title: '提示',
  1215. content: '您正在编辑资料,是否保存后返回?',
  1216. showCancel: true,
  1217. cancelText: '直接返回',
  1218. confirmText: '保存并返回',
  1219. success: (res) => {
  1220. if (res.confirm) {
  1221. // 保存后返回
  1222. this.saveProfile(true)
  1223. } else {
  1224. // 直接返回
  1225. this.performGoBack()
  1226. }
  1227. }
  1228. })
  1229. } else {
  1230. // 直接返回
  1231. this.performGoBack()
  1232. }
  1233. } catch (error) {
  1234. console.error('返回操作异常:', error)
  1235. // 异常时直接跳转到我的页面
  1236. uni.navigateTo({
  1237. url: '/pages/mine/index'
  1238. })
  1239. }
  1240. },
  1241. // 执行返回操作
  1242. performGoBack() {
  1243. // 检查页面栈
  1244. const pages = getCurrentPages()
  1245. if (pages.length > 1) {
  1246. // 有上一页,正常返回
  1247. uni.navigateBack({
  1248. delta: 1,
  1249. success: () => {
  1250. },
  1251. fail: (err) => {
  1252. console.error('返回失败:', err)
  1253. // 返回失败时,提示用户并提供选择
  1254. uni.showModal({
  1255. title: '返回失败',
  1256. content: '无法返回上一页,是否跳转到我的页面?',
  1257. showCancel: true,
  1258. cancelText: '留在此页',
  1259. confirmText: '去我的页面',
  1260. success: (modalRes) => {
  1261. if (modalRes.confirm) {
  1262. uni.navigateTo({
  1263. url: '/pages/mine/index'
  1264. })
  1265. }
  1266. }
  1267. })
  1268. }
  1269. })
  1270. } else {
  1271. // 没有上一页,直接跳转到我的页面
  1272. uni.navigateTo({
  1273. url: '/pages/mine/index'
  1274. })
  1275. }
  1276. }
  1277. }
  1278. }
  1279. </script>
  1280. <style lang="scss" scoped>
  1281. .profile-page {
  1282. min-height: 100vh;
  1283. background: linear-gradient(180deg, #FF8A9B 0%, #FFB4C0 25%, #FFF0F2 50%, #F8F8F8 100%);
  1284. display: flex;
  1285. flex-direction: column;
  1286. }
  1287. /* 顶部导航栏 */
  1288. .header {
  1289. position: relative;
  1290. display: flex;
  1291. align-items: center;
  1292. justify-content: center;
  1293. padding: 25rpx 30rpx;
  1294. padding-top: calc(25rpx + env(safe-area-inset-top));
  1295. background: linear-gradient(135deg, #FF8A9B 0%, #FFB4C0 100%);
  1296. box-shadow: 0 4rpx 20rpx rgba(255, 138, 155, 0.2);
  1297. .back-btn {
  1298. position: absolute;
  1299. left: 30rpx;
  1300. width: 70rpx;
  1301. height: 70rpx;
  1302. display: flex;
  1303. align-items: center;
  1304. justify-content: center;
  1305. background: rgba(255, 255, 255, 0.2);
  1306. border-radius: 50%;
  1307. .back-icon {
  1308. font-size: 44rpx;
  1309. color: #FFFFFF;
  1310. font-weight: bold;
  1311. }
  1312. }
  1313. .header-title {
  1314. font-size: 38rpx;
  1315. font-weight: bold;
  1316. color: #FFFFFF;
  1317. letter-spacing: 2rpx;
  1318. }
  1319. .edit-btn {
  1320. position: absolute;
  1321. right: 30rpx;
  1322. padding: 12rpx 30rpx;
  1323. background: rgba(255, 255, 255, 0.3);
  1324. border-radius: 35rpx;
  1325. min-width: 100rpx;
  1326. display: flex;
  1327. align-items: center;
  1328. justify-content: center;
  1329. .edit-text {
  1330. font-size: 28rpx;
  1331. color: #FFFFFF;
  1332. font-weight: 500;
  1333. }
  1334. }
  1335. }
  1336. /* 头像区域 */
  1337. .avatar-section {
  1338. display: flex;
  1339. flex-direction: column;
  1340. align-items: center;
  1341. padding: 50rpx 0 40rpx;
  1342. .avatar-wrapper {
  1343. position: relative;
  1344. margin-bottom: 25rpx;
  1345. cursor: pointer;
  1346. .avatar {
  1347. width: 180rpx;
  1348. height: 180rpx;
  1349. border-radius: 90rpx;
  1350. border: 6rpx solid #FFFFFF;
  1351. box-shadow: 0 8rpx 30rpx rgba(255, 138, 155, 0.3);
  1352. }
  1353. .avatar-badge {
  1354. position: absolute;
  1355. bottom: 5rpx;
  1356. right: 5rpx;
  1357. width: 50rpx;
  1358. height: 50rpx;
  1359. background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
  1360. border-radius: 25rpx;
  1361. display: flex;
  1362. align-items: center;
  1363. justify-content: center;
  1364. border: 4rpx solid #FFFFFF;
  1365. box-shadow: 0 4rpx 15rpx rgba(255, 215, 0, 0.4);
  1366. .vip-icon {
  1367. font-size: 28rpx;
  1368. }
  1369. }
  1370. .avatar-edit-hint {
  1371. position: absolute;
  1372. bottom: 0;
  1373. left: 0;
  1374. right: 0;
  1375. height: 60rpx;
  1376. background: rgba(0, 0, 0, 0.5);
  1377. border-radius: 0 0 90rpx 90rpx;
  1378. display: flex;
  1379. align-items: center;
  1380. justify-content: center;
  1381. .edit-hint-icon {
  1382. font-size: 32rpx;
  1383. color: #FFFFFF;
  1384. }
  1385. }
  1386. }
  1387. .nickname {
  1388. font-size: 40rpx;
  1389. font-weight: bold;
  1390. color: #333333;
  1391. margin-bottom: 12rpx;
  1392. }
  1393. .user-id {
  1394. font-size: 26rpx;
  1395. color: #999999;
  1396. background: rgba(255, 255, 255, 0.8);
  1397. padding: 8rpx 30rpx;
  1398. border-radius: 30rpx;
  1399. }
  1400. }
  1401. /* 编辑提示 */
  1402. .edit-tip {
  1403. margin: 0 30rpx;
  1404. background: linear-gradient(135deg, #E8F5E8 0%, #D4EDDA 100%);
  1405. border-radius: 15rpx;
  1406. padding: 20rpx 25rpx;
  1407. text-align: center;
  1408. border-left: 6rpx solid #28A745;
  1409. .tip-text {
  1410. font-size: 26rpx;
  1411. color: #28A745;
  1412. font-weight: 500;
  1413. }
  1414. }
  1415. /* 滚动内容区域 */
  1416. .content-scroll {
  1417. flex: 1;
  1418. padding: 20rpx 30rpx 0 30rpx;
  1419. box-sizing: border-box;
  1420. }
  1421. /* 各个section */
  1422. .section {
  1423. background-color: #FFFFFF;
  1424. border-radius: 20rpx;
  1425. padding: 30rpx 25rpx;
  1426. margin-bottom: 25rpx;
  1427. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
  1428. width: 100%;
  1429. box-sizing: border-box;
  1430. .section-title {
  1431. font-size: 34rpx;
  1432. font-weight: bold;
  1433. color: #333333;
  1434. margin-bottom: 30rpx;
  1435. padding-bottom: 20rpx;
  1436. padding-left: 30rpx;
  1437. border-bottom: 3rpx solid #F0F0F0;
  1438. display: flex;
  1439. align-items: center;
  1440. position: relative;
  1441. &::before {
  1442. content: '';
  1443. position: absolute;
  1444. left: 5rpx;
  1445. width: 8rpx;
  1446. height: 32rpx;
  1447. background: linear-gradient(135deg, #FF8A9B 0%, #FF6B8A 100%);
  1448. border-radius: 4rpx;
  1449. }
  1450. }
  1451. .info-item {
  1452. display: flex;
  1453. align-items: center;
  1454. justify-content: space-between;
  1455. padding: 30rpx 0;
  1456. padding-left: 30rpx;
  1457. border-bottom: 1rpx solid #F5F5F5;
  1458. min-height: 80rpx;
  1459. position: relative;
  1460. transition: background-color 0.2s;
  1461. &:active {
  1462. background-color: #F8F8F8;
  1463. }
  1464. &:last-child {
  1465. border-bottom: none;
  1466. }
  1467. &.column {
  1468. flex-direction: column;
  1469. align-items: flex-start;
  1470. }
  1471. .label {
  1472. font-size: 30rpx;
  1473. color: #333333;
  1474. flex-shrink: 0;
  1475. width: 180rpx;
  1476. font-weight: 500;
  1477. }
  1478. .value {
  1479. flex: 1;
  1480. text-align: right;
  1481. font-size: 30rpx;
  1482. color: #666666;
  1483. padding-left: 20rpx;
  1484. }
  1485. .input {
  1486. flex: 1;
  1487. text-align: right;
  1488. font-size: 30rpx;
  1489. color: #333333;
  1490. padding-left: 20rpx;
  1491. }
  1492. .picker-value {
  1493. flex: 1;
  1494. text-align: right;
  1495. font-size: 30rpx;
  1496. color: #666666;
  1497. padding-left: 20rpx;
  1498. }
  1499. .verify-status {
  1500. margin-left: 15rpx;
  1501. font-size: 22rpx;
  1502. flex-shrink: 0;
  1503. &.verified {
  1504. color: #4CAF50;
  1505. }
  1506. &.unverified {
  1507. color: #999999;
  1508. }
  1509. }
  1510. switch {
  1511. flex-shrink: 0;
  1512. }
  1513. }
  1514. }
  1515. /* 爱好标签 */
  1516. .hobby-tags {
  1517. display: flex;
  1518. flex-wrap: wrap;
  1519. gap: 18rpx;
  1520. margin-top: 20rpx;
  1521. width: 100%;
  1522. .hobby-tag {
  1523. padding: 15rpx 30rpx;
  1524. background: linear-gradient(135deg, #FFE5EC 0%, #FFD0DC 100%);
  1525. border-radius: 35rpx;
  1526. font-size: 26rpx;
  1527. color: #FF6B8A;
  1528. font-weight: 500;
  1529. position: relative;
  1530. display: flex;
  1531. align-items: center;
  1532. transition: all 0.3s;
  1533. &.add {
  1534. border: 2rpx dashed #FF8A9B;
  1535. background: transparent;
  1536. color: #FF8A9B;
  1537. justify-content: center;
  1538. }
  1539. &.deletable {
  1540. padding-right: 50rpx;
  1541. &:active {
  1542. background: linear-gradient(135deg, #FFB8C5 0%, #FFA3B3 100%);
  1543. transform: scale(0.95);
  1544. }
  1545. }
  1546. .delete-icon {
  1547. position: absolute;
  1548. right: 15rpx;
  1549. top: 50%;
  1550. transform: translateY(-50%);
  1551. width: 30rpx;
  1552. height: 30rpx;
  1553. background: rgba(255, 255, 255, 0.8);
  1554. border-radius: 50%;
  1555. display: flex;
  1556. align-items: center;
  1557. justify-content: center;
  1558. font-size: 24rpx;
  1559. color: #FF6B8A;
  1560. font-weight: bold;
  1561. }
  1562. }
  1563. .hobby-empty {
  1564. width: 100%;
  1565. text-align: center;
  1566. padding: 40rpx 0;
  1567. .empty-text {
  1568. font-size: 26rpx;
  1569. color: #999999;
  1570. }
  1571. }
  1572. }
  1573. /* 真实度评分 */
  1574. .authenticity-score {
  1575. display: flex;
  1576. flex-direction: column;
  1577. align-items: center;
  1578. padding: 40rpx 0 20rpx;
  1579. .score-circle {
  1580. width: 200rpx;
  1581. height: 200rpx;
  1582. border-radius: 100rpx;
  1583. background: linear-gradient(135deg, #FF8A9B 0%, #FF6B8A 100%);
  1584. display: flex;
  1585. flex-direction: column;
  1586. align-items: center;
  1587. justify-content: center;
  1588. margin-bottom: 30rpx;
  1589. box-shadow: 0 10rpx 40rpx rgba(255, 107, 138, 0.4);
  1590. position: relative;
  1591. &::before {
  1592. content: '';
  1593. position: absolute;
  1594. width: 220rpx;
  1595. height: 220rpx;
  1596. border-radius: 110rpx;
  1597. border: 2rpx solid rgba(255, 138, 155, 0.3);
  1598. top: -10rpx;
  1599. left: -10rpx;
  1600. }
  1601. .score-number {
  1602. font-size: 68rpx;
  1603. font-weight: bold;
  1604. color: #FFFFFF;
  1605. line-height: 1;
  1606. }
  1607. .score-total {
  1608. font-size: 26rpx;
  1609. color: rgba(255, 255, 255, 0.95);
  1610. margin-top: 5rpx;
  1611. }
  1612. }
  1613. .score-tips {
  1614. background: rgba(255, 138, 155, 0.1);
  1615. padding: 15rpx 30rpx;
  1616. border-radius: 30rpx;
  1617. .tip-text {
  1618. font-size: 24rpx;
  1619. color: #FF6B8A;
  1620. text-align: center;
  1621. }
  1622. }
  1623. }
  1624. .bottom-placeholder {
  1625. height: 80rpx;
  1626. }
  1627. /* 标签选择弹窗 */
  1628. .hobby-modal {
  1629. position: fixed;
  1630. top: 0;
  1631. left: 0;
  1632. right: 0;
  1633. bottom: 0;
  1634. background: rgba(0, 0, 0, 0.6);
  1635. display: flex;
  1636. align-items: center;
  1637. justify-content: center;
  1638. z-index: 9999;
  1639. .modal-content {
  1640. width: 90%;
  1641. max-height: 80vh;
  1642. background: #FFFFFF;
  1643. border-radius: 30rpx;
  1644. overflow: hidden;
  1645. display: flex;
  1646. flex-direction: column;
  1647. }
  1648. .modal-header {
  1649. display: flex;
  1650. align-items: center;
  1651. justify-content: space-between;
  1652. padding: 40rpx 30rpx 20rpx;
  1653. background: linear-gradient(135deg, #FF8A9B 0%, #FFB4C0 100%);
  1654. .modal-title {
  1655. font-size: 32rpx;
  1656. font-weight: bold;
  1657. color: #FFFFFF;
  1658. }
  1659. .close-btn {
  1660. width: 60rpx;
  1661. height: 60rpx;
  1662. display: flex;
  1663. align-items: center;
  1664. justify-content: center;
  1665. font-size: 40rpx;
  1666. color: #FFFFFF;
  1667. background: rgba(255, 255, 255, 0.2);
  1668. border-radius: 30rpx;
  1669. }
  1670. }
  1671. .modal-body {
  1672. flex: 1;
  1673. padding: 30rpx;
  1674. max-height: 60vh;
  1675. }
  1676. .hobby-category {
  1677. margin-bottom: 40rpx;
  1678. &:last-child {
  1679. margin-bottom: 0;
  1680. }
  1681. .category-title {
  1682. display: flex;
  1683. align-items: center;
  1684. margin-bottom: 20rpx;
  1685. .category-icon {
  1686. font-size: 36rpx;
  1687. margin-right: 15rpx;
  1688. }
  1689. .category-name {
  1690. font-size: 28rpx;
  1691. font-weight: bold;
  1692. color: #333333;
  1693. }
  1694. }
  1695. .category-tags {
  1696. display: flex;
  1697. flex-wrap: wrap;
  1698. gap: 15rpx;
  1699. .preset-tag {
  1700. padding: 12rpx 25rpx;
  1701. background: #F5F5F5;
  1702. border-radius: 30rpx;
  1703. font-size: 26rpx;
  1704. color: #666666;
  1705. position: relative;
  1706. transition: all 0.3s;
  1707. &.selected {
  1708. background: linear-gradient(135deg, #FF8A9B 0%, #FF6B8A 100%);
  1709. color: #FFFFFF;
  1710. padding-right: 45rpx;
  1711. }
  1712. &:active {
  1713. transform: scale(0.95);
  1714. }
  1715. .check-icon {
  1716. position: absolute;
  1717. right: 10rpx;
  1718. top: 50%;
  1719. transform: translateY(-50%);
  1720. font-size: 20rpx;
  1721. font-weight: bold;
  1722. }
  1723. }
  1724. }
  1725. }
  1726. .custom-input-section {
  1727. margin-top: 20rpx;
  1728. padding-top: 30rpx;
  1729. border-top: 1rpx solid #F0F0F0;
  1730. .input-wrapper {
  1731. display: flex;
  1732. align-items: center;
  1733. gap: 20rpx;
  1734. .custom-input {
  1735. flex: 1;
  1736. padding: 20rpx 25rpx;
  1737. background: #F8F8F8;
  1738. border-radius: 25rpx;
  1739. font-size: 26rpx;
  1740. border: 2rpx solid transparent;
  1741. &:focus {
  1742. border-color: #FF8A9B;
  1743. background: #FFFFFF;
  1744. }
  1745. }
  1746. .add-custom-btn {
  1747. padding: 20rpx 30rpx;
  1748. background: linear-gradient(135deg, #FF8A9B 0%, #FF6B8A 100%);
  1749. border-radius: 25rpx;
  1750. .add-text {
  1751. font-size: 26rpx;
  1752. color: #FFFFFF;
  1753. font-weight: 500;
  1754. }
  1755. }
  1756. }
  1757. }
  1758. .modal-footer {
  1759. display: flex;
  1760. align-items: center;
  1761. justify-content: space-between;
  1762. padding: 30rpx;
  1763. background: #F8F8F8;
  1764. .selected-count {
  1765. font-size: 26rpx;
  1766. color: #666666;
  1767. }
  1768. .done-btn {
  1769. padding: 20rpx 40rpx;
  1770. background: linear-gradient(135deg, #FF8A9B 0%, #FF6B8A 100%);
  1771. border-radius: 30rpx;
  1772. .done-text {
  1773. font-size: 28rpx;
  1774. color: #FFFFFF;
  1775. font-weight: bold;
  1776. }
  1777. }
  1778. }
  1779. }
  1780. // 补充身份证错误样式
  1781. .info-item {
  1782. &.error {
  1783. .input {
  1784. border-color: #ff4757;
  1785. color: #ff4757;
  1786. }
  1787. }
  1788. .verify-status {
  1789. &.warning {
  1790. color: #ffa502;
  1791. }
  1792. }
  1793. }
  1794. </style>