product.vue 17 KB

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