details.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. getOneSummary: [],
  13. webViewUrl: '',
  14. indicatorDots: true,
  15. vertical: false,
  16. autoplay: true,
  17. interval: 2000,
  18. duration: 500,
  19. animation_flag: false,
  20. isExplain: false,
  21. sec_index: 0,
  22. goodsNumber: 1,
  23. cartOrBuy: '',
  24. getgift: '',
  25. options_goods_id: '',
  26. countTime: 50, /// 测试倒计时
  27. basicStar: './../image/basic_star.png',
  28. activeStar: '../../image/star.png',
  29. halfStar: '../../image/half_star.png',
  30. starSrcs: [],
  31. comments: [],
  32. current: 0,
  33. carouselImgs: [],
  34. currentPrice: '', // 商品当前价格,
  35. promotionDays: ''
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad: function (options) {
  41. // let webViewUrl = `https://passport.lrlz.com/mobile/index.php?act=goods_common&op=detail&goods_id=${options.goods_id}&client_type=ios`
  42. // this.setData({
  43. // webViewUrl,
  44. // options_goods_id: options.goods_id
  45. // })
  46. setInterval(() => {
  47. this.setData({
  48. countTime: --this.data.countTime
  49. })
  50. }, 1000);
  51. // console.log(options.goods_id);
  52. this.getDatas(options.goods_id)
  53. // this.getDatas(7636)
  54. // this.getDatas(2) // 特殊商品
  55. // this.getDatas(260) // 特殊商品
  56. // this.getDatas(4807) // a.lrlz.com 有多条评论
  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. console.log(res);
  83. self.createStars(res.datas.common_info.comments_rate)
  84. if(res.datas.comments) {
  85. self.createComments(res.datas.comments)
  86. }
  87. let oneSummary = self.getOneSummary(res.datas.summary, goods_id)
  88. console.log('oneSummary:', oneSummary);
  89. let getgift = self.getgift(res.datas.summary, goods_id)
  90. let differencePrice = (oneSummary.goods_price - oneSummary.bonus_price).toFixed(2)
  91. oneSummary = Object.assign({}, { differencePrice }, oneSummary)
  92. let promotionDays = ''
  93. if(oneSummary['act_type'] == 2) {
  94. promotionDays = self.getPromotionDays(oneSummary['act_id'], res.datas.limitime)
  95. }
  96. self.setData({
  97. datas: res.datas,
  98. getOneSummary: oneSummary,
  99. imgUrls: res.datas.common_info.images,
  100. getgift,
  101. promotionDays
  102. })
  103. }
  104. else {
  105. console.log('error:', res.message);
  106. wx.showToast({
  107. icon: 'none',
  108. title: res.message,
  109. duration: 1500
  110. })
  111. }
  112. })
  113. },
  114. getOneSummary(sumarys, goods_id) {
  115. let getOneSummary = sumarys.filter((item, index) => {
  116. return item.goods_id == goods_id
  117. })
  118. return getOneSummary[0]
  119. },
  120. secSku(e) {
  121. let goodsId = e.currentTarget.dataset.goodsid
  122. let sec_index = e.currentTarget.dataset.secindex
  123. if (this.data.sec_index == sec_index) {
  124. return
  125. }
  126. let getOneSummary = this.getOneSummary(this.data.datas.summary, goodsId)
  127. let getgift = this.getgift(this.data.datas.summary, goodsId)
  128. if (getOneSummary.goods_storage < 1) {
  129. wx.showToast({
  130. title: '客官!暂时没有库存!',
  131. icon: 'none',
  132. duration: 1500
  133. })
  134. return
  135. }
  136. this.setData({
  137. sec_index,
  138. getOneSummary,
  139. goodsNumber: 1,
  140. getgift
  141. })
  142. },
  143. goodsNumHandle(e) {
  144. let type = e.currentTarget.dataset.type
  145. if (type == 'minus') {
  146. if (this.data.goodsNumber <= 1) {
  147. wx.showToast({
  148. title: '客官!不能再少了!',
  149. icon: 'none',
  150. duration: 1500
  151. })
  152. return
  153. }
  154. else {
  155. this.setData({
  156. goodsNumber: --this.data.goodsNumber
  157. })
  158. }
  159. }
  160. else {
  161. if (this.data.goodsNumber >= this.data.getOneSummary['goods_storage']) {
  162. wx.showToast({
  163. title: '客官!只有这么多了!',
  164. icon: 'none',
  165. duration: 1500
  166. })
  167. return
  168. }
  169. else {
  170. this.setData({
  171. goodsNumber: ++this.data.goodsNumber
  172. })
  173. }
  174. }
  175. },
  176. checkSubmit() {
  177. if (!app.globalData.userInfo) {
  178. wx.navigateTo({
  179. url: '/pages/login/login'
  180. });
  181. return;
  182. }
  183. let cartOrBuy = this.data.cartOrBuy
  184. this.setData({
  185. animation_flag: false
  186. })
  187. if (cartOrBuy == 'isCart') {
  188. wx.showLoading({
  189. title: '加载中',
  190. })
  191. getReq({
  192. act: 'cart',
  193. op: 'addex',
  194. quantity: this.data.goodsNumber,
  195. goods_id: this.data.getOneSummary.goods_id
  196. }, function (res) {
  197. wx.hideLoading()
  198. if (res.code == 200) {
  199. wx.showToast({
  200. icon: 'none',
  201. title: '添加成功!',
  202. duration: 1500
  203. })
  204. }
  205. else {
  206. wx.showToast({
  207. icon: 'none',
  208. title: res.message,
  209. duration: 1500
  210. })
  211. }
  212. })
  213. }
  214. else if (cartOrBuy == 'isBuy') {
  215. if (this.data.getOneSummary.goods_storage < 1) {
  216. wx.showToast({
  217. title: '客官!暂时没有库存!',
  218. icon: 'none',
  219. duration: 1500
  220. })
  221. return
  222. }
  223. wx.navigateTo({
  224. url: `/pages/confirmOrder/confirmOrder?goods_id=${this.data.getOneSummary.goods_id}&iscart=0&num=${this.data.goodsNumber}`
  225. })
  226. }
  227. else {
  228. return
  229. }
  230. },
  231. toWebView() {
  232. let webViewUrl = encodeURIComponent(this.data.webViewUrl)
  233. wx.navigateTo({
  234. url: `/pages/webView/webView?url=${webViewUrl}`
  235. })
  236. },
  237. animation_flag(e) {
  238. let cartOrBuy = e.currentTarget.dataset.cartorbuy || 'none'
  239. let flag = e.currentTarget.dataset.flag
  240. let animation_flag = flag == 'true' ? true : false
  241. this.setData({
  242. animation_flag,
  243. cartOrBuy,
  244. })
  245. },
  246. isExplain_flag(e) {
  247. let flag = e.currentTarget.dataset.flag
  248. let isExplain = flag == 'true' ? true : false
  249. this.setData({
  250. isExplain
  251. })
  252. },
  253. getgift(sumarys, goods_id) {
  254. let goods = this.getOneSummary(sumarys, goods_id)
  255. let giftSummary = ''
  256. if (goods.have_gift) {
  257. giftSummary = this.getGiftSummary(sumarys, goods.gifts)
  258. }
  259. return giftSummary
  260. },
  261. getGiftSummary(sumarys, gifts) {
  262. let arr = []
  263. gifts.map(item => {
  264. sumarys.filter(sum => {
  265. if (item.gift_goods_id == sum.goods_id) {
  266. arr.push({
  267. sum,
  268. gifts: item
  269. })
  270. }
  271. })
  272. })
  273. return arr
  274. },
  275. getPromotionDays(actId, limitime) {
  276. // console.log(this.data.datas);
  277. let limit = limitime.filter(item => {
  278. return item['xianshi_id'] == actId
  279. })
  280. let days = Math.floor((limit[0]['end_time'] * 1000 - new Date().getTime()) / (60 * 60 * 24 * 1000));
  281. return days;
  282. },
  283. createStars(rate) {
  284. let starSrcs = []
  285. for(let i = 0; i < 5; i++) {
  286. if(i < Math.floor(rate)) {
  287. starSrcs.push(this.data.activeStar)
  288. continue
  289. }
  290. if(i < Math.round(rate)) {
  291. starSrcs.push(this.data.halfStar)
  292. continue
  293. }
  294. starSrcs.push(this.data.basicStar)
  295. }
  296. this.setData({
  297. starSrcs
  298. })
  299. },
  300. createComments(basicComments) {
  301. let comments = []
  302. comments = basicComments.map(item => {
  303. let addtime = item.addtime || ''
  304. if (addtime) {
  305. let date = new Date(addtime * 1000);
  306. let year = date.getFullYear();
  307. let month = date.getMonth() + 1;
  308. let day = date.getDate();
  309. let transform_addtime = `${year}-${month}-${day}`
  310. item['transform_addtime'] = transform_addtime
  311. }
  312. return item
  313. })
  314. // console.log(comments);
  315. this.setData({
  316. comments
  317. })
  318. },
  319. allComments() {
  320. wx.navigateTo({
  321. url: `/pages/comments/comments?common_id=${this.data.datas.comment.common_id}`
  322. });
  323. return;
  324. },
  325. showCarousel(e) {
  326. let carouselImgs = e.currentTarget.dataset.images
  327. let current = e.currentTarget.dataset.current
  328. this.setData({
  329. show_carousel: true,
  330. carouselImgs,
  331. current
  332. })
  333. },
  334. hideCarousel() {
  335. this.setData({
  336. show_carousel: false
  337. })
  338. },
  339. /**
  340. * 生命周期函数--监听页面初次渲染完成
  341. */
  342. onReady: function () {
  343. },
  344. /**
  345. * 生命周期函数--监听页面显示
  346. */
  347. onShow: function () {
  348. if (app.globalData.fcodeErr) {
  349. wx.showToast({
  350. icon: 'none',
  351. title: app.globalData.fcodeErr,
  352. duration: 1500
  353. })
  354. app.globalData.fcodeErr = ''
  355. this.setData({
  356. animation_flag: false
  357. })
  358. setTimeout(() => {
  359. this.getDatas(this.data.options_goods_id)
  360. }, 1500)
  361. }
  362. if (app.globalData.backLogin) {
  363. this.getDatas(this.data.getOneSummary.goods_id);
  364. }
  365. },
  366. /**
  367. * 生命周期函数--监听页面隐藏
  368. */
  369. onHide: function () {
  370. },
  371. /**
  372. * 生命周期函数--监听页面卸载
  373. */
  374. onUnload: function () {
  375. },
  376. /**
  377. * 页面相关事件处理函数--监听用户下拉动作
  378. */
  379. onPullDownRefresh: function () {
  380. },
  381. /**
  382. * 页面上拉触底事件的处理函数
  383. */
  384. onReachBottom: function () {
  385. },
  386. /**
  387. * 用户点击右上角分享
  388. */
  389. onShareAppMessage: function () {
  390. let goods_id = this.data.options_goods_id
  391. let goods_name = this.data.getOneSummary.goods_mobile_name
  392. let imageUrl = this.data.imgUrls[0];
  393. return {
  394. title: `熊猫美妆为您推荐:${goods_name}`,
  395. path: `/pages/details/details?goods_id=${goods_id}`,
  396. imageUrl: imageUrl
  397. }
  398. }
  399. })