index.vue 54 KB

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