67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
|
|
"kfz-goods-pricing/internal/service"
|
|
)
|
|
|
|
// KfzHandler Kfz处理器
|
|
type KfzHandler struct{}
|
|
|
|
// NewKfzHandler 创建Kfz处理器实例
|
|
func NewKfzHandler() *KfzHandler {
|
|
return &KfzHandler{}
|
|
}
|
|
|
|
// KfzLogin 登录孔网并返回用户信息
|
|
func (h *KfzHandler) KfzLogin(w http.ResponseWriter, r *http.Request) {
|
|
clientIP := r.RemoteAddr
|
|
if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
|
|
clientIP = forwarded
|
|
}
|
|
log.Printf("[KfzLogin] 收到登录请求: clientIP=%s", clientIP)
|
|
|
|
r.ParseMultipartForm(32 << 20)
|
|
username := r.PostForm.Get("username")
|
|
password := r.PostForm.Get("password")
|
|
|
|
if username == "" || password == "" {
|
|
log.Printf("[KfzLogin] username或password为空: clientIP=%s", clientIP)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"code":500,"message":"username和password不能为空"}`))
|
|
return
|
|
}
|
|
|
|
log.Printf("[KfzLogin] 开始登录孔网: username=%s, clientIP=%s", username, clientIP)
|
|
token, err := service.OutKfzLogin(username, password)
|
|
if err != nil {
|
|
log.Printf("[KfzLogin] 孔网登录失败: username=%s, 错误=%v, 来源IP: %s", username, err, clientIP)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"code":500,"message":"` + err.Error() + `"}`))
|
|
return
|
|
}
|
|
log.Printf("[KfzLogin] 孔网登录成功: username=%s, token=%.10s..., 来源IP: %s", username, token, clientIP)
|
|
|
|
userInfo, err := service.OutKfzGetUserInfo(token)
|
|
if err != nil {
|
|
log.Printf("[KfzLogin] 获取用户信息失败: username=%s, 错误=%v, 来源IP: %s", username, err, clientIP)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"code":500,"message":"` + err.Error() + `"}`))
|
|
return
|
|
}
|
|
|
|
userInfo.Token = token
|
|
|
|
log.Printf("[KfzLogin] 登录成功: username=%s, userId=%d, nickname=%s, 来源IP: %s", username, userInfo.UserID, userInfo.Nickname, clientIP)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"code": 200,
|
|
"message": "success",
|
|
"data": userInfo,
|
|
})
|
|
}
|