sys.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { defineStore } from 'pinia'
  2. import type { apiResData } from '@/api/globals'
  3. interface SysState {
  4. opcity: number
  5. /**
  6. * 分类顶部激活id
  7. */
  8. topNavActive: string
  9. /**
  10. * 二级分类激活id
  11. */
  12. leftActive: string
  13. searchList: string[]
  14. /**
  15. * 回到顶部
  16. */
  17. backTop: boolean
  18. /**
  19. * 店铺名称
  20. */
  21. SelectShopInfo: Api.xsbShopList
  22. /**
  23. * 店铺列表
  24. */
  25. xsbShopList: Api.xsbShopList[]
  26. /**
  27. * 用户选中我的地址id
  28. */
  29. selectAddressId: number | undefined
  30. /**
  31. * 是否所有门店都有权限
  32. */
  33. allShopHasPermission: boolean
  34. /**
  35. * 优惠券到账弹窗是否显示
  36. */
  37. couponArrivalPopupVisible: boolean
  38. /**
  39. * 是否打开分类页面
  40. */
  41. isClassfiyPageOpen: boolean
  42. }
  43. export const useSysXsbStore = defineStore('system-xsb', {
  44. state: (): SysState => ({
  45. topNavActive: '',
  46. leftActive: '',
  47. searchList: [],
  48. backTop: false,
  49. SelectShopInfo: { shopId: 0 },
  50. xsbShopList: [],
  51. opcity: 0,
  52. selectAddressId: undefined,
  53. allShopHasPermission: false,
  54. couponArrivalPopupVisible: false,
  55. isClassfiyPageOpen: false,
  56. }),
  57. actions: {
  58. getTabbarItemValue(name: string) {
  59. if (name === 'xsb-cart') {
  60. const { getTotalNum } = storeToRefs(useSmqjhCartStore())
  61. return getTotalNum.value
  62. }
  63. },
  64. async getAllShopList() {
  65. return new Promise((resolve, reject) => {
  66. Apis.xsb.shopList({}).then(async (res) => {
  67. this.xsbShopList = res.data
  68. const hasPermission = res.data.every(it => !it.isPermiss)
  69. if (hasPermission) {
  70. // 所有门店都没有权限
  71. this.allShopHasPermission = true
  72. this.SelectShopInfo = { shopId: 0 }
  73. reject(new Error('所有门店都没有权限'))
  74. }
  75. else {
  76. this.allShopHasPermission = false
  77. const { Location } = storeToRefs(useAddressStore())
  78. await this.getnNearestShop(Location.value.latitude || 26.648327, Location.value.longitude || 106.620256)
  79. resolve(res)
  80. }
  81. }).catch((err) => { reject(err) })
  82. })
  83. },
  84. /**
  85. * 获取最近的门店
  86. * @param latitude 纬度
  87. * @param longitude 经度
  88. * @returns Promise<apiResData<{ nearestShopId: number }>>
  89. */
  90. async getnNearestShop(latitude: number, longitude: number): Promise<apiResData<{ nearestShopId: number }>> {
  91. return new Promise((resolve, reject) => {
  92. Apis.xsb.nearestShop({ data: { longitude, latitude } }).then((res) => {
  93. const newShop = this.xsbShopList.find(it => it.shopId === res.data.nearestShopId)
  94. if (newShop) {
  95. this.SelectShopInfo = this.SelectShopInfo.shopId ? this.SelectShopInfo : newShop
  96. }
  97. resolve(res)
  98. }).catch((err) => { reject(err) })
  99. })
  100. },
  101. },
  102. })