clubList.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { mapGetters } from 'vuex'
  2. import { geolocation } from '@/utils/map-utils'
  3. import { drawLogo, debounce } from '@/utils'
  4. export default {
  5. data() {
  6. return {
  7. isRequest: false, // 是否发起请求
  8. finished: true, // 列表加载是否完毕(加载完了所有数据)
  9. loadingMore: false, // 是否正在加载
  10. listQuery: {
  11. authUserId: '',
  12. lngAndLat: '',
  13. authParty: '',
  14. provinceId: '',
  15. cityId: '',
  16. townId: '',
  17. pageNum: 1,
  18. pageSize: 12,
  19. },
  20. list: [], // 机构列表
  21. starList: [], // 明星机构列表
  22. total: 0, // 机构数量
  23. }
  24. },
  25. computed: {
  26. ...mapGetters(['authUserId', 'routePrefix']),
  27. },
  28. created() {
  29. const cacheData = this.$getStorage(this.routePrefix, 'club_list_data')
  30. if (cacheData) {
  31. this.initFromCache(cacheData)
  32. this.$removeStorage(this.routePrefix, 'club_list_data')
  33. } else {
  34. this.init()
  35. }
  36. this.fetchStarClubList()
  37. },
  38. beforeDestroy() {
  39. this.$toast.clear()
  40. },
  41. methods: {
  42. // 绘制logo的方法
  43. drawLogo,
  44. // 页面初始化
  45. async init() {
  46. this.listQuery.authUserId = this.authUserId
  47. this.$toast.loading({
  48. message: '正在获取您附近的机构...',
  49. duration: 0,
  50. })
  51. try {
  52. const resLocation = await geolocation()
  53. this.listQuery.lngAndLat = `${resLocation.position.lng},${resLocation.position.lat}`
  54. } catch (error) {
  55. this.$toast('获取定位信息失败')
  56. } finally {
  57. this.filterClubList()
  58. }
  59. },
  60. // 搜索机构列表
  61. filterClubList() {
  62. this.listQuery.pageNum = 1
  63. this.list = []
  64. this.loadingMore = true
  65. this.isRequest = true
  66. this.fetchClubList()
  67. },
  68. // 获取机构列表
  69. fetchClubList: debounce(async function () {
  70. try {
  71. const res = await this.$http.api.getAuthClubList(this.listQuery)
  72. this.list = [...this.list, ...res.data.list]
  73. this.finished = !res.data.hasNextPage
  74. this.listQuery.pageNum++
  75. this.total = res.data.total
  76. } catch (error) {
  77. console.log('机构获取失败')
  78. } finally {
  79. this.$toast.clear()
  80. this.loadingMore = false
  81. this.isRequest = false
  82. }
  83. }, 500),
  84. // 获取明星机构列表
  85. async fetchStarClubList() {
  86. try {
  87. const res = await this.$http.api.getAuthClubStarList({
  88. authUserId: this.authUserId,
  89. })
  90. this.starList = res.data.slice(0, 2)
  91. } catch (error) {
  92. console.log(error)
  93. }
  94. },
  95. // 查看机构详情
  96. toDetail(item) {
  97. // 缓存数据
  98. this.$setStorage(this.routePrefix, 'club_list_data', this.$data, {
  99. expiredTime: 5 * 60 * 1000,
  100. })
  101. this.$setStorage(this.routePrefix, 'clubInfo', item)
  102. const url = `${this.routePrefix}/approve/club/detail?id=${item.authId}`
  103. this.$router.push(url)
  104. },
  105. // 格式化地址
  106. formatAddress(a1, a2) {
  107. let resutl = ''
  108. if (a1) resutl += a1
  109. if (a2) resutl += a2
  110. return resutl || '暂无'
  111. },
  112. // 格式化距离
  113. formatDistance(value) {
  114. return value > 1 ? value + 'km' : value * 1000 + 'm'
  115. },
  116. },
  117. }