details.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. webViewUrl: '',
  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. },
  42. /**
  43. * 生命周期函数--监听页面加载
  44. */
  45. onLoad: function (options) {
  46. let webViewUrl = `https://passport.lrlz.com/mobile/index.php?act=goods_common&op=detail&goods_id=${options.goods_id}&client_type=ios`
  47. this.setData({
  48. webViewUrl,
  49. options_goods_id: options.goods_id
  50. })
  51. setInterval(() => {
  52. this.setData({
  53. countTime: --this.data.countTime
  54. })
  55. }, 1000);
  56. this.getDatas(options.goods_id)
  57. var that = this;
  58. getReq({
  59. act: 'goods_common',
  60. op: 'detail',
  61. goods_id: options.goods_id
  62. // goods_id: 7636
  63. // goods_id: 4807
  64. // goods_id: 2
  65. }, function (res) {
  66. var str = res.split(/<[\/]?body>/gi)[1];
  67. WxParse.wxParse('article', 'html', str, that);
  68. });
  69. },
  70. getDatas(goods_id) {
  71. wx.showLoading({
  72. title: '加载中',
  73. })
  74. var self = this
  75. getReq({
  76. act: 'goods_common',
  77. op: 'index',
  78. goods_id
  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. console.log('oneSummary', oneSummary);
  95. let bundleList = ''
  96. if (oneSummary['have_bundle']) {
  97. let bundlingMap = new Map()
  98. res.datas['bundling'].map(item => {
  99. bundlingMap.set(item['bl_id'], item)
  100. })
  101. let goodsSumaryMap = goodsIdMap ? goodsIdMap : self.data.goodsIdMap
  102. bundleList = self.createBundles(oneSummary['bundles'], bundlingMap, goodsSumaryMap)
  103. }
  104. let getgift = self.getgift(res.datas.summary, goods_id)
  105. let differencePrice = (oneSummary.goods_price - oneSummary.bonus_price).toFixed(2)
  106. oneSummary = Object.assign({}, { differencePrice }, oneSummary)
  107. let promotionDays = ''
  108. if (oneSummary['act_type'] == 2) {
  109. promotionDays = self.getPromotionDays(oneSummary['act_id'], res.datas.limitime)
  110. }
  111. self.setData({
  112. datas: res.datas,
  113. getOneSummary: oneSummary,
  114. imgUrls: res.datas.common_info.images,
  115. getgift,
  116. promotionDays,
  117. goodsIdMap,
  118. bundleList
  119. })
  120. }
  121. else {
  122. console.log('error:', res.message);
  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 = sumarys.filter((item, index) => {
  133. return item.goods_id == goods_id
  134. })
  135. return getOneSummary[0]
  136. },
  137. secSku(e) {
  138. let goodsId = e.currentTarget.dataset.goodsid
  139. let sec_index = e.currentTarget.dataset.secindex
  140. if (this.data.sec_index == sec_index) {
  141. return
  142. }
  143. let getOneSummary = this.getOneSummary(this.data.datas.summary, goodsId)
  144. let getgift = this.getgift(this.data.datas.summary, goodsId)
  145. if (getOneSummary.goods_storage < 1) {
  146. wx.showToast({
  147. title: '客官!暂时没有库存!',
  148. icon: 'none',
  149. duration: 1500
  150. })
  151. return
  152. }
  153. this.setData({
  154. sec_index,
  155. getOneSummary,
  156. goodsNumber: 1,
  157. getgift
  158. })
  159. },
  160. // self.createBundles(oneSummary['bundles'],bundlingMap,goodsIdMap)
  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. toWebView() {
  283. let webViewUrl = encodeURIComponent(this.data.webViewUrl)
  284. wx.navigateTo({
  285. url: `/pages/webView/webView?url=${webViewUrl}`
  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. wx.hideLoading()
  420. if (res.code == 200) {
  421. wx.showToast({
  422. icon: 'none',
  423. title: '添加成功!',
  424. duration: 1500
  425. })
  426. }
  427. else {
  428. wx.showToast({
  429. icon: 'none',
  430. title: res.message,
  431. duration: 1500
  432. })
  433. }
  434. })
  435. },
  436. swiperImgLoad(e) {
  437. wx.hideLoading()
  438. if (this.data.swiperHeight) return;
  439. let swiperHeight = e.detail.height / e.detail.width * 750
  440. this.setData({
  441. swiperHeight,
  442. show: true
  443. })
  444. },
  445. /**
  446. * 生命周期函数--监听页面初次渲染完成
  447. */
  448. onReady: function () {
  449. },
  450. /**
  451. * 生命周期函数--监听页面显示
  452. */
  453. onShow: function () {
  454. if (app.globalData.fcodeErr) {
  455. wx.showToast({
  456. icon: 'none',
  457. title: app.globalData.fcodeErr,
  458. duration: 1500
  459. })
  460. app.globalData.fcodeErr = ''
  461. this.setData({
  462. animation_flag: false
  463. })
  464. setTimeout(() => {
  465. this.getDatas(this.data.options_goods_id)
  466. }, 1500)
  467. }
  468. if (app.globalData.backLogin) {
  469. this.getDatas(this.data.getOneSummary.goods_id);
  470. }
  471. },
  472. /**
  473. * 生命周期函数--监听页面隐藏
  474. */
  475. onHide: function () {
  476. },
  477. /**
  478. * 生命周期函数--监听页面卸载
  479. */
  480. onUnload: function () {
  481. },
  482. /**
  483. * 页面相关事件处理函数--监听用户下拉动作
  484. */
  485. onPullDownRefresh: function () {
  486. },
  487. /**
  488. * 页面上拉触底事件的处理函数
  489. */
  490. onReachBottom: function () {
  491. },
  492. /**
  493. * 用户点击右上角分享
  494. */
  495. onShareAppMessage: function () {
  496. let goods_id = this.data.options_goods_id
  497. let goods_name = this.data.getOneSummary.goods_mobile_name
  498. let imageUrl = this.data.imgUrls[0];
  499. return {
  500. title: `熊猫美妆为您推荐:${goods_name}`,
  501. path: `/pages/details/details?goods_id=${goods_id}`,
  502. imageUrl: imageUrl
  503. }
  504. }
  505. })