package main import ( "crypto/md5" "encoding/json" "errors" "fmt" "net" "net/http" "strings" util "utils" ) var KeyTool = "TopJy2022" type QyxyReq struct { Type int `json:"type"` // 0:信用代码,1:企业名称 Res string `json:"res"` TimeStamp int64 `json:"timestamp"` } type QyxyResp struct { Code int `json:"code"` Message string `json:"message"` Data *IData `json:"data"` } type IData struct { EntName string `json:"entName"` Uniscid string `json:"uniscid"` Lerep string `json:"lerep"` ListedCompany int `json:"listedCompany"` Province string `json:"province"` City string `json:"city"` Country string `json:"country"` RegisterAddr string `json:"registerAddr"` BuildTime string `json:"buildTime"` RegisterCapital float64 `json:"registerCapital"` RegisterAuthority string `json:"registerAuthority"` RegisterSubject string `json:"registerSubject"` EnterpriseType string `json:"enterpriseType"` State string `json:"state"` BusinessScope string `json:"businessScope"` BusiStartTime string `json:"busiStartTime"` BusiEndTime string `json:"busiEndTime"` MigrateFlag int `json:"migrateFlag"` //0无变化,1迁入,2迁出 } func GetQyxyInfo(w http.ResponseWriter, req *http.Request) { ConChan <- true defer func() { <-ConChan }() //req.Header.Add("Accept-Charset", "utf-8") var res QyxyReq if err := json.NewDecoder(req.Body).Decode(&res); err != nil { _ = req.Body.Close() util.Debug(err) } util.Debug(res) token := req.Header.Get("token") var result QyxyResp if AuthInfo(fmt.Sprintf("%s+%d", KeyTool, res.TimeStamp), token) { var data *IData if res.Type == 0 { data = getInfo(map[string]interface{}{"credit_no": res.Res}) } else { data = getInfo(map[string]interface{}{"company_name": res.Res}) } if data != nil { result = QyxyResp{Code: 200, Message: "获取信息成功", Data: data} } else { result = QyxyResp{Code: 200, Message: "未查询到企业信息", Data: data} } } else { result = QyxyResp{Code: 300, Message: "认证失败", Data: nil} } if err := json.NewEncoder(w).Encode(result); err != nil { util.Debug(err) } } type QyxyBulkReq struct { Type int `json:"type"` // 0:信用代码,1:企业名称 Res []string `json:"res"` // 企业名称/信用代码 TimeStamp int64 `json:"timestamp"` } type QyxyBulkResp struct { Code int `json:"code"` Message string `json:"message"` Data []*IData `json:"data"` } func GetQyxyInfoBulk(w http.ResponseWriter, req *http.Request) { ConChan <- true defer func() { <-ConChan }() //req.Header.Add("Accept-Charset", "utf-8") token := req.Header.Get("token") var res QyxyBulkReq if err := json.NewDecoder(req.Body).Decode(&res); err != nil { _ = req.Body.Close() util.Debug(err) } util.Debug(res) var result QyxyBulkResp var dataArr []*IData if AuthInfo(fmt.Sprintf("%s+%d", KeyTool, res.TimeStamp), token) { if res.Type == 0 { for _, re := range res.Res { data := getInfo(map[string]interface{}{"credit_no": re}) if data != nil { dataArr = append(dataArr, data) } else { dataArr = append(dataArr, &IData{Uniscid: re}) } } } else { for _, re := range res.Res { data := getInfo(map[string]interface{}{"company_name": re}) if data != nil { dataArr = append(dataArr, data) } else { dataArr = append(dataArr, &IData{EntName: re}) } } } result = QyxyBulkResp{Code: 200, Message: "获取信息成功", Data: dataArr} } else { result = QyxyBulkResp{Code: 300, Message: "认证失败", Data: dataArr} } if err := json.NewEncoder(w).Encode(result); err != nil { util.Debug(err) } } func getInfo(q map[string]interface{}) *IData { info, b := MgoTool.Find("qyxy_std", q, nil, nil, false, -1, -1) if b && len(*info) > 0 { if len(*info) == 1 { return getResp((*info)[0]) } else { var mapArr []map[string]interface{} for _, m := range *info { if m["company_status"] == "存续" && m["company_area"] == "河南" { mapArr = append(mapArr, m) } } if len(mapArr) > 0 { return getResp(mapArr[0]) } else { return nil } } } else { return nil } } func getResp(tmp map[string]interface{}) *IData { res := IData{} res.EntName = util.ObjToString(tmp["company_name"]) res.Uniscid = util.ObjToString(tmp["credit_no"]) res.Lerep = util.ObjToString(tmp["legal_person"]) if util.ObjToString(tmp["ListedCompany"]) != "" { res.ListedCompany = 1 } else { res.ListedCompany = 0 } res.Province = util.ObjToString(tmp["company_area"]) res.City = util.ObjToString(tmp["company_city"]) res.Country = util.ObjToString(tmp["company_district"]) res.RegisterAddr = util.ObjToString(tmp["company_address"]) res.BuildTime = util.ObjToString(tmp["establish_date"]) res.RegisterCapital = util.Float64All(tmp["capital"]) res.RegisterAuthority = util.ObjToString(tmp["authority"]) res.RegisterSubject = util.ObjToString(tmp["company_type"]) res.EnterpriseType = util.ObjToString(tmp["company_type_old"]) res.State = util.ObjToString(tmp["company_status"]) res.BusinessScope = util.ObjToString(tmp["business_scope"]) res.BusiStartTime = util.ObjToString(tmp["operation_startdate"]) res.BusiEndTime = util.ObjToString(tmp["operation_enddate"]) if util.ObjToString(tmp["company_status"]) == "迁出" { res.MigrateFlag = 2 } else { res.MigrateFlag = 0 } return &res } func GetIP(r *http.Request) (string, error) { ip := r.Header.Get("X-Real-IP") if net.ParseIP(ip) != nil { return ip, nil } ip = r.Header.Get("X-Forward-For") for _, i := range strings.Split(ip, ",") { if net.ParseIP(i) != nil { return i, nil } } ip, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { return "", err } if net.ParseIP(ip) != nil { return ip, nil } return "", errors.New("no valid ip found") } func AuthInfo(str, token string) bool { if MD5Encode(str) == token { return true } return false } func MD5Encode(str string) string { return strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(str)))) }