details.js 12 KB

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