collect.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. type: 0,
  7. collectList: [],
  8. page: 1,
  9. limit: 10,
  10. totalPages: 1
  11. },
  12. getCollectList() {
  13. wx.showLoading({
  14. title: '加载中...',
  15. });
  16. let that = this;
  17. util.request(api.CollectList, {
  18. type: that.data.type,
  19. page: that.data.page,
  20. limit: that.data.limit
  21. }).then(function(res) {
  22. if (res.errno === 0) {
  23. that.setData({
  24. collectList: that.data.collectList.concat(res.data.list),
  25. totalPages: res.data.pages
  26. });
  27. }
  28. wx.hideLoading();
  29. });
  30. },
  31. onLoad: function(options) {
  32. this.getCollectList();
  33. },
  34. onReachBottom() {
  35. if (this.data.totalPages > this.data.page) {
  36. this.setData({
  37. page: this.data.page + 1
  38. });
  39. this.getCollectList();
  40. } else {
  41. wx.showToast({
  42. title: '没有更多用户收藏了',
  43. icon: 'none',
  44. duration: 2000
  45. });
  46. return false;
  47. }
  48. },
  49. onReady: function() {
  50. },
  51. onShow: function() {
  52. },
  53. onHide: function() {
  54. // 页面隐藏
  55. },
  56. onUnload: function() {
  57. // 页面关闭
  58. },
  59. openGoods(event) {
  60. let that = this;
  61. let index = event.currentTarget.dataset.index;
  62. let goodsId = this.data.collectList[index].valueId;
  63. //触摸时间距离页面打开的毫秒数
  64. var touchTime = that.data.touchEnd - that.data.touchStart;
  65. console.log(touchTime);
  66. //如果按下时间大于350为长按
  67. if (touchTime > 350) {
  68. wx.showModal({
  69. title: '',
  70. content: '确定删除吗?',
  71. success: function(res) {
  72. if (res.confirm) {
  73. util.request(api.CollectAddOrDelete, {
  74. type: that.data.type,
  75. valueId: goodsId
  76. }, 'POST').then(function(res) {
  77. if (res.errno === 0) {
  78. console.log(res.data);
  79. wx.showToast({
  80. title: '删除成功',
  81. icon: 'success',
  82. duration: 2000
  83. });
  84. that.data.collectList.splice(index, 1)
  85. that.setData({
  86. collectList: that.data.collectList
  87. });
  88. }
  89. });
  90. }
  91. }
  92. })
  93. } else {
  94. wx.navigateTo({
  95. url: '/pages/goods/goods?id=' + goodsId,
  96. });
  97. }
  98. },
  99. //按下事件开始
  100. touchStart: function(e) {
  101. let that = this;
  102. that.setData({
  103. touchStart: e.timeStamp
  104. })
  105. },
  106. //按下事件结束
  107. touchEnd: function(e) {
  108. let that = this;
  109. that.setData({
  110. touchEnd: e.timeStamp
  111. })
  112. },
  113. })