|
@@ -0,0 +1,336 @@
|
|
|
+package front
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "github.com/donnie4w/go-logger/logger"
|
|
|
+ "io/ioutil"
|
|
|
+ "mime/multipart"
|
|
|
+ qu "qfw/util"
|
|
|
+ . "spiderutil"
|
|
|
+ "strings"
|
|
|
+ . "task"
|
|
|
+ "time"
|
|
|
+ . "util"
|
|
|
+)
|
|
|
+
|
|
|
+/*
|
|
|
+ spider_task程序统计spider_warn表中异常数据,并做同一条数据不同异常信息过滤入spider_warn_err表
|
|
|
+*/
|
|
|
+
|
|
|
+func (f *Front) WarnList() {
|
|
|
+ defer qu.Catch()
|
|
|
+ if f.Method() == "POST" {
|
|
|
+ defer qu.Catch()
|
|
|
+ repeat, _ := f.GetInteger("repeat")
|
|
|
+ from := f.GetString("from")
|
|
|
+ start, _ := f.GetInteger("start")
|
|
|
+ limit, _ := f.GetInteger("length")
|
|
|
+ draw, _ := f.GetInteger("draw")
|
|
|
+ startTime, _ := f.GetInt("starttime")
|
|
|
+ searchStr := f.GetString("search[value]")
|
|
|
+ search := strings.TrimSpace(searchStr)
|
|
|
+ qu.Debug(startTime, start, limit, draw)
|
|
|
+ sort := `{"%s":%d}`
|
|
|
+ orderIndex := f.GetString("order[0][column]")
|
|
|
+ orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
|
|
|
+ orderType := 1
|
|
|
+ if f.GetString("order[0][dir]") != "asc" {
|
|
|
+ orderType = -1
|
|
|
+ }
|
|
|
+ sort = fmt.Sprintf(sort, orderName, orderType)
|
|
|
+ query := map[string]interface{}{
|
|
|
+ "ok": false,
|
|
|
+ }
|
|
|
+ if repeat != -1 {
|
|
|
+ query["repeat"] = repeat == 1
|
|
|
+ }
|
|
|
+ if from != "-1" {
|
|
|
+ query["from"] = from
|
|
|
+ }
|
|
|
+ if startTime > 0 {
|
|
|
+ query["comeintime"] = map[string]interface{}{
|
|
|
+ "$gte": startTime,
|
|
|
+ "$lt": startTime + 86400,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if search != "" {
|
|
|
+ query["$or"] = []interface{}{
|
|
|
+ map[string]interface{}{"site": map[string]interface{}{"$regex": search}},
|
|
|
+ map[string]interface{}{"code": map[string]interface{}{"$regex": search}},
|
|
|
+ map[string]interface{}{"title": map[string]interface{}{"$regex": search}},
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fields := map[string]interface{}{
|
|
|
+ "data": 0,
|
|
|
+ }
|
|
|
+ count := MgoS.Count("spider_warn_err", query)
|
|
|
+ qu.Debug("query:", query, "sort:", sort, count)
|
|
|
+ // page := start / 10
|
|
|
+ list, _ := MgoS.Find("spider_warn_err", query, sort, fields, false, start, limit)
|
|
|
+ // for _, f := range *list {
|
|
|
+ // v["num"] = k + 1 + page*10
|
|
|
+ // }
|
|
|
+ f.ServeJson(map[string]interface{}{
|
|
|
+ "data": list,
|
|
|
+ "draw": draw,
|
|
|
+ "recordsFiltered": count,
|
|
|
+ "recordsTotal": count,
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ f.Render("lua/warnlist.html", &f.T)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (f *Front) WarnEdit() {
|
|
|
+ defer qu.Catch()
|
|
|
+ id := f.GetString("id")
|
|
|
+ num, _ := f.GetInteger("num")
|
|
|
+ data := map[string]interface{}{}
|
|
|
+ bidId := ""
|
|
|
+ one, _ := MgoS.FindById("spider_warn_err", id, map[string]interface{}{"data": 1, "field": 1})
|
|
|
+ if one != nil && len(*one) > 0 {
|
|
|
+ data, _ = (*one)["data"].(map[string]interface{})
|
|
|
+ detail := qu.ObjToString((data)["detail"])
|
|
|
+ contenthtml := qu.ObjToString((data)["contenthtml"])
|
|
|
+ summary := qu.ObjToString((data)["summary"])
|
|
|
+ f.T["detail"] = detail
|
|
|
+ f.T["contenthtml"] = contenthtml
|
|
|
+ f.T["summary"] = summary
|
|
|
+ delete(data, "_id")
|
|
|
+ delete(data, "detail")
|
|
|
+ delete(data, "contenthtml")
|
|
|
+ delete(data, "summary")
|
|
|
+ delete(data, "l_np_publishtime")
|
|
|
+ }
|
|
|
+ f.T["id"] = id //spider_warn_err id
|
|
|
+ f.T["bid"] = bidId //bidding id
|
|
|
+ f.T["data"] = data
|
|
|
+ f.T["num"] = num
|
|
|
+ f.Render("lua/warnedit.html", &f.T)
|
|
|
+}
|
|
|
+
|
|
|
+func (f *Front) UploadFile() {
|
|
|
+ defer qu.Catch()
|
|
|
+ success := false
|
|
|
+ msg := ""
|
|
|
+ files := f.GetString("files")
|
|
|
+ id := f.GetString("id")
|
|
|
+ qu.Debug(id)
|
|
|
+ mf, err := f.GetFiles()
|
|
|
+ if err != nil {
|
|
|
+ msg = "附件上传失败"
|
|
|
+ } else {
|
|
|
+ fileMap := make(map[string]*multipart.File)
|
|
|
+ for _, hf := range mf {
|
|
|
+ f, _ := hf.Open()
|
|
|
+ fileMap[hf.Filename] = &f
|
|
|
+ }
|
|
|
+ var fileArr []map[string]interface{}
|
|
|
+ if err := json.Unmarshal([]byte(files), &fileArr); err != nil {
|
|
|
+ logger.Info("UserInfo Unmarshal Failed:", err)
|
|
|
+ msg = "附件解析失败"
|
|
|
+ } else {
|
|
|
+ var arr []map[string]interface{}
|
|
|
+ for _, m := range fileArr {
|
|
|
+ filename := qu.ObjToString(m["filename"])
|
|
|
+ org_url := qu.ObjToString(m["org_url"])
|
|
|
+ f := fileMap[filename]
|
|
|
+ if f == nil {
|
|
|
+ msg = "附件解析失败"
|
|
|
+ goto L
|
|
|
+ }
|
|
|
+ bt, _ := ioutil.ReadAll(*f)
|
|
|
+ key := GetHashKey(bt) + TypeByExt(filename)
|
|
|
+ b, _ := OssPutObject(key, bytes.NewReader(bt))
|
|
|
+ if b {
|
|
|
+ tmp := make(map[string]interface{})
|
|
|
+ tmp["org_url"] = org_url
|
|
|
+ tmp["filename"] = filename
|
|
|
+ tmp["ftype"] = strings.Replace(TypeByExt(filename), ".", "", -1)
|
|
|
+ tmp["fid"] = key
|
|
|
+ tmp["url"] = "oss"
|
|
|
+ br := bytes.NewReader(bt)
|
|
|
+ tmp["size"] = qu.ConvertFileSize(br.Len())
|
|
|
+ arr = append(arr, tmp)
|
|
|
+ } else {
|
|
|
+ msg = "文件上传oss失败"
|
|
|
+ goto L
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(arr) > 0 {
|
|
|
+ attachments := map[string]interface{}{}
|
|
|
+ for i, tmp := range arr {
|
|
|
+ attachments[fmt.Sprint(i+1)] = tmp
|
|
|
+ }
|
|
|
+ success = MgoS.UpdateById("spider_warn_err", id, map[string]interface{}{"$set": map[string]interface{}{"data.projectinfo.attachments": attachments}})
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+L:
|
|
|
+ f.ServeJson(map[string]interface{}{"success": success, "msg": msg})
|
|
|
+}
|
|
|
+
|
|
|
+func (f *Front) LuaDataSend() {
|
|
|
+ defer qu.Catch()
|
|
|
+ success := false
|
|
|
+ msg := ""
|
|
|
+ id := f.GetString("id")
|
|
|
+ stype, _ := f.GetInteger("stype")
|
|
|
+ qu.Debug("id:", id, "stype:", stype)
|
|
|
+ if stype == 2 { //无需更新:只在spider_warn_err上打标记
|
|
|
+ TagToSpiderWarnErr(stype, id, "无需修改或发布", map[string]interface{}{})
|
|
|
+ success = true
|
|
|
+ } else {
|
|
|
+ bid := f.GetString("bid")
|
|
|
+ qu.Debug("bidding id:", bid)
|
|
|
+ reasons := f.GetString("reasons")
|
|
|
+ updateStr := f.GetStringComm("update")
|
|
|
+ update := map[string]interface{}{}
|
|
|
+ if err := json.Unmarshal([]byte(updateStr), &update); err != nil {
|
|
|
+ qu.Debug("data Unmarshal Failed:", err)
|
|
|
+ }
|
|
|
+ //处理字段
|
|
|
+ if len(update) > 0 {
|
|
|
+ FormatFields(update)
|
|
|
+ FormatNumber(update) //解决超大金额转成科学计数法的问题
|
|
|
+ }
|
|
|
+ one, _ := MgoS.FindById("spider_warn_err", id, map[string]interface{}{"data": 1})
|
|
|
+ if len(*one) > 0 {
|
|
|
+ data, _ := (*one)["data"].(map[string]interface{})
|
|
|
+ mustFiledNum := 0 //记录更新有效字段个数
|
|
|
+ result := map[string]interface{}{}
|
|
|
+ for k, _ := range Fields {
|
|
|
+ if v := update[k]; v != nil {
|
|
|
+ result[k] = v
|
|
|
+ mustFiledNum++
|
|
|
+ } else if v := data[k]; v != nil {
|
|
|
+ mustFiledNum++
|
|
|
+ result[k] = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if mustFiledNum > 0 {
|
|
|
+ flag, id, coll := SaveObj(4002, "title", result)
|
|
|
+ update["sendflag"] = flag
|
|
|
+ update["biddingid"] = id
|
|
|
+ update["biddingcoll"] = coll
|
|
|
+ TagToSpiderWarnErr(stype, id, reasons, update) //spider_warn_err日志
|
|
|
+ success = true
|
|
|
+ } else {
|
|
|
+ msg = "推送失败,无有效字段"
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ msg = "未找到该数据"
|
|
|
+ }
|
|
|
+ //if stype == 1 { //修复更新
|
|
|
+ // if bid == "" {
|
|
|
+ // rep = "bidding id 为空"
|
|
|
+ // goto L
|
|
|
+ // }
|
|
|
+ // old_data, _ := JYMgo.FindById("bidding", bid, nil) //查询原始信息,用作修改日志记录
|
|
|
+ // if old_data != nil && len(*old_data) > 0 {
|
|
|
+ // delete(*old_data, "_id")
|
|
|
+ // if (*old_data)["modifyinfo"] != nil {
|
|
|
+ // if m, ok := (*old_data)["modifyinfo"].(map[string]interface{}); ok { //合并modifyinfo
|
|
|
+ // for k, v := range m {
|
|
|
+ // modifyinfo[k] = v
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // rep = "未找到bidding数据,id:" + bid
|
|
|
+ // goto L
|
|
|
+ // }
|
|
|
+ // if len(modifyinfo) > 0 { //记录修改字段
|
|
|
+ // update["modifyinfo"] = modifyinfo
|
|
|
+ // }
|
|
|
+ // b := JYMgo.UpdateById("bidding", bid, map[string]interface{}{"$set": update}) //更新
|
|
|
+ // if b { //更新成功 //更新成功 重生bidding索引 项目合并
|
|
|
+ // qu.Debug("更新成功")
|
|
|
+ // udptask.BiddingIndexUdp(bid, "bidding") //coll:bidding 是因为调用此方法的数据都是增量数据
|
|
|
+ // if IsModifyPro(modifyinfo) { //修改了影响项目合并的字段 进行项目合并
|
|
|
+ // udptask.ProjectSetUdp(bid, "bidding")
|
|
|
+ // }
|
|
|
+ // delName1 := RedisDelKey1 + "*_" + bid
|
|
|
+ // redis.DelByCodePattern(RedisJYName, delName1)
|
|
|
+ // delete(update, "modifyinfo")
|
|
|
+ // l.SaveUpdateLog(2, reasons, bid, *old_data, update, modifyinfo) //标签日志
|
|
|
+ // TagToSpiderWarnErr(stype, id, reasons, update) //spider_warn_err日志
|
|
|
+ // success = true
|
|
|
+ // } else { //更新失败
|
|
|
+ // rep = "更新bidding失败,id:" + bid
|
|
|
+ // qu.Debug("更新失败")
|
|
|
+ // }
|
|
|
+ //} else { //3:修复发布 4:直接发布
|
|
|
+ // href := qu.ObjToString(update["href"])
|
|
|
+ // shaid := qu.ObjToString(update["s_sha"])
|
|
|
+ // competehref := qu.ObjToString(update["competehref"]) //竞品数据
|
|
|
+ // iscompete, _ := update["iscompete"].(bool) //新爬虫数据
|
|
|
+ // newId := primitive.NewObjectID()
|
|
|
+ // newSid := mongodb.BsonIdToSId(newId)
|
|
|
+ // qu.Debug("newId:", newSid)
|
|
|
+ // update["_id"] = newId
|
|
|
+ // if competehref != "" { //竞品爬虫
|
|
|
+ // href = competehref
|
|
|
+ // update["href"] = GetJyHref(newSid)
|
|
|
+ // } else if iscompete { //新爬虫
|
|
|
+ // update["competehref"] = "#"
|
|
|
+ // }
|
|
|
+ // delete(update, "iscompete")
|
|
|
+ // update["comeintime"] = time.Now().Unix() //更新入库时间
|
|
|
+ // update["modifyinfo"] = map[string]interface{}{} //添加空modifyinfo作为标识
|
|
|
+ // PutHrefRedis(href, shaid, newSid) //添加href进redis
|
|
|
+ // qu.Debug(update)
|
|
|
+ // JYMgo.SaveByOriID("bidding", update) //入bidding
|
|
|
+ // l.SaveUpdateLog(1, reasons, newSid, map[string]interface{}{}, update, map[string]interface{}{}) //标签日志
|
|
|
+ // TagToSpiderWarnErr(stype, id, reasons, update) //spider_warn_err日志
|
|
|
+ // success = true
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ f.ServeJson(map[string]interface{}{"success": success, "msg": msg})
|
|
|
+}
|
|
|
+
|
|
|
+func (f *Front) SpiderUpdate() {
|
|
|
+ defer qu.Catch()
|
|
|
+ state, _ := f.GetInteger("state")
|
|
|
+ reason := "批量处理无需更新"
|
|
|
+ if state == 4 { //批量直接发布
|
|
|
+ reason = "批量处理直接发布"
|
|
|
+ }
|
|
|
+ ids := strings.Split(f.GetString("ids"), ",")
|
|
|
+ for _, id := range ids {
|
|
|
+ TagToSpiderWarnErr(state, id, reason, map[string]interface{}{})
|
|
|
+ }
|
|
|
+ f.ServeJson(map[string]interface{}{"success": true})
|
|
|
+}
|
|
|
+
|
|
|
+func TagToSpiderWarnErr(state int, id, reason string, update map[string]interface{}) {
|
|
|
+ defer qu.Catch()
|
|
|
+ set := map[string]interface{}{
|
|
|
+ "ok": true,
|
|
|
+ "state": state,
|
|
|
+ "reason": reason,
|
|
|
+ "updatetime": time.Now().Unix(),
|
|
|
+ }
|
|
|
+ if len(update) > 0 {
|
|
|
+ set["update"] = update
|
|
|
+ }
|
|
|
+ MgoS.UpdateById("spider_warn_err", id, map[string]interface{}{"$set": set})
|
|
|
+}
|
|
|
+
|
|
|
+//func (f *Front) SaveUpdateLog(s_type int, reasons, bid string, old_data, update, modifyinfo map[string]interface{}) {
|
|
|
+// //日志记录
|
|
|
+// user := f.GetSession("user").(map[string]interface{})
|
|
|
+// log_data := map[string]interface{}{
|
|
|
+// "s_modifyuser": user["name"],
|
|
|
+// "s_type": s_type,
|
|
|
+// "i_modifytime": time.Now().Unix(),
|
|
|
+// "s_modifyreason": reasons,
|
|
|
+// "s_backupid": bid,
|
|
|
+// "o_oldinfo": old_data,
|
|
|
+// "o_newinfo": update,
|
|
|
+// "modifyinfo": modifyinfo,
|
|
|
+// }
|
|
|
+// MgoS.Save(JyRecord, log_data)
|
|
|
+//}
|