租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

605 lines
18 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
2 months ago
2 months ago
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <view class="title">其他农村资源录入</view>
  5. </view>
  6. <view class="form-container">
  7. <uv-form labelPosition="left" :model="form" :rules="rules" ref="form" labelWidth="80" labelStyle="font-size:28rpx;">
  8. <!-- 必填项 -->
  9. <view class="section-title">基本信息必填</view>
  10. <!-- 1. 出租/流转 -->
  11. <uv-form-item label="类型" prop="type" required>
  12. <uv-radio-group v-model="form.type" placement="row" @change="debounceSaveFormData">
  13. <uv-radio name="rent" label="出租"></uv-radio>
  14. <uv-radio name="transfer" customStyle="margin-left:30rpx;" label="流转"></uv-radio>
  15. </uv-radio-group>
  16. </uv-form-item>
  17. <!-- 2. 地址 -->
  18. <uv-form-item label="地址" prop="address" required @click="handleAddressSelect()">
  19. <uv-input v-model="form.address" @click="handleAddressSelect()" disabled disabledColor="#ffffff" placeholder="请选择资源位置" border="none">
  20. </uv-input>
  21. <template v-slot:right>
  22. <uv-icon name="arrow-right"></uv-icon>
  23. </template>
  24. </uv-form-item>
  25. <!-- 3. 联系人和电话 -->
  26. <uv-form-item label="联系人" prop="contactName" required>
  27. <uv-input v-model="form.contactName" type="text" placeholder="请输入联系人姓名" customStyle="border-radius: 5px;margin-top:5px;background-color: #f5f5f5;padding:5px 10px;" @input="debounceSaveFormData"></uv-input>
  28. </uv-form-item>
  29. <uv-form-item label="联系电话" prop="contactPhone" required>
  30. <uv-input v-model="form.contactPhone" type="number" placeholder="请输入联系电话" customStyle="border-radius: 5px;margin-top:5px;background-color: #f5f5f5;padding:5px 10px;" @input="debounceSaveFormData"></uv-input>
  31. </uv-form-item>
  32. <!-- 4. 照片和视频 -->
  33. <uv-form-item label="资源照片" labelWidth="250" prop="images" labelPosition="top" required>
  34. <view class="upload-tip">请上传资源相关照片</view>
  35. <uv-upload customStyle="margin-top:20rpx;"
  36. :fileList="form.images" @afterRead="afterImageRead"
  37. @delete="deleteImagePic" name="1"
  38. multiple :maxCount="10"></uv-upload>
  39. </uv-form-item>
  40. <uv-form-item label="资源视频" labelWidth="250" prop="videos" labelPosition="top" required>
  41. <view class="upload-tip">请上传资源视频时长不超过1分钟最多上传3个视频</view>
  42. <uv-upload customStyle="margin-top:20rpx;" accept="video" :fileList="form.videos" @afterRead="afterVideoRead" @delete="deleteVideoPic" name="1"
  43. multiple :maxCount="3"></uv-upload>
  44. </uv-form-item>
  45. <!-- 5. 价格 -->
  46. <uv-form-item label="价格" prop="price" required>
  47. <view class="price-container">
  48. <uv-input v-model="form.price" type="digit" placeholder="请输入价格" customStyle="border-radius: 5px;margin-top:5px;background-color: #f5f5f5;padding:5px 10px;flex:1;" @input="debounceSaveFormData"></uv-input>
  49. <text class="price-unit"></text>
  50. </view>
  51. <view class="input-tip">请输入数字支持小数点后两位</view>
  52. </uv-form-item>
  53. <!-- 6. 资源介绍 -->
  54. <uv-form-item label="资源介绍" prop="description" labelPosition="top" required>
  55. <uv-input
  56. v-model="form.description"
  57. type="textarea"
  58. placeholder="请详细介绍资源类型、用途、特色、优势等信息"
  59. customStyle="border-radius: 5px;margin-top:5px;background-color: #f5f5f5;padding:10px;"
  60. :autoHeight="true"
  61. :maxlength="1000"
  62. @input="debounceSaveFormData"
  63. ></uv-input>
  64. <view class="char-count">{{form.description.length}}/1000</view>
  65. <view class="input-tip">请详细描述资源的类型用途特色规模优势等</view>
  66. </uv-form-item>
  67. <!-- 分类标识 -->
  68. <uv-form-item label="分类标识" prop="classId" @click="handleClass()">
  69. <uv-input v-model="form.className" @click="handleClass()" disabled disabledColor="#ffffff" placeholder="选择分类标识" border="none">
  70. </uv-input>
  71. <template v-slot:right>
  72. <uv-icon name="arrow-right"></uv-icon>
  73. </template>
  74. </uv-form-item>
  75. <!-- 代理协议 -->
  76. <view class="section-title">代理协议</view>
  77. <view class="agreement-container">
  78. <view class="agreement-content">
  79. <text class="agreement-title">代理协议</text>
  80. <text class="agreement-text">
  81. 本人同意授权我平台发布信息代理销售等本人承诺所提供的信息真实有效并同意平台进行相关推广和销售代理
  82. </text>
  83. </view>
  84. <uv-checkbox v-model="form.agreeProtocol"
  85. name="1"
  86. shape="circle" activeColor="#1EC77A"
  87. @change="onAgreementChange">
  88. <text class="agreement-label">我已阅读并同意代理协议</text>
  89. </uv-checkbox>
  90. </view>
  91. <uv-form-item>
  92. <uv-button type="primary" text="提交信息" customStyle="margin-top: 30px;background-color: #1EC77A;border-radius: 30px;" @click="submit"></uv-button>
  93. </uv-form-item>
  94. </uv-form>
  95. </view>
  96. <!-- 分类选择器 -->
  97. <uv-picker ref="picker" :columns="columns" @confirm="confirm"></uv-picker>
  98. </view>
  99. </template>
  100. <script>
  101. import { saveOrUpdateHouse, houseType } from "@/common/api.js"
  102. import formStorage from "@/utils/formStorage.js"
  103. export default {
  104. data() {
  105. return {
  106. formKey: 'other_form', // 表单存储唯一标识
  107. saveTimer: null, // 防抖定时器
  108. commonClass: '', // 所属分类ID
  109. form: {
  110. type: '', // 出租/流转
  111. address: '', // 地址
  112. contactName: '', // 联系人
  113. contactPhone: '', // 联系电话
  114. images: [], // 资源照片
  115. videos: [], // 资源视频
  116. price: '', // 价格(开放式)
  117. description: '', // 资源介绍
  118. agreeProtocol: false, // 同意代理协议
  119. longitude: '', // 经度
  120. latitude: '', // 纬度
  121. classId: '', // 分类标识
  122. className: '' // 分类名称
  123. },
  124. columns: [], // 分类选择器数据
  125. houseTypeList: [], // 分类列表
  126. rules: {
  127. type: [
  128. { required: true, message: '请选择出租或流转', trigger: ['blur', 'change'] }
  129. ],
  130. address: [
  131. { required: true, message: '请选择资源地址', trigger: ['blur', 'change'] }
  132. ],
  133. contactName: [
  134. { required: true, message: '请输入联系人姓名', trigger: ['blur', 'change'] }
  135. ],
  136. contactPhone: [
  137. { required: true, message: '请输入联系电话', trigger: ['blur', 'change'] },
  138. { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: ['blur', 'change'] }
  139. ],
  140. price: [
  141. { required: true, message: '请输入价格', trigger: ['blur', 'change'] },
  142. { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的价格格式', trigger: ['blur', 'change'] }
  143. ],
  144. description: [
  145. { required: true, message: '请输入资源介绍', trigger: ['blur', 'change'] },
  146. { min: 10, message: '资源介绍至少需要10个字符', trigger: ['blur', 'change'] }
  147. ],
  148. classId: [
  149. { required: true, message: '请选择分类标识', trigger: ['blur', 'change'] }
  150. ]
  151. }
  152. }
  153. },
  154. onReady() {
  155. this.$refs.form.setRules(this.rules)
  156. },
  157. onLoad(options) {
  158. // 设置页面标题
  159. uni.setNavigationBarTitle({
  160. title: '其他农村资源录入'
  161. })
  162. // 接收传递的commonClass参数
  163. if(options.commonClass) {
  164. this.commonClass = options.commonClass
  165. }
  166. // 如果是编辑模式,设置classId用于回显
  167. if(options.classId) {
  168. this.form.classId = options.classId
  169. }
  170. // 初始化分类数据
  171. this.onHouseType()
  172. // 恢复表单数据
  173. this.restoreFormData()
  174. },
  175. onUnload() {
  176. // 页面卸载时保存数据
  177. this.saveFormData()
  178. // 清除定时器
  179. if (this.saveTimer) {
  180. clearTimeout(this.saveTimer)
  181. }
  182. },
  183. methods: {
  184. // 恢复表单数据
  185. restoreFormData() {
  186. const savedData = formStorage.getFormData(this.formKey)
  187. if (savedData) {
  188. // 合并保存的数据到表单
  189. this.form = { ...this.form, ...savedData }
  190. }
  191. },
  192. // 保存表单数据
  193. saveFormData() {
  194. formStorage.saveFormData(this.formKey, this.form)
  195. },
  196. // 防抖保存表单数据
  197. debounceSaveFormData() {
  198. clearTimeout(this.saveTimer)
  199. this.saveTimer = setTimeout(() => {
  200. this.saveFormData()
  201. }, 1000)
  202. },
  203. // 获取分类数据
  204. onHouseType(){
  205. let that = this
  206. houseType({}).then(response=>{
  207. let arr=[]
  208. that.houseTypeList = response.result
  209. response.result.forEach(items=>{
  210. arr.push(items.title)
  211. // 数据回显:如果当前classId匹配,设置className
  212. if(items.id == that.form.classId){
  213. that.form.className = items.title
  214. }
  215. })
  216. that.columns[0]=arr
  217. }).catch(error=>{
  218. })
  219. },
  220. // 地址选择
  221. handleAddressSelect() {
  222. const that = this;
  223. wx.chooseLocation({
  224. success: function (res) {
  225. console.log('选择的位置:', res);
  226. that.form.longitude = res.longitude
  227. that.form.latitude = res.latitude
  228. that.form.address = res.address
  229. }
  230. })
  231. },
  232. // 资源照片上传
  233. async afterImageRead(e) {
  234. let self = this
  235. e.file.forEach(file => {
  236. self.$Oss.ossUpload(file.url).then(url => {
  237. self.form.images.push({
  238. url
  239. })
  240. // 上传完成后保存表单数据
  241. self.debounceSaveFormData()
  242. })
  243. })
  244. },
  245. deleteImagePic(event) {
  246. this.form.images.splice(event.index, 1)
  247. // 删除后保存表单数据
  248. this.debounceSaveFormData()
  249. },
  250. // 视频上传
  251. async afterVideoRead(e) {
  252. let self = this
  253. for (let file of e.file) {
  254. try {
  255. // 检查视频时长
  256. const duration = await this.getVideoDuration(file.url)
  257. if (duration > 60) {
  258. uni.showToast({
  259. title: '视频时长不能超过1分钟',
  260. icon: 'none'
  261. })
  262. continue // 跳过这个视频
  263. }
  264. self.$Oss.ossUpload(file.url).then(url => {
  265. self.form.videos.push({
  266. url
  267. })
  268. // 上传完成后保存表单数据
  269. self.debounceSaveFormData()
  270. })
  271. } catch (error) {
  272. console.error('视频处理失败:', error)
  273. // 如果获取时长失败,仍然允许上传
  274. self.$Oss.ossUpload(file.url).then(url => {
  275. self.form.videos.push({
  276. url
  277. })
  278. // 上传完成后保存表单数据
  279. self.debounceSaveFormData()
  280. })
  281. }
  282. }
  283. },
  284. // 获取视频时长的辅助方法
  285. getVideoDuration(videoUrl) {
  286. return new Promise((resolve, reject) => {
  287. // #ifdef MP-WEIXIN
  288. uni.getVideoInfo({
  289. src: videoUrl,
  290. success: (res) => {
  291. resolve(res.duration)
  292. },
  293. fail: (err) => {
  294. reject(err)
  295. }
  296. })
  297. // #endif
  298. // #ifdef APP-PLUS
  299. plus.io.getVideoInfo({
  300. filePath: videoUrl,
  301. success: (res) => {
  302. resolve(res.duration)
  303. },
  304. fail: (err) => {
  305. reject(err)
  306. }
  307. })
  308. // #endif
  309. // #ifdef H5
  310. // H5环境暂不支持视频时长检测,直接通过
  311. resolve(0)
  312. // #endif
  313. })
  314. },
  315. deleteVideoPic(event) {
  316. this.form.videos.splice(event.index, 1)
  317. // 删除后保存表单数据
  318. this.debounceSaveFormData()
  319. },
  320. // 分类选择
  321. handleClass() {
  322. this.$refs.picker.open();
  323. },
  324. // 代理协议状态变化
  325. onAgreementChange(e) {
  326. console.log('代理协议状态变化:', e);
  327. this.form.agreeProtocol = e;
  328. // 状态变化后保存表单数据
  329. this.debounceSaveFormData()
  330. },
  331. confirm(e) {
  332. let that = this
  333. let {indexs,value,values} = e
  334. that.form.classId = that.houseTypeList[indexs[0]].id;
  335. that.form.className = that.houseTypeList[indexs[0]].title;
  336. // 选择后保存表单数据
  337. that.debounceSaveFormData()
  338. },
  339. // 提交表单
  340. submit() {
  341. console.log(this.form.agreeProtocol);
  342. // 检查是否同意代理协议
  343. if (!this.form.agreeProtocol) {
  344. uni.showToast({
  345. title: '请先同意代理协议',
  346. icon: 'none'
  347. });
  348. return;
  349. }
  350. this.$refs.form.validate().then(res => {
  351. // 验证必填的图片和视频
  352. if (this.form.images.length === 0) {
  353. uni.showToast({
  354. title: '请上传资源照片',
  355. icon: 'none'
  356. });
  357. return;
  358. }
  359. if (this.form.videos.length === 0) {
  360. uni.showToast({
  361. title: '请上传资源视频',
  362. icon: 'none'
  363. });
  364. return;
  365. }
  366. // 构建与index.vue兼容的提交参数
  367. const params = {
  368. userId: uni.getStorageSync('userInfo')?.id || "",
  369. id: "", // 新增数据,无ID
  370. classId: this.form.classId, // 使用分类标识
  371. commonClass: this.commonClass, // 所属分类
  372. address: this.form.address, // 地址
  373. homeAge: "", // 户主年龄 - 其他资源无此字段
  374. homeAz: "", // 是否经过安置 - 默认否
  375. homeBian: "", // 房屋周边 - 其他资源无此字段
  376. homeBjsx: "", // 报建手续 - 其他资源无此字段
  377. homeCai: "", // 菜地 - 其他资源无此字段
  378. homeCat: "", // 停车 - 其他资源无此字段
  379. homeGz: "", // 房屋主体是否改造 - 默认否
  380. homeHb: "", // 房屋朝向及海拔 - 其他资源无此字段
  381. homeBz: this.form.description, // 备注 - 使用资源介绍
  382. homeJg: "其他农村资源", // 房屋结构 - 使用固定值
  383. homeJl: "", // 距离场镇距离 - 其他资源无此字段
  384. homeJt: "", // 交通 - 其他资源无此字段
  385. homeJtzy: "", // 户主家庭职业 - 其他资源无此字段
  386. homeMi: "", // 面积 - 其他资源无此字段
  387. homeMj: "", // 房屋面积 - 其他资源无此字段
  388. homeMoney: "", // 佣金 - 其他资源无此字段
  389. homeNo: "", // 房屋编号 - 其他资源无此字段
  390. homeNum: "", // 房间数量 - 其他资源无此字段
  391. homePay: this.form.price, // 付款方式及押金 - 使用价格信息
  392. homePj: "", // 邻居对房东评价 - 其他资源无此字段
  393. homeSd: "", // 水电气网 - 其他资源无此字段
  394. homeShjl: "", // 距离成都西三环 - 其他资源无此字段
  395. homeSw: "", // 非正常死亡 - 默认无
  396. homeTf: "", // 府市民云房屋信息档案查询 - 其他资源无此字段
  397. homeTime: "", // 租期 - 其他资源无此字段
  398. homeType: this.form.type === 'rent' ? '出租' : '流转', // 户型 - 使用类型
  399. homeYs: "", // 钥匙 - 默认无
  400. homeYzmj: "", // 院子总面积 - 其他资源无此字段
  401. homeZy: "", // 坟包及电塔 工厂噪音 - 其他资源无此字段
  402. iconName: "其他农村资源", // 热点名称
  403. iconTitle: "其他资源", // 标签
  404. num: "0", // 浏览量 - 默认0
  405. price: this.form.price, // 价格
  406. timeGo: "", // 年限 - 其他资源无此字段
  407. title: `${this.form.type === 'rent' ? '出租' : '流转'}-其他农村资源-${this.form.address.split('市')[1] || this.form.address}`, // 标题 - 自动生成
  408. unit: "元", // 单位 - 其他资源价格单位灵活
  409. image: this.form.images.map(item => item.url).join(','), // 图片 - 使用资源照片
  410. iconImage: "", // 左上角图标
  411. homeImage: "", // 产权证照片 - 其他资源可能无产权证
  412. homeMp4: this.form.videos.map(item => item.url).join(','), // 视频
  413. latitude: this.form.latitude,
  414. longitude: this.form.longitude,
  415. // 新增字段用于标识其他资源
  416. category: 'other',
  417. contactName: this.form.contactName,
  418. contactPhone: this.form.contactPhone
  419. };
  420. console.log('其他资源提交参数:', params);
  421. uni.showLoading({
  422. title: '提交中...'
  423. });
  424. // 调用统一API
  425. saveOrUpdateHouse(params).then(response => {
  426. uni.hideLoading();
  427. // 提交成功后清除本地存储的表单数据
  428. formStorage.removeFormData(this.formKey);
  429. uni.showToast({
  430. title: response.message || '提交成功',
  431. icon: 'success'
  432. });
  433. setTimeout(() => {
  434. uni.redirectTo({
  435. url: "/pages_subpack/successful-apply/index"
  436. });
  437. }, 2000);
  438. }).catch(error => {
  439. uni.hideLoading();
  440. uni.showToast({
  441. title: error.message || '提交失败',
  442. icon: 'none'
  443. });
  444. console.error('其他资源提交失败:', error);
  445. });
  446. }).catch(errors => {
  447. console.log('表单验证失败:', errors);
  448. uni.showToast({
  449. title: '请补全必填项',
  450. icon: 'none'
  451. });
  452. });
  453. }
  454. }
  455. }
  456. </script>
  457. <style scoped>
  458. .container {
  459. min-height: 100vh;
  460. background-color: #f5f5f5;
  461. }
  462. .header {
  463. background: linear-gradient(135deg, #1EC77A 0%, #4CAF50 100%);
  464. padding: 40rpx 40rpx 60rpx;
  465. color: white;
  466. }
  467. .title {
  468. font-size: 36rpx;
  469. font-weight: bold;
  470. text-align: center;
  471. }
  472. .form-container {
  473. background: white;
  474. margin: -30rpx 20rpx 20rpx;
  475. border-radius: 20rpx;
  476. padding: 40rpx;
  477. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.1);
  478. }
  479. .section-title {
  480. font-size: 32rpx;
  481. font-weight: bold;
  482. color: #333;
  483. margin: 40rpx 0 30rpx;
  484. padding-left: 20rpx;
  485. border-left: 6rpx solid #1EC77A;
  486. }
  487. .section-title:first-child {
  488. margin-top: 0;
  489. }
  490. .input-tip {
  491. font-size: 22rpx;
  492. color: #999;
  493. margin-top: 10rpx;
  494. line-height: 1.4;
  495. }
  496. .upload-tip {
  497. font-size: 24rpx;
  498. color: #666;
  499. margin-bottom: 10rpx;
  500. line-height: 1.5;
  501. }
  502. .price-container {
  503. display: flex;
  504. align-items: center;
  505. gap: 10rpx;
  506. }
  507. .price-unit {
  508. font-size: 26rpx;
  509. color: #1EC77A;
  510. font-weight: bold;
  511. margin-left: 10rpx;
  512. }
  513. .char-count {
  514. font-size: 22rpx;
  515. color: #999;
  516. text-align: right;
  517. margin-top: 10rpx;
  518. }
  519. .agreement-container {
  520. margin: 30rpx 0;
  521. padding: 30rpx;
  522. background: #f8f9fa;
  523. border-radius: 15rpx;
  524. border: 2rpx solid #e9ecef;
  525. }
  526. .agreement-content {
  527. margin-bottom: 30rpx;
  528. }
  529. .agreement-title {
  530. display: block;
  531. font-size: 28rpx;
  532. font-weight: bold;
  533. color: #333;
  534. margin-bottom: 20rpx;
  535. }
  536. .agreement-text {
  537. display: block;
  538. font-size: 24rpx;
  539. color: #666;
  540. line-height: 1.6;
  541. }
  542. .agreement-label {
  543. font-size: 26rpx;
  544. color: #333;
  545. margin-left: 10rpx;
  546. }
  547. </style>