index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. import {
  2. getCombinationDetail
  3. } from '../../../api/activity.js';
  4. import {
  5. postCartAdd,
  6. collectAdd,
  7. collectDel
  8. } from '../../../api/store.js';
  9. import wxh from '../../../utils/wxh.js';
  10. import wxParse from '../../../wxParse/wxParse.js';
  11. const app = getApp();
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. id: 0,
  18. parameter: {
  19. 'navbar': '1',
  20. 'return': '1',
  21. 'title': '商品详情'
  22. },
  23. userInfo: {},
  24. itemNew: [],
  25. indicatorDots: false,
  26. circular: true,
  27. autoplay: true,
  28. interval: 3000,
  29. duration: 500,
  30. attribute: {
  31. 'cartAttr': false
  32. },
  33. productSelect: [],
  34. productAttr: [],
  35. productValue: [],
  36. isOpen: false,
  37. attr: '请选择',
  38. attrValue: '',
  39. AllIndex: 2,
  40. replyChance: '',
  41. limitNum: 1,
  42. timeer: null,
  43. iSplus: false,
  44. navH: "",
  45. navList: ['商品', '评价', '详情'],
  46. opacity: 0,
  47. scrollY: 0,
  48. topArr: [],
  49. toView: '',
  50. height: 0,
  51. heightArr: [],
  52. lock: false,
  53. scrollTop: 0
  54. },
  55. returns: function () {
  56. wx.navigateBack();
  57. },
  58. tap: function (e) {
  59. var id = e.currentTarget.dataset.id;
  60. var index = e.currentTarget.dataset.index;
  61. var that = this;
  62. // if (!this.data.good_list.length && id == "past2") {
  63. // id = "past3"
  64. // }
  65. this.setData({
  66. toView: id,
  67. navActive: index,
  68. lock: true,
  69. scrollTop: index > 0 ? that.data.topArr[index] - (app.globalData.navHeight / 2) : that.data.topArr[index]
  70. });
  71. },
  72. scroll: function (e) {
  73. var that = this,
  74. scrollY = e.detail.scrollTop;
  75. var opacity = scrollY / 200;
  76. opacity = opacity > 1 ? 1 : opacity;
  77. that.setData({
  78. opacity: opacity,
  79. scrollY: scrollY
  80. })
  81. if (that.data.lock) {
  82. that.setData({
  83. lock: false
  84. })
  85. return;
  86. }
  87. for (var i = 0; i < that.data.topArr.length; i++) {
  88. if (scrollY < that.data.topArr[i] - (app.globalData.navHeight / 2) + that.data.heightArr[i]) {
  89. that.setData({
  90. navActive: i
  91. });
  92. break
  93. }
  94. }
  95. },
  96. /**
  97. * 生命周期函数--监听页面加载
  98. */
  99. onLoad: function (options) {
  100. var that = this;
  101. this.setData({
  102. navH: app.globalData.navHeight
  103. });
  104. //设置商品列表高度
  105. wx.getSystemInfo({
  106. success: function (res) {
  107. that.setData({
  108. height: res.windowHeight
  109. //res.windowHeight:获取整个窗口高度为px,*2为rpx;98为头部占据的高度;
  110. })
  111. },
  112. });
  113. if (options.hasOwnProperty('id')) {
  114. this.setData({
  115. id: options.id
  116. });
  117. app.globalData.openPages = '/pages/activity/goods_combination_details/index?id=' + this.data.id + '&spid=' + this.data.userInfo.uid;
  118. } else {
  119. app.Tips({
  120. title: '参数错误',
  121. icon: 'none',
  122. duration: 1000,
  123. mask: true,
  124. }, {
  125. tab: 3,
  126. url: 1
  127. });
  128. };
  129. },
  130. infoScroll: function () {
  131. var that = this,
  132. topArr = [],
  133. heightArr = [];
  134. for (var i = 0; i < that.data.navList.length; i++) { //productList
  135. //获取元素所在位置
  136. var query = wx.createSelectorQuery().in(this);
  137. var idView = "#past" + i;
  138. // if (!that.data.good_list.length && i == 2) {
  139. // var idView = "#past" + 3;
  140. // }
  141. query.select(idView).boundingClientRect();
  142. query.exec(function (res) {
  143. var top = res[0].top;
  144. var height = res[0].height;
  145. topArr.push(top);
  146. heightArr.push(height);
  147. that.setData({
  148. topArr: topArr,
  149. heightArr: heightArr
  150. });
  151. });
  152. };
  153. },
  154. /**
  155. * 默认选中属性
  156. */
  157. DefaultSelect: function () {
  158. var productAttr = this.data.productAttr,
  159. storeInfo = this.data.storeInfo,
  160. productValue = this.data.productValue,
  161. value = [];
  162. for (var key in productValue) {
  163. if (productValue[key].quota > 0 && productValue[key].product_stock > 0) {
  164. value = this.data.productAttr.length ? key.split(",") : [];
  165. break;
  166. }
  167. }
  168. for (var i = 0, len = productAttr.length; i < len; i++) {
  169. if (productAttr[i].attr_value[0]) productAttr[i].checked = value[i];
  170. }
  171. var productSelect = this.data.productValue[value.sort().join(',')];
  172. if (productSelect) {
  173. this.setData({
  174. ["productSelect.store_name"]: storeInfo.store_name,
  175. ["productSelect.image"]: productSelect.image,
  176. ["productSelect.price"]: productSelect.price,
  177. ["productSelect.quota"]: productSelect.quota,
  178. ["productSelect.stock"]: productSelect.stock,
  179. ["productSelect.quota_show"]: productSelect.quota_show,
  180. ["productSelect.product_stock"]: productSelect.product_stock,
  181. ['productSelect.unique']: productSelect.unique,
  182. ['productSelect.cart_num']: 1,
  183. attrValue: value,
  184. attr: '已选择'
  185. });
  186. } else {
  187. this.setData({
  188. ["productSelect.store_name"]: storeInfo.store_name,
  189. ["productSelect.image"]: storeInfo.image,
  190. ["productSelect.price"]: storeInfo.price,
  191. ["productSelect.quota_show"]: storeInfo.quota_show || 0,
  192. ["productSelect.quota"]: storeInfo.quota || 0,
  193. ["productSelect.product_stock"]: storeInfo.product_stock || 0,
  194. ['productSelect.unique']: '',
  195. ['productSelect.cart_num']: 1,
  196. attrValue: '',
  197. attr: '请选择'
  198. });
  199. }
  200. this.setData({
  201. productAttr: productAttr,
  202. ["productSelect.num"]: storeInfo.num,
  203. cart_num: 1
  204. });
  205. },
  206. selecAttr: function () {
  207. this.setData({
  208. 'attribute.cartAttr': true
  209. });
  210. },
  211. /**
  212. * 收藏商品
  213. */
  214. setCollect: function () {
  215. if (app.globalData.isLog === false) {
  216. this.setData({
  217. isAuto: true,
  218. isHidden: false,
  219. });
  220. } else {
  221. var that = this;
  222. if (this.data.storeInfo.userCollect) {
  223. collectDel(this.data.storeInfo.product_id).then(res => {
  224. that.setData({
  225. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  226. })
  227. })
  228. } else {
  229. collectAdd(this.data.storeInfo.product_id).then(res => {
  230. that.setData({
  231. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  232. })
  233. })
  234. }
  235. }
  236. },
  237. goProduct: function () {
  238. return app.Tips('/pages/goods_details/index?id=' + this.data.storeInfo.product_id);
  239. },
  240. combinationDetail: function () {
  241. var that = this;
  242. var data = that.data.id;
  243. getCombinationDetail(data).then(function (res) {
  244. that.setData({
  245. // ["parameter.title"]: res.data.storeInfo.title.substring(0,16),
  246. imgUrls: res.data.storeInfo.images,
  247. storeInfo: res.data.storeInfo,
  248. pink: res.data.pink,
  249. pindAll: res.data.pindAll,
  250. reply: res.data.reply ? [res.data.reply] : [],
  251. replyCount: res.data.replyCount,
  252. itemNew: res.data.pink_ok_list,
  253. pink_ok_sum: res.data.pink_ok_sum,
  254. replyChance: res.data.replyChance,
  255. productAttr: res.data.productAttr,
  256. productValue: res.data.productValue
  257. });
  258. that.setTime();
  259. wxParse.wxParse('description', 'html', that.data.storeInfo.description, that, 0);
  260. app.globalData.openPages = '/pages/activity/goods_combination_details/index?id=' + that.data.id + '&scene=' + that.data.userInfo.uid;
  261. that.setProductSelect();
  262. that.DefaultSelect();
  263. setTimeout(function () {
  264. that.infoScroll();
  265. }, 500);
  266. }).catch(function (err) {
  267. return app.Tips({
  268. title: err
  269. }, {
  270. tab: 3,
  271. url: 1
  272. });
  273. })
  274. },
  275. onMyEvent: function (e) {
  276. this.setData({
  277. 'attribute.cartAttr': e.detail.window,
  278. isOpen: false
  279. })
  280. },
  281. setTime: function () { //到期时间戳
  282. var that = this;
  283. var endTimeList = that.data.pink;
  284. var countDownArr = [];
  285. var timeer = setInterval(function () {
  286. var newTime = new Date().getTime() / 1000;
  287. for (var i in endTimeList) {
  288. var endTime = endTimeList[i].stop_time;
  289. var obj = [];
  290. if (endTime - newTime > 0) {
  291. var time = endTime - newTime;
  292. var day = parseInt(time / (60 * 60 * 24));
  293. var hou = parseInt(time % (60 * 60 * 24) / 3600);
  294. var min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
  295. var sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
  296. hou = parseInt(hou) + parseInt(day * 24);
  297. obj = {
  298. day: that.timeFormat(day),
  299. hou: that.timeFormat(hou),
  300. min: that.timeFormat(min),
  301. sec: that.timeFormat(sec)
  302. }
  303. } else {
  304. obj = {
  305. day: '00',
  306. hou: '00',
  307. min: '00',
  308. sec: '00'
  309. }
  310. }
  311. endTimeList[i].time = obj;
  312. }
  313. that.setData({
  314. pink: endTimeList
  315. })
  316. }, 1000);
  317. that.setData({
  318. timeer: timeer
  319. })
  320. },
  321. timeFormat(param) { //小于10的格式化函数
  322. return param < 10 ? '0' + param : param;
  323. },
  324. /**
  325. * 购物车手动填写
  326. */
  327. iptCartNum: function (e) {
  328. this.setData({
  329. ['productSelect.cart_num']: e.detail,
  330. cart_num: e.detail
  331. });
  332. },
  333. /**
  334. * 购物车数量加和数量减
  335. */
  336. ChangeCartNum: function (e) {
  337. //是否 加|减
  338. var changeValue = e.detail;
  339. //获取当前变动属性
  340. var productSelect = this.data.productValue[this.data.attrValue];
  341. //如果没有属性,赋值给商品默认库存
  342. if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
  343. //不存在不加数量
  344. if (productSelect === undefined) return;
  345. if (this.data.cart_num) {
  346. productSelect.cart_num = this.data.cart_num;
  347. }
  348. //提取库存
  349. var stock = productSelect.stock || 0;
  350. var quotaShow = productSelect.quota_show || 0;
  351. var quota = productSelect.quota || 0;
  352. var productStock = productSelect.product_stock || 0;
  353. var num = this.data.storeInfo.num || 0;
  354. //设置默认数据
  355. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  356. //数量+
  357. console.log(this.data.productSelect.cart_num);
  358. if (changeValue) {
  359. productSelect.cart_num++;
  360. //大于库存时,等于库存
  361. let arrMin = [];
  362. arrMin.push(num);
  363. arrMin.push(quota);
  364. arrMin.push(productStock);
  365. let minN = Math.min.apply(null, arrMin);
  366. if (productSelect.cart_num >= minN) productSelect.cart_num = minN ? minN : 1;
  367. // if (quotaShow >= productStock) {
  368. // if (productSelect.cart_num >= productStock) productSelect.cart_num = productStock;
  369. // } else {
  370. // if (productSelect.cart_num >= quotaShow) productSelect.cart_num = quotaShow;
  371. // }
  372. this.setData({
  373. ['productSelect.cart_num']: productSelect.cart_num,
  374. cart_num: productSelect.cart_num
  375. });
  376. } else {
  377. //数量减
  378. productSelect.cart_num--;
  379. //小于1时,等于1
  380. if (productSelect.cart_num < 1) productSelect.cart_num = 1;
  381. this.setData({
  382. ['productSelect.cart_num']: productSelect.cart_num,
  383. cart_num: productSelect.cart_num
  384. });
  385. }
  386. },
  387. /**
  388. * 属性变动赋值
  389. */
  390. ChangeAttr: function (e) {
  391. var values = e.detail;
  392. var productSelect = this.data.productValue[values];
  393. var storeInfo = this.data.storeInfo;
  394. this.setData({
  395. cart_num: 1,
  396. ["productSelect.num"]: storeInfo.num
  397. });
  398. if (productSelect) {
  399. this.setData({
  400. ["productSelect.image"]: productSelect.image,
  401. ["productSelect.price"]: productSelect.price,
  402. ["productSelect.quota"]: productSelect.quota,
  403. ["productSelect.stock"]: productSelect.stock,
  404. ["productSelect.quota_show"]: productSelect.quota_show,
  405. ["productSelect.product_stock"]: productSelect.product_stock,
  406. ['productSelect.unique']: productSelect.unique,
  407. ['productSelect.cart_num']: 1,
  408. ['productSelect.is_on']: true,
  409. attrValue: values,
  410. attr: '已选择'
  411. });
  412. } else {
  413. this.setData({
  414. ["productSelect.image"]: storeInfo.image,
  415. ["productSelect.price"]: storeInfo.price,
  416. ["productSelect.quota_show"]: 0,
  417. ["productSelect.quota"]: 0,
  418. ['productSelect.unique']: '',
  419. ['productSelect.cart_num']: 0,
  420. ['productSelect.is_on']: true,
  421. attrValue: '',
  422. attr: '请选择'
  423. });
  424. }
  425. },
  426. setProductSelect: function () {
  427. var that = this;
  428. if (that.data.productSelect.length == 0) {
  429. that.setData({
  430. ['productSelect.image']: that.data.storeInfo.image,
  431. ['productSelect.store_name']: that.data.storeInfo.title,
  432. ['productSelect.price']: that.data.storeInfo.price,
  433. ['productSelect.quota']: that.data.storeInfo.stock,
  434. ['productSelect.unique']: '',
  435. ["productSelect.num"]: that.data.storeInfo.num,
  436. ['productSelect.cart_num']: 1,
  437. ['productSelect.is_on']: true,
  438. cart_num: 1
  439. })
  440. }
  441. },
  442. /*
  443. * 下订单
  444. */
  445. goCat: function () {
  446. var that = this;
  447. var productSelect = this.data.productValue[this.data.attrValue];
  448. //打开属性
  449. if (this.data.isOpen)
  450. this.setData({
  451. 'attribute.cartAttr': true
  452. })
  453. else
  454. this.setData({
  455. 'attribute.cartAttr': !this.data.attribute.cartAttr
  456. });
  457. //只有关闭属性弹窗时进行加入购物车
  458. if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({
  459. isOpen: true
  460. });
  461. //如果有属性,没有选择,提示用户选择
  462. if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen == true) return app.Tips({
  463. title: '请选择属性'
  464. });
  465. var data = {
  466. productId: that.data.storeInfo.product_id,
  467. secKillId: 0,
  468. bargainId: 0,
  469. combinationId: that.data.id,
  470. cartNum: that.data.cart_num,
  471. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  472. is_new: 1,
  473. };
  474. postCartAdd(data).then(function (res) {
  475. that.setData({
  476. isOpen: false
  477. });
  478. wx.navigateTo({
  479. url: '/pages/order_confirm/index?cartId=' + res.data.cartId
  480. });
  481. }).catch(function (res) {
  482. return app.Tips({
  483. title: res
  484. });
  485. })
  486. },
  487. /**
  488. * 生命周期函数--监听页面初次渲染完成
  489. */
  490. onReady: function () {
  491. },
  492. onLoadFun: function (e) {
  493. this.setData({
  494. userInfo: e.detail
  495. });
  496. this.combinationDetail();
  497. },
  498. /**
  499. * 生命周期函数--监听页面显示
  500. */
  501. onShow: function () {
  502. if (this.data.isClose) this.combinationDetail();
  503. },
  504. /**
  505. * 生命周期函数--监听页面隐藏
  506. */
  507. onHide: function () {
  508. this.setData({
  509. isClose: 1
  510. });
  511. this.data.timeer !== null && clearInterval(this.data.timeer);
  512. },
  513. showAll: function () {
  514. if (this.data.AllIndex > this.data.pink.length)
  515. this.data.AllIndex = this.data.pink.length;
  516. else
  517. this.data.AllIndex += 2;
  518. this.setData({
  519. AllIndex: this.data.AllIndex
  520. });
  521. },
  522. hideAll: function () {
  523. this.setData({
  524. AllIndex: 2
  525. });
  526. },
  527. /**
  528. * 生命周期函数--监听页面卸载
  529. */
  530. onUnload: function () {
  531. this.data.timeer !== null && clearInterval(this.data.timeer);
  532. },
  533. /**
  534. * 页面相关事件处理函数--监听用户下拉动作
  535. */
  536. onPullDownRefresh: function () {
  537. },
  538. /**
  539. * 页面上拉触底事件的处理函数
  540. */
  541. onReachBottom: function () {
  542. },
  543. /**
  544. * 用户点击右上角分享
  545. */
  546. onShareAppMessage: function () {
  547. var that = this;
  548. return {
  549. title: that.data.storeInfo.title,
  550. path: app.globalData.openPages,
  551. imageUrl: that.data.storeInfo.image,
  552. success: function () {
  553. wx.showToast({
  554. title: '分享成功',
  555. icon: 'success',
  556. duration: 2000
  557. })
  558. }
  559. }
  560. }
  561. })