product.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. if(obj.userInfo) {
  284. if(obj.userInfo.is_real != 1) {
  285. uni.showModal({
  286. title: '提示',
  287. content: '您未进行进行实名认证,请先实名认证!',
  288. showCancel: false,
  289. })
  290. }
  291. return
  292. }
  293. cartAdd({
  294. cartNum: '1', //商品数量
  295. uniqueId: '', //商品标签
  296. new: 0, //商品是否新增加到购物车1为不加入0为加入
  297. mer_id: '',
  298. productId: item.id //商品编号
  299. })
  300. .then(function(e) {
  301. uni.showToast({
  302. title: '成功加入购物车',
  303. type: 'top',
  304. duration: 500,
  305. icon: 'none'
  306. });
  307. obj.goodsDetail();
  308. })
  309. .catch(e => {
  310. console.log(e);
  311. });
  312. },
  313. goodsDetail() {
  314. let obj = this;
  315. // 获取普通商品信息
  316. goodsDetail({}, this.goodsid).then(function({ data }) {
  317. obj.list = data;
  318. obj.good_list = data.good_list; //保存猜你喜欢列表
  319. obj.reply = data.reply; //保存评论列表
  320. let goods = data.storeInfo;
  321. obj.goodsObjact = goods;
  322. if (obj.goodsObjact.description != null) {
  323. obj.description = obj.goodsObjact.description.replace(/\<img/gi, '<img class="rich-img"');
  324. } //小程序商品详情图超出屏幕问题
  325. obj.imgList = goods.slider_image; //保存轮播图
  326. obj.specList = data.productAttr; //保存分类列表
  327. if (Array.isArray(data.productValue) != true) {
  328. obj.many = 2;
  329. obj.specList = data.productAttr; //保存产品属性
  330. obj.productValue = data.productValue; //保存属性值
  331. obj.specSelected = []; //初始化默认选择对象
  332. for (let i = 0; i < obj.specList.length; i++) {
  333. // 设置默认数据
  334. let attrValue = obj.specList[i].attr_value[0];
  335. attrValue.check = true;
  336. obj.specSelected.push(attrValue.attr);
  337. }
  338. let str = obj.specSelected.join(',');
  339. console.log(str, 'str');
  340. // 设置默认值
  341. obj.actionPrice = obj.productValue[str].price;
  342. obj.goodsNumberMax = obj.productValue[str].stock;
  343. obj.actionImage = obj.productValue[str].image;
  344. obj.uniqueId = obj.productValue[str].unique;
  345. obj.goodsStore = obj.productValue[str].quota;
  346. } else {
  347. obj.many = 1;
  348. }
  349. obj.productValue = data.productValue; //保存分类查询数据
  350. obj.actionPrice = goods.price; //保存默认选中商品价格
  351. obj.actionImage = goods.image_base; //保存默认选中商品图片
  352. obj.goodsNumberMax = goods.stock; //保存默认选中最大可购买商品数量
  353. obj.shopId = data.mer_id; //保存商店id
  354. });
  355. },
  356. // 立即购买
  357. buy() {
  358. let obj = this;
  359. // 创建传值对象
  360. if(obj.userInfo) {
  361. if(obj.userInfo.is_real != 1) {
  362. uni.showModal({
  363. title: '提示',
  364. content: '您未进行进行实名认证,请先实名认证!',
  365. showCancel: false,
  366. })
  367. }
  368. return
  369. }
  370. let data = {
  371. cartNum: obj.goodsNumber, //商品数量
  372. new: 1,
  373. productId: obj.goodsid,//商品编号
  374. uniqueId:obj.uniqueId
  375. };
  376. if (obj.type == 2) {
  377. data.new = 0;
  378. }
  379. cartAdd(data)
  380. .then(function(e) {
  381. let da = e.data;
  382. if (obj.type == 1) {
  383. // 跳转到支付页
  384. uni.navigateTo({
  385. url: '/pages/order/createOrder?id=' + da.cartId
  386. });
  387. }
  388. if (obj.type == 2) {
  389. uni.showToast({
  390. title: '成功加入购物车',
  391. type: 'top',
  392. duration: 2000,
  393. icon: 'none'
  394. });
  395. obj.goodsDetail();
  396. }
  397. obj.toggleSpec()
  398. })
  399. .catch(e => {
  400. console.log(e);
  401. });
  402. },
  403. // 阻止触发上级事件
  404. stopPrevent() {}
  405. }
  406. };
  407. </script>
  408. <style lang="scss">
  409. /* 弹出层 */
  410. .popup {
  411. position: fixed;
  412. left: 0;
  413. top: 0;
  414. right: 0;
  415. bottom: 0;
  416. z-index: 99;
  417. &.show {
  418. display: block;
  419. .mask {
  420. animation: showPopup 0.2s linear both;
  421. }
  422. .layer {
  423. animation: showLayer 0.2s linear both;
  424. }
  425. }
  426. &.hide {
  427. .mask {
  428. animation: hidePopup 0.2s linear both;
  429. }
  430. .layer {
  431. animation: hideLayer 0.2s linear both;
  432. }
  433. }
  434. &.none {
  435. display: none;
  436. }
  437. .mask {
  438. position: fixed;
  439. top: 0;
  440. width: 100%;
  441. height: 100%;
  442. z-index: 1;
  443. background-color: rgba(0, 0, 0, 0.4);
  444. }
  445. .layer {
  446. position: fixed;
  447. z-index: 99;
  448. bottom: 0;
  449. width: 100%;
  450. min-height: 35vh;
  451. border-radius: 10rpx 10rpx 0 0;
  452. background-color: #fff;
  453. .btn {
  454. height: 66rpx;
  455. line-height: 66rpx;
  456. border-radius: 100rpx;
  457. background: $bg-green-gradual;
  458. font-size: $font-base + 2rpx;
  459. color: #fff;
  460. margin: 30rpx auto 20rpx;
  461. }
  462. }
  463. @keyframes showPopup {
  464. 0% {
  465. opacity: 0;
  466. }
  467. 100% {
  468. opacity: 1;
  469. }
  470. }
  471. @keyframes hidePopup {
  472. 0% {
  473. opacity: 1;
  474. }
  475. 100% {
  476. opacity: 0;
  477. }
  478. }
  479. @keyframes showLayer {
  480. 0% {
  481. transform: translateY(120%);
  482. }
  483. 100% {
  484. transform: translateY(0%);
  485. }
  486. }
  487. @keyframes hideLayer {
  488. 0% {
  489. transform: translateY(0);
  490. }
  491. 100% {
  492. transform: translateY(120%);
  493. }
  494. }
  495. }
  496. /* 规格选择弹窗 */
  497. .attr-content {
  498. padding: 25rpx 30rpx;
  499. .a-t {
  500. display: flex;
  501. image {
  502. width: 170rpx;
  503. height: 170rpx;
  504. flex-shrink: 0;
  505. border-radius: 8rpx;
  506. }
  507. .right {
  508. display: flex;
  509. flex-direction: column;
  510. padding-left: 24rpx;
  511. font-size: $font-sm + 2rpx;
  512. color: $font-color-base;
  513. line-height: 42rpx;
  514. width: 75%;
  515. .price {
  516. font-size: $font-lg;
  517. color: $uni-color-primary;
  518. margin: 10rpx 0rpx;
  519. }
  520. .name {
  521. font-size: 32rpx;
  522. color: $font-color-dark;
  523. height: 50rpx;
  524. overflow: hidden;
  525. text-overflow: ellipsis;
  526. white-space: nowrap;
  527. display: block;
  528. }
  529. .selected-text {
  530. margin-right: 10rpx;
  531. }
  532. }
  533. }
  534. .attr-list {
  535. display: flex;
  536. flex-direction: column;
  537. font-size: $font-base + 2rpx;
  538. color: $font-color-base;
  539. padding-top: 30rpx;
  540. padding-left: 10rpx;
  541. }
  542. .item-list {
  543. padding: 20rpx 0 0;
  544. display: flex;
  545. flex-wrap: wrap;
  546. text {
  547. display: flex;
  548. align-items: center;
  549. justify-content: center;
  550. background: #eee;
  551. margin-right: 20rpx;
  552. margin-bottom: 20rpx;
  553. border-radius: 100rpx;
  554. min-width: 60rpx;
  555. height: 60rpx;
  556. padding: 0 20rpx;
  557. font-size: $font-base;
  558. color: $font-color-dark;
  559. }
  560. .selected {
  561. background: #ddffdf;
  562. color: $uni-color-primary;
  563. }
  564. }
  565. }
  566. //默认商品底部高度
  567. .goodsBottom {
  568. height: 160rpx;
  569. }
  570. page {
  571. background: #f0f0f0;
  572. }
  573. //秒杀、拼团底部高度
  574. .contentBottomHeight {
  575. height: 110rpx;
  576. }
  577. //默认商品底部高度
  578. .goodsBottom {
  579. height: 160rpx;
  580. }
  581. /deep/ .iconenter {
  582. font-size: $font-base + 2rpx;
  583. color: #888;
  584. }
  585. /deep/ .con_image {
  586. width: 130rpx;
  587. height: 130rpx;
  588. display: inline-block;
  589. padding: 15rpx;
  590. image {
  591. width: 100%;
  592. height: 100%;
  593. }
  594. }
  595. /* 商品详情中限制图片大小 */
  596. /deep/ .rich-img {
  597. width: 100% !important;
  598. height: auto;
  599. }
  600. </style>