product.vue 15 KB

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