crm_eta.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. )
  10. const (
  11. LoginAuthCodeSource = 2
  12. )
  13. // MiddleServerResultData 中间服务响应体
  14. type MiddleServerResultData struct {
  15. Code int `json:"code" description:"状态码"`
  16. Msg string `json:"msg" description:"提示信息"`
  17. Data interface{} `json:"data" description:"返回数据"`
  18. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  19. }
  20. // GetLoginAuthCodeReq 获取登录编码请求体
  21. type GetLoginAuthCodeReq struct {
  22. AdminName string `json:"admin_name" description:"用户名"`
  23. Source int `json:"source" description:"来源: 1-CRM; 2-ETA"`
  24. }
  25. // GetAuthCodeFromMiddleServer CRM_ETA服务-获取登录编码
  26. func GetAuthCodeFromMiddleServer(adminName string) (authCode string, err error) {
  27. if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox {
  28. return
  29. }
  30. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/auth/auth_code")
  31. param := GetLoginAuthCodeReq{
  32. Source: LoginAuthCodeSource,
  33. AdminName: adminName,
  34. }
  35. data, e := json.Marshal(param)
  36. if e != nil {
  37. err = fmt.Errorf("data json marshal err: %s", e.Error())
  38. return
  39. }
  40. body := ioutil.NopCloser(strings.NewReader(string(data)))
  41. client := &http.Client{}
  42. req, e := http.NewRequest("POST", url, body)
  43. if e != nil {
  44. err = fmt.Errorf("http create request err: %s", e.Error())
  45. return
  46. }
  47. contentType := "application/json;charset=utf-8"
  48. req.Header.Set("Content-Type", contentType)
  49. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  50. resp, e := client.Do(req)
  51. if e != nil {
  52. err = fmt.Errorf("http client do err: %s", e.Error())
  53. return
  54. }
  55. defer func() {
  56. _ = resp.Body.Close()
  57. }()
  58. b, e := ioutil.ReadAll(resp.Body)
  59. if e != nil {
  60. err = fmt.Errorf("resp body read err: %s", e.Error())
  61. return
  62. }
  63. if len(b) == 0 {
  64. err = fmt.Errorf("resp body is empty")
  65. return
  66. }
  67. // 生产环境解密, 注意有个坑前后的双引号
  68. if utils.RunMode == "release" {
  69. str := string(b)
  70. str = strings.Trim(str, `"`)
  71. b = utils.DesBase64Decrypt([]byte(str), utils.DesKey)
  72. }
  73. result := new(MiddleServerResultData)
  74. if e = json.Unmarshal(b, &result); e != nil {
  75. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  76. return
  77. }
  78. if result.Code != 200 {
  79. err = fmt.Errorf("result: %s", string(b))
  80. return
  81. }
  82. code, ok := result.Data.(string)
  83. if !ok {
  84. err = fmt.Errorf("auth code empty")
  85. return
  86. }
  87. authCode = code
  88. return
  89. }
  90. // GetCrmTokenReq 获取Token请求体
  91. type GetCrmTokenReq struct {
  92. AuthCode string `json:"auth_code"`
  93. }
  94. // GetCrmTokenDataResp 获取Token信息响应体
  95. type GetCrmTokenDataResp struct {
  96. Code int `json:"code" description:"状态码"`
  97. Msg string `json:"msg" description:"提示信息"`
  98. Data GetCrmTokenData `json:"data" description:"返回数据"`
  99. }
  100. // GetCrmTokenData Token数据
  101. type GetCrmTokenData struct {
  102. Authorization string `description:"Auth Token"`
  103. AdminName string `description:"系统用户名称"`
  104. RealName string `description:"系统用户姓名"`
  105. RoleName string `description:"角色名称"`
  106. RoleTypeCode string `description:"角色类型编码"`
  107. SysRoleTypeCode string `description:"角色类型编码"`
  108. AdminId int `description:"系统用户id"`
  109. ProductName string `description:"产品名称:admin,ficc,权益"`
  110. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  111. }
  112. // CodeLoginFromMiddleServer 中间服务-编码登录
  113. func CodeLoginFromMiddleServer(authCode string) (tokenResp GetCrmTokenData, err error) {
  114. if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox {
  115. return
  116. }
  117. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/auth/eta_token")
  118. param := GetCrmTokenReq{
  119. AuthCode: authCode,
  120. }
  121. data, e := json.Marshal(param)
  122. if e != nil {
  123. err = fmt.Errorf("data json marshal err: %s", e.Error())
  124. return
  125. }
  126. body := ioutil.NopCloser(strings.NewReader(string(data)))
  127. client := &http.Client{}
  128. req, e := http.NewRequest("POST", url, body)
  129. if e != nil {
  130. err = fmt.Errorf("http create request err: %s", e.Error())
  131. return
  132. }
  133. contentType := "application/json;charset=utf-8"
  134. req.Header.Set("Content-Type", contentType)
  135. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  136. resp, e := client.Do(req)
  137. if e != nil {
  138. err = fmt.Errorf("http client do err: %s", e.Error())
  139. return
  140. }
  141. defer func() {
  142. _ = resp.Body.Close()
  143. }()
  144. b, e := ioutil.ReadAll(resp.Body)
  145. if e != nil {
  146. err = fmt.Errorf("resp body read err: %s", e.Error())
  147. return
  148. }
  149. if len(b) == 0 {
  150. err = fmt.Errorf("resp body is empty")
  151. return
  152. }
  153. // 生产环境解密, 注意有个坑前后的双引号
  154. if utils.RunMode == "release" {
  155. str := string(b)
  156. str = strings.Trim(str, `"`)
  157. b = utils.DesBase64Decrypt([]byte(str), utils.DesKey)
  158. }
  159. result := new(GetCrmTokenDataResp)
  160. if e = json.Unmarshal(b, &result); e != nil {
  161. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  162. return
  163. }
  164. if result.Code != 200 {
  165. err = fmt.Errorf("result: %s", string(b))
  166. return
  167. }
  168. tokenResp = result.Data
  169. return
  170. }