store.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { storeList } from '../../api/other';
  2. import { REPORT_BEHAVIOR } from '../../utils/util.js';
  3. Page({
  4. data: {
  5. list: [], // 门店列表
  6. page: 1, // 当前页
  7. pageSize: 10, // 每页条数
  8. loadingMore: false,// 是否正在加载
  9. noMore: false, // 是否已没有更多数据
  10. refreshing: false, // 下拉刷新状态
  11. latitude:0,
  12. longitude:0
  13. },
  14. onLoad() {
  15. wx.getLocation({
  16. type: 'wgs84',
  17. success: (res) => {
  18. this.setData({
  19. latitude: res.latitude,
  20. longitude: res.longitude
  21. })
  22. this.loadData(true)
  23. },
  24. fail: (err) => {
  25. wx.showModal({
  26. title: '提示',
  27. content: '获取定位失败,请检查是否开启定位权限',
  28. showCancel: false,
  29. confirmText: '我知道了'
  30. })
  31. this.loadData(true)
  32. }
  33. })
  34. },
  35. // 请求数据
  36. async loadData(isRefresh = false) {
  37. if (this.data.loadingMore) return;
  38. this.setData({ loadingMore: true });
  39. const page = isRefresh ? 1 : this.data.page;
  40. try {
  41. const res = await storeList({
  42. page,
  43. pageSize: this.data.pageSize,
  44. longitude:this.data.longitude,
  45. latitude:this.data.latitude
  46. });
  47. const newList = res.data.list || [];
  48. const allList = isRefresh ? newList : [...this.data.list, ...newList];
  49. this.setData({
  50. list: allList,
  51. loadingMore: false,
  52. page: page + 1,
  53. noMore: allList.length >= res.data.total,
  54. refreshing: false
  55. });
  56. } catch (err) {
  57. console.error(err);
  58. this.setData({ loadingMore: false, refreshing: false });
  59. wx.showToast({ title: '加载失败', icon: 'none' });
  60. }
  61. },
  62. // 页面触底事件
  63. onReachBottom() {
  64. if (!this.data.noMore) {
  65. this.loadData();
  66. }
  67. },
  68. // 下拉刷新
  69. onPullDownRefresh() {
  70. this.loadData(true);
  71. },
  72. // 点击门店
  73. goDetail(e) {
  74. const id = e.currentTarget.dataset.id;
  75. REPORT_BEHAVIOR('门店访问', { link_id: id })
  76. wx.navigateTo({
  77. url: `/pages/storeDetail/storeDetail?id=${id}`
  78. });
  79. },
  80. onShow() {
  81. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  82. this.getTabBar().setData({ selected: 1 });
  83. }
  84. }
  85. });