purchase-publish.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <template>
  2. <view class="purchase-publish-container">
  3. <!-- 表单内容 -->
  4. <view class="form-container">
  5. <!-- 收购标题 -->
  6. <view class="form-item">
  7. <view class="item-label">
  8. <text class="label-text">收购标题</text>
  9. <text class="required">*</text>
  10. </view>
  11. <view class="item-content">
  12. <input
  13. class="form-input"
  14. v-model="formData.title"
  15. placeholder="请输入收购标题"
  16. placeholder-style="color: #999;"
  17. maxlength="30"
  18. @input="onTitleInput"
  19. />
  20. <view class="char-count">{{ titleLength }}/30</view>
  21. </view>
  22. </view>
  23. <!-- 收购品类 -->
  24. <view class="form-item">
  25. <view class="item-label">
  26. <text class="label-text">收购品类</text>
  27. <text class="required">*</text>
  28. </view>
  29. <view class="item-content">
  30. <view class="category-selector" @click="showCategoryPicker = true">
  31. <text class="selector-text" :class="{ placeholder: !formData.category }">
  32. {{ formData.category || '请选择收购品类' }}
  33. </text>
  34. <text class="arrow-icon">></text>
  35. </view>
  36. </view>
  37. </view>
  38. <!-- 收购数量 -->
  39. <view class="form-item">
  40. <view class="item-label">
  41. <text class="label-text">收购数量</text>
  42. <text class="required">*</text>
  43. </view>
  44. <view class="item-content">
  45. <view class="input-with-unit">
  46. <input
  47. class="form-input"
  48. v-model="formData.quantity"
  49. placeholder="请输入收购数量"
  50. placeholder-style="color: #999;"
  51. type="number"
  52. />
  53. <text class="unit-text">斤</text>
  54. </view>
  55. </view>
  56. </view>
  57. <!-- 单价预算 -->
  58. <view class="form-item">
  59. <view class="item-label">
  60. <text class="label-text">单价预算</text>
  61. <text class="required">*</text>
  62. </view>
  63. <view class="item-content">
  64. <view class="input-with-unit">
  65. <text class="currency-symbol">¥</text>
  66. <input
  67. class="form-input"
  68. v-model="formData.budgetPrice"
  69. placeholder="请输入单价预算"
  70. placeholder-style="color: #999;"
  71. type="digit"
  72. />
  73. <text class="unit-text">元/斤</text>
  74. </view>
  75. </view>
  76. </view>
  77. <!-- 补充说明 -->
  78. <view class="form-item">
  79. <view class="item-label">
  80. <text class="label-text">补充说明</text>
  81. </view>
  82. <view class="item-content">
  83. <textarea
  84. class="form-textarea"
  85. v-model="formData.description"
  86. placeholder="请描述交货要求、时间等补充信息"
  87. placeholder-style="color: #999;"
  88. maxlength="200"
  89. auto-height
  90. @input="onDescInput"
  91. />
  92. <view class="char-count">{{ descLength }}/200</view>
  93. </view>
  94. </view>
  95. <!-- 上传参考图片 -->
  96. <view class="form-item">
  97. <view class="item-label">
  98. <text class="label-text">参考图片</text>
  99. <text class="optional">(最多2张)</text>
  100. </view>
  101. <view class="item-content">
  102. <view class="image-upload-area">
  103. <view
  104. class="image-item"
  105. v-for="(image, index) in formData.images"
  106. :key="index"
  107. >
  108. <image class="uploaded-image" :src="image" mode="aspectFill"></image>
  109. <view class="image-delete" @click="removeImage(index)">×</view>
  110. </view>
  111. <view
  112. class="image-upload-btn"
  113. v-if="formData.images.length < 2"
  114. @click="chooseImage"
  115. >
  116. <text class="upload-icon">+</text>
  117. <text class="upload-text">上传图片</text>
  118. </view>
  119. </view>
  120. </view>
  121. </view>
  122. <!-- 联系人信息 -->
  123. <view class="form-item">
  124. <view class="item-label">
  125. <text class="label-text">联系人信息</text>
  126. </view>
  127. <view class="item-content">
  128. <view class="contact-input-row">
  129. <text class="contact-label">联系人:</text>
  130. <input
  131. class="contact-input"
  132. v-model="formData.contactName"
  133. placeholder="请输入联系人姓名"
  134. placeholder-style="color: #999;"
  135. maxlength="10"
  136. />
  137. </view>
  138. <view class="contact-input-row">
  139. <text class="contact-label">电话:</text>
  140. <input
  141. class="contact-input"
  142. v-model="formData.contactPhone"
  143. placeholder="请输入联系电话"
  144. placeholder-style="color: #999;"
  145. type="number"
  146. maxlength="11"
  147. />
  148. </view>
  149. </view>
  150. </view>
  151. </view>
  152. <!-- 底部提交按钮 -->
  153. <view class="bottom-action-bar">
  154. <view class="submit-btn" @click="submitForm">
  155. <text class="btn-text">{{ isEditMode ? '保存修改' : '提交发布' }}</text>
  156. </view>
  157. </view>
  158. <!-- 品类选择弹窗 -->
  159. <view class="picker-modal" v-if="showCategoryPicker" @click="showCategoryPicker = false">
  160. <view class="picker-content" @click.stop>
  161. <view class="picker-header">
  162. <text class="picker-title">选择收购品类</text>
  163. <text class="picker-close" @click="showCategoryPicker = false">×</text>
  164. </view>
  165. <view class="picker-options">
  166. <view
  167. class="picker-option"
  168. v-for="category in categoryOptions"
  169. :key="category"
  170. @click="selectCategory(category)"
  171. >
  172. <text class="option-text">{{ category }}</text>
  173. </view>
  174. </view>
  175. </view>
  176. </view>
  177. </view>
  178. </template>
  179. <script>
  180. export default {
  181. data() {
  182. return {
  183. isEditMode: false, // 是否为编辑模式
  184. editItemId: '', // 编辑的收购信息ID
  185. formData: {
  186. title: '',
  187. category: '',
  188. quantity: '',
  189. budgetPrice: '',
  190. description: '',
  191. images: [],
  192. contactName: '',
  193. contactPhone: ''
  194. },
  195. categoryOptions: ['蔬菜', '水果', '粮食', '其他'],
  196. showCategoryPicker: false,
  197. titleLength: 0,
  198. descLength: 0
  199. }
  200. },
  201. onLoad(options) {
  202. // 检查是否为编辑模式
  203. if (options.action === 'edit' && options.id) {
  204. this.isEditMode = true;
  205. this.editItemId = options.id;
  206. // 设置页面标题
  207. uni.setNavigationBarTitle({
  208. title: '编辑收购信息'
  209. });
  210. // 加载收购信息数据
  211. this.loadPurchaseData();
  212. } else {
  213. // 新建模式
  214. this.isEditMode = false;
  215. uni.setNavigationBarTitle({
  216. title: '发布收购信息'
  217. });
  218. }
  219. },
  220. methods: {
  221. // 加载收购信息数据(编辑模式)
  222. loadPurchaseData() {
  223. // 模拟从API加载数据,实际应用中需要根据ID调用相应的API
  224. uni.showLoading({ title: '加载中...' });
  225. setTimeout(() => {
  226. // 收购信息的模拟数据
  227. const mockData = {
  228. title: '高价收购优质土豆',
  229. category: '蔬菜',
  230. quantity: '1000',
  231. budgetPrice: '3.5',
  232. description: '大量收购优质土豆,要求新鲜无病害,无青皮,规格统一。支持长期合作,价格优惠,现金结算。有货源的农户欢迎联系,我们提供上门收购服务。',
  233. images: [
  234. '/static/images/products/agriculture-tools.jpg'
  235. ],
  236. contactName: '李先生',
  237. contactPhone: '13856785678'
  238. };
  239. // 填充表单数据
  240. this.formData = { ...mockData };
  241. // 更新字符计数
  242. this.titleLength = this.formData.title.length;
  243. this.descLength = this.formData.description.length;
  244. uni.hideLoading();
  245. }, 1000);
  246. },
  247. // 标题输入处理
  248. onTitleInput(e) {
  249. this.titleLength = e.detail.value.length;
  250. },
  251. // 描述输入处理
  252. onDescInput(e) {
  253. this.descLength = e.detail.value.length;
  254. },
  255. // 选择品类
  256. selectCategory(category) {
  257. this.formData.category = category;
  258. this.showCategoryPicker = false;
  259. },
  260. // 选择图片
  261. chooseImage() {
  262. const remainingCount = 2 - this.formData.images.length;
  263. uni.chooseImage({
  264. count: remainingCount,
  265. sizeType: ['compressed'],
  266. sourceType: ['album', 'camera'],
  267. success: (res) => {
  268. this.formData.images = this.formData.images.concat(res.tempFilePaths);
  269. },
  270. fail: (err) => {
  271. console.error('选择图片失败:', err);
  272. }
  273. });
  274. },
  275. // 删除图片
  276. removeImage(index) {
  277. this.formData.images.splice(index, 1);
  278. },
  279. // 表单验证
  280. validateForm() {
  281. if (!this.formData.title.trim()) {
  282. uni.showToast({
  283. title: '请输入收购标题',
  284. icon: 'none'
  285. });
  286. return false;
  287. }
  288. if (!this.formData.category) {
  289. uni.showToast({
  290. title: '请选择收购品类',
  291. icon: 'none'
  292. });
  293. return false;
  294. }
  295. if (!this.formData.quantity || this.formData.quantity <= 0) {
  296. uni.showToast({
  297. title: '请输入有效的收购数量',
  298. icon: 'none'
  299. });
  300. return false;
  301. }
  302. if (!this.formData.budgetPrice || this.formData.budgetPrice <= 0) {
  303. uni.showToast({
  304. title: '请输入有效的单价预算',
  305. icon: 'none'
  306. });
  307. return false;
  308. }
  309. if (!this.formData.contactName.trim()) {
  310. uni.showToast({
  311. title: '请输入联系人姓名',
  312. icon: 'none'
  313. });
  314. return false;
  315. }
  316. if (!this.formData.contactPhone.trim()) {
  317. uni.showToast({
  318. title: '请输入联系电话',
  319. icon: 'none'
  320. });
  321. return false;
  322. }
  323. // 简单的手机号格式验证
  324. const phoneRegex = /^1[3-9]\d{9}$/;
  325. if (!phoneRegex.test(this.formData.contactPhone)) {
  326. uni.showToast({
  327. title: '请输入正确的手机号码',
  328. icon: 'none'
  329. });
  330. return false;
  331. }
  332. return true;
  333. },
  334. // 提交表单
  335. submitForm() {
  336. if (!this.validateForm()) {
  337. return;
  338. }
  339. // 显示加载提示
  340. const loadingTitle = this.isEditMode ? '保存中...' : '提交中...';
  341. uni.showLoading({
  342. title: loadingTitle
  343. });
  344. // 模拟提交
  345. setTimeout(() => {
  346. uni.hideLoading();
  347. if (this.isEditMode) {
  348. // 编辑模式 - 保存修改
  349. uni.showModal({
  350. title: '保存成功',
  351. content: '您的修改已保存成功!',
  352. showCancel: false,
  353. success: () => {
  354. uni.navigateBack();
  355. }
  356. });
  357. } else {
  358. // 新建模式 - 提交发布
  359. uni.showToast({
  360. title: '发布成功',
  361. icon: 'success'
  362. });
  363. // 返回上一页
  364. setTimeout(() => {
  365. uni.navigateBack();
  366. }, 1500);
  367. }
  368. }, 2000);
  369. }
  370. }
  371. }
  372. </script>
  373. <style lang="scss">
  374. .purchase-publish-container {
  375. min-height: 100vh;
  376. background-color: #f5f5f5;
  377. padding-bottom: calc(128rpx + env(safe-area-inset-bottom));
  378. }
  379. .form-container {
  380. padding: 20rpx;
  381. }
  382. .form-item {
  383. background-color: #fff;
  384. border-radius: 12rpx;
  385. padding: 24rpx;
  386. margin-bottom: 16rpx;
  387. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  388. }
  389. .item-label {
  390. display: flex;
  391. align-items: center;
  392. margin-bottom: 16rpx;
  393. }
  394. .label-text {
  395. font-size: 28rpx;
  396. color: #333;
  397. font-weight: bold;
  398. }
  399. .required {
  400. color: #ff4d4f;
  401. font-size: 28rpx;
  402. margin-left: 4rpx;
  403. }
  404. .optional {
  405. color: #999;
  406. font-size: 24rpx;
  407. margin-left: 8rpx;
  408. }
  409. .item-content {
  410. position: relative;
  411. }
  412. .form-input {
  413. width: 100%;
  414. height: 44rpx;
  415. font-size: 28rpx;
  416. color: #333;
  417. line-height: 44rpx;
  418. }
  419. .form-textarea {
  420. width: 100%;
  421. min-height: 120rpx;
  422. font-size: 28rpx;
  423. color: #333;
  424. line-height: 1.5;
  425. }
  426. .char-count {
  427. position: absolute;
  428. right: 0;
  429. bottom: -4rpx;
  430. font-size: 24rpx;
  431. color: #999;
  432. }
  433. // 品类选择器
  434. .category-selector {
  435. display: flex;
  436. align-items: center;
  437. justify-content: space-between;
  438. padding: 12rpx 0;
  439. border-bottom: 1rpx solid #f0f0f0;
  440. }
  441. .selector-text {
  442. font-size: 28rpx;
  443. color: #333;
  444. &.placeholder {
  445. color: #999;
  446. }
  447. }
  448. .arrow-icon {
  449. font-size: 24rpx;
  450. color: #999;
  451. }
  452. // 带单位的输入框
  453. .input-with-unit {
  454. display: flex;
  455. align-items: center;
  456. border-bottom: 1rpx solid #f0f0f0;
  457. padding: 12rpx 0;
  458. }
  459. .currency-symbol {
  460. font-size: 28rpx;
  461. color: #333;
  462. margin-right: 8rpx;
  463. }
  464. .unit-text {
  465. font-size: 28rpx;
  466. color: #666;
  467. margin-left: 8rpx;
  468. white-space: nowrap;
  469. }
  470. // 图片上传
  471. .image-upload-area {
  472. display: flex;
  473. flex-wrap: wrap;
  474. gap: 16rpx;
  475. }
  476. .image-item {
  477. position: relative;
  478. width: 160rpx;
  479. height: 160rpx;
  480. border-radius: 8rpx;
  481. overflow: hidden;
  482. }
  483. .uploaded-image {
  484. width: 100%;
  485. height: 100%;
  486. }
  487. .image-delete {
  488. position: absolute;
  489. top: -8rpx;
  490. right: -8rpx;
  491. width: 32rpx;
  492. height: 32rpx;
  493. background-color: #ff4d4f;
  494. color: #fff;
  495. border-radius: 50%;
  496. display: flex;
  497. align-items: center;
  498. justify-content: center;
  499. font-size: 20rpx;
  500. font-weight: bold;
  501. }
  502. .image-upload-btn {
  503. width: 160rpx;
  504. height: 160rpx;
  505. border: 2rpx dashed #ddd;
  506. border-radius: 8rpx;
  507. display: flex;
  508. flex-direction: column;
  509. align-items: center;
  510. justify-content: center;
  511. background-color: #fafafa;
  512. }
  513. .upload-icon {
  514. font-size: 48rpx;
  515. color: #999;
  516. margin-bottom: 8rpx;
  517. }
  518. .upload-text {
  519. font-size: 24rpx;
  520. color: #999;
  521. }
  522. // 联系人信息
  523. .contact-input-row {
  524. display: flex;
  525. align-items: center;
  526. margin-bottom: 20rpx;
  527. &:last-child {
  528. margin-bottom: 0;
  529. }
  530. }
  531. .contact-label {
  532. font-size: 28rpx;
  533. color: #333;
  534. width: 120rpx;
  535. font-weight: bold;
  536. }
  537. .contact-input {
  538. flex: 1;
  539. height: 44rpx;
  540. font-size: 28rpx;
  541. color: #333;
  542. line-height: 44rpx;
  543. border-bottom: 1rpx solid #f0f0f0;
  544. padding: 12rpx 0;
  545. }
  546. // 底部提交按钮
  547. .bottom-action-bar {
  548. position: fixed;
  549. bottom: 0;
  550. left: 0;
  551. right: 0;
  552. width: 100%;
  553. background-color: #fff;
  554. border-top: 1rpx solid #f0f0f0;
  555. padding: 20rpx 30rpx;
  556. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  557. z-index: 1000;
  558. box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
  559. box-sizing: border-box;
  560. }
  561. .submit-btn {
  562. width: 100%;
  563. height: 88rpx;
  564. background-color: #4CAF50;
  565. border-radius: 12rpx;
  566. display: flex;
  567. align-items: center;
  568. justify-content: center;
  569. transition: background-color 0.2s ease;
  570. box-sizing: border-box;
  571. &:active {
  572. background-color: #45a049;
  573. }
  574. .btn-text {
  575. font-size: 32rpx;
  576. color: #fff;
  577. font-weight: bold;
  578. }
  579. }
  580. // 品类选择弹窗
  581. .picker-modal {
  582. position: fixed;
  583. top: 0;
  584. left: 0;
  585. width: 100%;
  586. height: 100%;
  587. background-color: rgba(0, 0, 0, 0.5);
  588. z-index: 2000;
  589. display: flex;
  590. align-items: flex-end;
  591. }
  592. .picker-content {
  593. width: 100%;
  594. background-color: #fff;
  595. border-radius: 24rpx 24rpx 0 0;
  596. padding: 0;
  597. }
  598. .picker-header {
  599. display: flex;
  600. justify-content: space-between;
  601. align-items: center;
  602. padding: 30rpx;
  603. border-bottom: 1rpx solid #f0f0f0;
  604. }
  605. .picker-title {
  606. font-size: 32rpx;
  607. font-weight: bold;
  608. color: #333;
  609. }
  610. .picker-close {
  611. font-size: 36rpx;
  612. color: #999;
  613. }
  614. .picker-options {
  615. padding: 20rpx 0;
  616. }
  617. .picker-option {
  618. padding: 24rpx 30rpx;
  619. border-bottom: 1rpx solid #f8f8f8;
  620. transition: background-color 0.2s ease;
  621. &:last-child {
  622. border-bottom: none;
  623. }
  624. &:active {
  625. background-color: #f5f5f5;
  626. }
  627. }
  628. .option-text {
  629. font-size: 30rpx;
  630. color: #333;
  631. }
  632. </style>