product.vue 14 KB

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