product.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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" :seckillObj="seckillObj"></product-bottom>
  21. </view>
  22. </template>
  23. <script>
  24. import { goodsDetail, cartAdd } from '@/api/product.js';
  25. import { mapState } from 'vuex';
  26. import store from '@/store/index.js';
  27. import { saveUrl } from '@/utils/loginUtils.js';
  28. // #ifdef H5
  29. import { weixindata } from '@/utils/wxAuthorized';
  30. // #endif
  31. // 头部轮播图
  32. import topSwiper from './common/topSwiper.vue';
  33. // 标题
  34. import productContent from './common/productContent.vue';
  35. // 到货时间及优惠
  36. import discounts from './common/discounts.vue';
  37. // 规格信息
  38. import freshDetail from './common/freshDetail.vue';
  39. // 图文详情
  40. import contentText from './common/contentText.vue';
  41. // 底部按钮
  42. import productBottom from './common/productBottom.vue';
  43. // 猜你喜欢
  44. import guessLike from './common/guessLike.vue';
  45. // 评价
  46. import estimate from './common/estimate.vue';
  47. export default {
  48. components: {
  49. guessLike,
  50. topSwiper,
  51. productContent,
  52. discounts,
  53. freshDetail,
  54. contentText,
  55. productBottom,
  56. estimate
  57. },
  58. data() {
  59. return {
  60. reply: '', //评论
  61. list: '', //商品详情的数据
  62. type: 1, //默认支付方式add为
  63. goodsNumber: 1, //购买数量
  64. goodsid: '', //商品id
  65. description: '', //商品描述
  66. goodsObjact: {
  67. percent:1
  68. }, //保存商品数据
  69. //图片循环
  70. imgList: [],
  71. // 对比对象
  72. actionPrice: 0, //默认选中商品价格
  73. actionImage: '', //默认选中图片
  74. good_list: '', //猜你喜欢列表
  75. goodsNumberMax: 0, //最大可购买数量
  76. // 倒计时数据保存
  77. seckillObj: {
  78. stopTime: 0, //结束时间
  79. stop: false, //是否结束
  80. stopTimeH: 0, //小时
  81. stopTimeM: 0, //分钟
  82. stopTimeS: 0, //秒钟
  83. stopTimeD: 0, //天
  84. upTime: 0 //更新组件内部组件用
  85. },
  86. // 拼团数据保存
  87. pink: {
  88. id: '', //拼团编号
  89. uid: '', //用户编号
  90. people: '', //拼团人数
  91. price: '', //拼团价格
  92. stop_time: '', //拼团结束时间
  93. nickname: '', //团长昵称
  94. avatar: '', //团长头像
  95. count: '', //拼团剩余人数
  96. h: '', //时
  97. i: '', //分
  98. s: '' //秒
  99. },
  100. userInfo: ''
  101. };
  102. },
  103. filters: {
  104. parseIntTo(percent) {
  105. percent = +percent * 100;
  106. if (percent % 1 === 0) {
  107. return percent;
  108. } else {
  109. percent = percent.toFixed(1);
  110. return percent;
  111. }
  112. }
  113. },
  114. async onLoad(options) {
  115. let obj = this;
  116. obj.userInfo = uni.getStorageSync('userInfo');
  117. //保存商品id
  118. this.goodsid = options.id;
  119. // 判断有无人邀请
  120. if (options.spread) {
  121. // 存储邀请人
  122. uni.setStorageSync('spread', options.spread);
  123. }
  124. saveUrl();
  125. // 判断是否为秒杀商品
  126. this.goodsDetail();
  127. // 注册邀请信息
  128. // #ifdef H5
  129. let bool = uni.getStorageSync('weichatBrowser') || '';
  130. if (bool) {
  131. weixindata();
  132. }
  133. // #endif
  134. },
  135. computed: {
  136. ...mapState(['weichatObj', 'baseURL', 'urlFile'])
  137. },
  138. // 分享
  139. onShareAppMessage(options) {
  140. // 设置菜单中的转发按钮触发转发事件时的转发内容
  141. let pages = getCurrentPages(); //获取加载的页面
  142. let currentPage = pages[pages.length - 1]; //获取当前页面的对象
  143. let url = currentPage.route; //当前页面url
  144. let item = currentPage.options; //如果要获取url中所带的参数可以查看options
  145. let shareObj = {
  146. title: this.goodsObjact.store_name + ' 价格:' + this.goodsObjact.price, // 默认是小程序的名称(可以写slogan等)
  147. path: url + '?id=' + item.id + '&spread=' + this.userInfo.uid, // 默认是当前页面,必须是以‘/’开头的完整路径
  148. imageUrl: this.goodsObjact.image,
  149. success: function(res) {
  150. // 转发成功之后的回调
  151. if (res.errMsg == 'shareAppMessage:ok') {
  152. }
  153. },
  154. fail: function() {
  155. // 转发失败之后的回调
  156. if (res.errMsg == 'shareAppMessage:fail cancel') {
  157. // 用户取消转发
  158. } else if (res.errMsg == 'shareAppMessage:fail') {
  159. // 转发失败,其中 detail message 为详细失败信息
  160. }
  161. }
  162. };
  163. return shareObj;
  164. },
  165. methods: {
  166. //领取优惠券
  167. Getcoupon() {
  168. uni.navigateTo({
  169. url: '/pages/coupon/getcoupon'
  170. });
  171. },
  172. //详情页
  173. navToDetailPage(item) {
  174. let id = item.id;
  175. uni.navigateTo({
  176. url: '/pages/product/product?id=' + id
  177. });
  178. },
  179. // 跳转页面
  180. navTo(url) {
  181. uni.navigateTo({
  182. url
  183. });
  184. },
  185. //加入购物车
  186. Addcar(item) {
  187. let obj = this;
  188. cartAdd({
  189. cartNum: '1', //商品数量
  190. uniqueId: '', //商品标签
  191. new: 0, //商品是否新增加到购物车1为不加入0为加入
  192. mer_id: '',
  193. productId: item.id //商品编号
  194. })
  195. .then(function(e) {
  196. uni.showToast({
  197. title: '成功加入购物车',
  198. type: 'top',
  199. duration: 500,
  200. icon: 'none'
  201. });
  202. obj.goodsDetail();
  203. })
  204. .catch(e => {
  205. console.log(e);
  206. });
  207. },
  208. // 获取普通商品信息
  209. goodsDetail() {
  210. let obj = this;
  211. goodsDetail({}, this.goodsid).then(function({ data }) {
  212. obj.list = data;
  213. obj.good_list = data.good_list; //保存猜你喜欢列表
  214. obj.reply = data.reply; //保存评论列表
  215. let goods = data.storeInfo;
  216. obj.goodsObjact = goods;
  217. if (obj.goodsObjact.description != null) {
  218. obj.description = obj.goodsObjact.description.replace(/\<img/gi, '<img class="rich-img"');
  219. } //小程序商品详情图超出屏幕问题
  220. obj.imgList = goods.slider_image; //保存轮播图
  221. obj.specList = data.productAttr; //保存分类列表
  222. obj.productValue = data.productValue; //保存分类查询数据
  223. obj.actionPrice = goods.price; //保存默认选中商品价格
  224. obj.actionImage = goods.image_base; //保存默认选中商品图片
  225. obj.goodsNumberMax = goods.stock; //保存默认选中最大可购买商品数量
  226. obj.shopId = data.mer_id; //保存商店id
  227. });
  228. },
  229. // 立即购买
  230. buy(item) {
  231. let obj = this;
  232. obj.type = item;
  233. // 创建传值对象
  234. let data = {
  235. cartNum: obj.goodsNumber, //商品数量
  236. new: 1,
  237. productId: obj.goodsid
  238. };
  239. if(obj.type == 2) {
  240. data.new = 0;
  241. }
  242. cartAdd(data)
  243. .then(function(e) {
  244. let da = e.data;
  245. if (obj.type == 1) {
  246. // 跳转到支付页
  247. uni.navigateTo({
  248. url: '/pages/order/createOrder?id=' + da.cartId
  249. });
  250. }
  251. if (obj.type == 2) {
  252. uni.showToast({
  253. title: '成功加入购物车',
  254. type: 'top',
  255. duration: 2000,
  256. icon: 'none'
  257. });
  258. obj.goodsDetail();
  259. }
  260. })
  261. .catch(e => {
  262. console.log(e);
  263. });
  264. },
  265. // 阻止触发上级事件
  266. stopPrevent() {}
  267. }
  268. };
  269. </script>
  270. <style lang="scss">
  271. //默认商品底部高度
  272. .goodsBottom {
  273. height: 160rpx;
  274. }
  275. page {
  276. background: #f0f0f0;
  277. }
  278. //秒杀、拼团底部高度
  279. .contentBottomHeight {
  280. height: 110rpx;
  281. }
  282. //默认商品底部高度
  283. .goodsBottom {
  284. height: 160rpx;
  285. }
  286. /deep/ .iconenter {
  287. font-size: $font-base + 2rpx;
  288. color: #888;
  289. }
  290. /deep/ .con_image {
  291. width: 130rpx;
  292. height: 130rpx;
  293. display: inline-block;
  294. padding: 15rpx;
  295. image {
  296. width: 100%;
  297. height: 100%;
  298. }
  299. }
  300. /* 商品详情中限制图片大小 */
  301. /deep/ .rich-img {
  302. width: 100% !important;
  303. height: auto;
  304. }
  305. </style>