product.vue 15 KB

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