purchase-publish.vue 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 class="form-input" v-model="formData.title" placeholder="请输入收购标题"
  13. placeholder-style="color: #999;" maxlength="30" @input="onTitleInput" />
  14. <view class="char-count">{{ titleLength }}/30</view>
  15. </view>
  16. </view>
  17. <!-- 收购品类 -->
  18. <view class="form-item">
  19. <view class="item-label">
  20. <text class="label-text">收购品类</text>
  21. <text class="required">*</text>
  22. </view>
  23. <view class="item-content">
  24. <view class="category-selector" @click="showCategoryPicker = true">
  25. <text class="selector-text" :class="{ placeholder: !formData.categoryId }">
  26. <!-- {{ formData.categoryLabel || '请选择产品分类' }} -->
  27. {{getDictLabel('agricultural_category',formData.categoryId) || '请选择产品分类' }}
  28. </text>
  29. <text class="arrow-icon">></text>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="form-item">
  34. <!-- 所在地 -->
  35. <view class="item-label">
  36. <text class="label-text">所在地</text>
  37. <text class="required">*</text>
  38. </view>
  39. <LocationPicker v-model="formData.location" mode="edit" />
  40. </view>
  41. <!-- 收购数量 -->
  42. <view class="form-item">
  43. <view class="item-label">
  44. <text class="label-text">收购数量</text>
  45. <text class="required">*</text>
  46. </view>
  47. <view class="item-content">
  48. <view class="input-with-unit">
  49. <input class="form-input" v-model="formData.quantity" placeholder="请输入收购数量"
  50. placeholder-style="color: #999;" type="number" />
  51. <!-- <text class="unit-text">斤</text> -->
  52. </view>
  53. </view>
  54. </view>
  55. <!-- 单位-->
  56. <view class="form-item">
  57. <view class="item-label">
  58. <text class="label-text">单位</text>
  59. <text class="required">*</text>
  60. </view>
  61. <view class="item-content">
  62. <view class="category-selector" @click="showUnitPicker = true">
  63. <text class="selector-text" :class="{ placeholder: !formData.unit }">
  64. <!-- {{ formData.dictLabel || '请选择价格单位' }} -->
  65. {{ getDictLabel('agricultural_unit',formData.unit) || '请选择价格单位'}}
  66. </text>
  67. <text class="arrow-icon">></text>
  68. </view>
  69. </view>
  70. </view>
  71. <!-- 单价预算 -->
  72. <view class="form-item">
  73. <view class="item-label">
  74. <text class="label-text">单价预算</text>
  75. <text class="required">*</text>
  76. </view>
  77. <view class="item-content">
  78. <view class="input-with-unit">
  79. <text class="currency-symbol">¥</text>
  80. <input class="form-input" v-model="formData.price" placeholder="请输入单价预算"
  81. placeholder-style="color: #999;" type="digit" />
  82. <!-- <text class="unit-text">元/斤</text> -->
  83. </view>
  84. </view>
  85. </view>
  86. <!-- 补充说明 -->
  87. <view class="form-item">
  88. <view class="item-label">
  89. <text class="label-text">补充说明</text>
  90. </view>
  91. <view class="item-content">
  92. <textarea class="form-textarea" v-model="formData.description" placeholder="请描述交货要求、时间等补充信息"
  93. placeholder-style="color: #999;" maxlength="200" auto-height @input="onDescInput" />
  94. <view class="char-count">{{ descLength }}/200</view>
  95. </view>
  96. </view>
  97. <!-- 上传参考图片 -->
  98. <view class="form-item">
  99. <view class="item-label">
  100. <text class="label-text">参考图片</text>
  101. <text class="optional">(最多6张)</text>
  102. </view>
  103. <view class="item-content">
  104. <view class="image-upload-area">
  105. <view class="image-item" v-for="(image, index) in formData.images" :key="index">
  106. <image class="uploaded-image" :src="image.url" mode="aspectFill"></image>
  107. <view class="image-delete" @click="removeImage(index)">×</view>
  108. </view>
  109. <view class="image-upload-btn" v-if="formData.images.length < 6" @click="chooseImage">
  110. <text class="upload-icon">+</text>
  111. <text class="upload-text">上传图片</text>
  112. </view>
  113. </view>
  114. </view>
  115. </view>
  116. <!-- 联系人信息 -->
  117. <view class="form-item">
  118. <view class="item-label">
  119. <text class="label-text">联系人信息</text>
  120. </view>
  121. <view class="item-content">
  122. <view class="contact-input-row">
  123. <text class="contact-label">联系人:</text>
  124. <input class="contact-input" v-model="formData.contactName" placeholder="请输入联系人姓名"
  125. placeholder-style="color: #999;" maxlength="10" />
  126. </view>
  127. <view class="contact-input-row">
  128. <text class="contact-label">电话:</text>
  129. <input class="contact-input" v-model="formData.contactPhone" placeholder="请输入联系电话"
  130. placeholder-style="color: #999;" type="number" maxlength="11" />
  131. </view>
  132. </view>
  133. </view>
  134. </view>
  135. <!-- 底部提交按钮 -->
  136. <view class="bottom-action-bar">
  137. <view class="submit-btn" @click="submitForm">
  138. <text class="btn-text">{{ isEditMode ? '保存修改' : '提交发布' }}</text>
  139. </view>
  140. </view>
  141. <!-- 品类选择弹窗 -->
  142. <view class="picker-modal" v-if="showCategoryPicker" @click="showCategoryPicker = false">
  143. <view class="picker-content" @click.stop>
  144. <view class="picker-header">
  145. <text class="picker-title">选择收购品类</text>
  146. <text class="picker-close" @click="showCategoryPicker = false">×</text>
  147. </view>
  148. <view class="picker-options">
  149. <view class="picker-option"
  150. v-for="category in dictDataOptions.agricultural_category"
  151. :key="category.dictCode"
  152. @click="selectCategory(category)"
  153. :class="{ 'active': category.dictValue == formData.categoryId }">
  154. <text class="option-text">{{ category.dictLabel }}</text>
  155. </view>
  156. </view>
  157. </view>
  158. </view>
  159. <!-- 单位选择弹窗 -->
  160. <view class="picker-modal" v-if="showUnitPicker" @click="showUnitPicker = false">
  161. <view class="picker-content" @click.stop>
  162. <view class="picker-header">
  163. <text class="picker-title">选择价格单位</text>
  164. <text class="picker-close" @click="showUnitPicker = false">×</text>
  165. </view>
  166. <view class="picker-options">
  167. <view class="picker-option"
  168. v-for="unit in dictDataOptions.agricultural_unit"
  169. :key="unit.dictCode"
  170. @click="selectUnit(unit)"
  171. :class="{ 'active': unit.dictValue == formData.unit }">
  172. <text class="option-text">{{ unit.dictLabel }}</text>
  173. </view>
  174. </view>
  175. </view>
  176. </view>
  177. </view>
  178. </template>
  179. <script setup>
  180. import { ref, reactive, computed, onMounted } from 'vue'
  181. import LocationPicker from "@/components/common/LocationPicker.vue"
  182. import api from "@/config/api.js"
  183. import storage from "@/utils/storage.js"
  184. import { addProductInfo, getProductInfoById, editProductInfo } from '@/api/services/productInfo.js'
  185. import { useDict } from '@/utils/composables/useDict'
  186. import { getFormattedTime } from '@/utils/dateUtils'
  187. // 使用 useDict 替换 dictMixin
  188. const { dictData } = useDict(['agricultural_unit', 'agricultural_category'])
  189. // 响应式状态
  190. const isEditMode = ref(false)
  191. const editItemId = ref('')
  192. const showCategoryPicker = ref(false)
  193. const showUnitPicker = ref(false)
  194. const titleLength = ref(0)
  195. const descLength = ref(0)
  196. // 使用 reactive 管理复杂的表单对象
  197. const formData = reactive({
  198. title: '',
  199. categoryId: '',
  200. price: '',
  201. description: '',
  202. imageUrl: '',
  203. images: [],
  204. unit: '',
  205. quantity: '',
  206. contactName: '',
  207. contactPhone: '',
  208. location: '',
  209. publishTime: getFormattedTime(),
  210. userId: storage.getUserInfo().userid,
  211. type: 1, // 收购
  212. status: 1 // 审核中
  213. })
  214. // 计算属性:dictDataOptions
  215. const dictDataOptions = computed(() => dictData)
  216. // onLoad 替换为 onMounted + getCurrentPages
  217. onMounted(() => {
  218. const pages = getCurrentPages()
  219. const currentPage = pages[pages.length - 1]
  220. const options = currentPage.options
  221. // 检查是否为编辑模式
  222. if (options.action === 'edit' && options.id) {
  223. isEditMode.value = true
  224. editItemId.value = options.id
  225. // 设置页面标题
  226. uni.setNavigationBarTitle({
  227. title: '编辑收购信息'
  228. })
  229. // 加载收购信息数据
  230. loadPurchaseData()
  231. } else {
  232. // 新建模式
  233. isEditMode.value = false
  234. uni.setNavigationBarTitle({
  235. title: '发布收购信息'
  236. })
  237. }
  238. })
  239. // 方法定义
  240. // 加载收购信息数据(编辑模式)
  241. const loadPurchaseData = () => {
  242. uni.showLoading({
  243. title: '加载中...'
  244. })
  245. getProductInfoById(editItemId.value).then(res => {
  246. if (res.data.code === 200) {
  247. Object.assign(formData, res.data.data)
  248. // 更新字符计数
  249. titleLength.value = formData.title.length
  250. descLength.value = formData.description.length
  251. console.log("this.goodsDetail", formData)
  252. // 处理图片数据
  253. if (formData.imageUrl) {
  254. try {
  255. formData.images = formData.imageUrl.split(',').map(url => ({ url, status: 'success' }))
  256. console.log('解析后的图片数据:', formData.images)
  257. } catch (e) {
  258. console.error('解析图片数据失败:', e)
  259. formData.images = []
  260. }
  261. } else {
  262. formData.images = []
  263. }
  264. uni.hideLoading()
  265. } else {
  266. uni.showToast({
  267. title: res.data.msg || '获取农品信息失败',
  268. icon: 'none'
  269. })
  270. }
  271. })
  272. }
  273. // 标题输入处理
  274. const onTitleInput = (e) => {
  275. titleLength.value = e.detail.value.length
  276. }
  277. // 描述输入处理
  278. const onDescInput = (e) => {
  279. descLength.value = e.detail.value.length
  280. }
  281. // 选择分类
  282. const selectCategory = (category) => {
  283. formData.categoryId = category.dictValue
  284. formData.categoryLabel = category.dictLabel
  285. showCategoryPicker.value = false
  286. }
  287. // 选择单位
  288. const selectUnit = (unit) => {
  289. formData.unit = unit.dictValue
  290. formData.dictLabel = unit.dictLabel
  291. showUnitPicker.value = false
  292. }
  293. // 获取字典标签
  294. const getDictLabel = (dictKey, value) => {
  295. if (!dictData || !dictData[dictKey]) {
  296. return ''
  297. }
  298. const list = dictData[dictKey] || []
  299. const item = list.find(u => u.dictValue == value)
  300. return item ? item.dictLabel : ''
  301. }
  302. // 选择图片(优化跨平台兼容性)
  303. const chooseImage = () => {
  304. uni.chooseImage({
  305. count: 6 - formData.images.length,
  306. sizeType: ['original', 'compressed'],
  307. sourceType: ['album', 'camera'],
  308. success: (res) => {
  309. console.log('选择图片成功:', res);
  310. console.log('tempFiles:', res.tempFiles);
  311. console.log('tempFilePaths:', res.tempFilePaths);
  312. // 验证文件类型和大小
  313. const validFiles = [];
  314. const invalidFiles = [];
  315. const maxSize = 5 * 1024 * 1024; // 5MB 最大限制
  316. // 兼容不同平台的文件信息获取方式
  317. if (res.tempFiles && res.tempFiles.length > 0) {
  318. // 检查每个文件
  319. res.tempFiles.forEach((file, index) => {
  320. console.log(`文件 ${index}:`, file);
  321. // 获取文件路径
  322. const filePath = res.tempFilePaths[index];
  323. // 获取文件名(兼容不同平台)
  324. let fileName = '';
  325. if (file.name) {
  326. fileName = file.name;
  327. } else if (file.path) {
  328. // 从路径中提取文件名
  329. fileName = file.path.split('/').pop();
  330. } else if (filePath) {
  331. fileName = filePath.split('/').pop();
  332. }
  333. console.log(`文件名: ${fileName}, 大小: ${file.size}`);
  334. // 检查文件类型(兼容没有扩展名的情况)
  335. let isImage = true;
  336. if (fileName) {
  337. isImage = /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(fileName);
  338. }
  339. // 检查文件大小
  340. const fileSize = file.size || 0;
  341. const isValidSize = fileSize <= maxSize;
  342. if (isImage && isValidSize) {
  343. validFiles.push(filePath);
  344. } else {
  345. invalidFiles.push({
  346. path: filePath,
  347. size: fileSize,
  348. name: fileName,
  349. reason: !isImage ? '文件格式不支持' : `文件大于5MB (${(fileSize / 1024 / 1024).toFixed(2)}MB)`
  350. });
  351. }
  352. });
  353. } else {
  354. // 如果没有 tempFiles,直接使用 tempFilePaths(某些平台可能不返回详细信息)
  355. console.warn('未获取到 tempFiles,直接使用 tempFilePaths');
  356. res.tempFilePaths.forEach(path => {
  357. validFiles.push(path);
  358. });
  359. }
  360. console.log('有效文件:', validFiles);
  361. console.log('无效文件:', invalidFiles);
  362. // 显示无效文件提示
  363. if (invalidFiles.length > 0) {
  364. const reasons = invalidFiles.map(f => `${f.name}: ${f.reason}`).join('\n');
  365. uni.showModal({
  366. title: '部分文件无效',
  367. content: `${invalidFiles.length}个文件无效:\n${reasons}`,
  368. showCancel: false
  369. });
  370. }
  371. // 如果有有效文件,则上传
  372. if (validFiles.length > 0) {
  373. uploadImages(validFiles);
  374. } else if (invalidFiles.length > 0) {
  375. uni.showToast({
  376. title: '没有可上传的文件',
  377. icon: 'none',
  378. duration: 2000
  379. });
  380. }
  381. },
  382. fail: (err) => {
  383. console.error('选择图片失败:', err);
  384. uni.showToast({
  385. title: '选择图片失败',
  386. icon: 'none',
  387. duration: 2000
  388. });
  389. }
  390. });
  391. }
  392. // 上传图片到服务器(优化跨平台兼容性)
  393. const uploadImages = (tempFilePaths) => {
  394. uni.showLoading({
  395. title: '上传中...',
  396. mask: true
  397. });
  398. // 上传成功的图片计数
  399. let successCount = 0;
  400. let failCount = 0;
  401. const totalFiles = tempFilePaths.length;
  402. const newImages = [];
  403. // 遍历处理每张图片
  404. tempFilePaths.forEach((path, index) => {
  405. // 调用上传API
  406. uni.uploadFile({
  407. url: api.serve + '/file/upload',
  408. filePath: path,
  409. name: 'file',
  410. formData: {
  411. type: 'task'
  412. },
  413. header: {
  414. 'Authorization': `Bearer ${storage.getAccessToken()}`
  415. },
  416. success: (res) => {
  417. try {
  418. console.log('上传原始响应:', res);
  419. console.log('响应数据类型:', typeof res.data);
  420. console.log('响应状态码:', res.statusCode);
  421. let response;
  422. // 兼容不同平台的响应格式
  423. if (typeof res.data === 'string') {
  424. // H5 端返回字符串,需要解析
  425. response = JSON.parse(res.data);
  426. } else if (typeof res.data === 'object') {
  427. // Android/鸿蒙端可能直接返回对象
  428. response = res.data;
  429. } else {
  430. throw new Error('未知的响应格式');
  431. }
  432. console.log('解析后的响应:', response);
  433. // 检查响应是否成功
  434. if (response.code === 200 || response.code === '200') {
  435. // 兼容不同的数据结构
  436. let imageUrl = '';
  437. // 情况1: response.data 是字符串(直接是URL)
  438. if (typeof response.data === 'string') {
  439. imageUrl = response.data;
  440. }
  441. // 情况2: response.data 是对象,包含 url 字段
  442. else if (response.data && response.data.url) {
  443. imageUrl = response.data.url;
  444. }
  445. // 情况3: response 直接包含 url 字段
  446. else if (response.url) {
  447. imageUrl = response.url;
  448. }
  449. // 情况4: response.data 是数组
  450. else if (Array.isArray(response.data) && response.data.length > 0) {
  451. imageUrl = response.data[0].url || response.data[0];
  452. }
  453. if (imageUrl) {
  454. console.log('上传成功,图片URL:', imageUrl);
  455. // 上传成功,将图片信息添加到数组
  456. newImages.push({
  457. url: imageUrl,
  458. path: path,
  459. status: 'success',
  460. fileName: (response.data && response.data.fileName) || ''
  461. });
  462. successCount++;
  463. } else {
  464. console.error('无法从响应中提取图片URL:', response);
  465. failCount++;
  466. uni.showToast({
  467. title: '图片URL解析失败',
  468. icon: 'none',
  469. duration: 2000
  470. });
  471. }
  472. } else {
  473. failCount++;
  474. console.error('上传失败,错误信息:', response.msg || response.message);
  475. uni.showToast({
  476. title: response.msg || response.message || '上传失败',
  477. icon: 'none',
  478. duration: 2000
  479. });
  480. }
  481. } catch (e) {
  482. failCount++;
  483. console.error('解析响应失败:', e);
  484. console.error('原始响应数据:', res.data);
  485. uni.showToast({
  486. title: `解析失败: ${e.message}`,
  487. icon: 'none',
  488. duration: 2000
  489. });
  490. }
  491. },
  492. fail: (err) => {
  493. failCount++;
  494. console.error('上传请求失败:', err);
  495. uni.showToast({
  496. title: '上传请求失败',
  497. icon: 'none',
  498. duration: 2000
  499. });
  500. },
  501. complete: () => {
  502. // 当所有文件都已处理完成
  503. if (successCount + failCount === totalFiles) {
  504. uni.hideLoading();
  505. if (newImages.length > 0) {
  506. // 将新上传的图片添加到已有图片列表
  507. formData.images = [...formData.images, ...newImages];
  508. // 更新imageUrl字段,将图片URL用逗号连接
  509. formData.imageUrl = formData.images.map(item => item.url).join(',');
  510. console.log('所有图片上传完成:', formData.images);
  511. console.log('imageUrl:', formData.imageUrl);
  512. // 显示成功提示
  513. uni.showToast({
  514. title: `成功上传${successCount}张图片`,
  515. icon: 'success',
  516. duration: 2000
  517. });
  518. } else {
  519. // 全部失败
  520. uni.showToast({
  521. title: '图片上传失败,请重试',
  522. icon: 'none',
  523. duration: 2000
  524. });
  525. }
  526. // 如果有部分失败
  527. if (failCount > 0 && successCount > 0) {
  528. uni.showToast({
  529. title: `成功${successCount}张,失败${failCount}张`,
  530. icon: 'none',
  531. duration: 2000
  532. });
  533. }
  534. }
  535. }
  536. });
  537. });
  538. }
  539. const removeImage = (index) => {
  540. uni.showModal({
  541. title: '确认删除',
  542. content: '确定要删除这张图片吗?',
  543. success: (res) => {
  544. if (res.confirm) {
  545. formData.images.splice(index, 1)
  546. // 更新taskImages字段
  547. formData.imageUrl = formData.images.map(item => item.url).join(',')
  548. uni.showToast({
  549. title: '已删除',
  550. icon: 'none'
  551. })
  552. }
  553. }
  554. })
  555. }
  556. // 表单验证
  557. const validateForm = () => {
  558. if (!formData.title.trim()) {
  559. uni.showToast({
  560. title: '请输入收购标题',
  561. icon: 'none'
  562. })
  563. return false
  564. }
  565. if (!formData.categoryId) {
  566. uni.showToast({
  567. title: '请选择收购品类',
  568. icon: 'none'
  569. })
  570. return false
  571. }
  572. if (!formData.quantity || formData.quantity <= 0) {
  573. uni.showToast({
  574. title: '请输入有效的收购数量',
  575. icon: 'none'
  576. })
  577. return false
  578. }
  579. if (!formData.price || formData.price <= 0) {
  580. uni.showToast({
  581. title: '请输入有效的单价预算',
  582. icon: 'none'
  583. })
  584. return false
  585. }
  586. if (!formData.contactName.trim()) {
  587. uni.showToast({
  588. title: '请输入联系人姓名',
  589. icon: 'none'
  590. })
  591. return false
  592. }
  593. if (!formData.contactPhone.trim()) {
  594. uni.showToast({
  595. title: '请输入联系电话',
  596. icon: 'none'
  597. })
  598. return false
  599. }
  600. // 简单的手机号格式验证
  601. const phoneRegex = /^1[3-9]\d{9}$/
  602. if (!phoneRegex.test(formData.contactPhone)) {
  603. uni.showToast({
  604. title: '请输入正确的手机号码',
  605. icon: 'none'
  606. })
  607. return false
  608. }
  609. return true
  610. }
  611. // 提交表单
  612. const submitForm = () => {
  613. if (!validateForm()) {
  614. return
  615. }
  616. // 显示加载提示
  617. const loadingTitle = isEditMode.value ? '保存中...' : '提交中...'
  618. uni.showLoading({
  619. title: loadingTitle
  620. })
  621. uni.hideLoading()
  622. if (isEditMode.value) {
  623. editProductInfo(formData).then(res => {
  624. if (res.data.code === 200) {
  625. uni.showModal({
  626. title: '保存成功',
  627. content: '您的修改已保存成功!',
  628. showCancel: false,
  629. success: () => {
  630. uni.navigateBack()
  631. }
  632. })
  633. } else {
  634. uni.showToast({
  635. title: res.data.msg || '提交失败,请稍后重试',
  636. icon: 'none'
  637. })
  638. }
  639. }).catch(err => {
  640. console.error("提交异常:", err)
  641. uni.showToast({
  642. title: '网络错误,请检查后重试',
  643. icon: 'none'
  644. })
  645. })
  646. } else {
  647. // 新建模式 - 提交审核
  648. console.log("this.formData", formData)
  649. addProductInfo(formData).then(res => {
  650. console.log("新增出售农产品", res)
  651. if (res.data.code === 200) {
  652. uni.showModal({
  653. title: '提交成功',
  654. content: '您的收购农产品信息已提交审核,审核通过后将在收购页面展示。',
  655. showCancel: false,
  656. success: () => {
  657. uni.navigateBack()
  658. }
  659. })
  660. } else {
  661. uni.showToast({
  662. title: res.data.msg || '提交失败,请稍后重试',
  663. icon: 'none'
  664. })
  665. }
  666. }).catch(err => {
  667. console.error("提交异常:", err)
  668. uni.showToast({
  669. title: '网络错误,请检查后重试',
  670. icon: 'none'
  671. })
  672. })
  673. }
  674. }
  675. </script>
  676. <style lang="scss">
  677. .purchase-publish-container {
  678. min-height: 100vh;
  679. background-color: #f5f5f5;
  680. padding-bottom: calc(128rpx + env(safe-area-inset-bottom));
  681. }
  682. .form-container {
  683. padding: 20rpx;
  684. }
  685. .form-item {
  686. background-color: #fff;
  687. border-radius: 12rpx;
  688. padding: 24rpx;
  689. margin-bottom: 16rpx;
  690. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  691. }
  692. .item-label {
  693. display: flex;
  694. align-items: center;
  695. margin-bottom: 16rpx;
  696. }
  697. .label-text {
  698. font-size: 28rpx;
  699. color: #333;
  700. font-weight: bold;
  701. }
  702. .required {
  703. color: #ff4d4f;
  704. font-size: 28rpx;
  705. margin-left: 4rpx;
  706. }
  707. .optional {
  708. color: #999;
  709. font-size: 24rpx;
  710. margin-left: 8rpx;
  711. }
  712. .item-content {
  713. position: relative;
  714. }
  715. .form-input {
  716. width: 100%;
  717. height: 44rpx;
  718. font-size: 28rpx;
  719. color: #333;
  720. line-height: 44rpx;
  721. }
  722. .form-textarea {
  723. width: 100%;
  724. min-height: 120rpx;
  725. font-size: 28rpx;
  726. color: #333;
  727. line-height: 1.5;
  728. }
  729. .char-count {
  730. position: absolute;
  731. right: 0;
  732. bottom: -4rpx;
  733. font-size: 24rpx;
  734. color: #999;
  735. }
  736. // 品类选择器
  737. .category-selector {
  738. display: flex;
  739. align-items: center;
  740. justify-content: space-between;
  741. padding: 12rpx 0;
  742. border-bottom: 1rpx solid #f0f0f0;
  743. }
  744. .selector-text {
  745. font-size: 28rpx;
  746. color: #333;
  747. &.placeholder {
  748. color: #999;
  749. }
  750. }
  751. .arrow-icon {
  752. font-size: 24rpx;
  753. color: #999;
  754. }
  755. // 带单位的输入框
  756. .input-with-unit {
  757. display: flex;
  758. align-items: center;
  759. border-bottom: 1rpx solid #f0f0f0;
  760. padding: 12rpx 0;
  761. }
  762. .currency-symbol {
  763. font-size: 28rpx;
  764. color: #333;
  765. margin-right: 8rpx;
  766. }
  767. .unit-text {
  768. font-size: 28rpx;
  769. color: #666;
  770. margin-left: 8rpx;
  771. white-space: nowrap;
  772. }
  773. // 图片上传
  774. .image-upload-area {
  775. display: flex;
  776. flex-wrap: wrap;
  777. gap: 16rpx;
  778. }
  779. .image-item {
  780. position: relative;
  781. width: 160rpx;
  782. height: 160rpx;
  783. border-radius: 8rpx;
  784. overflow: hidden;
  785. }
  786. .uploaded-image {
  787. width: 100%;
  788. height: 100%;
  789. }
  790. .image-delete {
  791. position: absolute;
  792. top: -8rpx;
  793. right: -8rpx;
  794. width: 32rpx;
  795. height: 32rpx;
  796. background-color: #ff4d4f;
  797. color: #fff;
  798. border-radius: 50%;
  799. display: flex;
  800. align-items: center;
  801. justify-content: center;
  802. font-size: 20rpx;
  803. font-weight: bold;
  804. }
  805. .image-upload-btn {
  806. width: 160rpx;
  807. height: 160rpx;
  808. border: 2rpx dashed #ddd;
  809. border-radius: 8rpx;
  810. display: flex;
  811. flex-direction: column;
  812. align-items: center;
  813. justify-content: center;
  814. background-color: #fafafa;
  815. }
  816. .upload-icon {
  817. font-size: 48rpx;
  818. color: #999;
  819. margin-bottom: 8rpx;
  820. }
  821. .upload-text {
  822. font-size: 24rpx;
  823. color: #999;
  824. }
  825. // 联系人信息
  826. .contact-input-row {
  827. display: flex;
  828. align-items: center;
  829. margin-bottom: 20rpx;
  830. &:last-child {
  831. margin-bottom: 0;
  832. }
  833. }
  834. .contact-label {
  835. font-size: 28rpx;
  836. color: #333;
  837. width: 120rpx;
  838. font-weight: bold;
  839. }
  840. .contact-input {
  841. flex: 1;
  842. height: 44rpx;
  843. font-size: 28rpx;
  844. color: #333;
  845. line-height: 44rpx;
  846. border-bottom: 1rpx solid #f0f0f0;
  847. padding: 12rpx 0;
  848. }
  849. // 底部提交按钮
  850. .bottom-action-bar {
  851. position: fixed;
  852. bottom: 0;
  853. left: 0;
  854. right: 0;
  855. width: 100%;
  856. background-color: #fff;
  857. border-top: 1rpx solid #f0f0f0;
  858. padding: 20rpx 30rpx;
  859. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  860. z-index: 1000;
  861. box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
  862. box-sizing: border-box;
  863. }
  864. .submit-btn {
  865. width: 100%;
  866. height: 88rpx;
  867. background-color: #4CAF50;
  868. border-radius: 12rpx;
  869. display: flex;
  870. align-items: center;
  871. justify-content: center;
  872. transition: background-color 0.2s ease;
  873. box-sizing: border-box;
  874. &:active {
  875. background-color: #45a049;
  876. }
  877. .btn-text {
  878. font-size: 32rpx;
  879. color: #fff;
  880. font-weight: bold;
  881. }
  882. }
  883. // 品类选择弹窗
  884. .picker-modal {
  885. position: fixed;
  886. top: 0;
  887. left: 0;
  888. width: 100%;
  889. height: 100%;
  890. background-color: rgba(0, 0, 0, 0.5);
  891. z-index: 2000;
  892. display: flex;
  893. align-items: flex-end;
  894. }
  895. .picker-content {
  896. width: 100%;
  897. background-color: #fff;
  898. border-radius: 24rpx 24rpx 0 0;
  899. padding: 0;
  900. }
  901. .picker-header {
  902. display: flex;
  903. justify-content: space-between;
  904. align-items: center;
  905. padding: 30rpx;
  906. border-bottom: 1rpx solid #f0f0f0;
  907. }
  908. .picker-title {
  909. font-size: 32rpx;
  910. font-weight: bold;
  911. color: #333;
  912. }
  913. .picker-close {
  914. font-size: 36rpx;
  915. color: #999;
  916. }
  917. .picker-options {
  918. padding: 20rpx 0;
  919. }
  920. .picker-option {
  921. padding: 24rpx 30rpx;
  922. border-bottom: 1rpx solid #f8f8f8;
  923. transition: background-color 0.2s ease;
  924. &:last-child {
  925. border-bottom: none;
  926. }
  927. &:active {
  928. background-color: #f5f5f5;
  929. }
  930. }
  931. .option-text {
  932. font-size: 30rpx;
  933. color: #333;
  934. }
  935. </style>