7
7
package logic
8
8
9
9
import (
10
+ "fmt"
10
11
"net/http"
11
12
"strconv"
12
13
"strings"
@@ -81,7 +82,7 @@ func newViews() *views {
81
82
}
82
83
83
84
// TODO: 用户登录了,应该用用户标识,而不是IP
84
- func (this * views ) Incr (req * http.Request , objtype , objid int ) {
85
+ func (this * views ) Incr (req * http.Request , objtype , objid int , uids ... int ) {
85
86
ua := req .UserAgent ()
86
87
spiders := config .ConfigFile .MustValueArray ("global" , "spider" , "," )
87
88
for _ , spider := range spiders {
@@ -90,28 +91,34 @@ func (this *views) Incr(req *http.Request, objtype, objid int) {
90
91
}
91
92
}
92
93
93
- user := goutils .Ip2long (goutils .RemoteIp (req ))
94
-
95
94
key := strconv .Itoa (objtype ) + strconv .Itoa (objid )
96
95
96
+ var userKey string
97
+
98
+ if len (uids ) > 0 {
99
+ userKey = fmt .Sprintf ("%s_uid_%d" , key , uids [0 ])
100
+ } else {
101
+ userKey = fmt .Sprintf ("%s_ip_%d" , key , goutils .Ip2long (goutils .RemoteIp (req )))
102
+ }
103
+
97
104
this .locker .Lock ()
98
105
defer this .locker .Unlock ()
99
106
100
- if user != 0 {
101
- userKey := key + strconv .FormatUint (uint64 (user ), 10 )
102
-
103
- if _ , ok := this .users [userKey ]; ok {
104
- return
105
- } else {
106
- this .users [userKey ] = true
107
- }
107
+ if _ , ok := this .users [userKey ]; ok {
108
+ return
109
+ } else {
110
+ this .users [userKey ] = true
108
111
}
109
112
110
113
if _ , ok := this .data [key ]; ! ok {
111
114
this .data [key ] = newView (objtype , objid )
112
115
}
113
116
114
117
this .data [key ].incr ()
118
+
119
+ if len (uids ) > 0 {
120
+ viewObservable .NotifyObservers (uids [0 ], objtype , objid )
121
+ }
115
122
}
116
123
117
124
func (this * views ) Flush () {
0 commit comments