Просмотр исходного кода

登录新增提交ip地址信息

xxxxx 2 лет назад
Родитель
Сommit
aa0690f12f
3 измененных файлов с 99 добавлено и 21 удалено
  1. 27 18
      src/http/moudules/login.js
  2. 35 0
      src/utils/ipAndAddress.js
  3. 37 3
      src/views/Login.vue

+ 27 - 18
src/http/moudules/login.js

@@ -6,41 +6,50 @@ import axios from '../axios'
 
 // 登录
 export const login = data => {
-    return axios({
-        url: 'login',
-        method: 'post',
-        data
-    })
+  return axios({
+    url: 'login',
+    method: 'post',
+    data
+  })
 }
 
 // 登录2
 export const login2 = data => {
-    return axios({
-        url: '/login2',
-        method: 'post',
-        data
-    })
+  return axios({
+    url: '/login2',
+    method: 'post',
+    data
+  })
 }
 
 
 // 登出
 export const logout = () => {
-    return axios({
-        url: 'logout',
-        method: 'get'
-    })
+  return axios({
+    url: 'logout',
+    method: 'get'
+  })
 }
 //获取二维码
 export const getQRCode = (id) => {
   return axios({
-      url: '/getQrCode?uuid='+id,
-      method: 'get',
+    url: '/getQrCode?uuid=' + id,
+    method: 'get',
   })
 }
 //扫码登录状态
 export const QrCodeStatusQuery = (id) => {
   return axios({
-      url: '/getQrCodeStatus?uuid='+id,
-      method: 'get',
+    url: '/getQrCodeStatus?uuid=' + id,
+    method: 'get',
+  })
+}
+
+// 登录 以后提交 位置信息
+export const uploadLocationInfo = data => {
+  return axios({
+    url: '/sys/ipAddr/saveIpInfo',
+    method: 'post',
+    data
   })
 }

+ 35 - 0
src/utils/ipAndAddress.js

@@ -0,0 +1,35 @@
+
+/**获取当前登录的ip */
+export async function getUserIP(callback) {
+  await fetch('https://api.ipify.org?format=json').then(function (response) {
+    return response.json();
+  }).then(function (data) {
+    callback(data.ip);
+  }).catch(function (error) {
+    console.log('Error:', error);
+  })
+}
+// 获取当前登录的地理位置 经纬度
+export async function getUserLocation(ipInfo, callback) {
+  await fetch(`https://ipapi.co/${ipInfo}/json`)
+    .then(response => response.json())
+    .then(data => {
+      callback(data);
+      // 打印地理位置信息  经纬度
+      // console.log(data.latitude);
+      // console.log(data.longitude);
+    })
+    .catch(error => console.log(error));
+}
+// 获取当前登录的地理 信息
+export async function getAddressLocation(ipInfo, callback) {
+  await fetch(`https://qifu-api.baidubce.com/ip/geo/v1/district?ip=${ipInfo}`)
+    .then(response => response.json())
+    .then(res => {
+      if (res.code === 'Success') {
+        callback({ ...res.data, ip: res.ip });
+      }
+    })
+    .catch(error => console.log(error));
+}
+

+ 37 - 3
src/views/Login.vue

@@ -158,6 +158,11 @@ import LangSelector from "@/components/LangSelector";
 import { login } from "@/api/user";
 import CryptoJS from "crypto-js";
 // import { getSysList } from "../http/moudules/sys";
+import {
+  getUserIP,
+  getUserLocation,
+  getAddressLocation
+} from "@/utils/ipAndAddress.js";
 export default {
   name: "Login",
   inject: ["reload"],
@@ -189,7 +194,8 @@ export default {
       tipVisibility: false,
       optionList: [],
       sysId: null,
-      systemType: "1" //系统类型
+      systemType: "1", //系统类型
+      addressInfo: {} //当前登录信息
     };
   },
   created() {
@@ -238,8 +244,8 @@ export default {
             password: this.loginForm.password,
             captcha: this.loginForm.captcha.toLowerCase(),
             captchaId: this.loginForm.captchaId,
-            sysId: this.sysId,
-            systemType: this.systemType
+            sysId: this.sysId
+            // systemType: this.systemType
           };
 
           // this.$store
@@ -299,6 +305,7 @@ export default {
                   // alert(res.data);
                 } else {
                   Cookies.set("token", res.data); // 放置token到Cookie
+                  this.submitLocation();
                   this.getDictItems();
                   this.getUser();
                 }
@@ -317,6 +324,27 @@ export default {
         }
       });
     },
+    /**提交当前位置信息 */
+    submitLocation() {
+      let location = {
+        systemType: this.systemType,
+        ...this.addressInfo
+      };
+      this.$api.login.uploadLocationInfo(location).then(res => {
+        if (res.code == 200) {
+          this.$message({
+            message: res.msg,
+            type: "success"
+          });
+        } else {
+          this.$message({
+            message: res.msg,
+            type: "error"
+          });
+        }
+      });
+    },
+
     getDictItems() {
       //--数据字典--
       // 性别
@@ -451,6 +479,12 @@ export default {
   },
   mounted() {
     this.refreshCaptcha();
+    /**获取当前登录的ip地址 */
+    getUserIP(ip => {
+      getAddressLocation(ip, info => {
+        this.addressInfo = info;
+      });
+    });
   },
   computed: {
     ...mapState({