api.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "go-common/app/service/main/passport-game/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. )
  13. // MyInfo get info from api.
  14. func (d *Dao) MyInfo(c context.Context, accessKey string) (accountInfo *model.Info, err error) {
  15. params := url.Values{}
  16. params.Set("access_key", accessKey)
  17. params.Set("type", "json")
  18. var res struct {
  19. Code int `json:"code"`
  20. model.Info
  21. }
  22. if err = d.client.Get(c, d.myInfoURI, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  23. log.Error("myInfo url(%s) error(%v)", d.myInfoURI+"?"+params.Encode(), err)
  24. return
  25. }
  26. if res.Code != 0 {
  27. err = ecode.Int(res.Code)
  28. log.Error("myInfo url(%s) error(%v)", d.myInfoURI+"?"+params.Encode(), err)
  29. return
  30. }
  31. accountInfo = &res.Info
  32. return
  33. }
  34. // Oauth oauth via passport api.
  35. func (d *Dao) Oauth(c context.Context, uri, accessKey, from string) (token *model.Token, err error) {
  36. params := url.Values{}
  37. params.Set("access_key", accessKey)
  38. params.Set("from", from)
  39. var res struct {
  40. Code int `json:"code"`
  41. Token *struct {
  42. Mid string `json:"mid"`
  43. AppID int32 `json:"appid"`
  44. AccessToken string `json:"access_key"`
  45. CreateAt int64 `json:"create_at"`
  46. UserID string `json:"userid"`
  47. Uname string `json:"uname"`
  48. Expires string `json:"expires"`
  49. Permission string `json:"permission"`
  50. } `json:"access_info,omitempty"`
  51. Data *model.Token `json:"data,omitempty"`
  52. }
  53. if err = d.client.Get(c, uri, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  54. log.Error("model oauth url(%s) error(%v)", uri+"?"+params.Encode(), err)
  55. return
  56. }
  57. if res.Code != 0 {
  58. err = ecode.Int(res.Code)
  59. log.Error("model oauth url(%s) error(%v)", uri+"?"+params.Encode(), err)
  60. return
  61. }
  62. if res.Token != nil {
  63. t := res.Token
  64. var mid int64
  65. if mid, err = strconv.ParseInt(t.Mid, 10, 64); err != nil {
  66. log.Error("strconv.ParseInt(%s, 10, 64) error(%v)", t.Mid, err)
  67. return
  68. }
  69. var expires int64
  70. if expires, err = strconv.ParseInt(t.Expires, 10, 64); err != nil {
  71. log.Error("strconv.ParseInt(%s, 10, 64) error(%v)", t.Expires, err)
  72. return
  73. }
  74. token = &model.Token{
  75. Mid: mid,
  76. AppID: t.AppID,
  77. AccessToken: t.AccessToken,
  78. CreateAt: t.CreateAt,
  79. UserID: t.UserID,
  80. Uname: t.Uname,
  81. Expires: expires,
  82. Permission: t.Permission,
  83. }
  84. } else {
  85. token = res.Data
  86. }
  87. return
  88. }
  89. // Login login via model api.
  90. func (d *Dao) Login(c context.Context, query, cookie string) (loginToken *model.LoginToken, err error) {
  91. req, err := http.NewRequest("GET", d.loginURI+"?"+query, nil)
  92. if err != nil {
  93. log.Error("http.NewRequest(GET, %s) error(%v)", d.loginURI+"?"+query, err)
  94. return
  95. }
  96. req.Header.Set("Cookie", cookie)
  97. req.Header.Set("X-BACKEND-BILI-REAL-IP", metadata.String(c, metadata.RemoteIP))
  98. var res struct {
  99. Code int `json:"code"`
  100. Mid int64 `json:"mid"`
  101. AccessKey string `json:"access_key"`
  102. Expires int64 `json:"expires"`
  103. }
  104. if err = d.client.Do(c, req, &res); err != nil {
  105. log.Error("model login url(%s) error(%v)", d.loginURI+"?"+query, err)
  106. return
  107. }
  108. if res.Code != 0 {
  109. err = ecode.Int(res.Code)
  110. log.Error("model login url(%s) error(%v)", d.loginURI+"?"+query, err)
  111. return
  112. }
  113. loginToken = &model.LoginToken{
  114. Mid: res.Mid,
  115. AccessKey: res.AccessKey,
  116. Expires: res.Expires,
  117. }
  118. return
  119. }
  120. // LoginOrigin login via passport api.
  121. func (d *Dao) LoginOrigin(c context.Context, userid, rsaPwd string) (loginToken *model.LoginToken, err error) {
  122. params := url.Values{}
  123. params.Set("userid", userid)
  124. params.Set("pwd", rsaPwd)
  125. var res struct {
  126. Code int `json:"code"`
  127. Mid int64 `json:"mid"`
  128. AccessKey string `json:"access_key"`
  129. Expires int64 `json:"expires"`
  130. }
  131. if err = d.client.Get(c, d.loginURI, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  132. log.Error("login url(%s) error(%v)", d.loginURI+"?"+params.Encode(), err)
  133. return
  134. }
  135. if res.Code != 0 {
  136. err = ecode.Int(res.Code)
  137. log.Error("login url(%s) error(%v)", d.loginURI+"?"+params.Encode(), err)
  138. return
  139. }
  140. str, _ := json.Marshal(res)
  141. log.Info("login url(%s) res(%s)", d.loginURI+"?"+params.Encode(), str)
  142. loginToken = &model.LoginToken{
  143. Mid: res.Mid,
  144. AccessKey: res.AccessKey,
  145. Expires: res.Expires,
  146. }
  147. return
  148. }
  149. // RSAKeyOrigin get rsa pub key and ts hash via passport api.
  150. func (d *Dao) RSAKeyOrigin(c context.Context) (key *model.RSAKey, err error) {
  151. var res struct {
  152. *model.RSAKey
  153. Code int `json:"code"`
  154. }
  155. params := url.Values{}
  156. if err = d.client.Get(c, d.getKeyURI, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  157. log.Error("key url(%s) error(%v)", d.getKeyURI+"?"+params.Encode(), err)
  158. return
  159. }
  160. if res.Code != 0 {
  161. err = ecode.Int(res.Code)
  162. log.Error("key url(%s) error(%v)", d.getKeyURI+"?"+params.Encode(), err)
  163. return
  164. }
  165. key = res.RSAKey
  166. return
  167. }
  168. // RenewToken renew token via passport api.
  169. func (d *Dao) RenewToken(c context.Context, uri, ak, from string) (renewToken *model.RenewToken, err error) {
  170. params := url.Values{}
  171. params.Set("access_key", ak)
  172. params.Set("from", from)
  173. var res struct {
  174. Code int `json:"code"`
  175. Expires int64 `json:"expires"`
  176. Data struct {
  177. Expires int64 `json:"expires"`
  178. }
  179. }
  180. if err = d.client.Get(c, uri, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  181. log.Error("renewtoken url(%s) error(%v)", uri+"?"+params.Encode(), err)
  182. return
  183. }
  184. if res.Code != 0 {
  185. err = ecode.Int(res.Code)
  186. log.Error("renewtoken url(%s) error(%v)", uri+"?"+params.Encode(), err)
  187. return
  188. }
  189. expires := res.Expires
  190. if expires == 0 {
  191. expires = res.Data.Expires
  192. }
  193. renewToken = &model.RenewToken{
  194. Expires: expires,
  195. }
  196. return
  197. }
  198. // RegV3 RegV3
  199. func (d *Dao) RegV3(c context.Context, tdoRegV3 model.TdoRegV3) (regV3 *model.ResRegV3, err error) {
  200. p := url.Values{}
  201. p.Add("userpwd", tdoRegV3.Arg.Pwd)
  202. p.Add("userid", tdoRegV3.Arg.User)
  203. p.Add("captcha", tdoRegV3.Arg.Captcha)
  204. p.Add("ctoken", tdoRegV3.Arg.Ctoken)
  205. req, err := d.client.NewRequest(http.MethodPost, d.regV3URI, tdoRegV3.IP, p)
  206. if err != nil {
  207. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  208. return
  209. }
  210. req.Header.Set("X-Forwarded-For", tdoRegV3.IP)
  211. req.Header.Set("Cookie", tdoRegV3.Cookie)
  212. var response struct {
  213. Code int `json:"code"`
  214. Mid int `json:"mid"`
  215. }
  216. if err = d.client.Do(c, req, &response); err != nil {
  217. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  218. return
  219. }
  220. if response.Code != ecode.OK.Code() {
  221. log.Warn("regv3 url(%s) code(%d)", req.URL.String(), response.Code)
  222. err = ecode.Int(response.Code)
  223. return
  224. }
  225. regV3 = new(model.ResRegV3)
  226. regV3.Mid = response.Mid
  227. return
  228. }
  229. // RegV2 RegV2
  230. func (d *Dao) RegV2(c context.Context, tdoRegV2 model.TdoRegV2) (regV2 *model.ResRegV2, err error) {
  231. p := url.Values{}
  232. p.Add("captcha", tdoRegV2.Arg.Captcha)
  233. p.Add("ctoken", tdoRegV2.Arg.Ctoken)
  234. req, err := d.client.NewRequest(http.MethodPost, d.regV2URI, tdoRegV2.IP, p)
  235. if err != nil {
  236. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  237. return
  238. }
  239. req.Header.Set("X-Forwarded-For", tdoRegV2.IP)
  240. req.Header.Set("Cookie", tdoRegV2.Cookie)
  241. var response struct {
  242. Code int `json:"code"`
  243. Mid int `json:"mid"`
  244. AccessKey string `json:"access_key"`
  245. }
  246. if err = d.client.Do(c, req, &response); err != nil {
  247. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  248. return
  249. }
  250. if response.Code != ecode.OK.Code() {
  251. log.Warn("reg v2 url(%s) code(%d)", req.URL.String(), response.Code)
  252. err = ecode.Int(response.Code)
  253. return
  254. }
  255. regV2 = new(model.ResRegV2)
  256. regV2.Mid = response.Mid
  257. regV2.AccessKey = response.AccessKey
  258. return
  259. }
  260. // Reg Reg
  261. func (d *Dao) Reg(c context.Context, tdoReg model.TdoReg) (reg *model.ResReg, err error) {
  262. p := url.Values{}
  263. p.Add("userpwd", tdoReg.Arg.Userpwd)
  264. p.Add("user", tdoReg.Arg.User)
  265. p.Add("email", tdoReg.Arg.Email)
  266. // new request
  267. req, err := d.client.NewRequest(http.MethodPost, d.regURI, tdoReg.IP, p)
  268. if err != nil {
  269. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  270. return
  271. }
  272. req.Header.Set("X-Forwarded-For", tdoReg.IP)
  273. req.Header.Set("Cookie", tdoReg.Cookie)
  274. var response struct {
  275. Code int `json:"code"`
  276. Mid int `json:"mid"`
  277. }
  278. if err = d.client.Do(c, req, &response); err != nil {
  279. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  280. return
  281. }
  282. if response.Code != ecode.OK.Code() {
  283. log.Warn("reg url(%s) code(%d)", req.URL.String(), response.Code)
  284. err = ecode.Int(response.Code)
  285. return
  286. }
  287. reg = new(model.ResReg)
  288. reg.Mid = response.Mid
  289. return
  290. }
  291. // ByTel ByTel
  292. func (d *Dao) ByTel(c context.Context, tdoByTel model.TdoByTel) (byTel *model.ResByTel, err error) {
  293. p := url.Values{}
  294. p.Add("userpwd", tdoByTel.Arg.Userpwd)
  295. p.Add("tel", tdoByTel.Arg.Tel)
  296. p.Add("captcha", tdoByTel.Arg.Captcha)
  297. p.Add("country_id", tdoByTel.Arg.CountryID)
  298. p.Add("uname", tdoByTel.Arg.Uname)
  299. // new request
  300. req, err := d.client.NewRequest(http.MethodPost, d.byTelURI, tdoByTel.IP, p)
  301. if err != nil {
  302. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  303. return
  304. }
  305. req.Header.Set("X-Forwarded-For", tdoByTel.IP)
  306. req.Header.Set("Cookie", tdoByTel.Cookie)
  307. var response struct {
  308. Code int `json:"code"`
  309. Data struct {
  310. Mid int `json:"mid"`
  311. AccessKey string `json:"access_key"`
  312. }
  313. }
  314. if err = d.client.Do(c, req, &response); err != nil {
  315. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  316. return
  317. }
  318. if response.Code != ecode.OK.Code() {
  319. log.Warn("byTel url(%s) code(%d)", req.URL.String(), response.Code)
  320. err = ecode.Int(response.Code)
  321. return
  322. }
  323. byTel = new(model.ResByTel)
  324. byTel.Mid = response.Data.Mid
  325. byTel.AccessKey = response.Data.AccessKey
  326. return
  327. }
  328. // Captcha Captcha
  329. func (d *Dao) Captcha(c context.Context, ip string) (captchaData *model.CaptchaData, err error) {
  330. p := url.Values{}
  331. req, err := d.client.NewRequest(http.MethodGet, d.captchaURI, ip, p)
  332. if err != nil {
  333. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  334. return
  335. }
  336. resCaptcha := new(model.ResCaptcha)
  337. req.Header.Set("X-Forwarded-For", ip)
  338. if err = d.client.Do(c, req, &resCaptcha); err != nil {
  339. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  340. return
  341. }
  342. err = ecode.Int(resCaptcha.Code)
  343. captchaData = &resCaptcha.Data
  344. return
  345. }
  346. // SendSms SendSms
  347. func (d *Dao) SendSms(c context.Context, tdoSendSms model.TdoSendSms) (err error) {
  348. p := url.Values{}
  349. p.Add("captcha", tdoSendSms.Arg.Captcha)
  350. p.Add("tel", tdoSendSms.Arg.Tel)
  351. p.Add("country_id", tdoSendSms.Arg.CountryID)
  352. p.Add("ctoken", tdoSendSms.Arg.Ctoken)
  353. p.Add("reset_pwd", strconv.FormatBool(tdoSendSms.Arg.ResetPwd))
  354. req, err := d.client.NewRequest(http.MethodGet, d.sendSmsURI, tdoSendSms.IP, p)
  355. if err != nil {
  356. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  357. return
  358. }
  359. req.Header.Set("X-Forwarded-For", tdoSendSms.IP)
  360. req.Header.Set("Cookie", tdoSendSms.Cookie)
  361. resCaptcha := new(model.ResCaptcha)
  362. if err = d.client.Do(c, req, &resCaptcha); err != nil {
  363. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  364. return
  365. }
  366. err = ecode.Int(resCaptcha.Code)
  367. return
  368. }