index.js 15 KB

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