use-location.ts 859 B

1234567891011121314151617181920212223242526272829303132
  1. import type { LocationType } from "../wx/location";
  2. import { getFuzzyLocation } from "../wx/location";
  3. import request from "../request/method";
  4. export async function useLocation(type?: LocationType) {
  5. const scope = 'scope.userFuzzyLocation';
  6. const { authSetting } = await wx.getSetting();
  7. if (!authSetting?.[scope]) {
  8. await wx.authorize({ scope })
  9. }
  10. const location = await getFuzzyLocation(type);
  11. const {prov, city} = await request({
  12. url: `/surplus/getAddrByLatLong`,
  13. method: 'GET',
  14. params: {
  15. longitude: location.longitude,
  16. latitude: location.latitude,
  17. type,
  18. },
  19. transform({ data }) { return data; }
  20. });
  21. return {
  22. longitude: location.longitude,
  23. latitude: location.latitude,
  24. description: `${prov}${city}`.replace(/省|市|自治区|特别行政区|壮族|回族|维吾尔/g,''),
  25. }
  26. }