package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" qu "jygit.jydev.jianyu360.cn/data_processing/common_utils" "net/http" "reflect" ) const HREF = "http://127.0.0.1:8091/%s" type UserInfo struct { ID string `json:"_id"` Username string `json:"s_name"` Fullname string `json:"s_fullname"` Email string `json:"s_email"` Auth int `json:"i_auth"` Scope int `json:"i_scope"` Identity int `json:"i_identity"` Ids []string `json:"ids"` } type ListInfo struct { List []map[string]interface{} `json:"list"` Total int `json:"total"` } type Result map[string]interface{} var ( //User map[string]interface{} //user对象 User *UserInfo //user对象 ) // ServerActionUserLogin 登录 func (a *App) ServerActionUserLogin(param map[string]interface{}) map[string]interface{} { qu.Debug("param---", param) User = &UserInfo{} //User = map[string]interface{}{} getResult(User, param, "login") qu.Debug("user:", *User) if User.ID == "" { return map[string]interface{}{ "msg": "登录失败", "err": 1, "data": nil, } } return map[string]interface{}{ "msg": "", "err": 0, "data": User, } } // ServerActionCheckLogin 检查是否登录 func (a *App) ServerActionCheckLogin() map[string]interface{} { if User != nil && User.ID != "" { return map[string]interface{}{ "msg": "", "err": 0, "data": User, } } return map[string]interface{}{ "msg": "", "err": 1, "data": nil, } } // ServerActionUserLogout 退出登录 func (a *App) ServerActionUserLogout() map[string]interface{} { User = &UserInfo{} return map[string]interface{}{ "msg": "退出成功", "err": 0, "data": nil, } } // ServerActionCodeList 获取爬虫列表 func (a *App) ServerActionCodeList(param map[string]interface{}) map[string]interface{} { qu.Debug("param---", param) data := &ListInfo{} var msg string var err int if User != nil { formatUser(param) qu.Debug("param---", param) getResult(data, param, "getcodes") } else { msg = "用户登录异常,请重新登录!" err = 1 qu.Debug(msg) } return map[string]interface{}{ "msg": msg, "err": err, "data": data, } } // ServerActionGetModifyUsers 获取爬虫开发人员列表 func (a *App) ServerActionGetModifyUsers() map[string]interface{} { data := &ListInfo{} var msg string var err int if User != nil && User.Auth > 1 { //禁止开发查询 getResult(data, nil, "getmodifyusers") if len(User.Ids) > 0 && User.Identity == 0 { //外包审核员或管理员只能查看外包开发人员信息 resultUsers := []map[string]interface{}{} for _, user := range data.List { userid := qu.ObjToString(user["_id"]) for _, id := range User.Ids { if userid == id { resultUsers = append(resultUsers, user) break } } } data.List = resultUsers data.Total = len(resultUsers) } } else { msg = "查询开发列表失败" err = 1 qu.Debug(msg) } return map[string]interface{}{ "msg": msg, "err": err, "data": data, } } // ServerActionClaimCodes 爬虫认领 func (a *App) ServerActionClaimCodes() map[string]interface{} { msg := "认领失败!" err := 1 data := &Result{} if User.Auth == 1 { param := map[string]interface{}{} formatUser(param) getResult(data, param, "claimcode") if tmpMsg := (*data)["msg"]; tmpMsg != nil { msg = qu.ObjToString(tmpMsg) err = 0 } } return map[string]interface{}{ "msg": msg, "err": err, "data": nil, } } // ServerActionUpdateCode 爬虫更新 func (a *App) ServerActionUpdateCode(param []map[string]interface{}) map[string]interface{} { qu.Debug("param---", param) data := map[string]interface{}{} // //[ // { // "query":{}, // "set":{} // }, // {} //] //var msg string //var err int //arr := [][]map[string]interface{}{} //for _, tmp := range param { // query := tmp["query"] // set := tmp["set"] // if query == nil || set == nil { // msg = "更新条件错误!" // err = 1 // break // } // update:=[]map[string]interface{}{ // query, // } //} // //getResult(data, param, "updatecode") // //update := []map[string]interface{}{ // {"code": ""}, // {"$set": map[string]interface{}{ // "state": 1, // }}, //} //arr := [][]map[string]interface{}{} //arr = append(arr, update) //result := map[string]interface{}{ // "update": arr, //} ////1、更新css选择器 ////2、更新爬虫状态 return map[string]interface{}{ "msg": "", "err": 0, "data": data, } } // 格式化User对象 func formatUser(tmp map[string]interface{}) { v := reflect.ValueOf(User) if v.Kind() == reflect.Ptr { v = v.Elem() } t := v.Type() for i := 0; i < v.NumField(); i++ { field := t.Field(i).Tag.Get("json") value := v.Field(i).Interface() tmp[field] = value } } // 获取接口结果 func getResult(result interface{}, param map[string]interface{}, route string) { jsonData, err := json.Marshal(param) if err != nil { qu.Debug("Error marshaling request:", err) return } req, err := http.NewRequest("POST", fmt.Sprintf(HREF, route), bytes.NewBuffer(jsonData)) if err != nil { qu.Debug("Error creating request:", err) return } // 设置请求头 req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { qu.Debug("Error sending request:", err) return } defer resp.Body.Close() // 读取响应体 body, err := ioutil.ReadAll(resp.Body) if err != nil { qu.Debug("Error reading response body:", err) return } if err := json.Unmarshal(body, &result); err != nil { qu.Debug("Error unmarshaling response:", err) return } qu.Debug(result) }