-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotentialCustomerEcho.go
303 lines (263 loc) · 7.3 KB
/
PotentialCustomerEcho.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"net/http"
"github.com/labstack/echo"
"io"
"html/template"
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"log"
"strings"
"strconv"
)
func HtmlIndexNum(intStr string) int {
i, err := strconv.Atoi(intStr)
i = i + 1
if err != nil {
panic(err)
}
return i
}
// 自定义模板函数
//var funcMap = template.FuncMap{
// // rangge index + 1
// "HtmlIndexNum": func(intStr string) int {
// i, err := strconv.Atoi(intStr)
// i = i + 1
// if err != nil {
// panic(err)
// }
// return i
// },
//}
// var imgBase64 template.URL
// telImg', 'phoneImg', 'wxImg'
// base64 图片图片显示
type PotentialCustomerClean struct {
OriginUrl string `bson:"url"`
ComName string `bson:"comName"`
TelPhone string `bson:"telPhone"`
MobilePhone string `bson:"mobilePhone"`
Addr string `bson:"addr"`
WebSite string `bson:"webSite"`
Qq string `bson:"qq"`
WxImgbase64 template.URL `bson:"wxImgbase64"`
PhoneImgbase64 template.URL `bson:"phoneImgbase64"`
TelImgbase64 template.URL `bson:"telImgbase64"`
}
//zaimongodb中为一个key
type PotentialCustomerOneKey struct {
CleanData PotentialCustomerClean
}
type PotentialCustomerCleanHtml struct {
ComName string
TelPhone string
MobilePhone string
Addr string
WebSite string
Qq string
}
type PotentialCustomerDetail struct {
Url string `bson:"url",json:"url"`
ContactUrl string `bson:"contactUrl"`
ComName string `bson:"comName"`
ComInfo string `bson:"comInfo"`
ComInfoTxt string `bson:"comInfoTxt"`
TelImg string `bson:"telImg"`
PhoneImg string `bson:"phoneImg"`
WxImg string `bson:"wxImg"`
SpiderDate string `bson:"spiderDate"`
}
// 检查元素是否存在于数组?遍历?如何集合运算方法
func eleInArr(ele string, arr [] string) bool {
for _, v := range arr {
if (ele == v) {
fmt.Println("eleInArr", ele)
return true
}
}
return false
}
/*
1.实现 echo.Renderer 接口
*/
/*
"HtmlIndexNum":func (intStr string) int {
i, err := strconv.Atoi(intStr)
i = i + 1
if err != nil {panic(err)}
return i
}}
*/
type Template struct {
templates *template.Template
}
// Render renders a template document
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
// Add global methods if data is a map
if viewContext, isMap := data.(map[string]interface{}); isMap {
viewContext["reverse"] = c.Echo().Reverse
}
return t.templates.ExecuteTemplate(w, name, data)
}
/*
4.在 action 中渲染模板
*/
func Hello(c echo.Context) error {
return c.Render(http.StatusOK, "WeUI", "chkUrl")
}
/*
自定义一个 context
Define a custom context
Context - Go/Golang 框架 Echo 文档 http://go-echo.org/guide/context/
*/
type CustomContext struct {
echo.Context
}
func (c *CustomContext) Foo() {
println("foo")
}
type ScriptStruct struct {
Host string
Port int
Path string
ScriptName string
}
var s2 = ScriptStruct{"192.168.3.123", 8088, "/myDir/", "spider.go"}
//var ScriptArr [6]ScriptStruct
var ScriptArr [2]ScriptStruct
func (c *CustomContext) DumpScripts() {
println("bar")
ScriptArr[0] = ScriptStruct{"192.168.3.103", 8088, "/home/goDev/spider/", "spiderChkurl.go"}
ScriptArr[1] = ScriptStruct{"192.168.3.110", 8088, "/home/goDev/spider/", "spiderChkurl.go"}
//ScriptArr[2] = ScriptStruct{"192.168.3.123", 8088, "/home/goDev/spider/", "spiderChkurl.go"}
}
func main() {
/*
2.预编译模板
*/
t := &Template{
templates: template.Must(template.ParseGlob("goEchopublic/views/*.html")),
}
//t := &Template{
// templates: template,
//}
//t:=template.Template
//Func := template.FuncMap{"HtmlIndexNum": HtmlIndexNum} //把定义的函数实例
//t.Funcs(Func)
//t.Must(template.ParseGlob("goEchopublic/views/*.html"))
/*
3.注册模板
*/
e := echo.New()
e.Renderer = t
fmt.Println(t)
/*
静态文件
Echo#Static(prefix, root string) 用一个 url 路径注册一个新的路由来提供静态文件的访问服务。root 为文件根目录。
这样会将所有访问/static/*的请求去访问assets目录。例如,一个访问/static/js/main.js的请求会匹配到assets/js/main.js这个文件。
*/
e.Static("/static", "assets")
/*
创建一个中间件来扩展默认的 context
Create a middleware to extend default context
*/
e.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := &CustomContext{c}
return h(cc)
}
})
/*
这个中间件要在所有其它中间件之前注册到路由上。
This middleware should be registered before any other middleware.
*/
/*
在业务处理中使用
Use in handler
*/
e.GET("/scriptAdmin01", Hello)
e.GET("/scriptAdmin", func(c echo.Context) error {
cc := c.(*CustomContext)
cc.Foo()
cc.DumpScripts()
fmt.Println(ScriptArr)
return c.Render(http.StatusOK, "hello", s2)
})
e.GET("/chkUrl", func(c echo.Context) error {
fmt.Println("test-chkUrl")
cc := c.(*CustomContext)
cc.DumpScripts()
return c.Render(http.StatusOK, "chkUrl", ScriptArr)
})
e.POST("/chkUid", func(c echo.Context) error {
fmt.Println("chkUid")
uid := c.FormValue("uid")
fmt.Println(uid)
cc := c.(*CustomContext)
cc.DumpScripts()
msg := "uid:" + uid + "处理中。。。进度xxx"
fmt.Println(msg)
return c.Render(http.StatusOK, "tmp", msg)
})
// Named route "foobar"
e.GET("/something", func(c echo.Context) error {
return c.Render(http.StatusOK, "something.html", map[string]interface{}{
"name": "Dolly!",
})
}).Name = "foobar"
e.GET("/PotentialCustomerDetail/:spiderDate", func(c echo.Context) error {
spiderDate := c.Param("spiderDate")
//查询mongodb数据
session, err := mgo.Dial("mongodb://hbaseU:[email protected]:27017/hbase")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
Collection := session.DB("hbase").C("todayUrls")
var resultSOnline [] PotentialCustomerDetail
err = Collection.Find(bson.M{"spiderDate": spiderDate}).All(&resultSOnline)
if err != nil {
log.Fatal(err)
}
return c.Render(http.StatusOK, "PotentialCustomerDetail", resultSOnline)
})
e.GET("/PotentialCustomer/:spiderDate", func(c echo.Context) error {
spiderDate := c.Param("spiderDate")
//查询mongodb数据
session, err := mgo.Dial("mongodb://hbaseU:[email protected]:27017/hbase")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
Collection := session.DB("hbase").C("todayUrls")
var resultSOnline [] PotentialCustomerClean
err = Collection.Find(bson.M{"spiderDate": spiderDate}).All(&resultSOnline)
if err != nil {
log.Fatal(err)
}
// TODO 当日重复客户资料过滤:同平台的重复、跨平台的重复
fmt.Println(resultSOnline)
var comSet [] string
var showSet [] PotentialCustomerClean
for _, v := range resultSOnline {
chkName := v.ComName
strings.Replace(chkName, "\n", "", -1)
fmt.Println(chkName)
if (chkName != "") {
t := eleInArr(chkName, comSet)
if (!t) {
comSet = append(comSet, chkName)
showSet = append(showSet, v)
}
}
}
return c.Render(http.StatusOK, "PotentialCustomer", showSet)
})
e.Logger.Fatal(e.Start(":1326"))
}