index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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"]: 0,
  176. ["productSelect.quota"]: 0,
  177. ['productSelect.unique']: '',
  178. ['productSelect.cart_num']: this.data.productAttr.length ? 0 : 1,
  179. attrValue: '',
  180. attr: '请选择'
  181. });
  182. }
  183. this.setData({ productAttr: productAttr });
  184. },
  185. selecAttr: function () {
  186. this.setData({
  187. 'attribute.cartAttr': true
  188. });
  189. },
  190. /**
  191. * 收藏商品
  192. */
  193. setCollect: function () {
  194. if (app.globalData.isLog === false) {
  195. this.setData({
  196. isAuto: true,
  197. iShidden: false,
  198. });
  199. } else {
  200. var that = this;
  201. if (this.data.storeInfo.userCollect) {
  202. collectDel(this.data.storeInfo.product_id).then(res => {
  203. that.setData({
  204. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  205. })
  206. })
  207. } else {
  208. collectAdd(this.data.storeInfo.product_id).then(res => {
  209. that.setData({
  210. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  211. })
  212. })
  213. }
  214. }
  215. },
  216. goProduct:function(){
  217. return app.Tips('/pages/goods_details/index?id=' + this.data.storeInfo.product_id);
  218. },
  219. combinationDetail:function(){
  220. var that = this;
  221. var data = that.data.id;
  222. getCombinationDetail(data).then(function(res){
  223. that.setData({
  224. // ["parameter.title"]: res.data.storeInfo.title.substring(0,16),
  225. imgUrls: res.data.storeInfo.images,
  226. storeInfo: res.data.storeInfo,
  227. pink: res.data.pink,
  228. pindAll: res.data.pindAll,
  229. reply: res.data.reply ? [res.data.reply] : [],
  230. replyCount: res.data.replyCount,
  231. itemNew: res.data.pink_ok_list,
  232. pink_ok_sum: res.data.pink_ok_sum,
  233. replyChance: res.data.replyChance,
  234. productAttr: res.data.productAttr,
  235. productValue: res.data.productValue
  236. });
  237. that.setTime();
  238. wxParse.wxParse('description', 'html', that.data.storeInfo.description, that, 0);
  239. app.globalData.openPages = '/pages/activity/goods_combination_details/index?id=' + that.data.id + '&scene=' + that.data.userInfo.uid;
  240. that.setProductSelect();
  241. that.DefaultSelect();
  242. setTimeout(function () {
  243. that.infoScroll();
  244. }, 500);
  245. }).catch(function(err){
  246. return app.Tips({ title: err }, { tab: 3, url: 1 });
  247. })
  248. },
  249. onMyEvent: function (e) {
  250. this.setData({ 'attribute.cartAttr': e.detail.window, isOpen: false })
  251. },
  252. setTime: function () {//到期时间戳
  253. var that = this;
  254. var endTimeList = that.data.pink;
  255. var countDownArr = [];
  256. var timeer=setInterval(function(){
  257. var newTime = new Date().getTime() / 1000;
  258. for (var i in endTimeList) {
  259. var endTime = endTimeList[i].stop_time;
  260. var obj = [];
  261. if (endTime - newTime > 0) {
  262. var time = endTime - newTime;
  263. var day = parseInt(time / (60 * 60 * 24));
  264. var hou = parseInt(time % (60 * 60 * 24) / 3600);
  265. var min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
  266. var sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
  267. hou = parseInt(hou) + parseInt(day * 24);
  268. obj = {
  269. day: that.timeFormat(day),
  270. hou: that.timeFormat(hou),
  271. min: that.timeFormat(min),
  272. sec: that.timeFormat(sec)
  273. }
  274. } else {
  275. obj = {
  276. day: '00',
  277. hou: '00',
  278. min: '00',
  279. sec: '00'
  280. }
  281. }
  282. endTimeList[i].time = obj;
  283. }
  284. that.setData({
  285. pink: endTimeList
  286. })
  287. },1000);
  288. that.setData({
  289. timeer: timeer
  290. })
  291. },
  292. timeFormat(param) {//小于10的格式化函数
  293. return param < 10 ? '0' + param : param;
  294. },
  295. /**
  296. * 购物车手动填写
  297. *
  298. */
  299. iptCartNum: function (e) {
  300. this.setData({
  301. ['productSelect.cart_num']: e.detail,
  302. cart_num: e.detail
  303. });
  304. },
  305. /**
  306. * 购物车数量加和数量减
  307. *
  308. */
  309. ChangeCartNum: function (e) {
  310. //是否 加|减
  311. var changeValue = e.detail;
  312. //获取当前变动属性
  313. var productSelect = this.data.productValue[this.data.attrValue];
  314. if (this.data.cart_num) {
  315. productSelect.cart_num = this.data.cart_num;
  316. }
  317. //如果没有属性,赋值给商品默认库存
  318. if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
  319. //不存在不加数量
  320. if (productSelect === undefined) return;
  321. //提取库存
  322. var stock = productSelect.stock || 0;
  323. var quotaShow = productSelect.quota_show || 0;
  324. var productStock = productSelect.product_stock || 0;
  325. //设置默认数据
  326. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  327. //数量+
  328. console.log(this.data.productSelect.cart_num);
  329. if (changeValue) {
  330. productSelect.cart_num++;
  331. //大于库存时,等于库存
  332. if (quotaShow >= productStock) {
  333. if (productSelect.cart_num >= productStock) productSelect.cart_num = productStock;
  334. } else {
  335. if (productSelect.cart_num >= quotaShow) productSelect.cart_num = quotaShow;
  336. }
  337. this.setData({
  338. ['productSelect.cart_num']: productSelect.cart_num,
  339. cart_num: productSelect.cart_num
  340. });
  341. } else {
  342. //数量减
  343. productSelect.cart_num--;
  344. //小于1时,等于1
  345. if (productSelect.cart_num < 1) productSelect.cart_num = 1;
  346. this.setData({
  347. ['productSelect.cart_num']: productSelect.cart_num,
  348. cart_num: productSelect.cart_num
  349. });
  350. }
  351. },
  352. /**
  353. * 属性变动赋值
  354. *
  355. */
  356. ChangeAttr: function (e) {
  357. var values = e.detail;
  358. var productSelect = this.data.productValue[values];
  359. var storeInfo = this.data.storeInfo;
  360. this.setData({
  361. cart_num: 1
  362. });
  363. if (productSelect) {
  364. this.setData({
  365. ["productSelect.image"]: productSelect.image,
  366. ["productSelect.price"]: productSelect.price,
  367. ["productSelect.quota"]: productSelect.quota,
  368. ["productSelect.stock"]: productSelect.stock,
  369. ["productSelect.quota_show"]: productSelect.quota_show,
  370. ["productSelect.product_stock"]: productSelect.product_stock,
  371. ['productSelect.unique']: productSelect.unique,
  372. ['productSelect.cart_num']: 1,
  373. ['productSelect.is_on']: true,
  374. attrValue: values,
  375. attr: '已选择'
  376. });
  377. } else {
  378. this.setData({
  379. ["productSelect.image"]: storeInfo.image,
  380. ["productSelect.price"]: storeInfo.price,
  381. ["productSelect.quota_show"]: 0,
  382. ["productSelect.quota"]: 0,
  383. ['productSelect.unique']: '',
  384. ['productSelect.cart_num']: 0,
  385. ['productSelect.is_on']: true,
  386. attrValue: '',
  387. attr: '请选择'
  388. });
  389. }
  390. },
  391. setProductSelect: function () {
  392. var that = this;
  393. if (that.data.productSelect.length == 0) {
  394. that.setData({
  395. ['productSelect.image']: that.data.storeInfo.image,
  396. ['productSelect.store_name']: that.data.storeInfo.title,
  397. ['productSelect.price']: that.data.storeInfo.price,
  398. ['productSelect.quota']: that.data.storeInfo.stock,
  399. ['productSelect.unique']: '',
  400. ['productSelect.cart_num']: 1,
  401. ['productSelect.is_on']:true
  402. })
  403. }
  404. },
  405. /*
  406. * 下订单
  407. */
  408. goCat: function () {
  409. var that = this;
  410. var productSelect = this.data.productValue[this.data.attrValue];
  411. //打开属性
  412. if (this.data.isOpen)
  413. this.setData({ 'attribute.cartAttr': true })
  414. else
  415. this.setData({ 'attribute.cartAttr': !this.data.attribute.cartAttr });
  416. //只有关闭属性弹窗时进行加入购物车
  417. if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({ isOpen: true });
  418. //如果有属性,没有选择,提示用户选择
  419. if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen == true) return app.Tips({ title: '请选择属性' });
  420. var data = {
  421. productId: that.data.storeInfo.product_id,
  422. secKillId: 0,
  423. bargainId: 0,
  424. combinationId: that.data.id,
  425. cartNum: that.data.cart_num,
  426. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  427. is_new: 1,
  428. };
  429. postCartAdd(data).then(function (res) {
  430. that.setData({ isOpen: false });
  431. wx.navigateTo({ url: '/pages/order_confirm/index?cartId=' + res.data.cartId });
  432. }).catch(function(res){
  433. return app.Tips({ title: res });
  434. })
  435. },
  436. /**
  437. * 生命周期函数--监听页面初次渲染完成
  438. */
  439. onReady: function () {
  440. },
  441. onLoadFun:function(e){
  442. this.setData({ userInfo:e.detail });
  443. this.combinationDetail();
  444. },
  445. /**
  446. * 生命周期函数--监听页面显示
  447. */
  448. onShow: function () {
  449. if (this.data.isClose) this.combinationDetail();
  450. },
  451. /**
  452. * 生命周期函数--监听页面隐藏
  453. */
  454. onHide: function () {
  455. this.setData({ isClose: 1 });
  456. this.data.timeer !== null && clearInterval(this.data.timeer);
  457. },
  458. showAll:function(){
  459. if (this.data.AllIndex > this.data.pink.length)
  460. this.data.AllIndex = this.data.pink.length;
  461. else
  462. this.data.AllIndex+=2;
  463. this.setData({ AllIndex: this.data.AllIndex });
  464. },
  465. hideAll:function(){
  466. this.setData({ AllIndex: 2 });
  467. },
  468. /**
  469. * 生命周期函数--监听页面卸载
  470. */
  471. onUnload: function () {
  472. this.data.timeer !== null && clearInterval(this.data.timeer);
  473. },
  474. /**
  475. * 页面相关事件处理函数--监听用户下拉动作
  476. */
  477. onPullDownRefresh: function () {
  478. },
  479. /**
  480. * 页面上拉触底事件的处理函数
  481. */
  482. onReachBottom: function () {
  483. },
  484. /**
  485. * 用户点击右上角分享
  486. */
  487. onShareAppMessage: function () {
  488. var that = this;
  489. return {
  490. title: that.data.storeInfo.title,
  491. path: app.globalData.openPages,
  492. imageUrl: that.data.storeInfo.image,
  493. success: function () {
  494. wx.showToast({
  495. title: '分享成功',
  496. icon: 'success',
  497. duration: 2000
  498. })
  499. }
  500. }
  501. }
  502. })