details.js 12 KB

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