footprint.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. footprintList: []
  9. },
  10. onShow: function() {
  11. this.getFootprintList()
  12. },
  13. pullDownRefresh() {
  14. this.setData({ loading: true })
  15. this.getFootprintList()
  16. this.setData({ loading: false })
  17. },
  18. formatDate(date) {
  19. const month = new Date(date).getMonth() + 1
  20. const day = new Date(date).getDate()
  21. return month + '月' + day + '日'
  22. },
  23. getFootprintList() {
  24. util.showLoad('加载中')
  25. util.request(api.FootprintList).then(res => {
  26. util.hideLoad()
  27. if (res.errno === 0) {
  28. let footprintList = []
  29. for (const k in res.data) {
  30. if (Object.hasOwnProperty.call(res.data, k)) {
  31. const date = this.formatDate(k)
  32. const list = res.data[k].map(item => {
  33. item.picUrl = item.picUrl.split(',')
  34. return item
  35. })
  36. footprintList.push({ date, list })
  37. }
  38. }
  39. this.setData({ footprintList })
  40. } else util.showErrorToast(res.errmsg)
  41. }).catch(() => {
  42. util.hideLoad()
  43. util.showErrorToast('网络连接失败')
  44. })
  45. },
  46. // 编辑
  47. toggleEdit() {
  48. this.setData({ isEdit: !this.data.isEdit, selectList: [] })
  49. },
  50. selectFootprint({ detail }) {
  51. this.setData({ selectList: detail })
  52. },
  53. deleteFootprint() {
  54. const { selectList } = this.data
  55. if (!selectList.length) return
  56. util.showLoad('加载中')
  57. util.request(api.FootprintDelete, selectList, 'POST').then(res => {
  58. util.hideLoad()
  59. if (res.errno === 0) {
  60. this.setData({ isEdit: false })
  61. this.getFootprintList()
  62. } else util.showErrorToast(res.errmsg)
  63. }).catch(() => {
  64. util.hideLoad()
  65. util.showErrorToast('网络连接失败')
  66. })
  67. },
  68. // 商品详情
  69. toDetail({ currentTarget }) {
  70. if (!this.data.isEdit) {
  71. const { id } = currentTarget.dataset
  72. wx.navigateTo({ url: `/packageGoods/detail/detail?obj=${JSON.stringify({ id })}` })
  73. }
  74. }
  75. })