x002.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package dybd
  2. import (
  3. "gd_service/apis"
  4. "gd_service/common.in/config"
  5. "gd_service/consts"
  6. "gd_service/errors"
  7. "gd_service/thirdparty"
  8. dutils "gd_service/utils"
  9. "time"
  10. "github.com/astaxie/beego/orm"
  11. "gd_service/common.in/utils"
  12. "go.uber.org/zap"
  13. "github.com/tidwall/gjson"
  14. )
  15. func GetDybdX002Data(rdata string, respMap map[string]string) {
  16. arr := gjson.Parse(rdata).Array()
  17. for _, v := range arr {
  18. respMap["name"] = v.Get("xm").String()
  19. //respMap[""] = gjson.Get(rdata,"car_detail.reg_date").String()
  20. respMap["level"] = v.Get("cc").String() // 层次 专科,本科
  21. respMap["major"] = v.Get("zymc").String() //专业名称
  22. respMap["university"] = v.Get("yxmc").String() //毕业学校名称
  23. respMap["end_year"] = v.Get( "byrq").String() //毕业日期
  24. respMap["start_year"] = v.Get( "rxrq").String() //入学日期
  25. respMap["form"] = v.Get( "xxxs").String() //学习形式 普通全日制
  26. }
  27. }
  28. func getDybdX002Key(req *apis.ThirdpartRequest) string {
  29. key := consts.DybdX002Pre + req.Params[consts.DyBdX002P1] + req.Params[consts.DyBdX002P2]
  30. return key
  31. }
  32. func paramDybdX002Data(ret string) (reply string, err error) {
  33. code := gjson.Get(ret, "code").Int()
  34. if code == 200 {
  35. result := gjson.Get(ret, "data").String()
  36. arr := gjson.Parse(result).Array()
  37. if len(arr) == 0 {
  38. return result, errors.NoRecord
  39. } else {
  40. return result, nil
  41. }
  42. } else if code == 401{
  43. return "" ,errors.UserTokenExpire
  44. }else {
  45. return "", errors.VendorError
  46. }
  47. }
  48. func DybdX002Local(req *apis.ThirdpartRequest) (reply string, err error) {
  49. reply, err = dutils.GetDataByRedis(getDybdX002Key(req), int64(req.Req.ReuseTime), paramDybdX002Data)
  50. return reply, err
  51. }
  52. func StoreX002DataToMysql(rData map[string]string,resData string) {
  53. x002Data := apis.DybdX002Data{}
  54. name := rData[consts.DyBdX002P2]
  55. educationId := rData[consts.DyBdX002P1]
  56. o := orm.NewOrm()
  57. err := o.Raw("select * from t_gd_dybd_x002_data where education_id = ? and name=?",educationId,name).QueryRow(&x002Data)
  58. if err == nil{
  59. return
  60. }
  61. x002Data.Name = name
  62. x002Data.EducationId = educationId
  63. x002Data.Data = resData
  64. x002Data.Timestamp = time.Now().Unix()
  65. o.Insert(&x002Data)
  66. }
  67. func DybdX002(req *apis.ThirdpartRequest) (reply string, err error) {
  68. //req.LReq.Search = req.Params[consts.DyBdX002P1]
  69. //owner := req.Params["owner"]
  70. /*req.Params["qqfyyId"] = "test"
  71. req.Params["qqfyymc"] = "test"
  72. req.Params["qqrId"] = "test"
  73. req.Params["qqr"] = "test"*/
  74. redisKey := "dybd:xltoken:"
  75. token,err := getDybdToken(req.TryAgain,req.ProviderInfo.Timeout,config.Conf.ThirdPart.DybdXlAppkey,redisKey)
  76. if err != nil{
  77. l.Error("thridPart",
  78. zap.String("call", "getDybdToken"),
  79. zap.String("args",utils.MarshalJsonString(req.Params)),
  80. zap.String("error", err.Error()))
  81. req.LReq.ResponseParams = err.Error()
  82. dutils.SetAccessLogReqCodeMsgState(req.LReq, 1101, "gd_1101", err.Error(), false)
  83. return "",errors.VendorError
  84. }
  85. ret, err := thirdparty.DyBdClientPost(req.ProviderInfo.ThirdpartHost+"/"+req.ProviderInfo.ThirdpartApiRouter, req.Params, req.ProviderInfo.Timeout,token)
  86. if err != nil {
  87. l.Error("func",
  88. zap.String("call", "DyBdClientPost"),
  89. zap.String("args", utils.MarshalJsonString(req)),
  90. zap.String("error", err.Error()))
  91. req.LReq.ResponseParams = err.Error()
  92. dutils.SetAccessLogReqCodeMsgState(req.LReq, 1101, "gd_1101", "三方错误,请求失败", false)
  93. return "", errors.VendorError
  94. } else {
  95. req.LReq.ResponseParams = string(ret)
  96. reply, err = paramDybdX002Data(string(ret))
  97. code := gjson.GetBytes(ret, "code").String()
  98. message := gjson.GetBytes(ret, "message").String()
  99. if err == nil {
  100. dutils.SetAccessLogReqCodeMsgState(req.LReq, 0, code, message, true)
  101. dutils.StoreDataToRedis(getDybdX002Key(req), string(ret))
  102. //go StoreX002DataToMysql(req.Params,reply)
  103. } else if err == errors.NoRecord {
  104. dutils.SetAccessLogReqCodeMsgState(req.LReq, 1100, "gd_1100", message, false)
  105. dutils.StoreDataToRedis(getDybdX002Key(req), string(ret))
  106. } else if err == errors.UserTokenExpire{
  107. if !req.TryAgain {
  108. req.TryAgain = true
  109. return DybdX002(req)
  110. }
  111. err = errors.VendorError
  112. dutils.SetAccessLogReqCodeMsgState(req.LReq, 1101, code, message, false)
  113. }else {
  114. l.Error("func",
  115. zap.String("call", "DyBdClientPost"),
  116. zap.String("args", utils.MarshalJsonString(req.Params)),
  117. zap.String("error", err.Error()))
  118. dutils.SetAccessLogReqCodeMsgState(req.LReq,1101,code, message, false)
  119. }
  120. return reply, err
  121. }
  122. }