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"` } 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 { 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() param[field] = value } 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 { //外包审核员或管理员只能查看外包开发人员信息 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 := map[string]interface{}{} if User.Auth == 0 { param := map[string]interface{}{} userByte, _ := json.Marshal(User) json.Unmarshal(userByte, ¶m) //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() // param[field] = value //} 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{} { data := map[string]interface{}{} GetResult(data, param, "updatecode") return map[string]interface{}{ "msg": "", "err": 0, "data": data, } } func formatUser(user 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() user[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) }