product.vue 15 KB

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