front.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // front
  2. package front
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. qu "qfw/util"
  8. mgdb "qfw/util/mongodb"
  9. "qfw/util/redis"
  10. "regexp"
  11. "sort"
  12. "spider"
  13. util "spiderutil"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. u "util"
  19. "github.com/go-xweb/httpsession"
  20. "github.com/go-xweb/xweb"
  21. "github.com/lauyoume/gopinyin"
  22. "github.com/tealeg/xlsx"
  23. "gopkg.in/mgo.v2/bson"
  24. )
  25. type Front struct {
  26. *xweb.Action
  27. login xweb.Mapper `xweb:"/"`
  28. logout xweb.Mapper `xweb:"/center/logout"` //退出
  29. loadIndex xweb.Mapper `xweb:"/center"` //控制中心
  30. spidernew xweb.Mapper `xweb:"/center/spider"` //爬虫保存
  31. reg xweb.Mapper `xweb:"/center/reg"` //爬虫注册
  32. assign xweb.Mapper `xweb:"/center/user/assign"` //分配爬虫
  33. loadSpider xweb.Mapper `xweb:"/center/spider/edit/(.*)"` //爬虫加载
  34. viewSpider xweb.Mapper `xweb:"/center/spider/view/(.*)"` //爬虫查看
  35. downSpider xweb.Mapper `xweb:"/center/spider/download/(.*)"` //爬虫下载
  36. upState xweb.Mapper `xweb:"/center/spider/upstate"` //爬虫状态更新
  37. assort xweb.Mapper `xweb:"/center/spider/assort"` //审核人员分类
  38. batchShelves xweb.Mapper `xweb:"/center/spider/batchShelves"` //爬虫状态更新
  39. checktime xweb.Mapper `xweb:"/center/spider/checktime"` //爬虫核对
  40. disables xweb.Mapper `xweb:"/center/spider/disable"` //批量作废
  41. changeEvent xweb.Mapper `xweb:"/center/changeEvent"` //节点更新
  42. getJson xweb.Mapper `xweb:"/center/spider/json"` //
  43. delRedis xweb.Mapper `xweb:"/center/spider/delRedis"` //清理Redis
  44. updateESP xweb.Mapper `xweb:"/center/spider/updateesp"` //修改爬虫的节点和状态
  45. spiderModel xweb.Mapper `xweb:"/center/model"` //获取补充模型
  46. runStep xweb.Mapper `xweb:"/center/run"` //方法测试
  47. spiderPass xweb.Mapper `xweb:"/center/spider/pass"` //整体测试
  48. runPinYin xweb.Mapper `xweb:"/center/runpy"` //获取拼音
  49. saveStep xweb.Mapper `xweb:"/center/save"` //保存脚本
  50. loadModel xweb.Mapper `xweb:"/center/gmodel/(.*)"` //加载模型
  51. importdata xweb.Mapper `xweb:"/center/importdata"` //导入脚本
  52. importfile xweb.Mapper `xweb:"/center/importfile"` //批量导入爬虫
  53. oldedit xweb.Mapper `xweb:"/center/oldedit"` //老文件编辑
  54. findName xweb.Mapper `xweb:"/center/findname"` //即时查询名称
  55. checkrepeat xweb.Mapper `xweb:"/center/spider/isrepeat"` //脚本代码判重
  56. heart xweb.Mapper `xweb:"/center/heart"` //心跳监控
  57. spiderCopy xweb.Mapper `xweb:"/center/spider/copy"` //复制爬虫
  58. Base Base
  59. OtherBase OtherBase
  60. Step1 Step1
  61. Step2 Step2
  62. Step3 Step3
  63. StepRe3 StepRe3
  64. U U
  65. luaList xweb.Mapper `xweb:"/center/lualist.html"` //脚本管理
  66. user xweb.Mapper `xweb:"/center/user.html"` //用户管理
  67. delUser xweb.Mapper `xweb:"/center/user/del"` //删除用户
  68. updateUser xweb.Mapper `xweb:"/center/user/updateUser"` //修改用户信息
  69. checkUsenamer xweb.Mapper `xweb:"/center/user/checkUsenamer"` //校验用户名的唯一性
  70. checkEmail xweb.Mapper `xweb:"/center/user/checkEmail"` //校验邮箱的唯一性
  71. saveNewUser xweb.Mapper `xweb:"/center/user/saveNewUser"` //添加用户
  72. getCity xweb.Mapper `xweb:"/center/getCity"` //获取城市
  73. }
  74. const role_admin, role_examine, role_dev = 3, 2, 1 //管理员,审核员,开发员
  75. const Sp_state_0, Sp_state_1, Sp_state_2, Sp_state_3, Sp_state_4, Sp_state_5, Sp_state_6, Sp_state_7 = 0, 1, 2, 3, 4, 5, 6, 7 //0待完成,1待审核,2打回,3发布,4作废,5已上架,6已下架,7其他
  76. var spinfos sync.Map = sync.Map{}
  77. var SessMap map[string]*httpsession.Session
  78. var AutoTpl map[string]interface{}
  79. var Mails *util.Mail
  80. var Reg = regexp.MustCompile(`(http|https)://([\w]+\.)+[\w]+`)
  81. var Transfercode map[string]interface{}
  82. var LuaStateMap = map[int]string{
  83. 0: "待完成",
  84. 1: "待审核",
  85. 2: "未通过",
  86. 3: "已通过",
  87. 4: "已作废",
  88. 5: "已上架",
  89. 6: "已下架",
  90. 7: "无发布",
  91. 8: "需登录",
  92. 9: "无法处理",
  93. 10: "已删除",
  94. }
  95. func (f *Front) Login() error {
  96. username := f.GetString("username")
  97. password := f.GetString("password")
  98. f.SetSession("password", password)
  99. password = util.Se.EncodeString(password)
  100. query := bson.M{
  101. "s_name": username,
  102. "s_pass": password,
  103. }
  104. user := *mgdb.FindOne("user", query)
  105. if user != nil && user["i_delete"] == 0 {
  106. f.SetSession("userid", user["_id"].(bson.ObjectId).Hex())
  107. f.SetSession("username", user["s_fullname"])
  108. f.SetSession("loginuser", user["s_name"])
  109. f.SetSession("email", user["s_email"])
  110. f.SetSession("auth", user["i_auth"])
  111. comeintime := time.Unix(user["l_comeintime"].(int64), 0).Format("2006-01-02")
  112. f.SetSession("comeintime", comeintime)
  113. if qu.IntAll(user["i_auth"]) > role_admin {
  114. return f.Redirect("/center/user.html")
  115. } else if qu.IntAll(user["i_auth"]) == role_dev {
  116. return f.Redirect("/center/mytask")
  117. } else {
  118. return f.Redirect("/center")
  119. }
  120. } else {
  121. if username != "" {
  122. f.T["fail"] = "fail"
  123. }
  124. return f.Render("login.html", &f.T)
  125. }
  126. }
  127. //用户管理
  128. func (f *Front) User() {
  129. if f.Method() == "POST" {
  130. auth := qu.IntAll(f.GetSession("auth"))
  131. if auth > role_admin {
  132. start, _ := f.GetInteger("start")
  133. limit, _ := f.GetInteger("length")
  134. draw, _ := f.GetInteger("draw")
  135. query := bson.M{
  136. "i_delete": 0, //可用用户
  137. "i_auth": bson.M{
  138. "$lt": auth,
  139. },
  140. }
  141. user := *mgdb.Find("user", query, nil, nil, false, start, limit)
  142. count := mgdb.Count("user", query)
  143. page := start / 10
  144. for k, v := range user {
  145. v["num"] = k + 1 + page*10
  146. v["l_comeintime"] = time.Unix(v["l_comeintime"].(int64), 0).Format("2006-01-02")
  147. v["s_pass"] = util.Se.DecodeString(v["s_pass"].(string))
  148. v["userid"] = v["_id"].(bson.ObjectId).Hex()
  149. }
  150. f.ServeJson(map[string]interface{}{
  151. "draw": draw,
  152. "data": user,
  153. "recordsFiltered": count,
  154. "recordsTotal": count,
  155. })
  156. }
  157. } else {
  158. f.Render("user.html")
  159. }
  160. }
  161. //删除用户
  162. func (f *Front) DelUser() {
  163. userid := f.GetString("userid")
  164. auth := qu.IntAll(f.GetSession("auth"))
  165. if auth > role_admin {
  166. query := bson.M{
  167. "_id": bson.ObjectIdHex(userid),
  168. }
  169. update := bson.M{
  170. "$set": bson.M{
  171. "i_delete": 1,
  172. },
  173. }
  174. ok := mgdb.Update("user", query, update, false, false)
  175. if ok {
  176. f.ServeJson(map[string]interface{}{
  177. "status": "y",
  178. })
  179. } else {
  180. f.ServeJson(map[string]interface{}{
  181. "status": "n",
  182. })
  183. }
  184. } else {
  185. f.ServeJson(map[string]interface{}{
  186. "status": "e",
  187. })
  188. }
  189. }
  190. //修改用户
  191. func (f *Front) UpdateUser() {
  192. // username := f.GetString("username")
  193. password := f.GetString("password")
  194. userid := f.GetString("userid")
  195. f.SetSession("password", password)
  196. userAuth := f.GetString("userAuth")
  197. auth := qu.IntAll(f.GetSession("auth"))
  198. self := f.GetString("self")
  199. //log.Println("userid----:", userid, "username----:", username, "password----:", password, "auth----:", auth, "self----:", self)
  200. query := bson.M{
  201. "_id": bson.ObjectIdHex(userid),
  202. }
  203. update := bson.M{}
  204. if "y" == self { //修改个人信息 只修改个人密码
  205. password = util.Se.EncodeString(password)
  206. update = bson.M{
  207. "$set": bson.M{
  208. "s_pass": password,
  209. },
  210. }
  211. } else if "y" != self && auth == 4 { //修改他人信息 只修改他人权限
  212. if userAuth == "开发员" || userAuth == "审核员" || userAuth == "管理员" {
  213. switch userAuth {
  214. case "开发员":
  215. update = bson.M{
  216. "$set": bson.M{
  217. "i_auth": 1,
  218. },
  219. }
  220. case "审核员":
  221. update = bson.M{
  222. "$set": bson.M{
  223. "i_auth": 2,
  224. },
  225. }
  226. case "管理员":
  227. update = bson.M{
  228. "$set": bson.M{
  229. "i_auth": 3,
  230. },
  231. }
  232. }
  233. }
  234. } else {
  235. f.ServeJson("没有权限!")
  236. return
  237. }
  238. ok := mgdb.Update("user", query, update, false, false)
  239. if ok {
  240. f.ServeJson(map[string]interface{}{
  241. "status": "y",
  242. })
  243. } else {
  244. f.ServeJson(map[string]interface{}{
  245. "status": "n",
  246. })
  247. }
  248. }
  249. func (f *Front) CheckUsenamer() {
  250. username := f.GetString("username")
  251. if username != "" {
  252. query := bson.M{
  253. "s_name": username,
  254. }
  255. user := *mgdb.FindOne("user", query)
  256. if user != nil {
  257. f.ServeJson(map[string]interface{}{
  258. "status": "hasUser",
  259. })
  260. } else {
  261. f.ServeJson(map[string]interface{}{
  262. "status": "notHasUser",
  263. })
  264. }
  265. }
  266. }
  267. func (f *Front) CheckEmail() {
  268. email := f.GetString("email")
  269. //校验邮箱
  270. if email != "" {
  271. query := bson.M{
  272. "s_email": email,
  273. }
  274. user := *mgdb.FindOne("user", query)
  275. if user != nil {
  276. f.ServeJson(map[string]interface{}{
  277. "status": "hasEmail",
  278. })
  279. } else {
  280. f.ServeJson(map[string]interface{}{
  281. "status": "notHasEmail",
  282. })
  283. }
  284. }
  285. }
  286. //新增用户
  287. func (f *Front) SaveNewUser() {
  288. auth := qu.IntAll(f.GetSession("auth"))
  289. if auth != 4 {
  290. f.ServeJson("没有权限!")
  291. return
  292. } else {
  293. i_auth := 1
  294. username := f.GetString("username")
  295. password := f.GetString("password")
  296. relname := f.GetString("relname")
  297. password = util.Se.EncodeString(password)
  298. email := f.GetString("email")
  299. userAuth := f.GetString("userAuth")
  300. if userAuth == "开发员" {
  301. i_auth = 1
  302. } else if userAuth == "审核员" {
  303. i_auth = 2
  304. } else {
  305. i_auth = 3
  306. }
  307. time := time.Now().Unix()
  308. save := bson.M{
  309. "s_name": username,
  310. "s_fullname": relname,
  311. "s_email": email,
  312. "s_pass": password,
  313. "i_auth": i_auth,
  314. "i_delete": 0,
  315. "l_comeintime": time,
  316. }
  317. ok := mgdb.Save("user", save)
  318. if ok != "" {
  319. f.ServeJson(map[string]interface{}{
  320. "status": "y",
  321. })
  322. } else {
  323. f.ServeJson(map[string]interface{}{
  324. "status": "n",
  325. })
  326. }
  327. }
  328. }
  329. func (f *Front) Logout() {
  330. email := f.GetSession("email").(string)
  331. f.DelSession("username")
  332. f.DelSession("userid")
  333. f.DelSession("email")
  334. f.DelSession("user")
  335. f.DelSession("loginuser")
  336. f.DelSession("auth")
  337. delete(SessMap, email)
  338. f.Redirect("/center")
  339. }
  340. //控制中心
  341. func (f *Front) LoadIndex() {
  342. auth := qu.IntAll(f.GetSession("auth"))
  343. if f.Method() == "POST" {
  344. start, _ := f.GetInteger("start")
  345. limit, _ := f.GetInteger("length")
  346. draw, _ := f.GetInteger("draw")
  347. searchStr := f.GetString("search[value]")
  348. event, _ := f.GetInteger("taskEvent") //节点
  349. //searchN := strings.Replace(searchStr, " ", "", -1)
  350. //search := strings.Replace(searchN, "\n", "", -1)
  351. search := strings.TrimSpace(searchStr)
  352. state, _ := f.GetInteger("state")
  353. urgency, _ := f.GetInteger("urgency") //节点
  354. platform := f.GetString("platform")
  355. query := bson.M{}
  356. if event > -1 {
  357. query["event"] = event
  358. }
  359. if urgency > -1 {
  360. query["urgency"] = urgency
  361. }
  362. if platform != "-1" {
  363. query["platform"] = platform
  364. }
  365. if auth == role_examine { //审核员
  366. if state > -1 {
  367. query["state"] = state
  368. } else {
  369. query["state"] = Sp_state_1
  370. }
  371. if search != "" {
  372. query["$or"] = []interface{}{
  373. bson.M{"code": bson.M{"$regex": search}},
  374. bson.M{"createuser": bson.M{"$regex": search}},
  375. bson.M{"param_common.1": bson.M{"$regex": search}},
  376. }
  377. }
  378. } else if auth == role_dev { //开发员
  379. if state > -1 {
  380. query["state"] = state
  381. }
  382. query["createuserid"] = f.GetSession("userid")
  383. query["$or"] = []interface{}{
  384. bson.M{"code": bson.M{"$regex": search}},
  385. bson.M{"createuser": bson.M{"$regex": search}},
  386. bson.M{"param_common.1": bson.M{"$regex": search}},
  387. }
  388. } else { //管理员
  389. if state > -1 {
  390. query["state"] = state
  391. }
  392. query["$or"] = []interface{}{
  393. bson.M{"code": bson.M{"$regex": search}},
  394. bson.M{"createuser": bson.M{"$regex": search}},
  395. bson.M{"param_common.1": bson.M{"$regex": search}},
  396. }
  397. }
  398. sort := `{"%s":%d}`
  399. orderIndex := f.GetString("order[0][column]")
  400. orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
  401. orderType := 1
  402. if f.GetString("order[0][dir]") != "asc" {
  403. orderType = -1
  404. }
  405. if orderName == "param_common" {
  406. orderName = orderName + ".1"
  407. }
  408. sort = fmt.Sprintf(sort, orderName, orderType)
  409. page := start / 10
  410. //log.Println("sort", sort, orderName)
  411. luas := *mgdb.Find("luaconfig", query, sort, list_fields, false, start, limit)
  412. count := mgdb.Count("luaconfig", query)
  413. for k, v := range luas {
  414. v["num"] = k + 1 + page*10
  415. if v["modifytime"] != nil {
  416. v["modifytime"] = time.Unix(v["modifytime"].(int64), 0).Format("2006-01-02 15:04:05")
  417. } else {
  418. v["modifytime"] = "-"
  419. }
  420. if v["modifyuser"] == nil {
  421. v["modifytime"] = "-"
  422. }
  423. v["encode"] = util.Se.Encode2Hex(fmt.Sprint(v["code"]))
  424. if v["event"] == nil { //节点
  425. v["event"] = 0
  426. }
  427. //v["state"] = LuaStateMap[qu.IntAll(v["state"])]
  428. }
  429. f.ServeJson(map[string]interface{}{"draw": draw, "data": luas, "recordsFiltered": count, "recordsTotal": count})
  430. } else {
  431. events := []string{}
  432. for k, _ := range util.Config.Uploadevents {
  433. events = append(events, k)
  434. }
  435. sort.Strings(events)
  436. f.T["events"] = events
  437. f.Render("index.html", &f.T)
  438. }
  439. }
  440. func (f *Front) Checkrepeat() {
  441. code := f.GetString("code")
  442. one := *mgdb.FindOne("luaconfig", bson.M{"code": code})
  443. if len(one) > 0 {
  444. f.ServeJson("y")
  445. } else {
  446. f.ServeJson("n")
  447. }
  448. }
  449. //新建
  450. func (f *Front) Spidernew() error {
  451. auth := qu.IntAll(f.GetSession("auth"))
  452. if auth != role_admin {
  453. return nil
  454. }
  455. copy := f.GetString("copy")
  456. if copy != "" {
  457. one := *mgdb.FindOne("luaconfig", bson.M{"code": copy})
  458. delete(one, "_id")
  459. delete(one, "code")
  460. base := one["param_common"].([]interface{})
  461. base[0] = ""
  462. base[1] = ""
  463. one["param_common"] = base
  464. f.T["lua"] = one
  465. }
  466. f.T["isflow"] = 1 //新建爬虫时,初始化isflow的值
  467. f.T["actiontext"] = "新建"
  468. f.T["restate"] = 4 //此处设置restate=4无意义,只为了页面不报错
  469. return f.Render("spideredit.html", &f.T)
  470. }
  471. //得到模型
  472. func (f *Front) SpiderModel() {
  473. f.ServeJson(util.Config.Model)
  474. }
  475. func (f *Front) RunPinYin() {
  476. word := f.GetString("word")
  477. str := gopinyin.Convert(word, true)
  478. f.Write(str)
  479. }
  480. type U struct {
  481. User string
  482. Name string
  483. Pwd string
  484. }
  485. func (f *Front) Reg() {
  486. }
  487. func (f *Front) Importfile() {
  488. auth := qu.IntAll(f.GetSession("auth"))
  489. if auth != role_admin {
  490. f.ServeJson("没有权限")
  491. return
  492. }
  493. if f.Method() == "POST" {
  494. mf, _, err := f.GetFile("xlsx")
  495. errorinfo := map[string]interface{}{}
  496. if err == nil {
  497. binary, _ := ioutil.ReadAll(mf)
  498. xls, _ := xlsx.OpenBinary(binary)
  499. sheet := xls.Sheets[0]
  500. rows := sheet.Rows
  501. for k, v := range rows {
  502. if k != 0 {
  503. cells := v.Cells
  504. if cells[1].Value != "" {
  505. o := make(map[string]interface{})
  506. o["name"] = cells[0].Value
  507. o["code"] = cells[1].Value
  508. o["channel"] = cells[2].Value
  509. o["channeladdr"] = cells[3].Value
  510. o["author"] = cells[4].Value
  511. o["timestamp"] = time.Now().Unix()
  512. o["status"] = 1
  513. o["next"] = cells[4].Value
  514. o["event"] = cells[5].Value
  515. o["historyevent"] = cells[6].Value
  516. if cells[7].Value == "是" {
  517. o["isflow"] = 1
  518. } else {
  519. o["isflow"] = 0
  520. }
  521. if cells[8].Value == "紧急" {
  522. o["urgency"] = 1
  523. } else {
  524. o["urgency"] = 0
  525. }
  526. o["platform"] = cells[9].Value
  527. o["spidercompete"] = true
  528. // table := cells[6].Value
  529. // o["table"] = table
  530. // o["transfercode"] = qu.IntAll(Transfercode[table])
  531. query := bson.M{"code": cells[1].Value}
  532. rs := *mgdb.FindOne("import", query)
  533. if len(rs) > 0 {
  534. errorinfo[cells[1].Value] = "第" + strconv.Itoa(k) + "行重复,已经过滤"
  535. } else {
  536. ok, name := pf(o) //保存爬虫
  537. if ok == false {
  538. errorinfo[cells[1].Value] = "第" + strconv.Itoa(k) + "行找不到作者,已经过滤"
  539. } else {
  540. o["author"] = name
  541. mgdb.Save("import", o)
  542. }
  543. }
  544. }
  545. }
  546. }
  547. f.ServeJson(errorinfo)
  548. } else {
  549. f.ServeJson(false)
  550. }
  551. }
  552. }
  553. func pf(o map[string]interface{}) (bool, string) {
  554. AutoTpl["Base.SpiderName"] = o["name"]
  555. AutoTpl["Base.SpiderCode"] = o["code"]
  556. AutoTpl["Base.SpiderChannel"] = o["channel"]
  557. AutoTpl["Base.SpiderTargetChannelUrl"] = o["channeladdr"]
  558. author := o["author"].(string)
  559. one := *mgdb.FindOne("user", bson.M{"s_email": author})
  560. id := one["_id"].(bson.ObjectId).Hex()
  561. if len(one) == 0 {
  562. return false, ""
  563. }
  564. common := []interface{}{
  565. AutoTpl["Base.SpiderCode"],
  566. AutoTpl["Base.SpiderName"],
  567. AutoTpl["Base.SpiderChannel"],
  568. AutoTpl["Base.SpiderDownDetailPage"],
  569. AutoTpl["Base.SpiderStartPage"],
  570. AutoTpl["Base.SpiderMaxPage"],
  571. AutoTpl["Base.SpiderRunRate"],
  572. //AutoTpl["Base.Spider2Collection"],
  573. "bidding", //爬虫导入新建默认为bidding
  574. AutoTpl["Base.SpiderPageEncoding"],
  575. AutoTpl["Base.SpiderStoreMode"],
  576. AutoTpl["Base.SpiderStoreToMsgEvent"],
  577. AutoTpl["Base.SpiderTargetChannelUrl"],
  578. AutoTpl["Base.SpiderLastDownloadTime"],
  579. AutoTpl["Base.SpiderIsHistoricalMend"],
  580. AutoTpl["Base.SpiderIsMustDownload"],
  581. }
  582. ptime := []interface{}{
  583. AutoTpl["Step1.DateFormat"],
  584. AutoTpl["Step1.Address"],
  585. AutoTpl["Step1.ContentChooser"],
  586. }
  587. list := []interface{}{
  588. AutoTpl["Step2.Listadd"],
  589. AutoTpl["Step2.Listadds"],
  590. AutoTpl["Step2.BlockChooser"],
  591. AutoTpl["Step2.AddressChooser"],
  592. AutoTpl["Step2.TitleChooser"],
  593. AutoTpl["Step2.DateChooser"],
  594. AutoTpl["Step2.DateFormat"],
  595. }
  596. content := []interface{}{
  597. AutoTpl["Step3.ContentChooser"],
  598. AutoTpl["Step3.ElementChooser"],
  599. }
  600. param := map[string]interface{}{}
  601. param["param_common"] = common
  602. param["param_common"] = common
  603. //向导模式
  604. param["param_time"] = ptime
  605. param["param_list"] = list
  606. param["param_content"] = content
  607. param["type_time"] = 0
  608. param["type_list"] = 0
  609. param["type_content"] = 0
  610. //专家模式
  611. param["str_time"] = ""
  612. param["str_list"] = ""
  613. param["str_content"] = ""
  614. param["comeintime"] = time.Now().Unix()
  615. param["code"] = o["code"]
  616. param["createuser"] = one["s_name"]
  617. param["createuserid"] = id
  618. param["createuseremail"] = one["s_email"]
  619. param["modifyuser"] = one["s_name"]
  620. param["modifyuserid"] = id
  621. param["modifytime"] = time.Now().Unix()
  622. param["state"] = 0 //未完成
  623. if qu.IntAll(o["event"]) > 0 {
  624. param["event"] = qu.IntAll(o["event"])
  625. }
  626. s_model := "bid"
  627. configModel := util.Config.Model[s_model]
  628. model := map[string]interface{}{}
  629. for k, _ := range configModel {
  630. model[k] = ""
  631. }
  632. param["model"] = model
  633. param["next"] = o["next"]
  634. param["urgency"] = o["urgency"]
  635. param["isflow"] = o["isflow"]
  636. param["spidertype"] = "history"
  637. historyevent := qu.ObjToString(o["historyevent"])
  638. if movevent, ok := util.Config.Uploadevents[historyevent].(string); ok && movevent != "" {
  639. param["spidermovevent"] = movevent
  640. } else {
  641. param["spidermovevent"] = "7700"
  642. }
  643. param["historyevent"] = qu.IntAll(o["historyevent"])
  644. param["spiderhistorymaxpage"] = 1
  645. param["platform"] = o["platform"]
  646. param["spidercompete"] = o["spidercompete"]
  647. //qu.Debug("param---", param)
  648. issave := spider.SaveSpider(o["code"].(string), param)
  649. return issave, one["s_name"].(string)
  650. }
  651. func (f *Front) Importdata() {
  652. auth := qu.IntAll(f.GetSession("auth"))
  653. if auth == role_admin {
  654. if f.Method() == "GET" {
  655. f.Render("import.html")
  656. } else {
  657. rss := *mgdb.Find("import", nil, `{"timestamp": -1}`, nil, false, -1, -1)
  658. f.ServeJson(map[string]interface{}{
  659. "data": rss,
  660. })
  661. }
  662. } else {
  663. f.Write("您没有导入脚本的权限")
  664. }
  665. }
  666. func Wlog(name, code, man, manid, types string, content map[string]interface{}) {
  667. obj := bson.M{
  668. "name": name,
  669. "code": code,
  670. "man": man,
  671. "manid": manid,
  672. "types": types,
  673. "time": time.Now().Unix(),
  674. "content": content,
  675. }
  676. mgdb.Save("lua_logs", obj)
  677. }
  678. func (f *Front) Oldedit() {
  679. if f.Method() == "GET" {
  680. f.Render("oldedit.html")
  681. } else {
  682. }
  683. }
  684. func (f *Front) FindName() {
  685. words := f.GetString("words")
  686. if words == "" {
  687. f.ServeJson(bson.M{"error": "null"})
  688. }
  689. query := bson.M{"$or": []interface{}{
  690. bson.M{"param_common.0": bson.M{"$regex": words}},
  691. bson.M{"param_common.1": bson.M{"$regex": words}},
  692. bson.M{"createuser": bson.M{"$regex": words}},
  693. }, "oldlua": bson.M{"$exists": false}}
  694. rs := *mgdb.Find("luaconfig", query, bson.M{"modifytime": -1}, bson.M{"param_common": 1}, false, -1, -1)
  695. if len(rs) > 0 {
  696. f.ServeJson(bson.M{"data": rs})
  697. } else {
  698. f.ServeJson(bson.M{"error": "data"})
  699. }
  700. }
  701. //分配爬虫
  702. func (f *Front) Assign() {
  703. auth := qu.IntAll(f.GetSession("auth"))
  704. if auth != role_admin {
  705. f.Write("n")
  706. return
  707. }
  708. ids := f.GetString("ids")
  709. codes := f.GetString("codes")
  710. email := f.GetString("email")
  711. idarr := strings.Split(ids, ",")
  712. codesarr := strings.Split(codes, ",")
  713. var idsinter []interface{}
  714. isemail := strings.Index(ids, "@")
  715. user := *mgdb.FindOne("user", bson.M{"s_email": email})
  716. if len(user) > 0 {
  717. userid := user["_id"].(bson.ObjectId).Hex()
  718. name := user["s_name"].(string)
  719. var query bson.M
  720. if isemail > -1 {
  721. query = bson.M{
  722. "createuseremail": bson.M{
  723. "$in": idarr,
  724. },
  725. }
  726. } else {
  727. idsinter = make([]interface{}, len(idarr))
  728. for k, v := range idarr {
  729. idsinter[k] = bson.ObjectIdHex(v)
  730. }
  731. query = bson.M{
  732. "_id": bson.M{
  733. "$in": idsinter,
  734. },
  735. }
  736. }
  737. set := bson.M{
  738. "$set": bson.M{
  739. "createuserid": userid,
  740. "createuser": user["s_name"],
  741. "createuseremail": user["s_email"],
  742. "modifyuser": user["s_name"],
  743. "modifyuserid": userid,
  744. },
  745. }
  746. b := mgdb.Update("luaconfig", query, set, false, true)
  747. if b {
  748. f.Write("y")
  749. editModify(codesarr, userid, name) //分配
  750. } else {
  751. f.Write("n")
  752. }
  753. } else {
  754. f.Write("null")
  755. }
  756. }
  757. //修改维护人
  758. func editModify(codesarr []string, userid, name string) {
  759. //修改modifyid和modify
  760. for _, v := range codesarr {
  761. query := bson.M{
  762. "s_code": v,
  763. "i_state": bson.M{
  764. "$ne": 4,
  765. },
  766. }
  767. task := *mgdb.Find("task", query, nil, nil, false, -1, -1)
  768. if len(task) > 0 {
  769. for _, v := range task { //循环 修改任务
  770. update := bson.M{
  771. "$set": bson.M{
  772. "s_modify": name,
  773. "s_modifyid": userid,
  774. },
  775. }
  776. queryT := bson.M{
  777. "_id": v["_id"],
  778. }
  779. flag := mgdb.Update("task", queryT, update, false, false)
  780. log.Println("分配修改任务维护人:", flag)
  781. }
  782. } else {
  783. continue
  784. }
  785. }
  786. }
  787. //清理Redis
  788. func (f *Front) DelRedis() {
  789. hrefs := f.GetString("href")
  790. hrefsarr := strings.Split(hrefs, ",")
  791. auth := qu.IntAll(f.GetSession("auth"))
  792. err := []string{}
  793. if auth == role_admin { //权限控制
  794. if len(hrefsarr) > 0 {
  795. for k1, h := range hrefsarr {
  796. href := Reg.FindString(h)
  797. if href != "" {
  798. href = "url_repeat_" + href + "*"
  799. res := redis.GetKeysByPattern("title_repeat_judgement", href)
  800. if res != nil {
  801. for _, v := range res {
  802. hf := string(v.([]uint8))
  803. b := redis.Del("title_repeat_judgement", hf)
  804. if !b {
  805. err = append(err, "第"+strconv.Itoa(k1+1)+"个")
  806. }
  807. }
  808. }
  809. } else {
  810. err = append(err, "第"+strconv.Itoa(k1+1)+"个")
  811. }
  812. }
  813. }
  814. } else {
  815. err = append(err, "没有权限")
  816. }
  817. f.ServeJson(err)
  818. }
  819. func (f *Front) UpdateESP() {
  820. val := f.GetString("val")
  821. w := f.GetString("w")
  822. id := f.GetString("id")
  823. query := map[string]interface{}{
  824. "_id": qu.StringTOBsonId(id),
  825. }
  826. set := map[string]interface{}{}
  827. update := map[string]interface{}{
  828. "$set": set,
  829. }
  830. if w == "state" { //修改状态为待完成
  831. set["state"] = 0
  832. } else if w == "platform" {
  833. set["platform"] = val
  834. } else { //修改节点
  835. event, _ := strconv.Atoi(val)
  836. set["event"] = event
  837. set["historyevent"] = event
  838. //state := f.GetString("s")
  839. //if state == "5" { //已上架状态改为下架
  840. code := f.GetString("c")
  841. set["state"] = 6
  842. b, err := UpStateAndUpSpider(code, "", "", "", Sp_state_6) //线上爬虫下架
  843. if !b || err != nil {
  844. f.Write("n")
  845. return
  846. }
  847. //}
  848. }
  849. if mgdb.Update("luaconfig", query, update, false, false) {
  850. log.Println("Id:", id, " Update", w, val, "Success")
  851. f.Write("y")
  852. } else {
  853. log.Println("Id:", id, " Update", w, val, "Failed")
  854. f.Write("n")
  855. }
  856. f.Write("n")
  857. }
  858. func (f *Front) GetCity() {
  859. area := f.GetString("area")
  860. cityArr := []string{}
  861. cityArr = u.Province[area]
  862. f.ServeJson(cityArr)
  863. }
  864. func (f *Front) SpiderCopy() {
  865. defer qu.Catch()
  866. code := f.GetString("code")
  867. code = strings.TrimSpace(code)
  868. success := false
  869. text := ""
  870. encode := ""
  871. if code != "" {
  872. lua := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": code})
  873. if len(lua) > 0 {
  874. codeOld := qu.ObjToString(lua["code"])
  875. codeNew := codeOld + "_bu"
  876. luaTmp := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": codeNew})
  877. if len(luaTmp) > 0 {
  878. f.ServeJson(map[string]interface{}{"success": false, "text": "已存在补漏爬虫"})
  879. return
  880. }
  881. lua["code"] = codeNew
  882. if param_common := lua["param_common"].([]interface{}); len(param_common) < 13 {
  883. f.ServeJson(map[string]interface{}{"success": false, "text": "脚本异常,请联系管理员"})
  884. return
  885. } else {
  886. param_common = param_common[:13]
  887. param_common[0] = codeNew //code
  888. param_common = append(param_common, true) //历史补漏
  889. param_common = append(param_common, false) //强制下载
  890. lua["param_common"] = param_common
  891. }
  892. str_list := qu.ObjToString(lua["str_list"])
  893. lua["str_list"] = strings.ReplaceAll(str_list, codeOld, codeNew)
  894. lua["comeintime"] = time.Now().Unix()
  895. lua["modifytime"] = time.Now().Unix()
  896. lua["spidertype"] = "increment"
  897. lua["event"] = 7000
  898. lua["state"] = 6
  899. id := mgdb.Save("luaconfig", lua)
  900. if id != "" {
  901. success = true
  902. encode = util.Se.Encode2Hex(codeNew)
  903. } else {
  904. text = "新爬虫保存失败"
  905. }
  906. } else {
  907. text = "未查询到复制原"
  908. }
  909. } else {
  910. text = "脚本不能为空"
  911. }
  912. f.ServeJson(map[string]interface{}{"success": success, "text": text, "encode": encode})
  913. }