user.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. package eta_trial
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/eta_trial"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // EtaTrialUserReq 更新试用客户最后登录时间和次数请求体
  13. type EtaTrialUserReq struct {
  14. Mobile string `description:"手机号"`
  15. }
  16. // UpdateEtaTrialUserLogin CRM_ETA服务-更新试用客户最后登录时间和次数
  17. func UpdateEtaTrialUserLogin(pars EtaTrialUserReq) (res bool, err error) {
  18. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/update_login")
  19. params, e := json.Marshal(pars)
  20. if e != nil {
  21. err = fmt.Errorf("data json marshal err: %s", e.Error())
  22. return
  23. }
  24. body := ioutil.NopCloser(strings.NewReader(string(params)))
  25. client := &http.Client{}
  26. req, e := http.NewRequest("POST", url, body)
  27. if e != nil {
  28. err = fmt.Errorf("http create request err: %s", e.Error())
  29. return
  30. }
  31. contentType := "application/json;charset=utf-8"
  32. req.Header.Set("Content-Type", contentType)
  33. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  34. resp, e := client.Do(req)
  35. if e != nil {
  36. err = fmt.Errorf("http client do err: %s", e.Error())
  37. return
  38. }
  39. defer func() {
  40. _ = resp.Body.Close()
  41. }()
  42. b, e := ioutil.ReadAll(resp.Body)
  43. if e != nil {
  44. err = fmt.Errorf("resp body read err: %s", e.Error())
  45. return
  46. }
  47. if len(b) == 0 {
  48. err = fmt.Errorf("resp body is empty")
  49. return
  50. }
  51. // 生产环境解密, 注意有个坑前后的双引号
  52. if utils.RunMode == "release" {
  53. str := string(b)
  54. str = strings.Trim(str, `"`)
  55. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  56. }
  57. result := new(models.ResultData)
  58. if e = json.Unmarshal(b, &result); e != nil {
  59. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  60. return
  61. }
  62. if result.Code != 200 {
  63. err = fmt.Errorf("result: %s", string(b))
  64. return
  65. }
  66. res = true
  67. return
  68. }
  69. // GetEtaTrialUserReq 获取试用客户请求体
  70. type GetEtaTrialUserReq struct {
  71. Mobile string `description:"手机号"`
  72. }
  73. type EtaTrialUserResp struct {
  74. Code int `json:"code" description:"状态码"`
  75. Msg string `json:"msg" description:"提示信息"`
  76. Data eta_trial.EtaTrialUserItem `json:"data" description:"返回数据"`
  77. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  78. }
  79. // GetEtaTrialUser CRM_ETA服务-获取试用客户信息
  80. func GetEtaTrialUser(pars GetEtaTrialUserReq) (res eta_trial.EtaTrialUserItem, err error) {
  81. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/mobile_fetch")
  82. params, e := json.Marshal(pars)
  83. if e != nil {
  84. err = fmt.Errorf("data json marshal err: %s", e.Error())
  85. return
  86. }
  87. body := ioutil.NopCloser(strings.NewReader(string(params)))
  88. client := &http.Client{}
  89. req, e := http.NewRequest("POST", url, body)
  90. if e != nil {
  91. err = fmt.Errorf("http create request err: %s", e.Error())
  92. return
  93. }
  94. contentType := "application/json;charset=utf-8"
  95. req.Header.Set("Content-Type", contentType)
  96. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  97. resp, e := client.Do(req)
  98. if e != nil {
  99. err = fmt.Errorf("http client do err: %s", e.Error())
  100. return
  101. }
  102. defer func() {
  103. _ = resp.Body.Close()
  104. }()
  105. b, e := ioutil.ReadAll(resp.Body)
  106. if e != nil {
  107. err = fmt.Errorf("resp body read err: %s", e.Error())
  108. return
  109. }
  110. if len(b) == 0 {
  111. err = fmt.Errorf("resp body is empty")
  112. return
  113. }
  114. // 生产环境解密, 注意有个坑前后的双引号
  115. if utils.RunMode == "release" {
  116. str := string(b)
  117. str = strings.Trim(str, `"`)
  118. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  119. }
  120. //result := new(models.ResultData)
  121. result := new(EtaTrialUserResp)
  122. if e = json.Unmarshal(b, &result); e != nil {
  123. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  124. return
  125. }
  126. utils.FileLog.Info("%s", string(b))
  127. if result.Code != 200 {
  128. err = fmt.Errorf("result: %s", string(b))
  129. return
  130. }
  131. res = result.Data
  132. //r, ok := result.Data.(eta_trial.EtaTrialUserResp)
  133. //if !ok {
  134. // err = fmt.Errorf("result data err")
  135. // return
  136. //}
  137. //res = r
  138. return
  139. }
  140. // DisableEtaTrialUser CRM_ETA服务-禁用试用客户
  141. func DisableEtaTrialUser(pars EtaTrialUserReq) (res bool, err error) {
  142. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/disable")
  143. params, e := json.Marshal(pars)
  144. if e != nil {
  145. err = fmt.Errorf("data json marshal err: %s", e.Error())
  146. return
  147. }
  148. body := ioutil.NopCloser(strings.NewReader(string(params)))
  149. client := &http.Client{}
  150. req, e := http.NewRequest("POST", url, body)
  151. if e != nil {
  152. err = fmt.Errorf("http create request err: %s", e.Error())
  153. return
  154. }
  155. contentType := "application/json;charset=utf-8"
  156. req.Header.Set("Content-Type", contentType)
  157. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  158. resp, e := client.Do(req)
  159. if e != nil {
  160. err = fmt.Errorf("http client do err: %s", e.Error())
  161. return
  162. }
  163. defer func() {
  164. _ = resp.Body.Close()
  165. }()
  166. b, e := ioutil.ReadAll(resp.Body)
  167. if e != nil {
  168. err = fmt.Errorf("resp body read err: %s", e.Error())
  169. return
  170. }
  171. if len(b) == 0 {
  172. err = fmt.Errorf("resp body is empty")
  173. return
  174. }
  175. // 生产环境解密, 注意有个坑前后的双引号
  176. if utils.RunMode == "release" {
  177. str := string(b)
  178. str = strings.Trim(str, `"`)
  179. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  180. }
  181. result := new(models.ResultData)
  182. if e = json.Unmarshal(b, &result); e != nil {
  183. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  184. return
  185. }
  186. if result.Code != 200 {
  187. err = fmt.Errorf("result: %s", string(b))
  188. return
  189. }
  190. res = true
  191. return
  192. }
  193. // RemoveEtaTrialUser CRM_ETA服务-删除试用客户
  194. func RemoveEtaTrialUser(pars EtaTrialUserReq) (res bool, err error) {
  195. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/remove")
  196. params, e := json.Marshal(pars)
  197. if e != nil {
  198. err = fmt.Errorf("data json marshal err: %s", e.Error())
  199. return
  200. }
  201. body := ioutil.NopCloser(strings.NewReader(string(params)))
  202. client := &http.Client{}
  203. req, e := http.NewRequest("POST", url, body)
  204. if e != nil {
  205. err = fmt.Errorf("http create request err: %s", e.Error())
  206. return
  207. }
  208. contentType := "application/json;charset=utf-8"
  209. req.Header.Set("Content-Type", contentType)
  210. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  211. resp, e := client.Do(req)
  212. if e != nil {
  213. err = fmt.Errorf("http client do err: %s", e.Error())
  214. return
  215. }
  216. defer func() {
  217. _ = resp.Body.Close()
  218. }()
  219. b, e := ioutil.ReadAll(resp.Body)
  220. if e != nil {
  221. err = fmt.Errorf("resp body read err: %s", e.Error())
  222. return
  223. }
  224. if len(b) == 0 {
  225. err = fmt.Errorf("resp body is empty")
  226. return
  227. }
  228. // 生产环境解密, 注意有个坑前后的双引号
  229. if utils.RunMode == "release" {
  230. str := string(b)
  231. str = strings.Trim(str, `"`)
  232. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  233. }
  234. result := new(models.ResultData)
  235. if e = json.Unmarshal(b, &result); e != nil {
  236. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  237. return
  238. }
  239. if result.Code != 200 {
  240. err = fmt.Errorf("result: %s", string(b))
  241. return
  242. }
  243. res = true
  244. return
  245. }
  246. // EtaTrialUserEditReq 更新用户信息请求体
  247. type EtaTrialUserEditReq struct {
  248. RealName string `description:"姓名"`
  249. Position string `description:"职务"`
  250. Mobile string `description:"手机号"`
  251. Enabled int `description:"禁启用"`
  252. }
  253. // EditEtaTrialUser CRM_ETA服务-编辑试用客户
  254. func EditEtaTrialUser(pars EtaTrialUserEditReq) (res bool, err error) {
  255. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/edit")
  256. params, e := json.Marshal(pars)
  257. if e != nil {
  258. err = fmt.Errorf("data json marshal err: %s", e.Error())
  259. return
  260. }
  261. body := ioutil.NopCloser(strings.NewReader(string(params)))
  262. client := &http.Client{}
  263. req, e := http.NewRequest("POST", url, body)
  264. if e != nil {
  265. err = fmt.Errorf("http create request err: %s", e.Error())
  266. return
  267. }
  268. contentType := "application/json;charset=utf-8"
  269. req.Header.Set("Content-Type", contentType)
  270. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  271. resp, e := client.Do(req)
  272. if e != nil {
  273. err = fmt.Errorf("http client do err: %s", e.Error())
  274. return
  275. }
  276. defer func() {
  277. _ = resp.Body.Close()
  278. }()
  279. b, e := ioutil.ReadAll(resp.Body)
  280. if e != nil {
  281. err = fmt.Errorf("resp body read err: %s", e.Error())
  282. return
  283. }
  284. if len(b) == 0 {
  285. err = fmt.Errorf("resp body is empty")
  286. return
  287. }
  288. // 生产环境解密, 注意有个坑前后的双引号
  289. if utils.RunMode == "release" {
  290. str := string(b)
  291. str = strings.Trim(str, `"`)
  292. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  293. }
  294. result := new(models.ResultData)
  295. if e = json.Unmarshal(b, &result); e != nil {
  296. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  297. return
  298. }
  299. if result.Code != 200 {
  300. err = fmt.Errorf("result: %s", string(b))
  301. return
  302. }
  303. res = true
  304. return
  305. }
  306. // UpdateUserIndexNum CRM_ETA服务-更新用户累计新增指标数
  307. func UpdateUserIndexNum(pars EtaTrialUserReq) (res bool, err error) {
  308. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/update_index_num")
  309. params, e := json.Marshal(pars)
  310. if e != nil {
  311. err = fmt.Errorf("data json marshal err: %s", e.Error())
  312. return
  313. }
  314. body := ioutil.NopCloser(strings.NewReader(string(params)))
  315. client := &http.Client{}
  316. req, e := http.NewRequest("POST", url, body)
  317. if e != nil {
  318. err = fmt.Errorf("http create request err: %s", e.Error())
  319. return
  320. }
  321. contentType := "application/json;charset=utf-8"
  322. req.Header.Set("Content-Type", contentType)
  323. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  324. resp, e := client.Do(req)
  325. if e != nil {
  326. err = fmt.Errorf("http client do err: %s", e.Error())
  327. return
  328. }
  329. defer func() {
  330. _ = resp.Body.Close()
  331. }()
  332. b, e := ioutil.ReadAll(resp.Body)
  333. if e != nil {
  334. err = fmt.Errorf("resp body read err: %s", e.Error())
  335. return
  336. }
  337. if len(b) == 0 {
  338. err = fmt.Errorf("resp body is empty")
  339. return
  340. }
  341. // 生产环境解密, 注意有个坑前后的双引号
  342. if utils.RunMode == "release" {
  343. str := string(b)
  344. str = strings.Trim(str, `"`)
  345. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  346. }
  347. result := new(models.ResultData)
  348. if e = json.Unmarshal(b, &result); e != nil {
  349. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  350. return
  351. }
  352. if result.Code != 200 {
  353. err = fmt.Errorf("result: %s", string(b))
  354. return
  355. }
  356. res = true
  357. return
  358. }
  359. // UpdateUserChartNum CRM_ETA服务-更新用户累计新增图表数
  360. func UpdateUserChartNum(pars EtaTrialUserReq) (res bool, err error) {
  361. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/update_chart_num")
  362. params, e := json.Marshal(pars)
  363. if e != nil {
  364. err = fmt.Errorf("data json marshal err: %s", e.Error())
  365. return
  366. }
  367. body := ioutil.NopCloser(strings.NewReader(string(params)))
  368. client := &http.Client{}
  369. req, e := http.NewRequest("POST", url, body)
  370. if e != nil {
  371. err = fmt.Errorf("http create request err: %s", e.Error())
  372. return
  373. }
  374. contentType := "application/json;charset=utf-8"
  375. req.Header.Set("Content-Type", contentType)
  376. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  377. resp, e := client.Do(req)
  378. if e != nil {
  379. err = fmt.Errorf("http client do err: %s", e.Error())
  380. return
  381. }
  382. defer func() {
  383. _ = resp.Body.Close()
  384. }()
  385. b, e := ioutil.ReadAll(resp.Body)
  386. if e != nil {
  387. err = fmt.Errorf("resp body read err: %s", e.Error())
  388. return
  389. }
  390. if len(b) == 0 {
  391. err = fmt.Errorf("resp body is empty")
  392. return
  393. }
  394. // 生产环境解密, 注意有个坑前后的双引号
  395. if utils.RunMode == "release" {
  396. str := string(b)
  397. str = strings.Trim(str, `"`)
  398. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  399. }
  400. result := new(models.ResultData)
  401. if e = json.Unmarshal(b, &result); e != nil {
  402. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  403. return
  404. }
  405. if result.Code != 200 {
  406. err = fmt.Errorf("result: %s", string(b))
  407. return
  408. }
  409. res = true
  410. return
  411. }
  412. // UpdateEtaTrialUserActiveTimeReq 更新试用客户活跃时长请求体
  413. type UpdateEtaTrialUserActiveTimeReq struct {
  414. Mobile string `description:"手机号"`
  415. UserName string `description:"用户姓名"`
  416. ActiveTime int `description:"活跃时长, 单位秒"`
  417. Part string `description:"活跃板块"`
  418. }
  419. // UpdateEtaTrialUserActiveTime CRM_ETA服务-更新试用客户活跃时长
  420. func UpdateEtaTrialUserActiveTime(pars UpdateEtaTrialUserActiveTimeReq) (res bool, err error) {
  421. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/update_active_time")
  422. params, e := json.Marshal(pars)
  423. if e != nil {
  424. err = fmt.Errorf("data json marshal err: %s", e.Error())
  425. return
  426. }
  427. body := ioutil.NopCloser(strings.NewReader(string(params)))
  428. client := &http.Client{}
  429. req, e := http.NewRequest("POST", url, body)
  430. if e != nil {
  431. err = fmt.Errorf("http create request err: %s", e.Error())
  432. return
  433. }
  434. contentType := "application/json;charset=utf-8"
  435. req.Header.Set("Content-Type", contentType)
  436. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  437. resp, e := client.Do(req)
  438. if e != nil {
  439. err = fmt.Errorf("http client do err: %s", e.Error())
  440. return
  441. }
  442. defer func() {
  443. _ = resp.Body.Close()
  444. }()
  445. b, e := ioutil.ReadAll(resp.Body)
  446. if e != nil {
  447. err = fmt.Errorf("resp body read err: %s", e.Error())
  448. return
  449. }
  450. if len(b) == 0 {
  451. err = fmt.Errorf("resp body is empty")
  452. return
  453. }
  454. // 生产环境解密, 注意有个坑前后的双引号
  455. if utils.RunMode == "release" {
  456. str := string(b)
  457. str = strings.Trim(str, `"`)
  458. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  459. }
  460. result := new(models.ResultData)
  461. if e = json.Unmarshal(b, &result); e != nil {
  462. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  463. return
  464. }
  465. if result.Code != 200 {
  466. err = fmt.Errorf("result: %s", string(b))
  467. return
  468. }
  469. res = true
  470. return
  471. }
  472. // UpdateEtaTrialUserLoginDurationReq 更新试用客户登录时长请求体
  473. type UpdateEtaTrialUserLoginDurationReq struct {
  474. Mobile string `description:"手机号"`
  475. UserName string `description:"用户姓名"`
  476. ActiveTime int `description:"活跃时长, 单位秒"`
  477. }
  478. // UpdateEtaTrialUserLoginDuration CRM_ETA服务-更新试用客户登录时长
  479. func UpdateEtaTrialUserLoginDuration(pars UpdateEtaTrialUserLoginDurationReq) (res bool, err error) {
  480. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/user/update_login_duration")
  481. params, e := json.Marshal(pars)
  482. if e != nil {
  483. err = fmt.Errorf("data json marshal err: %s", e.Error())
  484. return
  485. }
  486. body := ioutil.NopCloser(strings.NewReader(string(params)))
  487. client := &http.Client{}
  488. req, e := http.NewRequest("POST", url, body)
  489. if e != nil {
  490. err = fmt.Errorf("http create request err: %s", e.Error())
  491. return
  492. }
  493. contentType := "application/json;charset=utf-8"
  494. req.Header.Set("Content-Type", contentType)
  495. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  496. resp, e := client.Do(req)
  497. if e != nil {
  498. err = fmt.Errorf("http client do err: %s", e.Error())
  499. return
  500. }
  501. defer func() {
  502. _ = resp.Body.Close()
  503. }()
  504. b, e := ioutil.ReadAll(resp.Body)
  505. if e != nil {
  506. err = fmt.Errorf("resp body read err: %s", e.Error())
  507. return
  508. }
  509. if len(b) == 0 {
  510. err = fmt.Errorf("resp body is empty")
  511. return
  512. }
  513. // 生产环境解密, 注意有个坑前后的双引号
  514. if utils.RunMode == "release" {
  515. str := string(b)
  516. str = strings.Trim(str, `"`)
  517. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  518. }
  519. result := new(models.ResultData)
  520. if e = json.Unmarshal(b, &result); e != nil {
  521. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  522. return
  523. }
  524. if result.Code != 200 {
  525. err = fmt.Errorf("result: %s", string(b))
  526. return
  527. }
  528. res = true
  529. return
  530. }