product.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <template>
  2. <view class="container">
  3. <!-- 轮播图 -->
  4. <top-swiper :imgList="imgList"></top-swiper>
  5. <!-- 标题 -->
  6. <product-content :goodsObjact="goodsObjact" :isgold="is_gold" :gold="gold"></product-content>
  7. <view class="c-list">
  8. <view class="c-row b-b" @click="toggleSpec">
  9. <text class="tit">购买数量</text>
  10. <view class="con">
  11. <text class="selected-text">{{ goodsNumber }}</text>
  12. </view>
  13. <text class="iconfont iconenter"></text>
  14. </view>
  15. </view>
  16. <!-- 评价 -->
  17. <estimate @navTo="navTo('/pages/product/reply?id=' + goodsid)" v-if="reply" :reply="reply" :list="list"></estimate>
  18. <!-- 图文详情 -->
  19. <content-text :description="description"></content-text>
  20. <!-- 底部高度撑开 -->
  21. <view class="contentBottomHeight"></view>
  22. <!-- 底部操作菜单 -->
  23. <product-bottom @buy="buy" :goodsObjact="goodsObjact" :goodsid="goodsid" @specOPne="specOPne" :is_gold="is_gold" :is_integral="is_integral"></product-bottom>
  24. <!-- 规格-模态层弹窗 -->
  25. <view class="popup spec" :class="specClass" @touchmove.stop.prevent="stopPrevent" @click="toggleSpec">
  26. <!-- 遮罩层 -->
  27. <view class="mask"></view>
  28. <view class="layer attr-content" @click.stop="stopPrevent">
  29. <view class="a-t">
  30. <image :src="actionImage"></image>
  31. <view class="right">
  32. <view class="good-name clamp">{{ goodsObjact.store_name }}</view>
  33. <text class="price">¥{{ actionPrice }}</text>
  34. <!-- <text class="stock">库存:{{ goodsObjact.stock }}件</text>
  35. <view class="selected" v-if="goodsType == 0">
  36. 已选:
  37. <text class="selected-text" v-for="(sItem, sIndex) in specSelected" :key="sIndex">{{ sItem }}</text>
  38. </view> -->
  39. </view>
  40. </view>
  41. <view v-for="(item, index) in specList" :key="index" class="attr-list">
  42. <text>{{ item.attr_name }}</text>
  43. <view class="item-list">
  44. <text
  45. v-for="(childItem, childIndex) in item.attr_value"
  46. :key="childIndex"
  47. class="tit"
  48. :class="{ selected: childItem.check }"
  49. @click="selectSpec(childItem, item, index)"
  50. >
  51. {{ childItem.attr }}
  52. </text>
  53. </view>
  54. </view>
  55. <view class="attr-list">
  56. <text>购买数量</text>
  57. <view class="item-list">
  58. <uni-number-box class="step" :isMin="true" :value="goodsNumber" :min="1" :max="goodsNumberMax" @eventChange="numberChange"></uni-number-box>
  59. </view>
  60. </view>
  61. <button class="btn" @click.stop="buy">完成</button>
  62. </view>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. import uniNumberBox from '@/components/uni-number-box.vue';
  68. import { goodsDetail, cartAdd } from '@/api/product.js';
  69. import { mapState } from 'vuex';
  70. import store from '@/store/index.js';
  71. import { saveUrl } from '@/utils/loginUtils.js';
  72. // #ifdef H5
  73. import { weixindata } from '@/utils/wxAuthorized';
  74. // #endif
  75. // 头部轮播图
  76. import topSwiper from './common/topSwiper.vue';
  77. // 标题
  78. import productContent from './common/productContent.vue';
  79. // 到货时间及优惠
  80. import discounts from './common/discounts.vue';
  81. // 规格信息
  82. import freshDetail from './common/freshDetail.vue';
  83. // 图文详情
  84. import contentText from './common/contentText.vue';
  85. // 底部按钮
  86. import productBottom from './common/productBottom.vue';
  87. // 猜你喜欢
  88. import guessLike from './common/guessLike.vue';
  89. // 评价
  90. import estimate from './common/estimate.vue';
  91. export default {
  92. components: {
  93. uniNumberBox,
  94. guessLike,
  95. topSwiper,
  96. productContent,
  97. discounts,
  98. freshDetail,
  99. contentText,
  100. productBottom,
  101. estimate
  102. },
  103. data() {
  104. return {
  105. goodsStore: 0, //选中库存
  106. specList: [],
  107. buys_show: true,
  108. buys_shows: false,
  109. specSelected: [], //选中的分类
  110. specClass: 'none', //显示隐藏弹窗
  111. many: 1, //1是单规格 2是多规格
  112. reply: '', //评论
  113. list: '', //商品详情的数据
  114. type: 1, //默认支付方式add为
  115. is_gold: 0,//是否是金豆商品
  116. is_integral:0,//是否是金券商品
  117. goodsType: 0,
  118. goodsNumber: 1, //购买数量
  119. goodsid: '', //商品id
  120. description: '', //商品描述
  121. goodsObjact: {
  122. percent: 1
  123. }, //保存商品数据
  124. //图片循环
  125. imgList: [],
  126. // 对比对象
  127. actionPrice: 0, //默认选中商品价格
  128. actionImage: '', //默认选中图片
  129. good_list: '', //猜你喜欢列表
  130. goodsNumberMax: 0, //最大可购买数量
  131. // 倒计时数据保存
  132. seckillObj: {
  133. stopTime: 0, //结束时间
  134. stop: false, //是否结束
  135. stopTimeH: 0, //小时
  136. stopTimeM: 0, //分钟
  137. stopTimeS: 0, //秒钟
  138. stopTimeD: 0, //天
  139. upTime: 0 //更新组件内部组件用
  140. },
  141. // 拼团数据保存
  142. pink: {
  143. id: '', //拼团编号
  144. uid: '', //用户编号
  145. people: '', //拼团人数
  146. price: '', //拼团价格
  147. stop_time: '', //拼团结束时间
  148. nickname: '', //团长昵称
  149. avatar: '', //团长头像
  150. count: '', //拼团剩余人数
  151. h: '', //时
  152. i: '', //分
  153. s: '' //秒
  154. },
  155. userInfo: ''
  156. };
  157. },
  158. filters: {
  159. parseIntTo(percent) {
  160. percent = +percent * 100;
  161. if (percent % 1 === 0) {
  162. return percent;
  163. } else {
  164. percent = percent.toFixed(1);
  165. return percent;
  166. }
  167. }
  168. },
  169. async onLoad(options) {
  170. let obj = this;
  171. obj.userInfo = uni.getStorageSync('userInfo');
  172. //保存商品id
  173. this.goodsid = options.id;
  174. this.goodsType = options.type;
  175. // 判断是否是金券商品
  176. if (options.is_integral){
  177. obj.is_integral = options.is_integral
  178. }
  179. // 判断是否是金豆商品
  180. if (options.is_gold){
  181. obj.is_gold = options.is_gold
  182. }
  183. // 判断有无人邀请
  184. if (options.spread) {
  185. // 存储邀请人
  186. uni.setStorageSync('spread', options.spread);
  187. }
  188. saveUrl();
  189. this.goodsDetail();
  190. // 注册邀请信息
  191. // #ifdef H5
  192. let bool = uni.getStorageSync('weichatBrowser') || '';
  193. if (bool) {
  194. weixindata();
  195. }
  196. // #endif
  197. },
  198. computed: {
  199. ...mapState(['weichatObj', 'baseURL', 'urlFile'])
  200. },
  201. // 分享
  202. onShareAppMessage(options) {
  203. // 设置菜单中的转发按钮触发转发事件时的转发内容
  204. let pages = getCurrentPages(); //获取加载的页面
  205. let currentPage = pages[pages.length - 1]; //获取当前页面的对象
  206. let url = currentPage.route; //当前页面url
  207. let item = currentPage.options; //如果要获取url中所带的参数可以查看options
  208. let shareObj = {
  209. title: this.goodsObjact.store_name + ' 价格:' + this.goodsObjact.price, // 默认是小程序的名称(可以写slogan等)
  210. path: url + '?id=' + item.id + '&spread=' + this.userInfo.uid, // 默认是当前页面,必须是以‘/’开头的完整路径
  211. imageUrl: this.goodsObjact.image,
  212. success: function(res) {
  213. // 转发成功之后的回调
  214. if (res.errMsg == 'shareAppMessage:ok') {
  215. }
  216. },
  217. fail: function() {
  218. // 转发失败之后的回调
  219. if (res.errMsg == 'shareAppMessage:fail cancel') {
  220. // 用户取消转发
  221. } else if (res.errMsg == 'shareAppMessage:fail') {
  222. // 转发失败,其中 detail message 为详细失败信息
  223. }
  224. }
  225. };
  226. return shareObj;
  227. },
  228. methods: {
  229. //选择规格
  230. selectSpec(item, arr, ind) {
  231. arr.attr_value.forEach(function(e) {
  232. e.check = false;
  233. });
  234. item.check = true;
  235. let obj = this;
  236. obj.specSelected[ind] = item.attr;
  237. let str = obj.specSelected.join(',');
  238. // 获取当前选中的对象
  239. if (obj.productValue[str]) {
  240. obj.buys_show = true;
  241. obj.buys_shows = false;
  242. obj.actionPrice = obj.productValue[str].price;
  243. obj.goodsNumberMax = obj.productValue[str].stock;
  244. obj.actionImage = obj.productValue[str].image;
  245. obj.uniqueId = obj.productValue[str].unique;
  246. obj.goodsStore = obj.productValue[str].stock;
  247. } else {
  248. (obj.buys_show = false), (obj.buys_shows = true);
  249. }
  250. if (obj.goodsStore == 0) {
  251. obj.buys_show = false;
  252. obj.buys_shows = true;
  253. }
  254. obj.specSelected[ind] = item.attr;
  255. },
  256. // 打開彈窗
  257. specOPne(type = 1) {
  258. let obj = this;
  259. obj.specClass = 'show';
  260. obj.type = type;
  261. },
  262. // 购买数量变化
  263. numberChange(e) {
  264. this.goodsNumber = e.number;
  265. },
  266. //规格弹窗开关
  267. toggleSpec(str) {
  268. if (this.specClass === 'show') {
  269. this.specClass = 'hide';
  270. setTimeout(() => {
  271. this.specClass = 'none';
  272. }, 250);
  273. } else if (this.specClass === 'none') {
  274. this.specClass = 'show';
  275. }
  276. // 保存当前购买类型
  277. this.type = str;
  278. },
  279. //领取优惠券
  280. Getcoupon() {
  281. uni.navigateTo({
  282. url: '/pages/coupon/getcoupon'
  283. });
  284. },
  285. //详情页
  286. navToDetailPage(item) {
  287. let id = item.id;
  288. uni.navigateTo({
  289. url: '/pages/product/product?id=' + id
  290. });
  291. },
  292. // 跳转页面
  293. navTo(url) {
  294. uni.navigateTo({
  295. url: '/pages/product/reply?id=' + this.goodsid
  296. });
  297. },
  298. goodsDetail() {
  299. let obj = this;
  300. // 获取普通商品信息
  301. goodsDetail({}, this.goodsid).then(function({ data }) {
  302. obj.list = data;
  303. obj.good_list = data.good_list; //保存猜你喜欢列表
  304. obj.reply = data.reply; //保存评论列表
  305. let goods = data.storeInfo;
  306. obj.goodsObjact = goods;
  307. if (obj.goodsObjact.description != null) {
  308. obj.description = obj.goodsObjact.description.replace(/\<img/gi, '<img class="rich-img"');
  309. } //小程序商品详情图超出屏幕问题
  310. obj.imgList = goods.slider_image; //保存轮播图
  311. obj.specList = data.productAttr; //保存分类列表
  312. obj.productValue = data.productValue; //保存分类查询数据
  313. obj.actionPrice = goods.price; //保存默认选中商品价格
  314. obj.actionImage = goods.image_base; //保存默认选中商品价格
  315. obj.goodsNumberMax = goods.stock; //保存默认选中最大可购买商品数量
  316. obj.shopId = data.mer_id; //保存商店id
  317. // 保存默认选中的对象
  318. obj.specSelected = []; //初始化默认选择对象
  319. for (let i = 0; i < obj.specList.length; i++) {
  320. // 设置默认数据
  321. let attrValue = obj.specList[i].attr_value[0];
  322. attrValue.check = true;
  323. obj.specSelected.push(attrValue.attr);
  324. }
  325. //保存默认选中的对象字符串名称
  326. let str = obj.specSelected.join(',');
  327. // 设置默认值
  328. // obj.actionImage = obj.productValue[str].image;
  329. // obj.uniqueId = obj.productValue[str].unique;
  330. // #ifdef H5
  331. // obj.shareDate();
  332. // #endif
  333. });
  334. },
  335. // 立即购买
  336. buy() {
  337. let obj = this;
  338. // 创建传值对象
  339. let data = {
  340. is_integral:obj.is_integral,//是否是金券商品
  341. cartNum: obj.goodsNumber, //商品数量
  342. new: 1,
  343. productId: obj.goodsid, //商品编号
  344. uniqueId: obj.uniqueId,
  345. is_gold: obj.is_gold,//是否是金豆商品
  346. };
  347. if (obj.type == 2) {
  348. data.new = 0;
  349. }
  350. cartAdd(data)
  351. .then(function(e) {
  352. let da = e.data;
  353. if (obj.type == 1) {
  354. // 跳转到支付页
  355. uni.navigateTo({
  356. url: '/pages/order/createOrder?id=' + da.cartId + '&is_gold=' + obj.is_gold + '&is_integral=' + obj.is_integral
  357. });
  358. }
  359. if (obj.type == 2) {
  360. uni.showToast({
  361. title: '成功加入购物车',
  362. type: 'top',
  363. duration: 2000,
  364. icon: 'none'
  365. });
  366. obj.goodsDetail();
  367. }
  368. obj.toggleSpec();
  369. })
  370. .catch(e => {
  371. console.log(e);
  372. });
  373. },
  374. // 阻止触发上级事件
  375. stopPrevent() {}
  376. }
  377. };
  378. </script>
  379. <style lang="scss">
  380. /* 弹出层 */
  381. .popup {
  382. position: fixed;
  383. left: 0;
  384. top: 0;
  385. right: 0;
  386. bottom: 0;
  387. z-index: 99;
  388. &.show {
  389. display: block;
  390. .mask {
  391. animation: showPopup 0.2s linear both;
  392. }
  393. .layer {
  394. animation: showLayer 0.2s linear both;
  395. }
  396. }
  397. &.hide {
  398. .mask {
  399. animation: hidePopup 0.2s linear both;
  400. }
  401. .layer {
  402. animation: hideLayer 0.2s linear both;
  403. }
  404. }
  405. &.none {
  406. display: none;
  407. }
  408. .mask {
  409. position: fixed;
  410. top: 0;
  411. width: 100%;
  412. height: 100%;
  413. z-index: 1;
  414. background-color: rgba(0, 0, 0, 0.4);
  415. }
  416. .layer {
  417. position: fixed;
  418. z-index: 99;
  419. bottom: 0;
  420. width: 100%;
  421. min-height: 35vh;
  422. border-radius: 10rpx 10rpx 0 0;
  423. background-color: #fff;
  424. .btn {
  425. height: 66rpx;
  426. line-height: 66rpx;
  427. border-radius: 100rpx;
  428. background: $uni-color-primary;
  429. font-size: $font-base + 2rpx;
  430. color: #fff;
  431. margin: 30rpx auto 20rpx;
  432. }
  433. }
  434. @keyframes showPopup {
  435. 0% {
  436. opacity: 0;
  437. }
  438. 100% {
  439. opacity: 1;
  440. }
  441. }
  442. @keyframes hidePopup {
  443. 0% {
  444. opacity: 1;
  445. }
  446. 100% {
  447. opacity: 0;
  448. }
  449. }
  450. @keyframes showLayer {
  451. 0% {
  452. transform: translateY(120%);
  453. }
  454. 100% {
  455. transform: translateY(0%);
  456. }
  457. }
  458. @keyframes hideLayer {
  459. 0% {
  460. transform: translateY(0);
  461. }
  462. 100% {
  463. transform: translateY(120%);
  464. }
  465. }
  466. }
  467. /* 规格选择弹窗 */
  468. .attr-content {
  469. padding: 10rpx 30rpx;
  470. .a-t {
  471. display: flex;
  472. image {
  473. width: 170rpx;
  474. height: 170rpx;
  475. flex-shrink: 0;
  476. border-radius: 8rpx;
  477. }
  478. .right {
  479. display: flex;
  480. flex-direction: column;
  481. padding-left: 24rpx;
  482. font-size: $font-sm + 2rpx;
  483. color: $font-color-base;
  484. line-height: 42rpx;
  485. .good-name {
  486. padding-top: 20rpx;
  487. max-width: 320rpx;
  488. font-size: 30rpx;
  489. font-family: PingFang SC;
  490. font-weight: bold;
  491. color: #1d2023;
  492. line-height: 42rpx;
  493. margin-bottom: 15rpx;
  494. }
  495. .price {
  496. font-size: 60rpx;
  497. font-family: PingFang SC;
  498. font-weight: bold;
  499. color: #ff6f0f;
  500. // font-size: $font-lg;
  501. // color: $uni-color-primary;
  502. // margin-bottom: 10rpx;
  503. }
  504. .selected-text {
  505. margin-right: 10rpx;
  506. }
  507. }
  508. }
  509. .attr-list {
  510. display: flex;
  511. flex-direction: column;
  512. font-size: $font-base + 2rpx;
  513. color: $font-color-base;
  514. padding-top: 30rpx;
  515. padding-left: 10rpx;
  516. }
  517. .item-list {
  518. padding: 20rpx 0 0;
  519. display: flex;
  520. flex-wrap: wrap;
  521. .tit {
  522. display: flex;
  523. align-items: center;
  524. justify-content: center;
  525. background: #eee;
  526. // margin-left: 10rpx;
  527. margin-right: 20rpx;
  528. margin-bottom: 20rpx;
  529. border-radius: 100rpx;
  530. min-width: 60rpx;
  531. height: 60rpx;
  532. padding: 0 20rpx;
  533. font-size: $font-base;
  534. color: $font-color-dark;
  535. }
  536. .selected {
  537. background: #fbebee;
  538. color: $uni-color-primary;
  539. }
  540. }
  541. }
  542. .c-list {
  543. margin-top: 20rpx;
  544. font-size: $font-sm + 2rpx;
  545. color: $font-color-base;
  546. background: #fff;
  547. .c-row {
  548. display: flex;
  549. align-items: center;
  550. padding: 20rpx 30rpx;
  551. position: relative;
  552. }
  553. .tit {
  554. width: 140rpx;
  555. }
  556. .con {
  557. flex: 1;
  558. color: $font-color-dark;
  559. .selected-text {
  560. margin-right: 10rpx;
  561. }
  562. }
  563. .bz-list {
  564. height: 40rpx;
  565. font-size: $font-sm + 2rpx;
  566. color: $font-color-dark;
  567. text {
  568. display: inline-block;
  569. margin-right: 30rpx;
  570. }
  571. }
  572. .con-list {
  573. flex: 1;
  574. display: flex;
  575. flex-direction: column;
  576. color: $font-color-dark;
  577. line-height: 40rpx;
  578. }
  579. .red {
  580. color: $uni-color-primary;
  581. }
  582. }
  583. //默认商品底部高度
  584. .goodsBottom {
  585. height: 160rpx;
  586. }
  587. page {
  588. background: #f0f0f0;
  589. }
  590. //秒杀、拼团底部高度
  591. .contentBottomHeight {
  592. height: 110rpx;
  593. }
  594. //默认商品底部高度
  595. .goodsBottom {
  596. height: 160rpx;
  597. }
  598. /deep/ .iconenter {
  599. font-size: $font-base + 2rpx;
  600. color: #888;
  601. }
  602. /deep/ .con_image {
  603. width: 130rpx;
  604. height: 130rpx;
  605. display: inline-block;
  606. padding: 15rpx;
  607. image {
  608. width: 100%;
  609. height: 100%;
  610. }
  611. }
  612. /* 商品详情中限制图片大小 */
  613. /deep/ .rich-img {
  614. width: 100% !important;
  615. height: auto;
  616. }
  617. </style>