store.js 2.1 KB

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