crm_eta.go 5.3 KB

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