product.vue 16 KB

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