product.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. isVip: '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. obj.userInfo = uni.getStorageSync('userInfo');
  167. //保存商品id
  168. this.goodsid = options.id;
  169. this.goodsType = options.type;
  170. // 判断有无人邀请
  171. if (options.spread) {
  172. // 存储邀请人
  173. uni.setStorageSync('spread', options.spread);
  174. }
  175. if (options.isVip) {
  176. obj.isVip = options.isVip
  177. }
  178. console.log(obj.isVip,'vip')
  179. saveUrl();
  180. this.goodsDetail();
  181. // 注册邀请信息
  182. // #ifdef H5
  183. let bool = uni.getStorageSync('weichatBrowser') || '';
  184. if (bool) {
  185. weixindata();
  186. }
  187. // #endif
  188. },
  189. computed: {
  190. ...mapState(['weichatObj', 'baseURL', 'urlFile']),
  191. },
  192. // 分享
  193. onShareAppMessage(options) {
  194. // 设置菜单中的转发按钮触发转发事件时的转发内容
  195. let pages = getCurrentPages(); //获取加载的页面
  196. let currentPage = pages[pages.length - 1]; //获取当前页面的对象
  197. let url = currentPage.route; //当前页面url
  198. let item = currentPage.options; //如果要获取url中所带的参数可以查看options
  199. let shareObj = {
  200. title: this.goodsObjact.store_name + ' 价格:' + this.goodsObjact.price, // 默认是小程序的名称(可以写slogan等)
  201. path: url + '?id=' + item.id + '&spread=' + this.userInfo.uid, // 默认是当前页面,必须是以‘/’开头的完整路径
  202. imageUrl: this.goodsObjact.image,
  203. success: function(res) {
  204. // 转发成功之后的回调
  205. if (res.errMsg == 'shareAppMessage:ok') {
  206. }
  207. },
  208. fail: function() {
  209. // 转发失败之后的回调
  210. if (res.errMsg == 'shareAppMessage:fail cancel') {
  211. // 用户取消转发
  212. } else if (res.errMsg == 'shareAppMessage:fail') {
  213. // 转发失败,其中 detail message 为详细失败信息
  214. }
  215. }
  216. };
  217. return shareObj;
  218. },
  219. methods: {
  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. cartAdd({
  289. cartNum: '1', //商品数量
  290. uniqueId: '', //商品标签
  291. new: 0, //商品是否新增加到购物车1为不加入0为加入
  292. mer_id: '',
  293. productId: item.id //商品编号
  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. console.log(obj.userInfo);
  355. if(obj.userInfo.lock_spread_user == null){
  356. if(obj.userInfo.unlock_spread_user == null){
  357. uni.showModal({
  358. title: '提示',
  359. content: '您未绑定或锁定邀请人,无法购买会员礼包,是否前往绑定关系',
  360. success: function (res) {
  361. if (res.confirm) {
  362. console.log('用户点击确定');
  363. uni.navigateTo({
  364. url:'/pages/set/user'
  365. })
  366. } else if (res.cancel) {
  367. console.log('用户点击取消');
  368. }
  369. }
  370. });
  371. return
  372. }else {
  373. uni.showModal({
  374. title: '提示',
  375. content: '已锁定邀请人:'+ obj.userInfo.unlock_spread_user.phone +'购买商品后将绑定关系',
  376. success: function (res) {
  377. if (res.confirm) {
  378. obj.buy1();
  379. } else if (res.cancel) {
  380. console.log('用户点击取消');
  381. }
  382. }
  383. });
  384. return
  385. }
  386. }
  387. // 创建传值对象
  388. let data = {
  389. cartNum: obj.goodsNumber, //商品数量
  390. new: 1,
  391. productId: obj.goodsid, //商品编号
  392. uniqueId: obj.uniqueId
  393. };
  394. if (obj.type == 2) {
  395. data.new = 0;
  396. }
  397. cartAdd(data)
  398. .then(function(e) {
  399. let da = e.data;
  400. if (obj.type == 1) {
  401. // 跳转到支付页
  402. uni.navigateTo({
  403. url: '/pages/order/createOrder?id=' + da.cartId
  404. });
  405. }
  406. if (obj.type == 2) {
  407. uni.showToast({
  408. title: '成功加入购物车',
  409. type: 'top',
  410. duration: 2000,
  411. icon: 'none'
  412. });
  413. obj.goodsDetail();
  414. }
  415. obj.toggleSpec();
  416. })
  417. .catch(e => {
  418. console.log(e);
  419. });
  420. },
  421. buy1(){
  422. let obj = this;
  423. let data = {
  424. cartNum: obj.goodsNumber, //商品数量
  425. new: 1,
  426. productId: obj.goodsid, //商品编号
  427. uniqueId: obj.uniqueId
  428. };
  429. if (obj.type == 2) {
  430. data.new = 0;
  431. }
  432. cartAdd(data)
  433. .then(function(e) {
  434. let da = e.data;
  435. if (obj.type == 1) {
  436. // 跳转到支付页
  437. uni.navigateTo({
  438. url: '/pages/order/createOrder?id=' + da.cartId
  439. });
  440. }
  441. if (obj.type == 2) {
  442. uni.showToast({
  443. title: '成功加入购物车',
  444. type: 'top',
  445. duration: 2000,
  446. icon: 'none'
  447. });
  448. obj.goodsDetail();
  449. }
  450. obj.toggleSpec();
  451. })
  452. .catch(e => {
  453. console.log(e);
  454. });
  455. },
  456. // 阻止触发上级事件
  457. stopPrevent() {}
  458. }
  459. };
  460. </script>
  461. <style lang="scss">
  462. /* 弹出层 */
  463. .popup {
  464. position: fixed;
  465. left: 0;
  466. top: 0;
  467. right: 0;
  468. bottom: 0;
  469. z-index: 99;
  470. &.show {
  471. display: block;
  472. .mask {
  473. animation: showPopup 0.2s linear both;
  474. }
  475. .layer {
  476. animation: showLayer 0.2s linear both;
  477. }
  478. }
  479. &.hide {
  480. .mask {
  481. animation: hidePopup 0.2s linear both;
  482. }
  483. .layer {
  484. animation: hideLayer 0.2s linear both;
  485. }
  486. }
  487. &.none {
  488. display: none;
  489. }
  490. .mask {
  491. position: fixed;
  492. top: 0;
  493. width: 100%;
  494. height: 100%;
  495. z-index: 1;
  496. background-color: rgba(0, 0, 0, 0.4);
  497. }
  498. .layer {
  499. position: fixed;
  500. z-index: 99;
  501. bottom: 0;
  502. width: 100%;
  503. min-height: 35vh;
  504. border-radius: 10rpx 10rpx 0 0;
  505. background-color: #fff;
  506. .btn {
  507. height: 66rpx;
  508. line-height: 66rpx;
  509. border-radius: 100rpx;
  510. background: $uni-color-primary;
  511. font-size: $font-base + 2rpx;
  512. color: #fff;
  513. margin: 30rpx auto 20rpx;
  514. }
  515. }
  516. @keyframes showPopup {
  517. 0% {
  518. opacity: 0;
  519. }
  520. 100% {
  521. opacity: 1;
  522. }
  523. }
  524. @keyframes hidePopup {
  525. 0% {
  526. opacity: 1;
  527. }
  528. 100% {
  529. opacity: 0;
  530. }
  531. }
  532. @keyframes showLayer {
  533. 0% {
  534. transform: translateY(120%);
  535. }
  536. 100% {
  537. transform: translateY(0%);
  538. }
  539. }
  540. @keyframes hideLayer {
  541. 0% {
  542. transform: translateY(0);
  543. }
  544. 100% {
  545. transform: translateY(120%);
  546. }
  547. }
  548. }
  549. /* 规格选择弹窗 */
  550. .attr-content {
  551. padding: 25rpx 30rpx;
  552. .a-t {
  553. display: flex;
  554. image {
  555. width: 170rpx;
  556. height: 170rpx;
  557. flex-shrink: 0;
  558. border-radius: 8rpx;
  559. }
  560. .right {
  561. display: flex;
  562. flex-direction: column;
  563. padding-left: 24rpx;
  564. font-size: $font-sm + 2rpx;
  565. color: $font-color-base;
  566. line-height: 42rpx;
  567. width: 75%;
  568. .price {
  569. font-size: $font-lg;
  570. color: $uni-color-primary;
  571. margin: 10rpx 0rpx;
  572. }
  573. .name {
  574. font-size: 32rpx;
  575. color: $font-color-dark;
  576. height: 50rpx;
  577. overflow: hidden;
  578. text-overflow: ellipsis;
  579. white-space: nowrap;
  580. display: block;
  581. }
  582. .selected-text {
  583. margin-right: 10rpx;
  584. }
  585. }
  586. }
  587. .attr-list {
  588. display: flex;
  589. flex-direction: column;
  590. font-size: $font-base + 2rpx;
  591. color: $font-color-base;
  592. padding-top: 30rpx;
  593. padding-left: 10rpx;
  594. }
  595. .item-list {
  596. padding: 20rpx 0 0;
  597. display: flex;
  598. flex-wrap: wrap;
  599. text {
  600. display: flex;
  601. align-items: center;
  602. justify-content: center;
  603. background: #eee;
  604. margin-right: 20rpx;
  605. margin-bottom: 20rpx;
  606. border-radius: 100rpx;
  607. min-width: 60rpx;
  608. height: 60rpx;
  609. padding: 0 20rpx;
  610. font-size: $font-base;
  611. color: $font-color-dark;
  612. }
  613. .selected {
  614. background: #ddffdf;
  615. color: $uni-color-primary;
  616. }
  617. }
  618. }
  619. //默认商品底部高度
  620. .goodsBottom {
  621. height: 160rpx;
  622. }
  623. page {
  624. background: #f0f0f0;
  625. }
  626. //秒杀、拼团底部高度
  627. .contentBottomHeight {
  628. height: 110rpx;
  629. }
  630. //默认商品底部高度
  631. .goodsBottom {
  632. height: 160rpx;
  633. }
  634. /deep/ .iconenter {
  635. font-size: $font-base + 2rpx;
  636. color: #888;
  637. }
  638. /deep/ .con_image {
  639. width: 130rpx;
  640. height: 130rpx;
  641. display: inline-block;
  642. padding: 15rpx;
  643. image {
  644. width: 100%;
  645. height: 100%;
  646. }
  647. }
  648. /* 商品详情中限制图片大小 */
  649. /deep/ .rich-img {
  650. width: 100% !important;
  651. height: auto;
  652. }
  653. </style>