user.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package eta_trial
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/eta_trial"
  6. "eta/eta_api/services/alarm_msg"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. )
  13. // EtaTrialUserReq 更新试用客户最后登录时间和次数请求体
  14. type EtaTrialUserReq struct {
  15. Mobile string `description:"手机号"`
  16. }
  17. // UpdateEtaTrialUserLogin eta_forum_hub服务-更新试用客户最后登录时间和次数
  18. func UpdateEtaTrialUserLogin(pars EtaTrialUserReq) (res bool, err error) {
  19. defer func() {
  20. if err != nil {
  21. alarm_msg.SendAlarmMsg(fmt.Sprintf("GetEtaTrialMobileCount, 新增试用客户手工录入权限失败, ErrMsg: %s", err.Error()), 3)
  22. }
  23. }()
  24. reqJson, e := json.Marshal(pars)
  25. if e != nil {
  26. err = fmt.Errorf("data json marshal err: %s", e.Error())
  27. return
  28. }
  29. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_login")
  30. resp := new(models.BaseResponse)
  31. err = json.Unmarshal(resultByte, &resp)
  32. if err != nil {
  33. return
  34. }
  35. if resp.Ret != 200 {
  36. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  37. return
  38. }
  39. res = true
  40. return
  41. }
  42. // GetEtaTrialUserReq 获取试用客户请求体
  43. type GetEtaTrialUserReq struct {
  44. Mobile string `description:"手机号"`
  45. }
  46. type EtaTrialUserResp struct {
  47. Ret int
  48. Msg string
  49. ErrMsg string
  50. ErrCode string
  51. Data eta_trial.EtaTrialUserItem
  52. Success bool `description:"true 执行成功,false 执行失败"`
  53. }
  54. // GetEtaTrialUser eta_forum_hub服务-获取试用客户信息
  55. func GetEtaTrialUser(pars GetEtaTrialUserReq) (res eta_trial.EtaTrialUserItem, err error) {
  56. defer func() {
  57. if err != nil {
  58. alarm_msg.SendAlarmMsg(fmt.Sprintf("GetEtaTrialUser, 获取试用客户信息, ErrMsg: %s", err.Error()), 3)
  59. }
  60. }()
  61. reqJson, e := json.Marshal(pars)
  62. if e != nil {
  63. err = fmt.Errorf("data json marshal err: %s", e.Error())
  64. return
  65. }
  66. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/mobile_fetch")
  67. resp := new(EtaTrialUserResp)
  68. err = json.Unmarshal(resultByte, &resp)
  69. if err != nil {
  70. return
  71. }
  72. if resp.Ret != 200 {
  73. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  74. return
  75. }
  76. res = resp.Data
  77. return
  78. }
  79. // DisableEtaTrialUser eta_forum_hub服务-禁用试用客户
  80. func DisableEtaTrialUser(pars EtaTrialUserReq) (res bool, err error) {
  81. defer func() {
  82. if err != nil {
  83. alarm_msg.SendAlarmMsg(fmt.Sprintf("DisableEtaTrialUser eta_forum_hub服务-禁用试用客户, ErrMsg: %s", err.Error()), 3)
  84. }
  85. }()
  86. reqJson, e := json.Marshal(pars)
  87. if e != nil {
  88. err = fmt.Errorf("data json marshal err: %s", e.Error())
  89. return
  90. }
  91. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/disable")
  92. resp := new(models.BaseResponse)
  93. err = json.Unmarshal(resultByte, &resp)
  94. if err != nil {
  95. return
  96. }
  97. if resp.Ret != 200 {
  98. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  99. return
  100. }
  101. res = true
  102. return
  103. }
  104. // RemoveEtaTrialUser eta_forum_hub服务-删除试用客户
  105. func RemoveEtaTrialUser(pars EtaTrialUserReq) (res bool, err error) {
  106. defer func() {
  107. if err != nil {
  108. alarm_msg.SendAlarmMsg(fmt.Sprintf("RemoveEtaTrialUser eta_forum_hub服务-删除试用客户失败, ErrMsg: %s", err.Error()), 3)
  109. }
  110. }()
  111. reqJson, e := json.Marshal(pars)
  112. if e != nil {
  113. err = fmt.Errorf("data json marshal err: %s", e.Error())
  114. return
  115. }
  116. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/remove")
  117. resp := new(models.BaseResponse)
  118. err = json.Unmarshal(resultByte, &resp)
  119. if err != nil {
  120. return
  121. }
  122. if resp.Ret != 200 {
  123. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  124. return
  125. }
  126. res = true
  127. return
  128. }
  129. // EtaTrialUserEditReq 更新用户信息请求体
  130. type EtaTrialUserEditReq struct {
  131. RealName string `description:"姓名"`
  132. Position string `description:"职务"`
  133. Mobile string `description:"手机号"`
  134. Enabled int `description:"禁启用"`
  135. }
  136. // EditEtaTrialUser eta_forum_hub服务-编辑试用客户
  137. func EditEtaTrialUser(pars EtaTrialUserEditReq) (res bool, err error) {
  138. defer func() {
  139. if err != nil {
  140. alarm_msg.SendAlarmMsg(fmt.Sprintf("EditEtaTrialUser eta_forum_hub服务-编辑试用客户失败, ErrMsg: %s", err.Error()), 3)
  141. }
  142. }()
  143. reqJson, e := json.Marshal(pars)
  144. if e != nil {
  145. err = fmt.Errorf("data json marshal err: %s", e.Error())
  146. return
  147. }
  148. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/edit")
  149. resp := new(models.BaseResponse)
  150. err = json.Unmarshal(resultByte, &resp)
  151. if err != nil {
  152. return
  153. }
  154. if resp.Ret != 200 {
  155. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  156. return
  157. }
  158. res = true
  159. return
  160. }
  161. // UpdateUserIndexNum eta_forum_hub服务-更新用户累计新增指标数
  162. func UpdateUserIndexNum(pars EtaTrialUserReq) (res bool, err error) {
  163. defer func() {
  164. if err != nil {
  165. alarm_msg.SendAlarmMsg(fmt.Sprintf(" UpdateUserIndexNum eta_forum_hub服务-更新用户累计新增指标数失败, ErrMsg: %s", err.Error()), 3)
  166. }
  167. }()
  168. reqJson, e := json.Marshal(pars)
  169. if e != nil {
  170. err = fmt.Errorf("data json marshal err: %s", e.Error())
  171. return
  172. }
  173. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_index_num")
  174. resp := new(models.BaseResponse)
  175. err = json.Unmarshal(resultByte, &resp)
  176. if err != nil {
  177. return
  178. }
  179. if resp.Ret != 200 {
  180. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  181. return
  182. }
  183. res = true
  184. return
  185. }
  186. // UpdateUserChartNum eta_forum_hub服务-更新用户累计新增图表数
  187. func UpdateUserChartNum(pars EtaTrialUserReq) (res bool, err error) {
  188. defer func() {
  189. if err != nil {
  190. alarm_msg.SendAlarmMsg(fmt.Sprintf("UpdateUserChartNum eta_forum_hub服务-更新用户累计新增图表数失败, ErrMsg: %s", err.Error()), 3)
  191. }
  192. }()
  193. reqJson, e := json.Marshal(pars)
  194. if e != nil {
  195. err = fmt.Errorf("data json marshal err: %s", e.Error())
  196. return
  197. }
  198. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_chart_num")
  199. resp := new(models.BaseResponse)
  200. err = json.Unmarshal(resultByte, &resp)
  201. if err != nil {
  202. return
  203. }
  204. if resp.Ret != 200 {
  205. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  206. return
  207. }
  208. res = true
  209. return
  210. }
  211. // UpdateEtaTrialUserActiveTimeReq 更新试用客户活跃时长请求体
  212. type UpdateEtaTrialUserActiveTimeReq struct {
  213. Mobile string `description:"手机号"`
  214. UserName string `description:"用户姓名"`
  215. ActiveTime int `description:"活跃时长, 单位秒"`
  216. Part string `description:"活跃板块"`
  217. }
  218. // UpdateEtaTrialUserActiveTime eta_forum_hub服务-更新试用客户活跃时长
  219. func UpdateEtaTrialUserActiveTime(pars UpdateEtaTrialUserActiveTimeReq) (res bool, err error) {
  220. defer func() {
  221. if err != nil {
  222. alarm_msg.SendAlarmMsg(fmt.Sprintf("UpdateEtaTrialUserActiveTime eta_forum_hub服务-更新试用客户活跃时长失败, ErrMsg: %s", err.Error()), 3)
  223. }
  224. }()
  225. reqJson, e := json.Marshal(pars)
  226. if e != nil {
  227. err = fmt.Errorf("data json marshal err: %s", e.Error())
  228. return
  229. }
  230. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_active_time")
  231. resp := new(models.BaseResponse)
  232. err = json.Unmarshal(resultByte, &resp)
  233. if err != nil {
  234. return
  235. }
  236. if resp.Ret != 200 {
  237. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  238. return
  239. }
  240. res = true
  241. return
  242. }
  243. // UpdateEtaTrialUserLoginDurationReq 更新试用客户登录时长请求体
  244. type UpdateEtaTrialUserLoginDurationReq struct {
  245. Mobile string `description:"手机号"`
  246. UserName string `description:"用户姓名"`
  247. ActiveTime int `description:"活跃时长, 单位秒"`
  248. }
  249. // UpdateEtaTrialUserLoginDuration eta_forum_hub服务-更新试用客户登录时长
  250. func UpdateEtaTrialUserLoginDuration(pars UpdateEtaTrialUserLoginDurationReq) (res bool, err error) {
  251. defer func() {
  252. if err != nil {
  253. alarm_msg.SendAlarmMsg(fmt.Sprintf("UpdateEtaTrialUserLoginDuration eta_forum_hub服务-更新试用客户登录时长失败, ErrMsg: %s", err.Error()), 3)
  254. }
  255. }()
  256. reqJson, e := json.Marshal(pars)
  257. if e != nil {
  258. err = fmt.Errorf("data json marshal err: %s", e.Error())
  259. return
  260. }
  261. _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_login_duration")
  262. resp := new(models.BaseResponse)
  263. err = json.Unmarshal(resultByte, &resp)
  264. if err != nil {
  265. return
  266. }
  267. if resp.Ret != 200 {
  268. err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
  269. return
  270. }
  271. res = true
  272. return
  273. }
  274. // post
  275. func post(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
  276. if utils.ETA_FORUM_HUB_URL == "" {
  277. err = fmt.Errorf("ETA社区桥接服务地址为空")
  278. return
  279. }
  280. postUrl := utils.ETA_FORUM_HUB_URL + urlStr
  281. result, err = HttpPost(postUrl, paramStr, "application/json")
  282. if err != nil {
  283. err = fmt.Errorf("调用ETA社区桥接服务接口失败 error:%s", err.Error())
  284. return
  285. }
  286. err = json.Unmarshal(result, &resp)
  287. if err != nil {
  288. return
  289. }
  290. return
  291. }
  292. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  293. body := ioutil.NopCloser(strings.NewReader(postData))
  294. client := &http.Client{}
  295. req, err := http.NewRequest("POST", url, body)
  296. if err != nil {
  297. return nil, err
  298. }
  299. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  300. if len(params) > 0 && params[0] != "" {
  301. contentType = params[0]
  302. }
  303. req.Header.Set("Content-Type", contentType)
  304. req.Header.Set("authorization", utils.MD5(utils.ETA_FORUM_HUB_NAME_EN+utils.ETA_FORUM_HUB_MD5_KEY))
  305. resp, err := client.Do(req)
  306. if err != nil {
  307. return nil, err
  308. }
  309. defer resp.Body.Close()
  310. b, err := ioutil.ReadAll(resp.Body)
  311. utils.FileLog.Debug("HttpPost:" + string(b))
  312. return b, err
  313. }
  314. // get
  315. func get(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
  316. if utils.ETA_FORUM_HUB_URL == "" {
  317. err = fmt.Errorf("ETA社区桥接服务地址为空")
  318. return
  319. }
  320. urlStr = urlStr + "?" + paramStr
  321. getUrl := utils.ETA_FORUM_HUB_URL + urlStr
  322. result, err = HttpGet(getUrl)
  323. if err != nil {
  324. err = fmt.Errorf("调用ETA社区桥接服务接口失败 error:%s", err.Error())
  325. return
  326. }
  327. err = json.Unmarshal(result, &resp)
  328. if err != nil {
  329. return
  330. }
  331. return
  332. }
  333. func HttpGet(url string) ([]byte, error) {
  334. client := &http.Client{}
  335. req, err := http.NewRequest("GET", url, nil)
  336. if err != nil {
  337. return nil, err
  338. }
  339. req.Header.Set("authorization", utils.MD5(utils.ETA_FORUM_HUB_NAME_EN+utils.ETA_FORUM_HUB_MD5_KEY))
  340. resp, err := client.Do(req)
  341. if err != nil {
  342. return nil, err
  343. }
  344. defer resp.Body.Close()
  345. b, err := ioutil.ReadAll(resp.Body)
  346. utils.FileLog.Debug("HttpPost:" + string(b))
  347. return b, err
  348. }