details.js 12 KB

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