details.js 8.7 KB

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