task.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "strings"
  10. util "utils"
  11. )
  12. var KeyTool = "TopJy2022"
  13. type QyxyReq struct {
  14. Type int `json:"type"` // 0:信用代码,1:企业名称
  15. Res string `json:"res"`
  16. TimeStamp int64 `json:"timestamp"`
  17. }
  18. type QyxyResp struct {
  19. Code int `json:"code"`
  20. Message string `json:"message"`
  21. Data *IData `json:"data"`
  22. }
  23. type IData struct {
  24. EntName string `json:"entName"`
  25. Uniscid string `json:"uniscid"`
  26. Lerep string `json:"lerep"`
  27. ListedCompany int `json:"listedCompany"`
  28. Province string `json:"province"`
  29. City string `json:"city"`
  30. Country string `json:"country"`
  31. RegisterAddr string `json:"registerAddr"`
  32. BuildTime string `json:"buildTime"`
  33. RegisterCapital float64 `json:"registerCapital"`
  34. RegisterAuthority string `json:"registerAuthority"`
  35. RegisterSubject string `json:"registerSubject"`
  36. EnterpriseType string `json:"enterpriseType"`
  37. State string `json:"state"`
  38. BusinessScope string `json:"businessScope"`
  39. BusiStartTime string `json:"busiStartTime"`
  40. BusiEndTime string `json:"busiEndTime"`
  41. MigrateFlag int `json:"migrateFlag"` //0无变化,1迁入,2迁出
  42. }
  43. func GetQyxyInfo(w http.ResponseWriter, req *http.Request) {
  44. ConChan <- true
  45. defer func() {
  46. <-ConChan
  47. }()
  48. //req.Header.Add("Accept-Charset", "utf-8")
  49. var res QyxyReq
  50. if err := json.NewDecoder(req.Body).Decode(&res); err != nil {
  51. _ = req.Body.Close()
  52. util.Debug(err)
  53. }
  54. util.Debug(res)
  55. token := req.Header.Get("token")
  56. var result QyxyResp
  57. if AuthInfo(fmt.Sprintf("%s+%d", KeyTool, res.TimeStamp), token) {
  58. var data *IData
  59. if res.Type == 0 {
  60. data = getInfo(map[string]interface{}{"credit_no": res.Res})
  61. } else {
  62. data = getInfo(map[string]interface{}{"company_name": res.Res})
  63. }
  64. if data != nil {
  65. result = QyxyResp{Code: 200, Message: "获取信息成功", Data: data}
  66. } else {
  67. result = QyxyResp{Code: 200, Message: "未查询到企业信息", Data: data}
  68. }
  69. } else {
  70. result = QyxyResp{Code: 300, Message: "认证失败", Data: nil}
  71. }
  72. if err := json.NewEncoder(w).Encode(result); err != nil {
  73. util.Debug(err)
  74. }
  75. }
  76. type QyxyBulkReq struct {
  77. Type int `json:"type"` // 0:信用代码,1:企业名称
  78. Res []string `json:"res"` // 企业名称/信用代码
  79. TimeStamp int64 `json:"timestamp"`
  80. }
  81. type QyxyBulkResp struct {
  82. Code int `json:"code"`
  83. Message string `json:"message"`
  84. Data []*IData `json:"data"`
  85. }
  86. func GetQyxyInfoBulk(w http.ResponseWriter, req *http.Request) {
  87. ConChan <- true
  88. defer func() {
  89. <-ConChan
  90. }()
  91. //req.Header.Add("Accept-Charset", "utf-8")
  92. token := req.Header.Get("token")
  93. var res QyxyBulkReq
  94. if err := json.NewDecoder(req.Body).Decode(&res); err != nil {
  95. _ = req.Body.Close()
  96. util.Debug(err)
  97. }
  98. util.Debug(res)
  99. var result QyxyBulkResp
  100. var dataArr []*IData
  101. if AuthInfo(fmt.Sprintf("%s+%d", KeyTool, res.TimeStamp), token) {
  102. if res.Type == 0 {
  103. for _, re := range res.Res {
  104. data := getInfo(map[string]interface{}{"credit_no": re})
  105. if data != nil {
  106. dataArr = append(dataArr, data)
  107. } else {
  108. dataArr = append(dataArr, &IData{Uniscid: re})
  109. }
  110. }
  111. } else {
  112. for _, re := range res.Res {
  113. data := getInfo(map[string]interface{}{"company_name": re})
  114. if data != nil {
  115. dataArr = append(dataArr, data)
  116. } else {
  117. dataArr = append(dataArr, &IData{EntName: re})
  118. }
  119. }
  120. }
  121. result = QyxyBulkResp{Code: 200, Message: "获取信息成功", Data: dataArr}
  122. } else {
  123. result = QyxyBulkResp{Code: 300, Message: "认证失败", Data: dataArr}
  124. }
  125. if err := json.NewEncoder(w).Encode(result); err != nil {
  126. util.Debug(err)
  127. }
  128. }
  129. func getInfo(q map[string]interface{}) *IData {
  130. info, b := MgoTool.Find("qyxy_std", q, nil, nil, false, -1, -1)
  131. if b && len(*info) > 0 {
  132. if len(*info) == 1 {
  133. return getResp((*info)[0])
  134. } else {
  135. var mapArr []map[string]interface{}
  136. for _, m := range *info {
  137. if m["company_status"] == "存续" && m["company_area"] == "河南" {
  138. mapArr = append(mapArr, m)
  139. }
  140. }
  141. if len(mapArr) > 0 {
  142. return getResp(mapArr[0])
  143. } else {
  144. return nil
  145. }
  146. }
  147. } else {
  148. return nil
  149. }
  150. }
  151. func getResp(tmp map[string]interface{}) *IData {
  152. res := IData{}
  153. res.EntName = util.ObjToString(tmp["company_name"])
  154. res.Uniscid = util.ObjToString(tmp["credit_no"])
  155. res.Lerep = util.ObjToString(tmp["legal_person"])
  156. if util.ObjToString(tmp["ListedCompany"]) != "" {
  157. res.ListedCompany = 1
  158. } else {
  159. res.ListedCompany = 0
  160. }
  161. res.Province = util.ObjToString(tmp["company_area"])
  162. res.City = util.ObjToString(tmp["company_city"])
  163. res.Country = util.ObjToString(tmp["company_district"])
  164. res.RegisterAddr = util.ObjToString(tmp["company_address"])
  165. res.BuildTime = util.ObjToString(tmp["establish_date"])
  166. res.RegisterCapital = util.Float64All(tmp["capital"])
  167. res.RegisterAuthority = util.ObjToString(tmp["authority"])
  168. res.RegisterSubject = util.ObjToString(tmp["company_type"])
  169. res.EnterpriseType = util.ObjToString(tmp["company_type_old"])
  170. res.State = util.ObjToString(tmp["company_status"])
  171. res.BusinessScope = util.ObjToString(tmp["business_scope"])
  172. res.BusiStartTime = util.ObjToString(tmp["operation_startdate"])
  173. res.BusiEndTime = util.ObjToString(tmp["operation_enddate"])
  174. if util.ObjToString(tmp["company_status"]) == "迁出" {
  175. res.MigrateFlag = 2
  176. } else {
  177. res.MigrateFlag = 0
  178. }
  179. return &res
  180. }
  181. func GetIP(r *http.Request) (string, error) {
  182. ip := r.Header.Get("X-Real-IP")
  183. if net.ParseIP(ip) != nil {
  184. return ip, nil
  185. }
  186. ip = r.Header.Get("X-Forward-For")
  187. for _, i := range strings.Split(ip, ",") {
  188. if net.ParseIP(i) != nil {
  189. return i, nil
  190. }
  191. }
  192. ip, _, err := net.SplitHostPort(r.RemoteAddr)
  193. if err != nil {
  194. return "", err
  195. }
  196. if net.ParseIP(ip) != nil {
  197. return ip, nil
  198. }
  199. return "", errors.New("no valid ip found")
  200. }
  201. func AuthInfo(str, token string) bool {
  202. if MD5Encode(str) == token {
  203. return true
  204. }
  205. return false
  206. }
  207. func MD5Encode(str string) string {
  208. return strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(str))))
  209. }