product.vue 16 KB

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