collect.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const util = require('../../utils/util')
  2. const api = require('../../config/api')
  3. Page({
  4. data: {
  5. loading: false,
  6. isEdit: false, // 是否显示选择按钮
  7. selectList: [], // 已选中收藏id
  8. collectList: [],
  9. page: 1,
  10. totalPages: 1
  11. },
  12. onShow: function() {
  13. this.getCollectList()
  14. },
  15. pullDownRefresh() {
  16. this.setData({ page: 1, loading: true })
  17. this.getCollectList()
  18. this.setData({ loading: false })
  19. },
  20. // 加载更多
  21. scrolltolower() {
  22. if (this.data.totalPages > this.data.page) {
  23. this.setData({ page: this.data.page + 1 })
  24. this.getCollectList()
  25. } else util.showToast('已经到底了')
  26. },
  27. getCollectList() {
  28. util.showLoad('加载中')
  29. util.request(api.CollectList, { page: this.data.page, type: 0 }).then(res => {
  30. util.hideLoad()
  31. if (res.errno === 0) {
  32. const list = res.data.collectList.map(item => {
  33. item.picUrl = item.picUrl.split(',')
  34. return item
  35. })
  36. const collectList = this.data.page == 1 ? list : this.data.collectList.concat(list)
  37. this.setData({ collectList, totalPages: res.data.totalPages })
  38. } else util.showErrorToast(res.errmsg)
  39. }).catch(() => {
  40. util.hideLoad()
  41. util.showErrorToast('网络连接失败')
  42. })
  43. },
  44. // 编辑
  45. toggleEdit() {
  46. this.setData({ isEdit: !this.data.isEdit, selectList: [] })
  47. },
  48. selectCollect({ detail }) {
  49. this.setData({ selectList: detail })
  50. },
  51. deleteCollect() {
  52. const { selectList } = this.data
  53. if (!selectList.length) return
  54. util.showLoad('加载中')
  55. util.request(api.CollectDelete, selectList, 'POST').then(res => {
  56. util.hideLoad()
  57. if (res.errno === 0) {
  58. this.setData({ isEdit: false })
  59. this.getCollectList()
  60. } else util.showErrorToast(res.errmsg)
  61. }).catch(() => {
  62. util.hideLoad()
  63. util.showErrorToast('网络连接失败')
  64. })
  65. },
  66. // 商品详情
  67. toDetail({ currentTarget }) {
  68. if (!this.data.isEdit) {
  69. const { id } = currentTarget.dataset
  70. wx.navigateTo({ url: `/packageGoods/detail/detail?obj=${JSON.stringify({ id })}` })
  71. }
  72. }
  73. })