details.js 13 KB

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