resource-input.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <template>
  2. <view class="resource-input-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="header">
  5. <view class="header-left" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <text class="header-title">信息录入</text>
  9. <view class="header-right">
  10. <text class="more-icon">⋯</text>
  11. </view>
  12. </view>
  13. <scroll-view scroll-y class="content">
  14. <view class="form-container">
  15. <!-- 基本信息 -->
  16. <view class="form-section" :class="{ 'active': activeSection === 'basic' }">
  17. <view class="section-title">基本信息</view>
  18. <view class="form-item">
  19. <text class="form-label">姓名<text class="required">*</text></text>
  20. <input type="text" class="form-input" placeholder="请输入姓名" v-model="formData.name" />
  21. </view>
  22. <view class="form-item">
  23. <text class="form-label">年龄</text>
  24. <input type="number" class="form-input" placeholder="请输入年龄" v-model="formData.age" />
  25. </view>
  26. <view class="form-item">
  27. <text class="form-label">性别</text>
  28. <picker mode="selector" :range="genderOptions" :value="genderIndex" @change="onGenderChange">
  29. <view class="form-input picker-input">
  30. <text v-if="formData.gender">{{ genderOptions[genderIndex] }}</text>
  31. <text v-else class="placeholder">请选择性别</text>
  32. <text class="picker-arrow">▼</text>
  33. </view>
  34. </picker>
  35. </view>
  36. <view class="form-item">
  37. <text class="form-label">星座</text>
  38. <picker mode="selector" :range="constellationOptions" :value="constellationIndex" @change="onConstellationChange">
  39. <view class="form-input picker-input">
  40. <text v-if="formData.constellation">{{ constellationOptions[constellationIndex] }}</text>
  41. <text v-else class="placeholder">请选择星座</text>
  42. <text class="picker-arrow">▼</text>
  43. </view>
  44. </picker>
  45. </view>
  46. </view>
  47. <!-- 身体状况 -->
  48. <view class="form-section" :class="{ 'active': activeSection === 'physical' }">
  49. <view class="section-title">身体状况</view>
  50. <view class="form-item">
  51. <text class="form-label">身高 (cm)</text>
  52. <input type="number" class="form-input" placeholder="请输入身高" v-model="formData.height" />
  53. </view>
  54. <view class="form-item">
  55. <text class="form-label">体重 (kg)</text>
  56. <input type="number" class="form-input" placeholder="请输入体重" v-model="formData.weight" />
  57. </view>
  58. </view>
  59. <!-- 社会属性 -->
  60. <view class="form-section" :class="{ 'active': activeSection === 'social' }">
  61. <view class="section-title">社会属性</view>
  62. <view class="form-item">
  63. <text class="form-label">婚况</text>
  64. <picker mode="selector" :range="maritalStatusOptions" :value="maritalStatusIndex >= 0 ? maritalStatusIndex : 0" @change="onMaritalStatusChange">
  65. <view class="form-input picker-input">
  66. <text v-if="maritalStatusIndex >= 0 && maritalStatusIndex < maritalStatusOptions.length">{{ maritalStatusOptions[maritalStatusIndex] }}</text>
  67. <text v-else class="placeholder">请选择婚况</text>
  68. <text class="picker-arrow">▼</text>
  69. </view>
  70. </picker>
  71. </view>
  72. <view class="form-item">
  73. <text class="form-label">学历</text>
  74. <picker mode="selector" :range="diplomaOptions" :value="diplomaIndex" @change="onDiplomaChange">
  75. <view class="form-input picker-input">
  76. <text v-if="formData.diploma">{{ diplomaOptions[diplomaIndex] }}</text>
  77. <text v-else class="placeholder">请选择学历</text>
  78. <text class="picker-arrow">▼</text>
  79. </view>
  80. </picker>
  81. </view>
  82. <view class="form-item">
  83. <text class="form-label">年收入 (万元)</text>
  84. <input type="text" class="form-input" placeholder="请输入年收入" v-model="formData.income" />
  85. </view>
  86. <view class="form-item">
  87. <text class="form-label">住址</text>
  88. <input type="text" class="form-input" placeholder="请输入住址" v-model="formData.address" />
  89. </view>
  90. <view class="form-item">
  91. <text class="form-label">户籍地</text>
  92. <input type="text" class="form-input" placeholder="请输入户籍地" v-model="formData.domicile" />
  93. </view>
  94. <view class="form-item">
  95. <text class="form-label">职业</text>
  96. <input type="text" class="form-input" placeholder="请输入职业" v-model="formData.occupation" />
  97. </view>
  98. <view class="form-item">
  99. <text class="form-label">购房</text>
  100. <picker mode="selector" :range="houseOptions" :value="houseIndex" @change="onHouseChange">
  101. <view class="form-input picker-input">
  102. <text v-if="formData.house !== null && formData.house !== undefined">{{ houseOptions[houseIndex] }}</text>
  103. <text v-else class="placeholder">请选择</text>
  104. <text class="picker-arrow">▼</text>
  105. </view>
  106. </picker>
  107. </view>
  108. <view class="form-item">
  109. <text class="form-label">购车</text>
  110. <picker mode="selector" :range="carOptions" :value="carIndex" @change="onCarChange">
  111. <view class="form-input picker-input">
  112. <text v-if="formData.car !== null && formData.car !== undefined">{{ carOptions[carIndex] }}</text>
  113. <text v-else class="placeholder">请选择</text>
  114. <text class="picker-arrow">▼</text>
  115. </view>
  116. </picker>
  117. </view>
  118. </view>
  119. <!-- 联系方式 -->
  120. <view class="form-section">
  121. <view class="section-title">联系方式</view>
  122. <view class="form-item">
  123. <text class="form-label">手机号<text class="required">*</text></text>
  124. <input type="text" class="form-input" placeholder="请输入手机号" v-model="formData.phone" />
  125. </view>
  126. <view class="form-item">
  127. <text class="form-label">备用手机号</text>
  128. <input type="text" class="form-input" placeholder="请输入备用手机号" v-model="formData.backupPhone" />
  129. </view>
  130. </view>
  131. </view>
  132. </scroll-view>
  133. <!-- 底部提交按钮 -->
  134. <view class="submit-bar">
  135. <button class="submit-btn" @click="handleSubmit">提交</button>
  136. </view>
  137. </view>
  138. </template>
  139. <script>
  140. import api from '@/utils/api.js'
  141. export default {
  142. data() {
  143. return {
  144. activeSection: 'basic',
  145. currentUserId: null, // 当前登录用户的用户ID
  146. formData: {
  147. name: '',
  148. age: null,
  149. gender: null,
  150. constellation: '',
  151. height: null,
  152. weight: null,
  153. marrStatus: null,
  154. diploma: '',
  155. income: '',
  156. address: '',
  157. domicile: '',
  158. occupation: '',
  159. house: null,
  160. car: null,
  161. phone: '',
  162. backupPhone: ''
  163. },
  164. genderOptions: ['男', '女'],
  165. genderIndex: 0,
  166. constellationOptions: ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'],
  167. constellationIndex: 0,
  168. maritalStatusOptions: ['未婚', '离异', '丧偶'],
  169. maritalStatusIndex: -1, // -1表示未选择,0-未婚,1-离异,2-丧偶
  170. diplomaOptions: ['高中', '专科', '本科', '硕士', '博士', '无'],
  171. diplomaIndex: 0,
  172. houseOptions: ['无', '有'],
  173. houseIndex: 0,
  174. carOptions: ['无', '有'],
  175. carIndex: 0
  176. }
  177. },
  178. onLoad() {
  179. // 获取当前登录用户信息
  180. const userInfo = uni.getStorageSync('userInfo') || {}
  181. const userId = uni.getStorageSync('userId')
  182. // 从userInfo中获取用户ID,将作为红娘ID
  183. this.currentUserId = userInfo.userId || userId || null
  184. console.log('当前登录用户ID:', this.currentUserId)
  185. },
  186. methods: {
  187. goBack() {
  188. uni.navigateBack()
  189. },
  190. onGenderChange(e) {
  191. this.genderIndex = e.detail.value
  192. this.formData.gender = this.genderIndex + 1 // 1-男,2-女
  193. },
  194. onConstellationChange(e) {
  195. this.constellationIndex = e.detail.value
  196. this.formData.constellation = this.constellationOptions[this.constellationIndex]
  197. },
  198. onMaritalStatusChange(e) {
  199. this.maritalStatusIndex = parseInt(e.detail.value)
  200. // 0-未婚,1-离异,2-丧偶
  201. this.formData.marrStatus = this.maritalStatusIndex
  202. console.log('婚姻状态选择:', this.maritalStatusIndex, '对应值:', this.formData.marrStatus, '选项:', this.maritalStatusOptions[this.maritalStatusIndex])
  203. },
  204. onDiplomaChange(e) {
  205. this.diplomaIndex = e.detail.value
  206. this.formData.diploma = this.diplomaOptions[this.diplomaIndex]
  207. },
  208. onHouseChange(e) {
  209. this.houseIndex = e.detail.value
  210. this.formData.house = this.houseIndex // 0-无,1-有
  211. },
  212. onCarChange(e) {
  213. this.carIndex = e.detail.value
  214. this.formData.car = this.carIndex // 0-无,1-有
  215. },
  216. async handleSubmit() {
  217. // 验证必填项
  218. if (!this.formData.name || !this.formData.name.trim()) {
  219. uni.showToast({
  220. title: '请输入姓名',
  221. icon: 'none'
  222. })
  223. return
  224. }
  225. if (!this.formData.phone || !this.formData.phone.trim()) {
  226. uni.showToast({
  227. title: '请输入手机号',
  228. icon: 'none'
  229. })
  230. return
  231. }
  232. // 验证手机号格式
  233. const phoneReg = /^1[3-9]\d{9}$/
  234. if (!phoneReg.test(this.formData.phone)) {
  235. uni.showToast({
  236. title: '请输入正确的手机号',
  237. icon: 'none'
  238. })
  239. return
  240. }
  241. uni.showLoading({
  242. title: '提交中...'
  243. })
  244. try {
  245. // 确保婚姻状态已选择
  246. if (this.maritalStatusIndex < 0 && (this.formData.marrStatus === null || this.formData.marrStatus === undefined)) {
  247. uni.showToast({
  248. title: '请选择婚况',
  249. icon: 'none'
  250. })
  251. return
  252. }
  253. // 转换数据类型
  254. const submitData = {
  255. ...this.formData,
  256. age: this.formData.age ? parseInt(this.formData.age) : null,
  257. height: this.formData.height ? parseInt(this.formData.height) : null,
  258. weight: this.formData.weight ? parseInt(this.formData.weight) : null,
  259. // 确保婚姻状态被正确设置(0-未婚,1-离异,2-丧偶)
  260. marrStatus: this.formData.marrStatus !== null && this.formData.marrStatus !== undefined
  261. ? parseInt(this.formData.marrStatus)
  262. : (this.maritalStatusIndex >= 0 ? parseInt(this.maritalStatusIndex) : null),
  263. // 确保备用手机号被正确传递(即使是空字符串也要传递)
  264. backupPhone: this.formData.backupPhone !== null && this.formData.backupPhone !== undefined
  265. ? this.formData.backupPhone.trim()
  266. : null
  267. }
  268. // 如果婚姻状态仍然为null,但maritalStatusIndex有值,使用maritalStatusIndex
  269. if (submitData.marrStatus === null && this.maritalStatusIndex >= 0) {
  270. submitData.marrStatus = parseInt(this.maritalStatusIndex)
  271. }
  272. console.log('提交数据 - 婚姻状态:', submitData.marrStatus, 'maritalStatusIndex:', this.maritalStatusIndex, '选项:', this.maritalStatusOptions[submitData.marrStatus])
  273. console.log('提交数据 - 备用手机号:', submitData.backupPhone, '类型:', typeof submitData.backupPhone)
  274. console.log('完整提交数据:', JSON.stringify(submitData, null, 2))
  275. // 调用后端接口(通过网关)
  276. const baseUrl = process.env.NODE_ENV === 'development'
  277. ? 'http://localhost:8083/api' // 开发环境 - 通过网关
  278. : 'https://your-domain.com/api' // 生产环境
  279. // 构建请求URL,包含当前登录用户的ID作为参数(将作为红娘ID)
  280. let url = `${baseUrl}/my-resource/add`
  281. if (this.currentUserId) {
  282. url += `?currentUserId=${this.currentUserId}`
  283. }
  284. const res = await uni.request({
  285. url: url,
  286. method: 'POST',
  287. data: submitData,
  288. header: {
  289. 'Content-Type': 'application/json'
  290. }
  291. })
  292. uni.hideLoading()
  293. if (res[1].statusCode === 200 && res[1].data.code === 200) {
  294. uni.showToast({
  295. title: '提交成功',
  296. icon: 'success'
  297. })
  298. // 延迟返回上一页
  299. setTimeout(() => {
  300. uni.navigateBack()
  301. }, 1500)
  302. } else {
  303. uni.showToast({
  304. title: res[1].data.message || '提交失败',
  305. icon: 'none',
  306. duration: 2000
  307. })
  308. }
  309. } catch (error) {
  310. uni.hideLoading()
  311. console.error('提交失败:', error)
  312. uni.showToast({
  313. title: '提交失败,请稍后重试',
  314. icon: 'none'
  315. })
  316. }
  317. }
  318. }
  319. }
  320. </script>
  321. <style lang="scss" scoped>
  322. .resource-input-page {
  323. min-height: 100vh;
  324. background: #FFF9F9;
  325. display: flex;
  326. flex-direction: column;
  327. }
  328. .header {
  329. display: flex;
  330. align-items: center;
  331. justify-content: space-between;
  332. padding: 25rpx 30rpx;
  333. padding-top: calc(25rpx + env(safe-area-inset-top));
  334. background: #FFF9F9;
  335. border-bottom: 1rpx solid #F0F0F0;
  336. .header-left {
  337. width: 60rpx;
  338. }
  339. .back-icon {
  340. font-size: 40rpx;
  341. color: #333;
  342. }
  343. .header-title {
  344. flex: 1;
  345. text-align: center;
  346. font-size: 38rpx;
  347. font-weight: bold;
  348. color: #9C27B0;
  349. }
  350. .header-right {
  351. width: 60rpx;
  352. text-align: right;
  353. }
  354. .more-icon {
  355. font-size: 40rpx;
  356. color: #333;
  357. }
  358. }
  359. .content {
  360. flex: 1;
  361. padding-bottom: 120rpx;
  362. }
  363. .form-container {
  364. padding: 20rpx;
  365. }
  366. .form-section {
  367. background: #FFFFFF;
  368. border-radius: 20rpx;
  369. padding: 30rpx;
  370. margin-bottom: 20rpx;
  371. border: 2rpx solid transparent;
  372. &.active {
  373. border-color: #FF4081;
  374. }
  375. .section-title {
  376. font-size: 32rpx;
  377. font-weight: bold;
  378. color: #333;
  379. margin-bottom: 30rpx;
  380. padding-bottom: 15rpx;
  381. border-bottom: 2rpx solid #F0F0F0;
  382. }
  383. }
  384. .form-item {
  385. margin-bottom: 30rpx;
  386. &:last-child {
  387. margin-bottom: 0;
  388. }
  389. .form-label {
  390. display: block;
  391. font-size: 28rpx;
  392. color: #666;
  393. margin-bottom: 15rpx;
  394. .required {
  395. color: #FF4444;
  396. margin-left: 5rpx;
  397. }
  398. }
  399. .form-input {
  400. width: 100%;
  401. height: 80rpx;
  402. background: #F8F8F8;
  403. border: 2rpx solid #E0E0E0;
  404. border-radius: 10rpx;
  405. padding: 0 20rpx;
  406. font-size: 28rpx;
  407. color: #333;
  408. box-sizing: border-box;
  409. &:focus {
  410. border-color: #9C27B0;
  411. background: #FFFFFF;
  412. }
  413. }
  414. .picker-input {
  415. display: flex;
  416. align-items: center;
  417. justify-content: space-between;
  418. .placeholder {
  419. color: #999;
  420. }
  421. .picker-arrow {
  422. font-size: 24rpx;
  423. color: #999;
  424. }
  425. }
  426. }
  427. .submit-bar {
  428. position: fixed;
  429. bottom: 0;
  430. left: 0;
  431. right: 0;
  432. padding: 20rpx 30rpx;
  433. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  434. background: #FFFFFF;
  435. border-top: 1rpx solid #F0F0F0;
  436. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  437. .submit-btn {
  438. width: 100%;
  439. height: 88rpx;
  440. background: linear-gradient(135deg, #9C27B0 0%, #BA68C8 100%);
  441. color: #FFFFFF;
  442. font-size: 32rpx;
  443. font-weight: bold;
  444. border-radius: 44rpx;
  445. border: none;
  446. display: flex;
  447. align-items: center;
  448. justify-content: center;
  449. }
  450. }
  451. </style>