瀏覽代碼

爬虫列表更新

mxs 10 月之前
父節點
當前提交
9119789f1e
共有 1 個文件被更改,包括 66 次插入10 次删除
  1. 66 10
      server.go

+ 66 - 10
server.go

@@ -19,10 +19,15 @@ type UserInfo struct {
 	Email    string   `json:"s_email"`
 	Auth     int      `json:"i_auth"`
 	Scope    int      `json:"i_scope"`
-	identity int      `json:"i_identity"`
+	Identity int      `json:"i_identity"`
 	Ids      []string `json:"ids"`
 }
 
+type CodesInfo struct {
+	List  []map[string]interface{} `json:"list"`
+	Total int                      `json:"total"`
+}
+
 var (
 	//User map[string]interface{} //user对象
 	User *UserInfo //user对象
@@ -35,6 +40,13 @@ func (a *App) ServerActionUserLogin(param map[string]interface{}) map[string]int
 	//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,
@@ -44,21 +56,41 @@ func (a *App) ServerActionUserLogin(param map[string]interface{}) map[string]int
 
 // 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": User,
+		"data": nil,
 	}
 }
 
 // ServerActionCodeList 获取爬虫列表
 func (a *App) ServerActionCodeList(param map[string]interface{}) map[string]interface{} {
 	qu.Debug("param---", param)
-	data := map[string]interface{}{}
+	data := &CodesInfo{}
 	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")
@@ -96,7 +128,20 @@ func (a *App) ServerActionClaimCodes() map[string]interface{} {
 	err := 1
 	data := map[string]interface{}{}
 	if User.Auth == 0 {
-		GetResult(data, nil, "claimcode")
+		param := map[string]interface{}{}
+		userByte, _ := json.Marshal(User)
+		json.Unmarshal(userByte, &param)
+		//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
@@ -105,19 +150,29 @@ func (a *App) ServerActionClaimCodes() map[string]interface{} {
 	return map[string]interface{}{
 		"msg":  msg,
 		"err":  err,
-		"data": data,
+		"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 GetResult(result interface{}, param map[string]interface{}, route string) {
 	jsonData, err := json.Marshal(param)
 	if err != nil {
-		fmt.Println("Error marshaling request:", err)
+		qu.Debug("Error marshaling request:", err)
 		return
 	}
 	req, err := http.NewRequest("POST", fmt.Sprintf(HREF, route), bytes.NewBuffer(jsonData))
 	if err != nil {
-		fmt.Println("Error creating request:", err)
+		qu.Debug("Error creating request:", err)
 		return
 	}
 	// 设置请求头
@@ -126,18 +181,19 @@ func GetResult(result interface{}, param map[string]interface{}, route string) {
 	client := &http.Client{}
 	resp, err := client.Do(req)
 	if err != nil {
-		fmt.Println("Error sending request:", err)
+		qu.Debug("Error sending request:", err)
 		return
 	}
 	defer resp.Body.Close()
 	// 读取响应体
 	body, err := ioutil.ReadAll(resp.Body)
 	if err != nil {
-		fmt.Println("Error reading response body:", err)
+		qu.Debug("Error reading response body:", err)
 		return
 	}
 	if err := json.Unmarshal(body, &result); err != nil {
-		fmt.Println("Error unmarshaling response:", err)
+		qu.Debug("Error unmarshaling response:", err)
 		return
 	}
+	qu.Debug(result)
 }