details.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. app.navigateto('/pages/login/login')
  221. return;
  222. }
  223. let cartOrBuy = this.data.cartOrBuy
  224. this.setData({
  225. animation_flag: false
  226. })
  227. if (cartOrBuy == 'isCart') {
  228. wx.showLoading({
  229. title: '加载中',
  230. })
  231. getReq({
  232. act: 'cart',
  233. op: 'addex',
  234. quantity: this.data.goodsNumber,
  235. goods_id: this.data.getOneSummary.goods_id
  236. }, function (res) {
  237. wx.hideLoading()
  238. if (res.code == 200) {
  239. wx.showToast({
  240. icon: 'none',
  241. title: '添加成功!',
  242. duration: 1500
  243. })
  244. }
  245. else {
  246. wx.showToast({
  247. icon: 'none',
  248. title: res.message,
  249. duration: 1500
  250. })
  251. }
  252. })
  253. }
  254. else if (cartOrBuy == 'isBuy') {
  255. if (this.data.getOneSummary.is_fcode) {
  256. if (!this.data.getOneSummary.has_fcode) {
  257. wx.showToast({
  258. title: '您没有该商品F码,请领取F码后再购买.',
  259. icon: 'none',
  260. duration: 1500
  261. })
  262. return
  263. }
  264. }
  265. if (this.data.getOneSummary.goods_storage < 1) {
  266. wx.showToast({
  267. title: '客官!暂时没有库存!',
  268. icon: 'none',
  269. duration: 1500
  270. })
  271. return
  272. }
  273. app.navigateto(`/pages/confirmOrder/confirmOrder?goods_id=${this.data.getOneSummary.goods_id}&iscart=0&num=${this.data.goodsNumber}`)
  274. }
  275. else {
  276. return
  277. }
  278. },
  279. animation_flag(e) {
  280. let cartOrBuy = e.currentTarget.dataset.cartorbuy || 'none'
  281. let flag = e.currentTarget.dataset.flag
  282. let animation_flag = flag == 'true' ? true : false
  283. this.setData({
  284. animation_flag,
  285. cartOrBuy,
  286. })
  287. },
  288. isExplain_flag(e) {
  289. let flag = e.currentTarget.dataset.flag
  290. let isExplain = flag == 'true' ? true : false
  291. this.setData({
  292. isExplain
  293. })
  294. },
  295. getgift(sumarys, goods_id) {
  296. let goods = this.getOneSummary(sumarys, goods_id)
  297. let giftSummary = ''
  298. if (goods.have_gift) {
  299. giftSummary = this.getGiftSummary(sumarys, goods.gifts)
  300. }
  301. return giftSummary
  302. },
  303. getGiftSummary(sumarys, gifts) {
  304. let arr = []
  305. gifts.map(item => {
  306. sumarys.filter(sum => {
  307. if (item.gift_goods_id == sum.goods_id) {
  308. arr.push({
  309. sum,
  310. gifts: item
  311. })
  312. }
  313. })
  314. })
  315. return arr
  316. },
  317. getPromotionDays(actId, limitime) {
  318. let limit = limitime.filter(item => {
  319. return item['xianshi_id'] == actId
  320. })
  321. let days = Math.floor((limit[0]['end_time'] * 1000 - new Date().getTime()) / (60 * 60 * 24 * 1000));
  322. return days;
  323. },
  324. createStars(rate) {
  325. let starSrcs = []
  326. for (let i = 0; i < 5; i++) {
  327. if (i < Math.floor(rate)) {
  328. starSrcs.push(this.data.activeStar)
  329. continue
  330. }
  331. if (i < Math.round(rate)) {
  332. starSrcs.push(this.data.halfStar)
  333. continue
  334. }
  335. starSrcs.push(this.data.basicStar)
  336. }
  337. this.setData({
  338. starSrcs
  339. })
  340. },
  341. createComments(basicComments) {
  342. let comments = []
  343. comments = basicComments.map(item => {
  344. let addtime = item.addtime || ''
  345. if (addtime) {
  346. let date = new Date(addtime * 1000);
  347. let year = date.getFullYear();
  348. let month = date.getMonth() + 1;
  349. let day = date.getDate();
  350. let transform_addtime = `${year}-${month}-${day}`
  351. item['transform_addtime'] = transform_addtime
  352. }
  353. return item
  354. })
  355. this.setData({
  356. comments
  357. })
  358. },
  359. allComments() {
  360. app.navigateto(`/pages/comments/comments?common_id=${this.data.datas.comment.common_id}`)
  361. return;
  362. },
  363. showCarousel(e) {
  364. let carouselImgs = e.currentTarget.dataset.images
  365. let current = e.currentTarget.dataset.current
  366. this.setData({
  367. show_carousel: true,
  368. carouselImgs,
  369. current
  370. })
  371. },
  372. hideCarousel() {
  373. this.setData({
  374. show_carousel: false
  375. })
  376. },
  377. // 套装切换展开样式
  378. toggleBundle(e) {
  379. let { index } = e.currentTarget.dataset
  380. if (index == this.data.bundleIndex) {
  381. index = -1
  382. }
  383. this.setData({
  384. bundleIndex: index
  385. })
  386. },
  387. bundleDetail(e) {
  388. let { id } = e.currentTarget.dataset
  389. if (id == this.data.options_goods_id) return;
  390. app.navigateto(`/pages/details/details?goods_id=${id}&title=商品详情`)
  391. },
  392. addBundleCart(e) {
  393. if (!app.globalData.userInfo) {
  394. app.navigateto('/pages/login/login')
  395. return;
  396. }
  397. let bl_id = e.currentTarget.dataset.blid
  398. getReq({
  399. act: 'cart',
  400. op: 'addex',
  401. quantity: 1,
  402. bl_id
  403. }, function (res) {
  404. if (res.code == 200) {
  405. wx.showToast({
  406. icon: 'none',
  407. title: '添加成功!',
  408. duration: 1500
  409. })
  410. }
  411. else {
  412. wx.showToast({
  413. icon: 'none',
  414. title: res.message,
  415. duration: 1500
  416. })
  417. }
  418. })
  419. },
  420. swiperImgLoad(e) {
  421. if (this.data.swiperHeight) return;
  422. let swiperHeight = e.detail.height / e.detail.width * 750
  423. this.setData({
  424. swiperHeight,
  425. show: true
  426. })
  427. },
  428. /**
  429. * 生命周期函数--监听页面初次渲染完成
  430. */
  431. onReady: function () {
  432. },
  433. /**
  434. * 生命周期函数--监听页面显示
  435. */
  436. onShow: function () {
  437. if (app.globalData.backLogin && !this.data.firstLoad) {
  438. app.setFromSource(this.data.fromSource)
  439. this.getDatas(this.data.getOneSummary.goods_id);
  440. }
  441. },
  442. /**
  443. * 生命周期函数--监听页面隐藏
  444. */
  445. onHide: function () {
  446. },
  447. /**
  448. * 生命周期函数--监听页面卸载
  449. */
  450. onUnload: function () {
  451. },
  452. /**
  453. * 页面相关事件处理函数--监听用户下拉动作
  454. */
  455. onPullDownRefresh: function () {
  456. },
  457. /**
  458. * 页面上拉触底事件的处理函数
  459. */
  460. onReachBottom: function () {
  461. },
  462. /**
  463. * 用户点击右上角分享
  464. */
  465. onShareAppMessage: function () {
  466. let goods_id = this.data.options_goods_id
  467. let goods_name = this.data.getOneSummary.goods_mobile_name
  468. let imageUrl = this.data.imgUrls[0];
  469. return {
  470. title: `${app.globalData.channel}为您推荐:${goods_name}`,
  471. path: `/pages/details/details?goods_id=${goods_id}`,
  472. imageUrl: imageUrl
  473. }
  474. }
  475. })