ipAndAddress.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**获取当前登录的ip */
  2. export async function getUserIP(callback) {
  3. await fetch('https://api.ipify.org?format=json').then(function (response) {
  4. return response.json();
  5. }).then(function (data) {
  6. callback(data.ip);
  7. }).catch(function (error) {
  8. console.log('Error:', error);
  9. })
  10. }
  11. // 获取当前登录的地理位置 经纬度
  12. export async function getUserLocation(ipInfo, callback) {
  13. await fetch(`https://ipapi.co/${ipInfo}/json`)
  14. .then(response => response.json())
  15. .then(data => {
  16. callback(data);
  17. // 打印地理位置信息 经纬度
  18. // console.log(data.latitude);
  19. // console.log(data.longitude);
  20. })
  21. .catch(error => console.log(error));
  22. }
  23. // 获取当前登录的地理 信息
  24. export async function getAddressLocation(ipInfo, callback) {
  25. await fetch(`https://qifu-api.baidubce.com/ip/geo/v1/district?ip=${ipInfo}`)
  26. .then(response => response.json())
  27. .then(res => {
  28. if (res.code === 'Success') {
  29. callback({ ...res.data, ip: res.ip });
  30. }
  31. })
  32. .catch(error => console.log(error));
  33. }