details.js 13 KB

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