Skip to content

Commit

Permalink
后台访问记录完成
Browse files Browse the repository at this point in the history
  • Loading branch information
刘珅 committed Apr 8, 2017
1 parent a7ad803 commit 4cc5d60
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
27 changes: 27 additions & 0 deletions go/controller/visit/visit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package visit

import (
"../../model"
"gopkg.in/kataras/iris.v6"
)

// Latest30Day 近30天,每天的PV
func Latest30Day(ctx *iris.Context) {
var userVisit model.UserVisit;
result := userVisit.Latest30DayPV()
var data iris.Map;
if result == nil {
data = iris.Map{
"list": [0]int{},
}
} else {
data = iris.Map{
"list": result,
}
}
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.SUCCESS,
"msg" : "success",
"data" : data,
})
}
66 changes: 66 additions & 0 deletions go/model/uservisit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package model

import (
"time"
"../config"
"strings"
"github.com/jinzhu/gorm"
)

// UserVisit 访客记录
type UserVisit struct {
ID uint `gorm:"primary_key" json:"id"`
URL string `json:"url"`
Referrer string `json:"referrer"`
ClientID string `json:"clientID"`
UserID uint `json:"userID"`
VisitTime time.Time `json:"visitTime"`
IP string `json:"ip"`
DeviceWidth uint `json:"deviceWidth"`
DeviceHeight uint `json:"deviceHeight"`
BrowserName string `json:"browserName"`
BrowserVersion string `json:"browserVersion"`
DeviceModel string `json:"deviceModel"`
Country string `json:"country"`
Language string `json:"language"`
OSName string `json:"osName"`
OSVersion string `json:"osVersion"`
}

// PVPerDay 每天的pv
type PVPerDay []struct {
PV int `json:"pv"`
Date string `gorm:"column:date" json:"date"`
}

// Latest30DayPV 近30天的PV
func (userVisit UserVisit) Latest30DayPV() (PVPerDay) {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
todaySec := today.Unix() //秒
before29 := todaySec - 29 * 24 * 60 * 60;
before29Date := time.Unix(before29, 0)

sqlData := before29Date.Format("2006-01-02")
sqlArr := []string{
"SELECT count(*) as pv, DATE_FORMAT(visit_time,'%Y-%m-%d') as date",
"FROM user_visit",
"WHERE visit_time >= ?",
"GROUP BY DATE_FORMAT(visit_time,'%Y-%m-%d');",
};

sql := strings.Join(sqlArr, " ")
db, err := gorm.Open(config.DBConfig.Dialect, config.DBConfig.URL)
if err != nil {
return nil
}

defer db.Close()

var result PVPerDay
err = db.Raw(sql, sqlData).Scan(&result).Error
if err != nil {
return nil
}
return result
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"./go/controller/product"
"./go/controller/order"
"./go/controller/user"
"./go/controller/visit"
_ "github.com/jinzhu/gorm/dialects/mysql"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
Expand Down Expand Up @@ -49,6 +50,8 @@ func main() {
adminRouter.Get("/user/yesterday", user.YesterdayRegisterUser)
adminRouter.Get("/user/latest/30", user.Latest30Day)
adminRouter.Get("/user/analyze", user.Analyze)

adminRouter.Get("/visit/pv/latest/30", visit.Latest30Day)
}

app.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
Expand Down
1 change: 1 addition & 0 deletions nodejs/.babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"plugins": [
"transform-runtime",
"recharts",
["import", { libraryName: "antd", style: "css" }]
]
}
2 changes: 2 additions & 0 deletions nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"babel-core": "6.24.0",
"babel-loader": "6.4.1",
"babel-plugin-import": "1.1.1",
"babel-plugin-recharts": "1.1.0",
"babel-plugin-transform-runtime": "6.23.0",
"babel-polyfill": "6.23.0",
"babel-preset-es2015": "6.24.0",
Expand All @@ -38,6 +39,7 @@
"react-redux": "5.0.3",
"react-router": "3.0.2",
"react-router-redux": "4.0.8",
"recharts": "0.22.0",
"redux": "3.6.0",
"redux-devtools": "3.3.2",
"redux-thunk": "2.2.0",
Expand Down

0 comments on commit 4cc5d60

Please sign in to comment.