product.vue 16 KB

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