From 91aedfe56b7f68c2edbb8fb5d173c5809ef3ee97 Mon Sep 17 00:00:00 2001 From: "DESKTOP-DDTRVOR\\asus" <1162187704@qq.com> Date: Thu, 9 Jan 2025 15:17:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sever/test/server2.go | 155 +++++++++++++++++++++++++++++++++++------- 1 file changed, 131 insertions(+), 24 deletions(-) diff --git a/sever/test/server2.go b/sever/test/server2.go index bc21300f..2328b98a 100644 --- a/sever/test/server2.go +++ b/sever/test/server2.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" @@ -9,11 +8,34 @@ import ( "fmt" "io/ioutil" "net/http" + "net/url" credential "github.com/bytedance/douyin-openapi-credential-go/client" openApiSdkClient "github.com/bytedance/douyin-openapi-sdk-go/client" ) +type APIResponse struct { + BaseResp struct { + StatusCode int `json:"StatusCode"` + StatusMessage string `json:"StatusMessage"` + } `json:"BaseResp"` + Data struct { + Records []struct { + Aid string `json:"aid"` + Cost int `json:"cost"` + Did string `json:"did"` + EventName string `json:"event_name"` + EventTime string `json:"event_time"` + OpenID string `json:"open_id"` + ID int `json:"id"` + } `json:"records"` + Total int `json:"total"` + } `json:"data"` + ErrMsg string `json:"err_msg"` + ErrNo int `json:"err_no"` + LogID string `json:"log_id"` +} + // 获取 access_token 的函数 func GetAccessToken() (string, error) { // 初始化 SDK 客户端,设置 app_id 和 secret @@ -122,43 +144,47 @@ func main() { // 从请求中获取参数 openID := r.URL.Query().Get("open_id") // 获取 open_id mpID := r.URL.Query().Get("mp_id") // 获取 mp_id - iv := r.URL.Query().Get("iv") // 获取初始向量 dateHour := r.URL.Query().Get("date_hour") // 获取 date_hour + pageNo := r.URL.Query().Get("pageNo") // 获取 date_hour + pageSize := r.URL.Query().Get("pageSize") // 获取 date_hour // 检查是否有缺少参数 - if openID == "" || mpID == "" || dateHour == "" { + if mpID == "" || dateHour == "" { http.Error(w, "缺少必要的参数", http.StatusBadRequest) return } - // 解密 encryptedData - decryptedOpenID, err := DecryptAES128CBC(openID, sessionKey, iv) - if err != nil { - http.Error(w, fmt.Sprintf("解密失败: %v", err), http.StatusInternalServerError) - return - } // 动态获取 access_token accessToken, err := GetAccessToken() + apiURL := "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" if err != nil { http.Error(w, fmt.Sprintf("获取 access_token 失败: %v", err), http.StatusInternalServerError) return } fmt.Println("动态token为", accessToken) + params := url.Values{} + params.Add("open_id", openID) + params.Add("mp_id", mpID) + params.Add("date_hour", dateHour) + params.Add("access_token", accessToken) + params.Add("page_no", pageNo) + params.Add("page_size", pageSize) // 构造请求体 - requestBody := map[string]string{ - "open_id": openID, - "mp_id": mpID, - "date_hour": dateHour, - "access_token": accessToken, - } - requestBodyJSON, err := json.Marshal(requestBody) // 转为 JSON 格式 - if err != nil { - http.Error(w, "请求体编码失败", http.StatusInternalServerError) - return - } + // requestBody := map[string]string{ + // "open_id": openID, + // "mp_id": mpID, + // "date_hour": dateHour, + // "access_token": accessToken, + // "page_no": pageNo, + // "page_size": pageSize, + // } + fullURL := fmt.Sprintf("%s?%s", apiURL, params.Encode()) + // requestBodyJSON, err := json.Marshal(requestBody) // 转为 JSON 格式 + // if err != nil { + // http.Error(w, "请求体编码失败", http.StatusInternalServerError) + // return + // } - // 向抖音 API 发起请求 - apiURL := "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm" // 抖音 API 地址 - resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(requestBodyJSON)) + resp, err := http.Get(fullURL) if err != nil { http.Error(w, "调用抖音 API 失败", http.StatusInternalServerError) return @@ -171,11 +197,92 @@ func main() { http.Error(w, "读取抖音 API 响应失败", http.StatusInternalServerError) return } + var apiResponse APIResponse + err = json.Unmarshal(responseBody, &apiResponse) + if err != nil { + fmt.Printf("Error while parsing response JSON: %v\n", err) + return + } + if apiResponse.ErrNo != 0 { + fmt.Printf("API Error: %s (ErrNo: %d)\n", apiResponse.ErrMsg, apiResponse.ErrNo) + return + } + totalCost := 0 + totalRecords := len(apiResponse.Data.Records) + for _, record := range apiResponse.Data.Records { + totalCost += record.Cost + } + + if totalRecords == 0 { + fmt.Println("No records found. Unable to calculate ECPM.") + return + } + + ecpm := float64(totalCost) / 100000 * 1000 / float64(totalRecords) + fmt.Println("返回值为", responseBody) + // 根据key返回相应的值(这里可以替换为实际的业务逻辑) + value := fmt.Sprintf("Value for key '%s'", ecpm) // 将抖音 API 的响应返回给客户端 w.Header().Set("Content-Type", "application/json") w.WriteHeader(resp.StatusCode) - w.Write(responseBody) + w.Write([]byte(value)) + }) + http.HandleFunc("/Login", func(w http.ResponseWriter, r *http.Request) { + // 检查请求方法是否为GET + if r.Method != http.MethodGet { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return + } + + // 从请求参数中获取"code"和"Anonymous" + code := r.URL.Query().Get("code") + Anonymous := r.URL.Query().Get("Anonymous") + + fmt.Println("code:", code) + fmt.Println("Anonymous:", Anonymous) + + // 参数校验 + if code == "" { + http.Error(w, "Missing 'code' or 'Anonymous' parameter", http.StatusBadRequest) + return + } + + // 初始化 SDK 客户端 + opt := new(credential.Config). + SetClientKey("tt8b32fd8f14071db707"). + SetClientSecret("44018e80b1bde34395a52de67ce1e0c37c572d80") + + sdkClient, err := openApiSdkClient.NewClient(opt) + if err != nil { + fmt.Println("sdk init err:", err) + http.Error(w, "SDK 初始化失败", http.StatusInternalServerError) + return + } + + // 构建请求参数 + sdkRequest := &openApiSdkClient.V2Jscode2sessionRequest{} + sdkRequest.SetAnonymousCode(Anonymous) + sdkRequest.SetAppid("tt8b32fd8f14071db707") + sdkRequest.SetCode(code) + sdkRequest.SetSecret("44018e80b1bde34395a52de67ce1e0c37c572d80") + + // 调用 SDK + sdkResponse, err := sdkClient.V2Jscode2session(sdkRequest) + if err != nil { + fmt.Println("sdk call err:", err) + http.Error(w, "SDK 调用失败", http.StatusInternalServerError) + return + } + + // 打印登录数据 + fmt.Println("登录数据:", sdkResponse) + + // 返回响应 + value := fmt.Sprintf("Session Data: %v", sdkResponse.Data) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write([]byte(value)) }) // 启动 HTTP 服务器