product.vue 17 KB

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