mid_info.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "strings"
  7. "go-common/app/service/main/account-recovery/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. )
  12. // GetMidInfo get mid info by more condition
  13. func (d *Dao) GetMidInfo(c context.Context, qType string, qKey string) (v *model.MIDInfo, err error) {
  14. params := url.Values{}
  15. params.Set("q_type", qType)
  16. params.Set("q_key", qKey)
  17. res := new(struct {
  18. Code int `json:"code"`
  19. Data model.MIDInfo `json:"data"`
  20. })
  21. if err = d.httpClient.Get(c, d.c.AccRecover.MidInfoURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  22. log.Error("GetMidInfo HTTP request err %+v", err)
  23. return
  24. }
  25. if res.Code != 0 {
  26. log.Error("GetMidInfo server err_code %d,params: qType=%s,qKey=%s", res.Code, qType, qKey)
  27. err = ecode.ServerErr
  28. return
  29. }
  30. log.Info("GetMidInfo url=%v, params: qType=%s,qKey=%s, res: %+v", d.c.AccRecover.MidInfoURL, qType, qKey, res)
  31. return &res.Data, nil
  32. }
  33. // GetUserInfo get user info by mid
  34. func (d *Dao) GetUserInfo(c context.Context, mid int64) (v *model.UserInfo, err error) {
  35. params := url.Values{}
  36. params.Add("mid", strconv.Itoa(int(mid)))
  37. res := new(struct {
  38. Code int `json:"code"`
  39. Data model.UserInfo `json:"data"`
  40. })
  41. if err = d.httpClient.Get(c, d.c.AccRecover.GetUserInfoURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  42. log.Error("GetUserInfo HTTP request err %+v", err)
  43. return
  44. }
  45. if res.Code != 0 {
  46. log.Error("GetUserInfo server err_code %d,params: mid=%d", res.Code, mid)
  47. err = ecode.ServerErr
  48. return
  49. }
  50. log.Info("GetUserInfo url=%v, params: mid=%d, res: %+v", d.c.AccRecover.GetUserInfoURL, mid, res)
  51. return &res.Data, nil
  52. }
  53. // UpdatePwd update password
  54. func (d *Dao) UpdatePwd(c context.Context, mid int64, operator string) (user *model.User, err error) {
  55. params := url.Values{}
  56. params.Set("mid", strconv.Itoa(int(mid)))
  57. params.Set("operator", operator)
  58. res := new(struct {
  59. Code int `json:"code"`
  60. Data model.User `json:"data"`
  61. })
  62. if err = d.httpClient.Post(c, d.c.AccRecover.UpPwdURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  63. log.Error("UpdatePwd HTTP request err %+v", err)
  64. return
  65. }
  66. if res.Code != 0 {
  67. log.Error("UpdatePwd server err_code %d,params: mid=%d", res.Code, mid)
  68. err = ecode.Int(res.Code)
  69. return
  70. }
  71. log.Info("UpdatePwd url=%v, params: mid=%d,operator=%s, res: %+v", d.c.AccRecover.UpPwdURL, mid, operator, res)
  72. return &res.Data, nil
  73. }
  74. // CheckSafe safe info
  75. func (d *Dao) CheckSafe(c context.Context, mid int64, question int8, answer string) (check *model.Check, err error) {
  76. params := url.Values{}
  77. params.Add("mid", strconv.Itoa(int(mid)))
  78. params.Add("question", strconv.Itoa(int(question)))
  79. params.Add("answer", answer)
  80. res := new(struct {
  81. Code int `json:"code"`
  82. Data model.Check `json:"data"`
  83. })
  84. if err = d.httpClient.Post(c, d.c.AccRecover.CheckSafeURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  85. log.Error("CheckSafe HTTP request err %+v", err)
  86. return
  87. }
  88. if res.Code != 0 {
  89. log.Error("CheckSafe server err_code %d,params: mid=%d,question=%d,answer=%s", res.Code, mid, question, answer)
  90. err = ecode.Int(res.Code)
  91. return
  92. }
  93. log.Info("CheckSafe url=%v, params: mid=%d,question=%d,answer=%s, res: %+v", d.c.AccRecover.CheckSafeURL, mid, question, answer, res)
  94. return &res.Data, nil
  95. }
  96. // GetUserType get user_type
  97. func (d *Dao) GetUserType(c context.Context, mid int64) (gams []*model.Game, err error) {
  98. params := url.Values{}
  99. params.Add("mid", strconv.Itoa(int(mid)))
  100. res := new(struct {
  101. Code int `json:"code"`
  102. Data []*model.Game `json:"items"`
  103. })
  104. if err = d.httpClient.Get(c, d.c.AccRecover.GameURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  105. log.Error("GetUserType HTTP request err %+v", err)
  106. return
  107. }
  108. if res.Code != 0 {
  109. log.Error("GetUserType server err_code %d,params: mid=%d", res.Code, mid)
  110. err = ecode.Int(res.Code)
  111. return
  112. }
  113. log.Info("GetUserType url=%v, params: mid=%d, res: %+v", d.c.AccRecover.GameURL, mid, res)
  114. return res.Data, nil
  115. }
  116. // CheckReg check reg info
  117. func (d *Dao) CheckReg(c context.Context, mid int64, regTime int64, regType int8, regAddr string) (v *model.Check, err error) {
  118. params := url.Values{}
  119. params.Add("mid", strconv.Itoa(int(mid)))
  120. params.Add("reg_time", strconv.FormatInt(regTime, 10))
  121. params.Add("reg_type", strconv.Itoa(int(regType)))
  122. params.Add("reg_addr", regAddr)
  123. res := new(struct {
  124. Code int `json:"code"`
  125. Data model.Check `json:"data"`
  126. })
  127. if err = d.httpClient.Post(c, d.c.AccRecover.CheckRegURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  128. log.Error("CheckReg HTTP request err %+v", err)
  129. return
  130. }
  131. if res.Code != 0 {
  132. log.Error("CheckReg server err_code %d,params: mid=%d,regTime=%d,regType=%d,regAddr=%s", res.Code, mid, regTime, regType, regAddr)
  133. err = ecode.Int(res.Code)
  134. return
  135. }
  136. log.Info("CheckReg url=%v, params: mid=%d,regTime=%d,regType=%d,regAddr=%s, res: %+v", d.c.AccRecover.CheckRegURL, mid, regTime, regType, regAddr, res)
  137. return &res.Data, nil
  138. }
  139. // UpdateBatchPwd batch update password
  140. func (d *Dao) UpdateBatchPwd(c context.Context, mids string, operator string) (userMap map[string]*model.User, err error) {
  141. params := url.Values{}
  142. params.Set("mids", mids)
  143. params.Set("operator", operator)
  144. res := new(struct {
  145. Code int `json:"code"`
  146. Data map[string]*model.User `json:"data"`
  147. })
  148. if err = d.httpClient.Post(c, d.c.AccRecover.UpBatchPwdURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  149. log.Error("UpdateBatchPwd HTTP request err %+v", err)
  150. return
  151. }
  152. if res.Code != 0 {
  153. log.Error("UpdateBatchPwd server err_code %d,params: mids=%s", res.Code, mids)
  154. err = ecode.Int(res.Code)
  155. return
  156. }
  157. log.Info("UpdateBatchPwd url=%v, params: mid=%s,operator=%s, res: %+v", d.c.AccRecover.UpBatchPwdURL, mids, operator, res)
  158. return res.Data, nil
  159. }
  160. // CheckCard check card
  161. func (d *Dao) CheckCard(c context.Context, mid int64, cardType int8, cardCode string) (ok bool, err error) {
  162. params := url.Values{}
  163. params.Set("mid", strconv.FormatInt(mid, 10))
  164. params.Set("card_type", strconv.Itoa(int(cardType)))
  165. params.Set("card_code", cardCode)
  166. res := new(struct {
  167. Code int `json:"code"`
  168. Data bool `json:"data"`
  169. })
  170. if err = d.httpClient.Get(c, d.c.AccRecover.CheckCardURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  171. log.Error("CheckCard HTTP request err %+v", err)
  172. return
  173. }
  174. if res.Code != 0 {
  175. log.Error("CheckCard server err_code %d,params: mid=%d,cardType=%d,cardCode=%s", res.Code, mid, cardType, cardCode)
  176. err = ecode.Int(res.Code)
  177. return
  178. }
  179. log.Info("CheckCard url=%v, params: mid=%d,cardType=%d,cardCode=%s, res: %+v", d.c.AccRecover.CheckCardURL, mid, cardType, cardCode, res)
  180. return res.Data, nil
  181. }
  182. // CheckPwds check pwd
  183. func (d *Dao) CheckPwds(c context.Context, mid int64, pwds string) (v string, err error) {
  184. params := url.Values{}
  185. params.Set("mid", strconv.FormatInt(mid, 10))
  186. params.Set("pwd", pwds)
  187. res := new(struct {
  188. Code int `json:"code"`
  189. Data string `json:"data"`
  190. })
  191. if err = d.httpClient.Post(c, d.c.AccRecover.CheckPwdURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  192. log.Error("CheckPwds HTTP request err %+v", err)
  193. return
  194. }
  195. if res.Code != 0 {
  196. log.Error("CheckPwds server err_code %d,params: mid=%d,pwds=%s", res.Code, mid, pwds)
  197. err = ecode.Int(res.Code)
  198. return
  199. }
  200. log.Info("CheckPwds url=%v, params: mid=%d,pwds=%s, res: %+v", d.c.AccRecover.CheckPwdURL, mid, pwds, res)
  201. return res.Data, nil
  202. }
  203. // GetLoginIPs get login ip
  204. func (d *Dao) GetLoginIPs(c context.Context, mid int64, limit int64) (ipInfo []*model.LoginIPInfo, err error) {
  205. params := url.Values{}
  206. params.Set("mid", strconv.FormatInt(mid, 10))
  207. params.Set("limit", strconv.FormatInt(limit, 10))
  208. res := new(struct {
  209. Code int `json:"code"`
  210. Data []*model.LoginIPInfo `json:"data"`
  211. })
  212. if err = d.httpClient.Get(c, d.c.AccRecover.GetLoginIPURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  213. log.Error("GetLoginIPs HTTP request err %+v", err)
  214. return
  215. }
  216. if res.Code != 0 {
  217. log.Error("GetLoginIPs server err_code %d,params: mid=%d,limit=%d", res.Code, mid, limit)
  218. err = ecode.ServerErr
  219. return
  220. }
  221. log.Info("GetLoginIPs url=%v, params: mid=%d,limit=%d, res: %+v", d.c.AccRecover.GetLoginIPURL, mid, limit, res)
  222. return res.Data, nil
  223. }
  224. // GetAddrByIP get addr by ip
  225. func (d *Dao) GetAddrByIP(c context.Context, mid int64, limit int64) (addrs string, err error) {
  226. ipInfo, err := d.GetLoginIPs(c, mid, limit)
  227. if err != nil || len(ipInfo) == 0 {
  228. return
  229. }
  230. var ipLen = len(ipInfo)
  231. ips := make([]string, 0, ipLen)
  232. //ip去重复和空串
  233. for i := 0; i < ipLen; i++ {
  234. if (i > 0 && ipInfo[i-1].LoginIP == ipInfo[i].LoginIP) || len(ipInfo[i].LoginIP) == 0 {
  235. continue
  236. }
  237. ips = append(ips, ipInfo[i].LoginIP)
  238. }
  239. ipMap, err := d.Infos(c, ips)
  240. i := 0
  241. for _, loc := range ipMap {
  242. if loc.Country != "" {
  243. addrs += loc.Country + "-"
  244. }
  245. if loc.Province != "" {
  246. addrs += loc.Province + "-"
  247. }
  248. if loc.City != "" {
  249. addrs += loc.City + "-"
  250. }
  251. addrs = strings.TrimRight(addrs, "-") + ","
  252. i++
  253. if i >= 3 {
  254. break
  255. }
  256. }
  257. addrs = strings.TrimRight(addrs, ",")
  258. return
  259. }