From 1fc929a63e8215d6bc97fca19872a25825b649e0 Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Mon, 6 Jul 2020 14:40:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=80=E6=9C=89=E8=AE=BF?= =?UTF-8?q?=E5=AE=A2=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 3 ++ controller/visitor.go | 12 ++++++-- models/visitors.go | 16 +++++++++-- static/html/chat_main.html | 59 ++++++++++++++++++++++++++------------ 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index aff4ef2..0755062 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,9 @@ import ( "io/ioutil" "os" ) +var( + PageSize uint=2 +) const Dir = "config/" const AccountConf = Dir + "account.json" const MysqlConf = Dir + "mysql.json" diff --git a/controller/visitor.go b/controller/visitor.go index c36c977..7fc5c9f 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -2,7 +2,9 @@ package controller import ( "github.com/gin-gonic/gin" + "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/models" + "strconv" ) func GetVisitor(c *gin.Context) { @@ -15,11 +17,17 @@ func GetVisitor(c *gin.Context) { }) } func GetVisitors(c *gin.Context) { - vistors:=models.FindVisitors() + page,_:=strconv.Atoi(c.Query("page")) + vistors:=models.FindVisitors(uint(page)) + count:=models.CountVisitors() c.JSON(200, gin.H{ "code": 200, "msg": "ok", - "result":vistors, + "result":gin.H{ + "list":vistors, + "count":count, + "pagesize":config.PageSize, + }, }) } func GetVisitorMessage(c *gin.Context) { diff --git a/models/visitors.go b/models/visitors.go index 38fa4be..69e76af 100644 --- a/models/visitors.go +++ b/models/visitors.go @@ -1,5 +1,7 @@ package models +import "github.com/taoshihan1991/imaptool/config" + type Visitor struct { Model Name string `json:"name"` @@ -37,9 +39,13 @@ func FindVisitorByVistorId(visitorId string)Visitor{ DB.Where("visitor_id = ?", visitorId).First(&v) return v } -func FindVisitors()[]Visitor{ +func FindVisitors(page uint)[]Visitor{ + offset:=(page-1)*config.PageSize + if offset<0{ + offset=0 + } var visitors []Visitor - DB.Order("status desc, updated_at desc").Find(&visitors) + DB.Offset(offset).Limit(config.PageSize).Order("status desc, updated_at desc").Find(&visitors) return visitors } func FindVisitorsOnline()[]Visitor{ @@ -51,4 +57,10 @@ func UpdateVisitorStatus(visitorId string,status uint){ visitor:=Visitor{ } DB.Model(&visitor).Where("visitor_id = ?",visitorId).Update("status", status) +} +//查询条数 +func CountVisitors()uint{ + var count uint + DB.Model(&Visitor{}).Count(&count) + return count } \ No newline at end of file diff --git a/static/html/chat_main.html b/static/html/chat_main.html index 01bd800..a5c90fb 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -93,6 +93,14 @@ + + @@ -176,6 +184,9 @@ status:"", }, visitors:[], + visitorCount:0, + visitorCurrentPage:1, + visitorPageSize:10, }, methods: { //跳转 @@ -399,26 +410,38 @@ handleTabClick(tab, event){ let _this=this; if(tab.name=="second"){ - $.ajax({ - type:"get", - url:"/visitors", - headers:{ - "token":localStorage.getItem("token") - }, - success: function(data) { - if(data.result!=null){ - _this.visitors=data.result; - } - if(data.code!=200){ - _this.$message({ - message: data.msg, - type: 'error' - }); - } - } - }); + this.getVisitorPage(1); } }, + //所有访客分页展示 + visitorPage(page){ + this.getVisitorPage(page); + }, + //获取访客分页 + getVisitorPage(page){ + let _this=this; + $.ajax({ + type:"get", + url:"/visitors", + data:{page:page}, + headers:{ + "token":localStorage.getItem("token") + }, + success: function(data) { + if(data.result.list!=null){ + _this.visitors=data.result.list; + _this.visitorCount=data.result.count; + _this.visitorPageSize=data.result.pagesize; + } + if(data.code!=200){ + _this.$message({ + message: data.msg, + type: 'error' + }); + } + } + }); + }, //滚到底部 scrollBottom(){ this.$nextTick(() => {