Sfoglia il codice sorgente

新增爬虫列表信息查询方法

mxs 10 mesi fa
parent
commit
87870f8ce0
1 ha cambiato i file con 98 aggiunte e 6 eliminazioni
  1. 98 6
      server.go

+ 98 - 6
server.go

@@ -1,12 +1,104 @@
 package main
 
 import (
+	"bytes"
+	"encoding/json"
 	"fmt"
-	"time"
+	"io/ioutil"
+	qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
+	"net/http"
+	"reflect"
 )
-// CountYestodayArts
-func (a *App) Login(username,password string) map[string]interface{}{
-	fmt.Println(username,password)
-	time.Sleep(2*time.Second)
-	return map[string]interface{}{"error": 1}
+
+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"`
+}
+
+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)
+	return map[string]interface{}{
+		"msg":  "",
+		"err":  0,
+		"data": User,
+	}
+}
+
+// ServerActionCodeList 获取爬虫列表
+func (a *App) ServerActionCodeList(param map[string]interface{}) map[string]interface{} {
+	qu.Debug("param---", param)
+	data := map[string]interface{}{}
+	var msg string
+	var err int
+	if User != nil {
+		v := reflect.ValueOf(User)
+		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,
+	}
+}
+
+func GetResult(result interface{}, param map[string]interface{}, route string) {
+	jsonData, err := json.Marshal(param)
+	if err != nil {
+		fmt.Println("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)
+		return
+	}
+	// 设置请求头
+	req.Header.Set("Content-Type", "application/json")
+	// 发送请求
+	client := &http.Client{}
+	resp, err := client.Do(req)
+	if err != nil {
+		fmt.Println("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)
+		return
+	}
+	if err := json.Unmarshal(body, &result); err != nil {
+		fmt.Println("Error unmarshaling response:", err)
+		return
+	}
 }