purchase-publish.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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, .arrow-icon::after {
  752. font-size: 24rpx;
  753. color: #999;
  754. content: ">";
  755. }
  756. // 带单位的输入框
  757. .input-with-unit {
  758. display: flex;
  759. align-items: center;
  760. border-bottom: 1rpx solid #f0f0f0;
  761. padding: 12rpx 0;
  762. }
  763. .currency-symbol {
  764. font-size: 28rpx;
  765. color: #333;
  766. margin-right: 8rpx;
  767. }
  768. .unit-text {
  769. font-size: 28rpx;
  770. color: #666;
  771. margin-left: 8rpx;
  772. white-space: nowrap;
  773. }
  774. // 图片上传
  775. .image-upload-area {
  776. display: flex;
  777. flex-wrap: wrap;
  778. gap: 16rpx;
  779. }
  780. .image-item {
  781. position: relative;
  782. width: 160rpx;
  783. height: 160rpx;
  784. border-radius: 8rpx;
  785. overflow: hidden;
  786. }
  787. .uploaded-image {
  788. width: 100%;
  789. height: 100%;
  790. }
  791. .image-delete {
  792. position: absolute;
  793. top: -8rpx;
  794. right: -8rpx;
  795. width: 32rpx;
  796. height: 32rpx;
  797. background-color: #ff4d4f;
  798. color: #fff;
  799. border-radius: 50%;
  800. display: flex;
  801. align-items: center;
  802. justify-content: center;
  803. font-size: 20rpx;
  804. font-weight: bold;
  805. }
  806. .image-upload-btn {
  807. width: 160rpx;
  808. height: 160rpx;
  809. border: 2rpx dashed #ddd;
  810. border-radius: 8rpx;
  811. display: flex;
  812. flex-direction: column;
  813. align-items: center;
  814. justify-content: center;
  815. background-color: #fafafa;
  816. }
  817. .upload-icon {
  818. font-size: 48rpx;
  819. color: #999;
  820. margin-bottom: 8rpx;
  821. }
  822. .upload-text {
  823. font-size: 24rpx;
  824. color: #999;
  825. }
  826. // 联系人信息
  827. .contact-input-row {
  828. display: flex;
  829. align-items: center;
  830. margin-bottom: 20rpx;
  831. &:last-child {
  832. margin-bottom: 0;
  833. }
  834. }
  835. .contact-label {
  836. font-size: 28rpx;
  837. color: #333;
  838. width: 120rpx;
  839. font-weight: bold;
  840. }
  841. .contact-input {
  842. flex: 1;
  843. height: 44rpx;
  844. font-size: 28rpx;
  845. color: #333;
  846. line-height: 44rpx;
  847. border-bottom: 1rpx solid #f0f0f0;
  848. padding: 12rpx 0;
  849. }
  850. // 底部提交按钮
  851. .bottom-action-bar {
  852. position: fixed;
  853. bottom: 0;
  854. left: 0;
  855. right: 0;
  856. width: 100%;
  857. background-color: #fff;
  858. border-top: 1rpx solid #f0f0f0;
  859. padding: 20rpx 30rpx;
  860. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  861. z-index: 1000;
  862. box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
  863. box-sizing: border-box;
  864. }
  865. .submit-btn {
  866. width: 100%;
  867. height: 88rpx;
  868. background-color: #4CAF50;
  869. border-radius: 12rpx;
  870. display: flex;
  871. align-items: center;
  872. justify-content: center;
  873. transition: background-color 0.2s ease;
  874. box-sizing: border-box;
  875. &:active {
  876. background-color: #45a049;
  877. }
  878. .btn-text {
  879. font-size: 32rpx;
  880. color: #fff;
  881. font-weight: bold;
  882. }
  883. }
  884. // 品类选择弹窗
  885. .picker-modal {
  886. position: fixed;
  887. top: 0;
  888. left: 0;
  889. width: 100%;
  890. height: 100%;
  891. background-color: rgba(0, 0, 0, 0.5);
  892. z-index: 2000;
  893. display: flex;
  894. align-items: flex-end;
  895. }
  896. .picker-content {
  897. width: 100%;
  898. background-color: #fff;
  899. border-radius: 24rpx 24rpx 0 0;
  900. padding: 0;
  901. }
  902. .picker-header {
  903. display: flex;
  904. justify-content: space-between;
  905. align-items: center;
  906. padding: 30rpx;
  907. border-bottom: 1rpx solid #f0f0f0;
  908. }
  909. .picker-title {
  910. font-size: 32rpx;
  911. font-weight: bold;
  912. color: #333;
  913. }
  914. .picker-close {
  915. font-size: 36rpx;
  916. color: #999;
  917. }
  918. .picker-options {
  919. padding: 20rpx 0;
  920. }
  921. .picker-option {
  922. padding: 24rpx 30rpx;
  923. border-bottom: 1rpx solid #f8f8f8;
  924. transition: background-color 0.2s ease;
  925. &:last-child {
  926. border-bottom: none;
  927. }
  928. &:active {
  929. background-color: #f5f5f5;
  930. }
  931. }
  932. .option-text {
  933. font-size: 30rpx;
  934. color: #333;
  935. }
  936. </style>