react go實現用戶歷史登錄列表頁面

refer: http://ip-api.com/

1.首先需要創建一個保存用戶歷史的登錄的表,然后連接go

2.在用戶登錄的時候,獲取用戶的IP IP位置,在后端直接處理數據即可(不需要在前端傳遞數據)

(1)增加路由:

apiv1.POST("/history_login_logs", v1.AddHistoryLoginLog)

(2)在model里增加(例如:models/history_login_logs.go)
?

 func AddHistoryLoginLog(user_id int, ip_address string, ip_location string, login_at time.Time) bool {db.Create(&HistoryLoginLogs{UserId:    user_id,IpAddress: ip_address,IpLocation:   ip_location,LoginAt: login_at,})return true}


? (3) 在登錄后的方法中增加(需要引入

import("time""io/ioutil""fmt""encoding/json"
)type Location struct {Status      string  `json:"status"`Country     string  `json:"country"`CountryCode string  `json:"countryCode"`Region      string  `json:"region"`RegionName  string  `json:"regionName"`City        string  `json:"city"`Zip         string  `json:"zip"`Lat         float64 `json:"lat"`Lon         float64 `json:"lon"`Timezone    string  `json:"timezone"`Isp         string  `json:"isp"`Org         string  `json:"org"`As          string  `json:"as"`Query       string  `json:"query"`
}...
ipAddress := c.ClientIP()
fmt.Println("== ip_address:", ipAddress)
resp, err := http.Get("http://ip-api.com/json/" + ipAddress + "?lang=zh-CN")
if err != nil {fmt.Println("Error:", err)return
}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Println("Error:", err)return}var location Location
err = json.Unmarshal(body, &location)if err != nil {fmt.Println("Error:", err)return}fmt.Println("=== Location:", location)
City := location.CitycurrentTime := time.Now()
models.AddHistoryLoginLog(user.ID, ipAddress, City, currentTime)
...

? (4)增加action (例如:routers/api/v1/history_login_log.go)(需要引入import? "net/http"?? "time"? "fmt")
?

type AddHistoryLoginLogRequest struct {UserID     int    `json:"user_id" binding:"required"`IPAddress  string `json:"ip_address" binding:"required"`City string `json:"ip_location" binding:"required"`CurrentTime time.Time `json:"login_at" binding:"required"`}func AddHistoryLoginLog(c *gin.Context) {var request AddHistoryLoginLogRequestif err := c.ShouldBindJSON(&request); err != nil {fmt.Println("== err: ", err)return}models.AddHistoryLoginLog(request.UserID, request.IPAddress, request.City, request.CurrentTime)}

3.在前端寫一個展示的列表頁面即可。(登錄時間寫現在的時間即可。)
例如:src/pages/HistoryLoginLog/index.jsx

import React, { Component } from 'react'
import { Table } from 'antd';
import axios from 'axios'
import Config from '@/settings'
import { getToken, removeToken } from '@/utils/auth'const columns = [{title: '登錄名',dataIndex: 'user_id',key: 'user_id',render: text => <a>{text}</a>,},{title: '登陸時間',dataIndex: 'login_at',key: 'login_at',// 這里是進行時間的處理,轉換為北京時間,格式為:2023/08/16 21:40render: text => {const dateObj = new Date(text);const localizedDate = dateObj.toLocaleString('zh-CN', {timeZone: 'Asia/Shanghai',year: 'numeric',month: '2-digit',day: '2-digit',hour: '2-digit',minute: '2-digit',});return <span>{localizedDate}</span>;},},{title: '登陸ip',dataIndex: 'ip_address',key: 'ip_address',},{title: '登陸位置',dataIndex: 'ip_location',key: 'ip_locatio',}
];export default class CalculationPlan extends Component {state = {data: [],loading: true,}async fetchData() {try {const response = await axios.get(`${Config.BASE_URL}/api/v1/history_login_logs?token=${getToken()}`)if (response.data.message == "ok") {const sortedData = response.data.data.sort((a, b) => new Date(b.id) - new Date(a.id));this.setState({data: sortedData,loading: false,})}} catch (error) {console.error(error)removeToken()window.location.href = '/'}}componentDidMount() {this.fetchData()}render() {const { data, loading } = this.statereturn (<Table columns={columns} dataSource={data} loading={loading} />)}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/40036.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/40036.shtml
英文地址,請注明出處:http://en.pswp.cn/news/40036.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用Java服務器實現UDP消息的發送和接收(多線程)

目錄 簡介&#xff1a;1. 導入必要的庫2. 創建服務器端代碼3. 創建客戶端代碼4. 實現多線程處理5. 測試運行示例代碼&#xff1a;函數說明服務器端代碼說明&#xff1a;客戶端代碼說明&#xff1a; 總結&#xff1a; 簡介&#xff1a; 在本篇博客中&#xff0c;我們將介紹如何…

genism word2vec方法

文章目錄 概述使用示例模型的保存與使用訓練參數詳解&#xff08;[原鏈接](https://blog.csdn.net/weixin_44852067/article/details/130221655)&#xff09;語料庫訓練 概述 word2vec是按句子來處理的Sentences(句子們) 使用示例 from gensim.models import Word2Vec #sent…

《起風了》C++源代碼

使用方法 Visual Studio、Dev-C、Visual Studio Code等C/C創建一個 .cpp 文件&#xff0c;直接粘貼賦值即可。 #include <iostream> #include <Windows.h> #pragma comment(lib,"winmm.lib") using namespace std; enum Scale {Rest 0, C8 108, B7 …

目錄——車載網絡安全

本文主要匯總車載網絡安全專欄文章,以方便各位讀者閱讀。 ISO21434 概述(一) ISO21434 組織網絡安全管理(二) ISO21434 項目網絡安全管理(三) ISO21434 分布式網絡安全(四) SO21434 持續進行的網絡安全(五) ISO21434 概念階段網絡安全(六)

線性代數(四) 特征值相似矩陣

前言 前面主要講述的是方程組和矩陣的關系&#xff0c;現在了解下矩陣和矩陣的關系 方陣的特征值與特征向量 假設A為n階方陣&#xff0c;對于一個數 λ \lambda λ 若存在&#xff1a;非零列向量 α \alpha α&#xff0c;使得&#xff1a; A α ? λ α ? A\vec{\alp…

2022年電賽C題——小車跟隨行駛系統——做題記錄以及經驗分享

前言 自己打算將做過的電賽真題&#xff0c;主要包含控制組的&#xff0c;近幾年出現的小車控制題目&#xff0c;自己做過的真題以及在準備電賽期間刷真題出現的問題以及經驗分享給大家 這次帶來的是22年電賽C題——小車跟隨行駛系統&#xff0c;這道題目指定使用的是TI的單片…

spring ico容器 spring注入方式 spring與tomcat整合

一、簡介 1、什么是spring&#xff1f; Spring是一個開源的輕量級Java應用開發框架&#xff0c;它提供了一種簡單、高效、靈活的方式來構建企業級應用程序。Spring框架的核心特點是依賴注入&#xff08;Dependency Injection&#xff09;和面向切面編程&#xff08;Aspect-Ori…

SpringBoot整合Redis完整篇

SpringBoot整合Redis完整篇 1、在springboot項目的pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schem…

分布式鎖有哪些應用場景和實現?

電商網站都會遇到秒殺、特價之類的活動&#xff0c;大促活動有一個共同特點就是訪問量激增&#xff0c;在高并發下會出現成千上萬人搶購一個商品的場景。雖然在系統設計時會通過限流、異步、排隊等方式優化&#xff0c;但整體的并發還是平時的數倍以上&#xff0c;參加活動的商…

WebRTC音視頻通話-實現GPUImage視頻美顏濾鏡效果iOS

WebRTC音視頻通話-實現GPUImage視頻美顏濾鏡效果 在WebRTC音視頻通話的GPUImage美顏效果圖如下 可以看下 之前搭建ossrs服務&#xff0c;可以查看&#xff1a;https://blog.csdn.net/gloryFlow/article/details/132257196 之前實現iOS端調用ossrs音視頻通話&#xff0c;可以查…

2023秋招筆試

檸檬微趣 將java的鏈表升序排序&#xff0c;鏈表用Class Node{int val,Node next}實現 import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner;/*** 輸入一串數字&#xff0c;放入list中&#xff0c;實現sortList&#xff0c;返回升序的li…

將單個訓練數據集文件拆分為:image文件和label文件(pytorch學習+螞蟻蜜蜂數據集)

螞蟻蜜蜂分類數據集下載鏈接&#xff1a;https://download.pytorch.org/tutorial/hymenoptera_data.zip 要實現如圖操作&#xff1a; 將ants分為ants_image和ants_label 將bees分成bees_image和bees_label 創建ants_label和bees_label&#xff0c;并且以圖片名作為txt文件的…

Apche Kafka + Spring的消息監聽容器

目錄 一、消息的接收1.1、消息監聽器 二、消息監聽容器2.1、 實現方法2.1.1、KafkaMessageListenerContainer2.1.1.1、 基本概念2.1.1.2、如何使用 KafkaMessageListenerContainer 2.1.2、ConcurrentMessageListenerContainer 三、偏移 四、監聽器容器自動啟動 一、消息的接收 …

【機器學習】sklearn數據集的使用,數據集的獲取和劃分

「作者主頁」&#xff1a;士別三日wyx 「作者簡介」&#xff1a;CSDN top100、阿里云博客專家、華為云享專家、網絡安全領域優質創作者 「推薦專欄」&#xff1a;對網絡安全感興趣的小伙伴可以關注專欄《網絡安全入門到精通》 sklearn數據集 二、安裝sklearn二、獲取數據集三、…

mac錄屏工具,錄屏沒有聲音的解決辦法

mac錄屏工具&#xff0c;錄屏沒有聲音的解決辦法 在使用macbook錄制屏幕時&#xff0c;發現自帶的錄屏工具QuickTime Player沒有聲音&#xff0c;于是嘗試了多款錄屏工具&#xff0c;對其做一些經驗總結&#xff08;省流&#xff1a;APP Store直接可以免費下載使用Omi錄屏專家…

第三課-界面介紹SD-Stable Diffusion 教程

前言 我們已經安裝好了SD&#xff0c;這篇文章不介紹難以理解的原理&#xff0c;說使用。以后再介紹原理。 我的想法是&#xff0c;先學會畫&#xff0c;然后明白原理&#xff0c;再去提高技術。 我失敗過&#xff0c;知道三天打魚兩天曬網的痛苦&#xff0c;和很多人一樣試了…

TiDB數據庫從入門到精通系列之六:使用 TiCDC 將 TiDB 的數據同步到 Apache Kafka

TiDB數據庫從入門到精通系列之六&#xff1a;使用 TiCDC 將 TiDB 的數據同步到 Apache Kafka 一、技術流程二、搭建環境三、創建Kafka changefeed四、寫入數據以產生變更日志五、配置 Flink 消費 Kafka 數據 一、技術流程 快速搭建 TiCDC 集群、Kafka 集群和 Flink 集群創建 c…

【網絡編程系列】網絡編程實戰

&#x1f49d;&#x1f49d;&#x1f49d;歡迎來到我的博客&#xff0c;很高興能夠在這里和您見面&#xff01;希望您在這里可以感受到一份輕松愉快的氛圍&#xff0c;不僅可以獲得有趣的內容和知識&#xff0c;也可以暢所欲言、分享您的想法和見解。 推薦:kuan 的首頁,持續學…

使用Vue.js框架的指令和事件綁定實現一個購物車的頁面布局

使用了v-model指令來實現全選/全不選的功能&#xff0c;當全選框被點擊時&#xff0c;isAllChecked的值會被改變。使用了v-if指令來判斷購物車中是否有商品&#xff0c;如果有商品則渲染商品列表&#xff0c;否則顯示購物車為空的提示。使用了v-for指令來遍歷datalist數組&…

jvm內存溢出排查(使用idea自帶的內存泄漏分析工具)

文章目錄 1.確保生成內存溢出文件2.使用idea自帶的內存泄漏分析工具3.具體實驗一下 1.確保生成內存溢出文件 想分析堆內存溢出&#xff0c;一定在運行jar包時就寫上參數-XX:HeapDumpOnOutOfMemoryError&#xff0c;可以看我之前關于如何運行jar包的文章。若你沒有寫。可以寫上…