details.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // pages/details/details.js
  2. const getReq = require('./../../config.js').getReq;
  3. const app = getApp();
  4. import recordSource from '../../utils/recordSource';
  5. var WxParse = require('../../wxParse/wxParse.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. imgUrls: [],
  12. datas: {},
  13. goodsIdMap: '',
  14. bundleList: '', // 套餐
  15. getOneSummary: [],
  16. indicatorDots: true,
  17. vertical: false,
  18. autoplay: true,
  19. interval: 2000,
  20. duration: 500,
  21. animation_flag: false,
  22. isExplain: false,
  23. sec_index: 0,
  24. goodsNumber: 1,
  25. cartOrBuy: '',
  26. getgift: '',
  27. options_goods_id: '',
  28. countTime: 50, /// 测试倒计时
  29. basicStar: './../image/basic_star.png',
  30. activeStar: '../../image/star.png',
  31. halfStar: '../../image/half_star.png',
  32. starSrcs: [],
  33. comments: [],
  34. current: 0,
  35. carouselImgs: [],
  36. currentPrice: '', // 商品当前价格,
  37. promotionDays: '',
  38. bundleIndex: -1,
  39. swiperHeight: '',
  40. show: false,
  41. firstLoad: true,
  42. fromSource: ''
  43. },
  44. /**
  45. * 生命周期函数--监听页面加载
  46. */
  47. onLoad: function (options) {
  48. this.setData({
  49. options_goods_id: options.goods_id
  50. })
  51. this.getDatas(options.goods_id)
  52. var that = this;
  53. getReq({
  54. act: 'goods_common',
  55. op: 'detail',
  56. goods_id: options.goods_id
  57. }, function (res) {
  58. var str = res.split(/<[\/]?body>/gi)[1];
  59. WxParse.wxParse('article', 'html', str, that);
  60. });
  61. },
  62. getDatas(goods_id) {
  63. let fromSource = recordSource(app, `act=goods_common&op=index&goods_id=${goods_id}&client_type=mini`)
  64. wx.showLoading({
  65. title: '加载中',
  66. })
  67. var self = this
  68. getReq({
  69. act: 'goods_common',
  70. op: 'index',
  71. goods_id,
  72. from: fromSource
  73. }, function (res) {
  74. wx.hideLoading()
  75. if (res.code == 200) {
  76. self.createStars(res.datas.common_info.comments_rate)
  77. if (res.datas.comments) {
  78. self.createComments(res.datas.comments)
  79. }
  80. let goodsIdMap = ''
  81. if (!self.data.goodsIdMap) {
  82. goodsIdMap = new Map()
  83. res.datas.summary.map(item => {
  84. goodsIdMap.set(item['goods_id'], item)
  85. });
  86. }
  87. let oneSummary = self.getOneSummary(res.datas.summary, goods_id)
  88. let bundleList = ''
  89. if (oneSummary['have_bundle']) {
  90. let bundlingMap = new Map()
  91. res.datas['bundling'].map(item => {
  92. bundlingMap.set(item['bl_id'], item)
  93. })
  94. let goodsSumaryMap = goodsIdMap ? goodsIdMap : self.data.goodsIdMap
  95. bundleList = self.createBundles(oneSummary['bundles'], bundlingMap, goodsSumaryMap)
  96. }
  97. let getgift = self.getgift(res.datas.summary, goods_id)
  98. let differencePrice = (oneSummary.goods_price - oneSummary.bonus_price).toFixed(2)
  99. oneSummary = Object.assign({}, { differencePrice }, oneSummary)
  100. let promotionDays = ''
  101. if (oneSummary['act_type'] == 2) {
  102. promotionDays = self.getPromotionDays(oneSummary['act_id'], res.datas.limitime)
  103. }
  104. self.setData({
  105. datas: res.datas,
  106. getOneSummary: oneSummary,
  107. imgUrls: res.datas.common_info.images,
  108. getgift,
  109. promotionDays,
  110. goodsIdMap,
  111. bundleList,
  112. firstLoad: false,
  113. fromSource: app.globalData.fromSource
  114. })
  115. }
  116. else {
  117. wx.showToast({
  118. icon: 'none',
  119. title: res.message,
  120. duration: 1500
  121. })
  122. }
  123. })
  124. },
  125. getOneSummary(sumarys, goods_id) {
  126. let getOneSummary
  127. sumarys.find((item, index) => {
  128. if(item.goods_id == goods_id) {
  129. item['bonus_price'] = parseFloat(item['bonus_price'])
  130. item['goods_promotion_price'] = parseFloat(item['goods_promotion_price'])
  131. item['goods_price'] = parseFloat(item['goods_price'])
  132. getOneSummary = item
  133. return true
  134. }
  135. })
  136. return getOneSummary
  137. },
  138. secSku(e) {
  139. let goodsId = e.currentTarget.dataset.goodsid
  140. let sec_index = e.currentTarget.dataset.secindex
  141. if (this.data.sec_index == sec_index) {
  142. return
  143. }
  144. let getOneSummary = this.getOneSummary(this.data.datas.summary, goodsId)
  145. let getgift = this.getgift(this.data.datas.summary, goodsId)
  146. if (getOneSummary.goods_storage < 1) {
  147. wx.showToast({
  148. title: '客官!暂时没有库存!',
  149. icon: 'none',
  150. duration: 1500
  151. })
  152. return
  153. }
  154. this.setData({
  155. sec_index,
  156. getOneSummary,
  157. goodsNumber: 1,
  158. getgift
  159. })
  160. },
  161. createBundles(bundles, bundlingMap, goodsIdMap) {
  162. let bundlesList = []
  163. bundles.map(item => {
  164. if (bundlingMap.get(item)) {
  165. let bundlesSumary = []
  166. let bundling = bundlingMap.get(item)
  167. let goodsTotalPrice = 0
  168. bundling['goods'].map(good => {
  169. let resultGood = goodsIdMap.get(good['goods_id'])
  170. resultGood['bl_goods_price'] = good['bl_goods_price']
  171. goodsTotalPrice += parseFloat(resultGood['goods_price'])
  172. bundlesSumary.push(resultGood)
  173. })
  174. let reduce_price = goodsTotalPrice - bundling['bl_price']
  175. bundling['reduce_price'] = reduce_price
  176. bundlesList.push({
  177. bundling,
  178. bundlesSumary
  179. })
  180. }
  181. })
  182. return bundlesList
  183. },
  184. goodsNumHandle(e) {
  185. let type = e.currentTarget.dataset.type
  186. if (type == 'minus') {
  187. if (this.data.goodsNumber <= 1) {
  188. wx.showToast({
  189. title: '客官!不能再少了!',
  190. icon: 'none',
  191. duration: 1500
  192. })
  193. return
  194. }
  195. else {
  196. this.setData({
  197. goodsNumber: --this.data.goodsNumber
  198. })
  199. }
  200. }
  201. else {
  202. if (this.data.goodsNumber >= this.data.getOneSummary['goods_storage']) {
  203. wx.showToast({
  204. title: '客官!只有这么多了!',
  205. icon: 'none',
  206. duration: 1500
  207. })
  208. return
  209. }
  210. else {
  211. this.setData({
  212. goodsNumber: ++this.data.goodsNumber
  213. })
  214. }
  215. }
  216. },
  217. checkSubmit() {
  218. if (!app.globalData.userInfo) {
  219. wx.navigateTo({
  220. url: '/pages/login/login'
  221. });
  222. return;
  223. }
  224. let cartOrBuy = this.data.cartOrBuy
  225. this.setData({
  226. animation_flag: false
  227. })
  228. if (cartOrBuy == 'isCart') {
  229. wx.showLoading({
  230. title: '加载中',
  231. })
  232. getReq({
  233. act: 'cart',
  234. op: 'addex',
  235. quantity: this.data.goodsNumber,
  236. goods_id: this.data.getOneSummary.goods_id
  237. }, function (res) {
  238. wx.hideLoading()
  239. if (res.code == 200) {
  240. wx.showToast({
  241. icon: 'none',
  242. title: '添加成功!',
  243. duration: 1500
  244. })
  245. }
  246. else {
  247. wx.showToast({
  248. icon: 'none',
  249. title: res.message,
  250. duration: 1500
  251. })
  252. }
  253. })
  254. }
  255. else if (cartOrBuy == 'isBuy') {
  256. if (this.data.getOneSummary.is_fcode) {
  257. if (!this.data.getOneSummary.has_fcode) {
  258. wx.showToast({
  259. title: '您没有该商品F码,请领取F码后再购买.',
  260. icon: 'none',
  261. duration: 1500
  262. })
  263. return
  264. }
  265. }
  266. if (this.data.getOneSummary.goods_storage < 1) {
  267. wx.showToast({
  268. title: '客官!暂时没有库存!',
  269. icon: 'none',
  270. duration: 1500
  271. })
  272. return
  273. }
  274. wx.navigateTo({
  275. url: `/pages/confirmOrder/confirmOrder?goods_id=${this.data.getOneSummary.goods_id}&iscart=0&num=${this.data.goodsNumber}`
  276. })
  277. }
  278. else {
  279. return
  280. }
  281. },
  282. animation_flag(e) {
  283. let cartOrBuy = e.currentTarget.dataset.cartorbuy || 'none'
  284. let flag = e.currentTarget.dataset.flag
  285. let animation_flag = flag == 'true' ? true : false
  286. this.setData({
  287. animation_flag,
  288. cartOrBuy,
  289. })
  290. },
  291. isExplain_flag(e) {
  292. let flag = e.currentTarget.dataset.flag
  293. let isExplain = flag == 'true' ? true : false
  294. this.setData({
  295. isExplain
  296. })
  297. },
  298. getgift(sumarys, goods_id) {
  299. let goods = this.getOneSummary(sumarys, goods_id)
  300. let giftSummary = ''
  301. if (goods.have_gift) {
  302. giftSummary = this.getGiftSummary(sumarys, goods.gifts)
  303. }
  304. return giftSummary
  305. },
  306. getGiftSummary(sumarys, gifts) {
  307. let arr = []
  308. gifts.map(item => {
  309. sumarys.filter(sum => {
  310. if (item.gift_goods_id == sum.goods_id) {
  311. arr.push({
  312. sum,
  313. gifts: item
  314. })
  315. }
  316. })
  317. })
  318. return arr
  319. },
  320. getPromotionDays(actId, limitime) {
  321. let limit = limitime.filter(item => {
  322. return item['xianshi_id'] == actId
  323. })
  324. let days = Math.floor((limit[0]['end_time'] * 1000 - new Date().getTime()) / (60 * 60 * 24 * 1000));
  325. return days;
  326. },
  327. createStars(rate) {
  328. let starSrcs = []
  329. for (let i = 0; i < 5; i++) {
  330. if (i < Math.floor(rate)) {
  331. starSrcs.push(this.data.activeStar)
  332. continue
  333. }
  334. if (i < Math.round(rate)) {
  335. starSrcs.push(this.data.halfStar)
  336. continue
  337. }
  338. starSrcs.push(this.data.basicStar)
  339. }
  340. this.setData({
  341. starSrcs
  342. })
  343. },
  344. createComments(basicComments) {
  345. let comments = []
  346. comments = basicComments.map(item => {
  347. let addtime = item.addtime || ''
  348. if (addtime) {
  349. let date = new Date(addtime * 1000);
  350. let year = date.getFullYear();
  351. let month = date.getMonth() + 1;
  352. let day = date.getDate();
  353. let transform_addtime = `${year}-${month}-${day}`
  354. item['transform_addtime'] = transform_addtime
  355. }
  356. return item
  357. })
  358. this.setData({
  359. comments
  360. })
  361. },
  362. allComments() {
  363. wx.navigateTo({
  364. url: `/pages/comments/comments?common_id=${this.data.datas.comment.common_id}`
  365. });
  366. return;
  367. },
  368. showCarousel(e) {
  369. let carouselImgs = e.currentTarget.dataset.images
  370. let current = e.currentTarget.dataset.current
  371. this.setData({
  372. show_carousel: true,
  373. carouselImgs,
  374. current
  375. })
  376. },
  377. hideCarousel() {
  378. this.setData({
  379. show_carousel: false
  380. })
  381. },
  382. // 套装切换展开样式
  383. toggleBundle(e) {
  384. let { index } = e.currentTarget.dataset
  385. if (index == this.data.bundleIndex) {
  386. index = -1
  387. }
  388. this.setData({
  389. bundleIndex: index
  390. })
  391. },
  392. bundleDetail(e) {
  393. let { id } = e.currentTarget.dataset
  394. if (id == this.data.options_goods_id) return;
  395. wx.navigateTo({
  396. url: `/pages/details/details?goods_id=${id}&title=商品详情`
  397. })
  398. },
  399. addBundleCart(e) {
  400. if (!app.globalData.userInfo) {
  401. wx.navigateTo({
  402. url: '/pages/login/login'
  403. });
  404. return;
  405. }
  406. let bl_id = e.currentTarget.dataset.blid
  407. getReq({
  408. act: 'cart',
  409. op: 'addex',
  410. quantity: 1,
  411. bl_id
  412. }, function (res) {
  413. if (res.code == 200) {
  414. wx.showToast({
  415. icon: 'none',
  416. title: '添加成功!',
  417. duration: 1500
  418. })
  419. }
  420. else {
  421. wx.showToast({
  422. icon: 'none',
  423. title: res.message,
  424. duration: 1500
  425. })
  426. }
  427. })
  428. },
  429. swiperImgLoad(e) {
  430. if (this.data.swiperHeight) return;
  431. let swiperHeight = e.detail.height / e.detail.width * 750
  432. this.setData({
  433. swiperHeight,
  434. show: true
  435. })
  436. },
  437. /**
  438. * 生命周期函数--监听页面初次渲染完成
  439. */
  440. onReady: function () {
  441. },
  442. /**
  443. * 生命周期函数--监听页面显示
  444. */
  445. onShow: function () {
  446. if (app.globalData.backLogin && !this.data.firstLoad) {
  447. app.setFromSource(this.data.fromSource)
  448. this.getDatas(this.data.getOneSummary.goods_id);
  449. }
  450. },
  451. /**
  452. * 生命周期函数--监听页面隐藏
  453. */
  454. onHide: function () {
  455. },
  456. /**
  457. * 生命周期函数--监听页面卸载
  458. */
  459. onUnload: function () {
  460. },
  461. /**
  462. * 页面相关事件处理函数--监听用户下拉动作
  463. */
  464. onPullDownRefresh: function () {
  465. },
  466. /**
  467. * 页面上拉触底事件的处理函数
  468. */
  469. onReachBottom: function () {
  470. },
  471. /**
  472. * 用户点击右上角分享
  473. */
  474. onShareAppMessage: function () {
  475. let goods_id = this.data.options_goods_id
  476. let goods_name = this.data.getOneSummary.goods_mobile_name
  477. let imageUrl = this.data.imgUrls[0];
  478. return {
  479. title: `熊猫美妆为您推荐:${goods_name}`,
  480. path: `/pages/details/details?goods_id=${goods_id}`,
  481. imageUrl: imageUrl
  482. }
  483. }
  484. })