mall-detail.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <view class="detail-container">
  3. <!-- 商品轮播图 -->
  4. <view class="swiper-container">
  5. <swiper class="goods-swiper" :indicator-dots="true" :autoplay="true" :circular="true">
  6. <swiper-item v-for="(image, index) in goodsImages" :key="index">
  7. <image class="swiper-image" :src="getImageUrl(image)" mode="aspectFill"></image>
  8. </swiper-item>
  9. </swiper>
  10. </view>
  11. <!-- 商品基本信息 -->
  12. <view class="goods-basic-info">
  13. <view class="goods-title">{{ goodsDetail.title }}</view>
  14. <view class="goods-subtitle">{{ goodsDetail.description }}</view>
  15. <view class="price-container">
  16. <text class="current-price">¥{{ goodsDetail.price }}</text>
  17. <!-- <text class="price-unit">/{{ goodsDetail.unit }}</text> -->
  18. <text class="original-price" v-if="goodsDetail.originalPrice">¥{{ goodsDetail.originalPrice }}</text>
  19. </view>
  20. <view class="specs-info">
  21. <text class="specs-label">规格:</text>
  22. <text class="specs-value">{{ goodsDetail.specifications }}</text>
  23. </view>
  24. </view>
  25. <!-- 商品详情介绍 -->
  26. <view class="goods-detail-info">
  27. <view class="detail-title">商品详情</view>
  28. <!-- 产品特点 -->
  29. <view class="detail-section">
  30. <view class="section-title">产品特点</view>
  31. <view class="feature-list">
  32. <view
  33. class="feature-item"
  34. v-for="(feature, index) in goodsDetail.features"
  35. :key="index"
  36. >
  37. <view class="feature-icon">✓</view>
  38. <text class="feature-text">{{ feature }}</text>
  39. </view>
  40. </view>
  41. </view>
  42. <!-- 使用说明 -->
  43. <view class="detail-section">
  44. <view class="section-title">使用说明</view>
  45. <view class="usage-content">
  46. <text>{{ goodsDetail.usageGuide }}</text>
  47. </view>
  48. </view>
  49. <!-- 产品图片展示 -->
  50. <view class="detail-section">
  51. <view class="section-title">产品展示</view>
  52. <view class="detail-images">
  53. <image
  54. class="detail-image"
  55. v-for="(item, index) in goodsDetail.detailImages"
  56. :key="index"
  57. :src="getImageUrl(item)"
  58. mode="widthFix"
  59. @click="previewImage(item, index)"
  60. ></image>
  61. </view>
  62. </view>
  63. </view>
  64. <!-- 底部操作栏 -->
  65. <view class="bottom-actions">
  66. <view class="action-btn consult-btn" @click="handleConsult">
  67. 立即咨询
  68. </view>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import { getMallById } from '@/api/services/mall.js';
  74. import api from "@/config/api.js";
  75. export default {
  76. data() {
  77. return {
  78. goodsId: '',
  79. goodsDetail: {
  80. // title: '高产玉米种子',
  81. // description: '优质杂交种,抗病性强,适应性广',
  82. // price: '68',
  83. // originalPrice: '88',
  84. // unit: '袋',
  85. // specifications: '500g/袋,发芽率≥95%',
  86. // features: [
  87. // '高产稳产,比普通品种增产15-20%',
  88. // '抗病性强,抗大小斑病、丝黑穗病',
  89. // '适应性广,适合多种土壤和气候条件',
  90. // '株型紧凑,抗倒伏能力强',
  91. // '籽粒饱满,商品性好'
  92. // ],
  93. // usage: '播种前先进行种子处理,选择肥沃、排水良好的土壤,按照每亩3-4千粒的密度播种。播种深度2-3厘米,播种后及时覆土镇压。生长期注意水肥管理,适时中耕除草。',
  94. // detailImages: [
  95. // '/static/images/products/corn-seeds-new.jpg',
  96. // '/static/images/products/seeds-packets.jpg',
  97. // '/static/images/products/agriculture-tools.jpg'
  98. // ]
  99. },
  100. goodsImages: []
  101. }
  102. },
  103. onLoad(options) {
  104. if (options.id) {
  105. this.goodsId = options.id;
  106. this.loadGoodsDetail(this.goodsId);
  107. }
  108. if (options.title) {
  109. uni.setNavigationBarTitle({
  110. title: decodeURIComponent(options.title)
  111. });
  112. }
  113. },
  114. methods: {
  115. getImageUrl(item) {
  116. // 默认返回url或path
  117. console.log("api.upload + item.url",api.upload + item.url);
  118. return api.upload + item.url;
  119. },
  120. // 加载商品详情
  121. loadGoodsDetail(goodsId) {
  122. uni.showLoading({
  123. title: '加载中'
  124. });
  125. getMallById(goodsId).then(res=>{
  126. if (res.data.code === 200) {
  127. const { data } = res.data;
  128. this.goodsDetail = data
  129. // 处理图片数据
  130. if (this.goodsDetail.detailImages && this.goodsDetail.swiperImages) {
  131. try {
  132. this.goodsImages = data.swiperImages
  133. // 尝试解析图片数据字符串
  134. const imageUrls = this.goodsDetail.detailImages.split(',');
  135. const swiperImages = this.goodsImages.split(',');
  136. this.goodsDetail.detailImages = imageUrls.filter(url => url && url.trim()).map(url => ({
  137. url: url.trim(), // 保存原始URL,显示时会通过getImageUrl方法处理
  138. status: 'success'
  139. }));
  140. this.goodsImages = swiperImages.filter(url => url && url.trim()).map(url => ({
  141. url: url.trim(), // 保存原始URL,显示时会通过getImageUrl方法处理
  142. status: 'success'
  143. }));
  144. // console.log('解析后的图片数据:', this.goodsDetail.detailImages);
  145. } catch (e) {
  146. console.error('解析图片数据失败:', e);
  147. this.goodsDetail.detailImages = [];
  148. this.goodsDetail.swiperImages = [];
  149. }
  150. } else {
  151. this.goodsDetail.detailImages = [];
  152. this.goodsDetail.swiperImages = [];
  153. }
  154. uni.hideLoading();
  155. } else {
  156. uni.showToast({
  157. title: res.data.msg || '获取商品信息失败',
  158. icon: 'none'
  159. });
  160. }
  161. })
  162. // 这里可以根据goodsId从后端获取商品详情
  163. // 目前使用模拟数据
  164. // const goodsData = {
  165. // 1: {
  166. // title: '高产玉米种子',
  167. // description: '优质杂交种,抗病性强,适应性广',
  168. // price: '68',
  169. // originalPrice: '88',
  170. // unit: '袋',
  171. // specifications: '500g/袋,发芽率≥95%',
  172. // features: [
  173. // '高产稳产,比普通品种增产15-20%',
  174. // '抗病性强,抗大小斑病、丝黑穗病',
  175. // '适应性广,适合多种土壤和气候条件',
  176. // '株型紧凑,抗倒伏能力强',
  177. // '籽粒饱满,商品性好'
  178. // ],
  179. // usage: '播种前先进行种子处理,选择肥沃、排水良好的土壤,按照每亩3-4千粒的密度播种。播种深度2-3厘米,播种后及时覆土镇压。生长期注意水肥管理,适时中耕除草。',
  180. // detailImages: [
  181. // '/static/images/products/corn-seeds-new.jpg',
  182. // '/static/images/products/seeds-packets.jpg',
  183. // '/static/images/products/agriculture-tools.jpg'
  184. // ]
  185. // }
  186. // };
  187. // if (goodsData[this.goodsId]) {
  188. // this.goodsDetail = goodsData[this.goodsId];
  189. // // 根据商品类型设置轮播图
  190. // this.setGoodsImages(this.goodsId);
  191. // }
  192. },
  193. // 设置商品轮播图
  194. setGoodsImages(goodsId) {
  195. const imageMap = {
  196. '1': [ // 玉米种子
  197. '/static/images/products/corn-seeds-new.jpg',
  198. '/static/images/products/seeds-packets.jpg',
  199. '/static/images/products/agriculture-tools.jpg',
  200. '/static/images/products/wheat-seeds.jpg'
  201. ],
  202. '2': [ // 复合肥料
  203. '/static/images/products/fertilizer.jpg',
  204. '/static/images/products/organic-fertilizer-new.jpg',
  205. '/static/images/products/soil-test.jpg',
  206. '/static/images/products/greenhouse-film.jpg'
  207. ],
  208. '3': [ // 植物保护剂
  209. '/static/images/products/plant-protection.jpg',
  210. '/static/images/products/agriculture-tools.jpg',
  211. '/static/images/products/pesticide.jpg',
  212. '/static/images/products/insecticide.jpg'
  213. ],
  214. '4': [ // 水稻种子
  215. '/static/images/products/seeds-packets.jpg',
  216. '/static/images/products/rice-seeds.jpg',
  217. '/static/images/products/corn-seeds-new.jpg',
  218. '/static/images/products/wheat-seeds.jpg'
  219. ]
  220. };
  221. if (imageMap[goodsId]) {
  222. this.goodsImages = imageMap[goodsId];
  223. }
  224. },
  225. // 预览图片
  226. previewImage(image, index) {
  227. // 获取所有图片的完整URL
  228. const urls = this.goodsDetail.detailImages.map(file => this.getImageUrl(file));
  229. console.log("urls",urls);
  230. uni.previewImage({
  231. current: index,
  232. urls: urls
  233. });
  234. },
  235. // 立即咨询
  236. handleConsult() {
  237. uni.showModal({
  238. title: '咨询服务',
  239. content: '您可以通过以下方式联系我们:\n1. 拨打客服热线:400-xxx-xxxx\n2. 在线客服(工作时间:9:00-18:00)',
  240. confirmText: '拨打电话',
  241. cancelText: '取消',
  242. success: (res) => {
  243. if (res.confirm) {
  244. uni.makePhoneCall({
  245. phoneNumber: '400-xxx-xxxx'
  246. });
  247. }
  248. }
  249. });
  250. }
  251. }
  252. }
  253. </script>
  254. <style lang="scss">
  255. .detail-container {
  256. min-height: 100vh;
  257. background-color: #f5f5f5;
  258. padding-bottom: 120rpx; // 为底部操作栏留出空间
  259. }
  260. .swiper-container {
  261. width: 100%;
  262. height: 600rpx;
  263. background-color: #fff;
  264. }
  265. .goods-swiper {
  266. width: 100%;
  267. height: 100%;
  268. }
  269. .swiper-image {
  270. width: 100%;
  271. height: 100%;
  272. }
  273. .goods-basic-info {
  274. background-color: #fff;
  275. padding: 30rpx;
  276. margin-bottom: 20rpx;
  277. }
  278. .goods-title {
  279. font-size: 36rpx;
  280. font-weight: bold;
  281. color: #333;
  282. line-height: 1.4;
  283. margin-bottom: 16rpx;
  284. }
  285. .goods-subtitle {
  286. font-size: 28rpx;
  287. color: #666;
  288. line-height: 1.5;
  289. margin-bottom: 24rpx;
  290. }
  291. .price-container {
  292. display: flex;
  293. align-items: baseline;
  294. margin-bottom: 24rpx;
  295. }
  296. .current-price {
  297. font-size: 40rpx;
  298. font-weight: bold;
  299. color: #4CAF50;
  300. }
  301. .price-unit {
  302. font-size: 28rpx;
  303. color: #999;
  304. margin-left: 8rpx;
  305. margin-right: 16rpx;
  306. }
  307. .original-price {
  308. font-size: 28rpx;
  309. color: #999;
  310. text-decoration: line-through;
  311. }
  312. .specs-info {
  313. display: flex;
  314. align-items: center;
  315. }
  316. .specs-label {
  317. font-size: 28rpx;
  318. color: #666;
  319. }
  320. .specs-value {
  321. font-size: 28rpx;
  322. color: #333;
  323. }
  324. .goods-detail-info {
  325. background-color: #fff;
  326. padding: 30rpx;
  327. }
  328. .detail-title {
  329. font-size: 32rpx;
  330. font-weight: bold;
  331. color: #333;
  332. margin-bottom: 30rpx;
  333. text-align: center;
  334. }
  335. .detail-section {
  336. margin-bottom: 40rpx;
  337. &:last-child {
  338. margin-bottom: 0;
  339. }
  340. }
  341. .section-title {
  342. font-size: 30rpx;
  343. font-weight: bold;
  344. color: #333;
  345. margin-bottom: 20rpx;
  346. padding-left: 20rpx;
  347. position: relative;
  348. &::before {
  349. content: '';
  350. position: absolute;
  351. left: 0;
  352. top: 50%;
  353. transform: translateY(-50%);
  354. width: 8rpx;
  355. height: 32rpx;
  356. background-color: #4CAF50;
  357. border-radius: 4rpx;
  358. }
  359. }
  360. .feature-list {
  361. .feature-item {
  362. display: flex;
  363. align-items: center;
  364. margin-bottom: 16rpx;
  365. .feature-icon {
  366. width: 32rpx;
  367. height: 32rpx;
  368. background-color: #4CAF50;
  369. color: #fff;
  370. border-radius: 50%;
  371. display: flex;
  372. align-items: center;
  373. justify-content: center;
  374. font-size: 20rpx;
  375. font-weight: bold;
  376. margin-right: 16rpx;
  377. flex-shrink: 0;
  378. }
  379. .feature-text {
  380. font-size: 28rpx;
  381. color: #666;
  382. line-height: 1.5;
  383. }
  384. }
  385. }
  386. .usage-content {
  387. font-size: 28rpx;
  388. color: #666;
  389. line-height: 1.6;
  390. padding: 20rpx;
  391. background-color: #f8f8f8;
  392. border-radius: 12rpx;
  393. }
  394. .detail-images {
  395. display: flex;
  396. flex-direction: column;
  397. gap: 20rpx;
  398. }
  399. .detail-image {
  400. width: 100%;
  401. border-radius: 12rpx;
  402. }
  403. .bottom-actions {
  404. position: fixed;
  405. bottom: 0;
  406. left: 0;
  407. right: 0;
  408. background-color: #fff;
  409. padding: 20rpx 30rpx;
  410. border-top: 1rpx solid #f0f0f0;
  411. z-index: 1000;
  412. }
  413. .action-btn {
  414. text-align: center;
  415. padding: 24rpx 0;
  416. border-radius: 12rpx;
  417. font-size: 32rpx;
  418. font-weight: bold;
  419. }
  420. .consult-btn {
  421. background-color: #4CAF50;
  422. color: #fff;
  423. }
  424. </style>