Browse Source

添加其他页面的浏览次数记录

tangs 6 years ago
parent
commit
3b3ba74d8c
4 changed files with 58 additions and 30 deletions
  1. 1 20
      .idea/misc.xml
  2. 23 9
      src/view/viewapi/viewapi.go
  3. 1 0
      src/view/viewdb/items.go
  4. 33 1
      src/view/viewdb/record.go

+ 1 - 20
.idea/misc.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="GOROOT" path="E:\Go" />
   <component name="GoLibraries">
     <option name="urls">
       <list>
@@ -7,13 +8,6 @@
       </list>
     </option>
   </component>
-  <component name="MavenImportPreferences">
-    <option name="generalSettings">
-      <MavenGeneralSettings>
-        <option name="mavenHome" value="Bundled (Maven 3)" />
-      </MavenGeneralSettings>
-    </option>
-  </component>
   <component name="ProjectInspectionProfilesVisibleTreeState">
     <entry key="Project Default">
       <profile-state>
@@ -30,19 +24,6 @@
       </profile-state>
     </entry>
   </component>
-  <component name="ProjectLevelVcsManager" settingsEditedManually="false">
-    <OptionsSetting value="true" id="Add" />
-    <OptionsSetting value="true" id="Remove" />
-    <OptionsSetting value="true" id="Checkout" />
-    <OptionsSetting value="true" id="Update" />
-    <OptionsSetting value="true" id="Status" />
-    <OptionsSetting value="true" id="Edit" />
-    <ConfirmationsSetting value="0" id="Add" />
-    <ConfirmationsSetting value="0" id="Remove" />
-  </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="Go 1.8.3" project-jdk-type="Go SDK">
-    <output url="file://$PROJECT_DIR$/out" />
-  </component>
   <component name="masterDetails">
     <states>
       <state key="Copyright.UI">

+ 23 - 9
src/view/viewapi/viewapi.go

@@ -29,21 +29,35 @@ func ViewRecord(w http.ResponseWriter, r *http.Request) {
 
 	real_ip := r.Header.Get("X-Real-IP")
 
-	info, err := viewdb.FindArticleId(name)
-	if err != nil {
-		code = 2
-		log.Error("[ViewRecord] Find article id by name(%v) error ->(%v)", name, err)
+	// 记录文章浏览次数
+	if len(name) > 0 {
+		var info = util.Map{}
+		info, err = viewdb.FindArticleId(name)
+		if err != nil {
+			code = 2
+			log.Error("[ViewRecord] Find article id by name(%v) error ->(%v)", name, err)
+			return
+		}
+
+		var aid int = info.Int("id")
+		err = viewdb.Record_Mongo(info.Int("id"), real_ip)
+		if err != nil {
+			code = 2
+			log.Error("[ViewRecord] record with id(%v) error =>(%v)", aid, err)
+			return
+		}
+		log.Debug("[ViewRecord] record with id(%v) success", aid)
 		return
 	}
 
-	var aid int = info.Int("id")
-	err = viewdb.Record_Mongo(info.Int("id"), real_ip)
+	// 记录页面浏览次数
+	path := r.URL.Path
+	err = viewdb.Record_Path_Mongo(path, real_ip)
 	if err != nil {
 		code = 2
-		log.Error("[ViewRecord] record with id(%v) error =>(%v)", aid, err)
+		log.Error("[ViewRecord] record path with path(%v), ip(%v) error ->(%v)", path, real_ip, err	)
 		return
 	}
-
-	log.Debug("[ViewRecord] record with id(%v) success", aid)
+	log.Debug("[ViewRecord] record path with path(%v), ip(%v) success", path, real_ip)
 	return
 }

+ 1 - 0
src/view/viewdb/items.go

@@ -6,6 +6,7 @@ import "github.com/tangs-drm/go-tool/util"
 type PageView struct {
 	Id string `json:"id" bson:"_id"`
 	Ip string `json:"ip" bson:"ip"`
+	Path string `json:"path" bson:"path"`
 	Article int `json:"article" bson:"article"`
 	Count int `json:"count" bson:"count"`
 	Attrs util.Map `json:"attrs" bson:"attrs"`

+ 33 - 1
src/view/viewdb/record.go

@@ -6,16 +6,23 @@ import (
 	"github.com/tangs-drm/go-tool/log"
 	"github.com/tangs-drm/go-tool/dbm"
 	"fmt"
+	"time"
 )
 
+func TimeZero() int64 {
+	now := time.Now()
+	timeZero := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
+	return timeZero.UnixNano() / 1e6
+}
+
 func Record_Mongo(article_id int, ip string) error {
 	info, err := C(VIEW_TIMES).Upsert(bson.M{
 		"article": article_id,
 		"ip": ip,
+		"time": TimeZero(),
 	}, bson.M{
 		"$setOnInsert": bson.M{
 			"_id": bson.NewObjectId().Hex(),
-			"time": util.Now(),
 			"attrs": util.Map{},
 		},
 		"$set": bson.M{
@@ -34,6 +41,31 @@ func Record_Mongo(article_id int, ip string) error {
 	return nil
 }
 
+func Record_Path_Mongo(path string, ip string) error {
+	info, err := C(VIEW_TIMES).Upsert(bson.M{
+		"path": path,
+		"ip": ip,
+		"time": TimeZero(),
+	}, bson.M{
+		"$setOnInsert": bson.M{
+			"_id": bson.NewObjectId().Hex(),
+			"attrs": util.Map{},
+		},
+		"$set": bson.M{
+			"lastTime": util.Now(),
+		},
+		"$inc": bson.M{
+			"count": 1,
+		},
+	})
+	if err != nil {
+		log.Error("[Record_Path_Mongo] record with path(%v), ip(%v), time(%v) error ->(%v)", path, ip, util.Now(), err)
+		return err
+	}
+	log.Debug("[Record_Path_Mongo] record by path(%v), ip(%v), time(%v) success with info(%v)", path, ip, util.Now(), util.S2Json(info))
+	return nil
+}
+
 // FindArticleId 根据文章名字查出文章的id
 func FindArticleId(name string) (util.Map, error) {
 	var sqlString = fmt.Sprintf("SELECT ID FROM posts WHERE SLUG = '%v'", name)