index.js 15 KB

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