From 69cc0042d0b7f829676867a204d146eb6f6a189e Mon Sep 17 00:00:00 2001 From: zggsong Date: Thu, 27 Oct 2022 17:08:05 +0800 Subject: [PATCH] feat: auto convert header userinfo --- main.go | 1 - model/model.go | 19 ++++++++++++++ serve/core/core.go | 59 +++++++++++++++++++++++++++++++++++-------- serve/serve.go | 11 ++++---- util/util.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 2f96b3f..b7cad22 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,6 @@ func init() { func main() { // 监听目录下文件 - //go util.ListeningDirectory("upload") global.GLO_RECV_CHAN = make(chan map[string]string) global.GLO_CONFIG_CHAN = make(chan model.Config) diff --git a/model/model.go b/model/model.go index c4ea9fb..00c2c82 100644 --- a/model/model.go +++ b/model/model.go @@ -90,3 +90,22 @@ type Config struct { MailFromName string MailTo []string } + +type HeaderUser struct { + CompanyOwner int `json:"CompanyOwner"` + CompanyCode string `json:"CompanyCode"` + CompanyName string `json:"CompanyName"` + BUCode string `json:"BUCode"` + BUName string `json:"BUName"` + DeptCode string `json:"DeptCode"` + DeptName string `json:"DeptName"` + Code string `json:"Code"` + Name string `json:"Name"` + IDCardNo string `json:"IDCardNo"` + Gender string `json:"Gender"` + Telephone string `json:"Telephone"` + Email string `json:"Email"` + Language string `json:"Language"` + LoginType int `json:"LoginType"` + DataSource string `json:"DataSource"` +} diff --git a/serve/core/core.go b/serve/core/core.go index 6f5ae23..69dd3fd 100644 --- a/serve/core/core.go +++ b/serve/core/core.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "luxshare-daily-report/model" + "luxshare-daily-report/util" "net/http" "net/url" "strings" @@ -19,7 +20,7 @@ import ( // @param passwd // @return string // -func Login(userName, passwd string) (string, error) { +func Login(userName, passwd string) (string, string, error) { loginUrl := "https://m.luxshare-ict.com/api/Account/Login" contentType := "application/x-www-form-urlencoded" @@ -35,17 +36,55 @@ func Login(userName, passwd string) (string, error) { resp, err := http.Post(loginUrl, contentType, strings.NewReader(postData.Encode())) defer resp.Body.Close() if err != nil { - return "", errors.New(fmt.Sprintf("[ERROR] (Login) Request Error: %v", err)) + return "", "", errors.New(fmt.Sprintf("[ERROR] (Login) Request Error: %v", err)) } body, _ := ioutil.ReadAll(resp.Body) //fmt.Println(string(body)) var loginModel model.LoginResp err = json.Unmarshal(body, &loginModel) if err != nil { - return "", errors.New(fmt.Sprintf("[ERROR] (Login) Resp Json Unmarshal Error: %v", err)) + return "", "", errors.New(fmt.Sprintf("[ERROR] (Login) Resp Json Unmarshal Error: %v", err)) + } + //失败 + if !loginModel.IsSuccess { + err = errors.New(util.Strval(loginModel.ErrMsg)) + return "", "", err } ticket := loginModel.Data.Ticket - return ticket, nil + + data, user := model.LoginRespData{}, model.UserInfo{} + if loginModel.Data == data || loginModel.Data.UserInfo == user { + err = errors.New(fmt.Sprintf("[ERROR] Login not return user information")) + return ticket, "", err + } + userStr := convert(loginModel.Data.UserInfo) + + return ticket, userStr, nil +} + +func convert(info model.UserInfo) string { + user := model.HeaderUser{ + CompanyOwner: info.CompanyOwner, + CompanyCode: info.CompanyCode, + CompanyName: info.CompanyName, + BUCode: info.BUCode, + BUName: info.BUName, + DeptCode: info.DeptCode, + DeptName: info.DeptName, + Code: info.Code, + Name: info.Name, + IDCardNo: info.IDCardNo, + Gender: info.Gender, + Telephone: info.Telephone, + Email: info.Email, + Language: "zh-cn", + LoginType: info.LoginType, + DataSource: "M", + } + bytes, _ := json.Marshal(user) + //url编码 + escape := url.QueryEscape(string(bytes)) + return escape } // @@ -55,7 +94,7 @@ func Login(userName, passwd string) (string, error) { // @param images // @return []string // -func Upload2Azure(auth string, images map[string]string) ([]string, error) { +func Upload2Azure(auth, user string, images map[string]string) ([]string, error) { var client = &http.Client{} uploadUrl := "https://p.luxshare-ict.com/api/Azure/TencentFileToAzure" contentType := "application/x-www-form-urlencoded" @@ -71,7 +110,7 @@ func Upload2Azure(auth string, images map[string]string) ([]string, error) { return nil, errors.New(fmt.Sprintf("[ERROR] (Upload2Azure) Error creating upload request: %v", err)) } request.Header.Set("Content-Type", contentType) - request.Header.Set("__user__", `%7B%22CompanyOwner%22:888,%22CompanyCode%22:%22KSAT%22,%22CompanyName%22:%22%E6%B1%9F%E8%8B%8F%E6%9C%BA%E5%99%A8%E4%BA%BA%22,%22BUCode%22:%22U00001%22,%22BUName%22:%22%E6%99%BA%E8%83%BD%E5%88%B6%E9%80%A0%E5%BC%80%E5%8F%91%E4%B8%AD%E5%BF%83%22,%22DeptCode%22:%22U12544%22,%22DeptName%22:%22%E8%AE%BE%E5%A4%87%E4%BF%A1%E6%81%AF%E5%8C%96%E8%AF%BE%22,%22Code%22:%2213901424%22,%22Name%22:%22%E5%AE%8B%E5%A9%89%E5%86%9B%22,%22IDCardNo%22:%22340826199808161410%22,%22Gender%22:%22M%22,%22Telephone%22:%2217855513383%22,%22Email%22:%22Wanjun.Song@luxshare-ict.com%22,%22Language%22:%22zh-cn%22,%22LoginType%22:4,%22DataSource%22:%22M%22%7D`) + request.Header.Set("__user__", user) request.Header.Set("Authorization", fmt.Sprintf("BaseAuth %v", auth)) resp, err := client.Do(request) @@ -97,7 +136,7 @@ func Upload2Azure(auth string, images map[string]string) ([]string, error) { // @param images // @return error // -func EpidemicRegistration(auth string, images []string) error { +func EpidemicRegistration(auth, user string, images []string) error { var client = &http.Client{} uploadUrl := "https://m.luxshare-ict.com/api/EpidemicSys/EpidemicRegistration/LVIQuestSave2" contentType := "application/x-www-form-urlencoded" @@ -147,7 +186,7 @@ func EpidemicRegistration(auth string, images []string) error { return errors.New(fmt.Sprintf("[ERROR] (EpidemicRegistration) Error creating upload request: %v", err)) } request.Header.Set("Content-Type", contentType) - request.Header.Set("__user__", `%7B%22CompanyOwner%22:888,%22CompanyCode%22:%22KSAT%22,%22CompanyName%22:%22%E6%B1%9F%E8%8B%8F%E6%9C%BA%E5%99%A8%E4%BA%BA%22,%22BUCode%22:%22U00001%22,%22BUName%22:%22%E6%99%BA%E8%83%BD%E5%88%B6%E9%80%A0%E5%BC%80%E5%8F%91%E4%B8%AD%E5%BF%83%22,%22DeptCode%22:%22U12544%22,%22DeptName%22:%22%E8%AE%BE%E5%A4%87%E4%BF%A1%E6%81%AF%E5%8C%96%E8%AF%BE%22,%22Code%22:%2213901424%22,%22Name%22:%22%E5%AE%8B%E5%A9%89%E5%86%9B%22,%22IDCardNo%22:%22340826199808161410%22,%22Gender%22:%22M%22,%22Telephone%22:%2217855513383%22,%22Email%22:%22Wanjun.Song@luxshare-ict.com%22,%22Language%22:%22zh-cn%22,%22LoginType%22:4,%22DataSource%22:%22M%22%7D`) + request.Header.Set("__user__", user) request.Header.Set("Authorization", fmt.Sprintf("BaseAuth %v", auth)) resp, err := client.Do(request) @@ -176,7 +215,7 @@ func EpidemicRegistration(auth string, images []string) error { // @param auth // @return error // -func RefreshDoor(auth string) error { +func RefreshDoor(auth, user string) error { var client = &http.Client{} refreshUrl := "https://m.luxshare-ict.com/api/EpidemicSys/EpidemicRegistration/RefreshDoor" @@ -184,7 +223,7 @@ func RefreshDoor(auth string) error { if err != nil { return errors.New(fmt.Sprintf("[ERROR] (RefreshDoor) Error creating upload request: %v", err)) } - request.Header.Set("__user__", `%7B%22CompanyOwner%22:888,%22CompanyCode%22:%22KSAT%22,%22CompanyName%22:%22%E6%B1%9F%E8%8B%8F%E6%9C%BA%E5%99%A8%E4%BA%BA%22,%22BUCode%22:%22U00001%22,%22BUName%22:%22%E6%99%BA%E8%83%BD%E5%88%B6%E9%80%A0%E5%BC%80%E5%8F%91%E4%B8%AD%E5%BF%83%22,%22DeptCode%22:%22U12544%22,%22DeptName%22:%22%E8%AE%BE%E5%A4%87%E4%BF%A1%E6%81%AF%E5%8C%96%E8%AF%BE%22,%22Code%22:%2213901424%22,%22Name%22:%22%E5%AE%8B%E5%A9%89%E5%86%9B%22,%22IDCardNo%22:%22340826199808161410%22,%22Gender%22:%22M%22,%22Telephone%22:%2217855513383%22,%22Email%22:%22Wanjun.Song@luxshare-ict.com%22,%22Language%22:%22zh-cn%22,%22LoginType%22:4,%22DataSource%22:%22M%22%7D`) + request.Header.Set("__user__", user) request.Header.Set("Authorization", fmt.Sprintf("BaseAuth %v", auth)) resp, err := client.Do(request) diff --git a/serve/serve.go b/serve/serve.go index 585cee8..90a5426 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -94,12 +94,13 @@ https://github.com/zggsong`)) func DeclarationService(files map[string]string) { //登陆获取auth //ticket := "Q9okHMY42Fk7kzLA3rvPTCbUShhX3zqlbaT97CDjUbxql0NH0AAqKYw+XfSjwoytijuuHXOc7vNY9GePZoIZSg==" - ticket, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord) + ticket, userStr, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord) if ticket == "" || err != nil { log.Printf("ticket: %v, err:%v", ticket, err.Error()) util.SendMessageError(err) return } + log.Printf("[DEBUG] __user__: %v", userStr) //上传图片 var m = make(map[string]string, 2) @@ -110,7 +111,7 @@ func DeclarationService(files map[string]string) { m["xcm"] = resXcm m["jkm"] = resJkm //imagesLinks := []string{"https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_2a05b9ab03844033a63b2c2ac06556ab.jpg", "https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_7e64fd5c643c49299571583163623f7b.jpg"} - imagesLinks, err := core.Upload2Azure(ticket, m) + imagesLinks, err := core.Upload2Azure(ticket, userStr, m) //log.Printf("[DEBUG] get images links: %s", imagesLinks) if err != nil { log.Printf(err.Error()) @@ -126,7 +127,7 @@ func DeclarationService(files map[string]string) { //申报 for i := 0; i < 3; i++ { - err = core.EpidemicRegistration(ticket, imagesLinks) + err = core.EpidemicRegistration(ticket, userStr, imagesLinks) if err != nil && i > 1 { log.Printf("重试3次失败,%v", err.Error()) util.SendMessageError(err) @@ -138,11 +139,9 @@ func DeclarationService(files map[string]string) { } log.Printf("[INFO] 每日申报成功") - //time.Sleep(time.Second * 3) - //刷新门禁 for i := 0; i < 3; i++ { - err = core.RefreshDoor(ticket) + err = core.RefreshDoor(ticket, userStr) if err != nil && i > 1 { log.Printf("重试3次失败,%v", err.Error()) util.SendMessageError(err) diff --git a/util/util.go b/util/util.go index 65c13ed..8f7d5a8 100644 --- a/util/util.go +++ b/util/util.go @@ -1,8 +1,10 @@ package util import ( + "encoding/json" "fmt" "luxshare-daily-report/message" + "strconv" ) // SendMessageError 发送错误信息 @@ -33,3 +35,63 @@ func send(body message.Body) { m.Send(body) } } + +// Strval +// 获取变量的字符串值 +// 浮点型 3.0将会转换成字符串3, "3" +// 非数值或字符类型的变量将会被转换成JSON格式字符串 +func Strval(value interface{}) string { + // interface 转 string + var key string + if value == nil { + return key + } + + switch value.(type) { + case float64: + ft := value.(float64) + key = strconv.FormatFloat(ft, 'f', -1, 64) + case float32: + ft := value.(float32) + key = strconv.FormatFloat(float64(ft), 'f', -1, 64) + case int: + it := value.(int) + key = strconv.Itoa(it) + case uint: + it := value.(uint) + key = strconv.Itoa(int(it)) + case int8: + it := value.(int8) + key = strconv.Itoa(int(it)) + case uint8: + it := value.(uint8) + key = strconv.Itoa(int(it)) + case int16: + it := value.(int16) + key = strconv.Itoa(int(it)) + case uint16: + it := value.(uint16) + key = strconv.Itoa(int(it)) + case int32: + it := value.(int32) + key = strconv.Itoa(int(it)) + case uint32: + it := value.(uint32) + key = strconv.Itoa(int(it)) + case int64: + it := value.(int64) + key = strconv.FormatInt(it, 10) + case uint64: + it := value.(uint64) + key = strconv.FormatUint(it, 10) + case string: + key = value.(string) + case []byte: + key = string(value.([]byte)) + default: + newValue, _ := json.Marshal(value) + key = string(newValue) + } + + return key +}