crm_eta.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/services/alarm_msg"
  6. "hongze/hz_crm_api/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. const (
  12. LoginAuthCodeSource = 1
  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. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/auth/auth_code")
  29. param := GetLoginAuthCodeReq{
  30. Source: LoginAuthCodeSource,
  31. AdminName: adminName,
  32. }
  33. data, e := json.Marshal(param)
  34. if e != nil {
  35. err = fmt.Errorf("data json marshal err: %s", e.Error())
  36. return
  37. }
  38. body := ioutil.NopCloser(strings.NewReader(string(data)))
  39. client := &http.Client{}
  40. req, e := http.NewRequest("POST", url, body)
  41. if e != nil {
  42. err = fmt.Errorf("http create request err: %s", e.Error())
  43. return
  44. }
  45. contentType := "application/json;charset=utf-8"
  46. req.Header.Set("Content-Type", contentType)
  47. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  48. resp, e := client.Do(req)
  49. if e != nil {
  50. err = fmt.Errorf("http client do err: %s", e.Error())
  51. return
  52. }
  53. defer func() {
  54. _ = resp.Body.Close()
  55. }()
  56. b, e := ioutil.ReadAll(resp.Body)
  57. if e != nil {
  58. err = fmt.Errorf("resp body read err: %s", e.Error())
  59. return
  60. }
  61. if len(b) == 0 {
  62. err = fmt.Errorf("resp body is empty")
  63. return
  64. }
  65. // 生产环境解密, 注意有个坑前后的双引号
  66. if utils.RunMode == "release" {
  67. str := string(b)
  68. str = strings.Trim(str, `"`)
  69. b = utils.DesBase64Decrypt([]byte(str))
  70. }
  71. result := new(MiddleServerResultData)
  72. if e = json.Unmarshal(b, &result); e != nil {
  73. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  74. return
  75. }
  76. if result.Code != 200 {
  77. err = fmt.Errorf("result: %s", string(b))
  78. return
  79. }
  80. code, ok := result.Data.(string)
  81. if !ok {
  82. err = fmt.Errorf("auth code empty")
  83. return
  84. }
  85. authCode = code
  86. return
  87. }
  88. // GetEtaTokenReq 获取Token请求体
  89. type GetEtaTokenReq struct {
  90. AuthCode string `json:"auth_code"`
  91. }
  92. // GetEtaTokenDataResp 获取Token信息响应体
  93. type GetEtaTokenDataResp struct {
  94. Code int `json:"code" description:"状态码"`
  95. Msg string `json:"msg" description:"提示信息"`
  96. Data GetEtaTokenData `json:"data" description:"返回数据"`
  97. }
  98. // GetEtaTokenData Token数据
  99. type GetEtaTokenData struct {
  100. Authorization string `description:"Auth Token"`
  101. AdminName string `description:"系统用户名称"`
  102. RealName string `description:"系统用户姓名"`
  103. RoleName string `description:"角色名称"`
  104. RoleTypeCode string `description:"角色类型编码"`
  105. SysRoleTypeCode string `description:"角色类型编码"`
  106. AdminId int `description:"系统用户id"`
  107. ProductName string `description:"产品名称:admin,ficc,权益"`
  108. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  109. }
  110. // CodeLoginFromMiddleServer 中间服务-编码登录
  111. func CodeLoginFromMiddleServer(authCode string) (tokenResp GetEtaTokenData, err error) {
  112. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/auth/crm_token")
  113. param := GetEtaTokenReq{
  114. AuthCode: authCode,
  115. }
  116. data, e := json.Marshal(param)
  117. if e != nil {
  118. err = fmt.Errorf("data json marshal err: %s", e.Error())
  119. return
  120. }
  121. body := ioutil.NopCloser(strings.NewReader(string(data)))
  122. client := &http.Client{}
  123. req, e := http.NewRequest("POST", url, body)
  124. if e != nil {
  125. err = fmt.Errorf("http create request err: %s", e.Error())
  126. return
  127. }
  128. contentType := "application/json;charset=utf-8"
  129. req.Header.Set("Content-Type", contentType)
  130. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  131. resp, e := client.Do(req)
  132. if e != nil {
  133. err = fmt.Errorf("http client do err: %s", e.Error())
  134. return
  135. }
  136. defer func() {
  137. _ = resp.Body.Close()
  138. }()
  139. b, e := ioutil.ReadAll(resp.Body)
  140. if e != nil {
  141. err = fmt.Errorf("resp body read err: %s", e.Error())
  142. return
  143. }
  144. if len(b) == 0 {
  145. err = fmt.Errorf("resp body is empty")
  146. return
  147. }
  148. // 生产环境解密, 注意有个坑前后的双引号
  149. if utils.RunMode == "release" {
  150. str := string(b)
  151. str = strings.Trim(str, `"`)
  152. b = utils.DesBase64Decrypt([]byte(str))
  153. }
  154. result := new(GetEtaTokenDataResp)
  155. if e = json.Unmarshal(b, &result); e != nil {
  156. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  157. return
  158. }
  159. if result.Code != 200 {
  160. err = fmt.Errorf("result: %s", string(b))
  161. return
  162. }
  163. tokenResp = result.Data
  164. return
  165. }
  166. // EtaTrialManualUserAddAuthReq 体验版用户添加手工权限
  167. type EtaTrialManualUserAddAuthReq struct {
  168. AdminId int `json:"admin_id" description:"用户ID"`
  169. AdminName string `json:"admin_name" description:"用户名"`
  170. }
  171. // EtaTrialManualUserAddAuth CRM_ETA服务-体验版用户添加手工权限
  172. func EtaTrialManualUserAddAuth(adminId int, adminName string) (err error) {
  173. // 体验版无测试环境
  174. //if utils.RunMode != "release" {
  175. // return
  176. //}
  177. defer func() {
  178. if err != nil {
  179. alarm_msg.SendAlarmMsg(fmt.Sprintf("EtaTrialManualUserAddAuth, 新增试用客户手工录入权限失败, ErrMsg: %s", err.Error()), 3)
  180. }
  181. }()
  182. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/manual_user/add_auth")
  183. param := EtaTrialManualUserAddAuthReq{
  184. AdminId: adminId,
  185. AdminName: adminName,
  186. }
  187. data, e := json.Marshal(param)
  188. if e != nil {
  189. err = fmt.Errorf("data json marshal err: %s", e.Error())
  190. return
  191. }
  192. body := ioutil.NopCloser(strings.NewReader(string(data)))
  193. client := &http.Client{}
  194. req, e := http.NewRequest("POST", url, body)
  195. if e != nil {
  196. err = fmt.Errorf("http create request err: %s", e.Error())
  197. return
  198. }
  199. contentType := "application/json;charset=utf-8"
  200. req.Header.Set("Content-Type", contentType)
  201. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  202. resp, e := client.Do(req)
  203. if e != nil {
  204. err = fmt.Errorf("http client do err: %s", e.Error())
  205. return
  206. }
  207. defer func() {
  208. _ = resp.Body.Close()
  209. }()
  210. b, e := ioutil.ReadAll(resp.Body)
  211. if e != nil {
  212. err = fmt.Errorf("resp body read err: %s", e.Error())
  213. return
  214. }
  215. if len(b) == 0 {
  216. err = fmt.Errorf("resp body is empty")
  217. return
  218. }
  219. // 生产环境解密, 注意有个坑前后的双引号
  220. if utils.RunMode == "release" {
  221. str := string(b)
  222. str = strings.Trim(str, `"`)
  223. b = utils.DesBase64Decrypt([]byte(str))
  224. }
  225. result := new(MiddleServerResultData)
  226. if e = json.Unmarshal(b, &result); e != nil {
  227. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  228. return
  229. }
  230. if result.Code != 200 {
  231. err = fmt.Errorf("result: %s", string(b))
  232. return
  233. }
  234. return
  235. }