collect.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }).finally(() => {
  29. wx.hideLoading();
  30. });
  31. },
  32. switchTab: function(event) {
  33. let type = event.currentTarget.dataset.index;
  34. this.setData({
  35. collectList: [],
  36. type,
  37. page: 1,
  38. limit: 10,
  39. totalPages: 1
  40. });
  41. this.getCollectList();
  42. },
  43. onLoad: function(options) {
  44. this.getCollectList();
  45. },
  46. onReachBottom() {
  47. if (this.data.totalPages > this.data.page) {
  48. this.setData({
  49. page: this.data.page + 1
  50. });
  51. this.getCollectList();
  52. } else {
  53. wx.showToast({
  54. title: '没有更多用户收藏了',
  55. icon: 'none',
  56. duration: 2000
  57. });
  58. return false;
  59. }
  60. },
  61. onReady: function() {
  62. },
  63. onShow: function() {
  64. },
  65. onHide: function() {
  66. // 页面隐藏
  67. },
  68. onUnload: function() {
  69. // 页面关闭
  70. },
  71. openCollect(event) {
  72. let that = this;
  73. let index = event.currentTarget.dataset.index;
  74. let valueId = this.data.collectList[index].valueId;
  75. //触摸时间距离页面打开的毫秒数
  76. var touchTime = that.data.touchEnd - that.data.touchStart;
  77. //如果按下时间大于350为长按
  78. if (touchTime > 350) {
  79. wx.showModal({
  80. title: '',
  81. content: '确定删除吗?',
  82. success: function(res) {
  83. if (res.confirm) {
  84. util.request(api.CollectAddOrDelete, {
  85. type: that.data.type,
  86. valueId: valueId
  87. }, 'POST').then(function(res) {
  88. if (res.errno === 0) {
  89. wx.showToast({
  90. title: '删除成功',
  91. icon: 'success',
  92. duration: 2000
  93. });
  94. that.data.collectList.splice(index, 1)
  95. that.setData({
  96. collectList: that.data.collectList
  97. });
  98. }
  99. });
  100. }
  101. }
  102. })
  103. } else {
  104. var prefix = '/pages/goods/goods?id='
  105. if(this.data.type == 1){
  106. prefix = "/pages/topicDetail/topicDetail?id="
  107. }
  108. wx.navigateTo({
  109. url: prefix + valueId,
  110. });
  111. }
  112. },
  113. //按下事件开始
  114. touchStart: function(e) {
  115. let that = this;
  116. that.setData({
  117. touchStart: e.timeStamp
  118. })
  119. },
  120. //按下事件结束
  121. touchEnd: function(e) {
  122. let that = this;
  123. that.setData({
  124. touchEnd: e.timeStamp
  125. })
  126. },
  127. })