realname.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package http
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "strconv"
  6. "go-common/app/interface/main/account/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "github.com/pkg/errors"
  12. )
  13. // modify modify user relation.
  14. func realnameStatus(c *bm.Context) {
  15. var (
  16. err error
  17. mid, _ = c.Get("mid")
  18. )
  19. var resData struct {
  20. Status int8 `json:"status"`
  21. }
  22. if resData.Status, err = realnameSvc.Status(c, mid.(int64)); err != nil {
  23. log.Error("%+v", err)
  24. c.JSON(nil, err)
  25. return
  26. }
  27. c.JSON(resData, nil)
  28. }
  29. func realnameApplyStatus(c *bm.Context) {
  30. var (
  31. err error
  32. mid, _ = c.Get("mid")
  33. )
  34. var resData struct {
  35. Status int8 `json:"status"`
  36. Remark string `json:"remark"`
  37. Realname string `json:"realname"`
  38. Card string `json:"card"`
  39. }
  40. if resData.Status, resData.Remark, resData.Realname, resData.Card, err = realnameSvc.ApplyStatus(c, mid.(int64)); err != nil {
  41. log.Error("%+v", err)
  42. c.JSON(nil, err)
  43. return
  44. }
  45. c.JSON(resData, nil)
  46. }
  47. func realnameCardTypes(c *bm.Context) {
  48. var (
  49. err error
  50. params = c.Request.Form
  51. platform = params.Get("platform")
  52. buildStr = params.Get("build")
  53. mobiapp = params.Get("mobi_app")
  54. device = params.Get("device")
  55. build int
  56. )
  57. if build, err = strconv.Atoi(buildStr); err != nil {
  58. log.Error("%+v", errors.WithStack(err))
  59. c.JSON(nil, ecode.RequestErr)
  60. return
  61. }
  62. var resData []*model.RealnameCardType
  63. if resData, err = realnameSvc.CardTypes(c, platform, mobiapp, device, build); err != nil {
  64. log.Error("%+v", err)
  65. c.JSON(nil, err)
  66. return
  67. }
  68. c.JSON(resData, nil)
  69. }
  70. func realnameCardTypesV2(c *bm.Context) {
  71. var (
  72. err error
  73. )
  74. var resData []*model.RealnameCardType
  75. if resData, err = realnameSvc.CardTypesV2(c); err != nil {
  76. log.Error("%+v", err)
  77. c.JSON(nil, err)
  78. return
  79. }
  80. c.JSON(resData, nil)
  81. }
  82. func realnameCountryList(c *bm.Context) {
  83. var (
  84. err error
  85. )
  86. var resData []*model.RealnameCountry
  87. if resData, err = realnameSvc.CountryList(c); err != nil {
  88. log.Error("%+v", err)
  89. c.JSON(nil, err)
  90. return
  91. }
  92. c.JSON(resData, nil)
  93. }
  94. func realnameTelCapture(c *bm.Context) {
  95. var (
  96. err error
  97. mid, _ = c.Get("mid")
  98. )
  99. if err = realnameSvc.TelCapture(c, mid.(int64)); err != nil {
  100. log.Error("%+v", err)
  101. c.JSON(nil, err)
  102. return
  103. }
  104. c.JSON(nil, nil)
  105. }
  106. func realnameTelInfo(c *bm.Context) {
  107. var (
  108. err error
  109. mid, _ = c.Get("mid")
  110. )
  111. var resData struct {
  112. Tel string `json:"tel"`
  113. }
  114. if resData.Tel, err = realnameSvc.TelInfo(c, mid.(int64)); err != nil {
  115. log.Error("%+v", err)
  116. c.JSON(nil, err)
  117. return
  118. }
  119. c.JSON(resData, nil)
  120. }
  121. func realnameApply(c *bm.Context) {
  122. var (
  123. err error
  124. mid, _ = c.Get("mid")
  125. params = c.Request.Form
  126. realname = params.Get("real_name")
  127. cardTypeStr = params.Get("card_type")
  128. cardType int
  129. cardNum = params.Get("card_num")
  130. countryIDStr = params.Get("country")
  131. countryID int
  132. captureStr = params.Get("capture")
  133. capture int
  134. handIMGToken = params.Get("img1_token")
  135. frontIMGToken = params.Get("img2_token")
  136. backIMGToken = params.Get("img3_token")
  137. )
  138. if cardType, err = strconv.Atoi(cardTypeStr); err != nil {
  139. log.Error("%+v", errors.WithStack(err))
  140. c.JSON(nil, ecode.RequestErr)
  141. return
  142. }
  143. if countryIDStr == "" {
  144. countryID = 0 // 默认0:中国
  145. } else {
  146. if countryID, err = strconv.Atoi(countryIDStr); err != nil {
  147. log.Error("%+v", errors.WithStack(err))
  148. c.JSON(nil, ecode.RequestErr)
  149. return
  150. }
  151. }
  152. if countryID < 0 {
  153. countryID = 0
  154. }
  155. if capture, err = strconv.Atoi(captureStr); err != nil {
  156. log.Error("%+v", errors.WithStack(err))
  157. c.JSON(nil, ecode.RequestErr)
  158. return
  159. }
  160. if err = realnameSvc.Apply(c, mid.(int64), realname, cardType, cardNum, countryID, capture, handIMGToken, frontIMGToken, backIMGToken); err != nil {
  161. log.Error("%+v")
  162. c.JSON(nil, err)
  163. return
  164. }
  165. c.JSON(nil, nil)
  166. }
  167. func realnameUpload(c *bm.Context) {
  168. var (
  169. mid, _ = c.Get("mid")
  170. )
  171. defer c.Request.Form.Del("img") // 防止日志不出现
  172. c.Request.ParseMultipartForm(32 << 20)
  173. imgBytes, err := func() ([]byte, error) {
  174. img := c.Request.FormValue("img")
  175. if img != "" {
  176. log.Info("Succeeded to parse img file from form value: mid: %d, length: %d", mid, len(img))
  177. return []byte(img), nil
  178. }
  179. log.Warn("Failed to parse img file from form value, fallback to form file: mid: %d", mid)
  180. f, _, err := c.Request.FormFile("img")
  181. if err != nil {
  182. return nil, errors.Wrapf(err, "parse img form file: mid: %d", mid)
  183. }
  184. defer f.Close()
  185. data, err := ioutil.ReadAll(f)
  186. if err != nil {
  187. return nil, errors.Wrapf(err, "read img form file: mid: %d", mid)
  188. }
  189. if len(data) <= 0 {
  190. return nil, errors.Wrapf(err, "form file data: mid: %d, length: %d", mid, len(data))
  191. }
  192. log.Info("Succeeded to parse file from form file: mid: %d, length: %d", mid, len(data))
  193. return data, nil
  194. }()
  195. if err != nil {
  196. log.Error("Failed to parse realname upload file: mid: %d: %+v", mid, err)
  197. c.JSON(nil, ecode.RequestErr)
  198. return
  199. }
  200. var resData struct {
  201. SRC string `json:"token"`
  202. }
  203. if resData.SRC, err = realnameSvc.Upload(c, mid.(int64), imgBytes); err != nil {
  204. log.Error("%+v", err)
  205. c.JSON(nil, err)
  206. return
  207. }
  208. c.JSON(resData, nil)
  209. }
  210. func realnamePreview(c *bm.Context) {
  211. var (
  212. img []byte
  213. err error
  214. mid, _ = c.Get("mid")
  215. params = c.Request.Form
  216. src = params.Get("src")
  217. )
  218. if img, err = realnameSvc.Preview(c, mid.(int64), src); err != nil {
  219. log.Error("%+v", err)
  220. c.JSON(nil, err)
  221. return
  222. }
  223. c.Writer.Header().Set("Content-Type", http.DetectContentType(img))
  224. c.JSON(img, err)
  225. }
  226. // alipay api
  227. func realnameChannel(c *bm.Context) {
  228. c.JSON(realnameSvc.Channel(c))
  229. }
  230. func realnameCaptcha(c *bm.Context) {
  231. var (
  232. mid, _ = c.Get("mid")
  233. userAgent = c.Request.UserAgent()
  234. ip = metadata.String(c, metadata.RemoteIP)
  235. err error
  236. )
  237. var resp struct {
  238. URL string `json:"url"`
  239. Remote int `json:"remote"`
  240. }
  241. resp.URL, resp.Remote, err = realnameSvc.CaptchaGTRegister(c, mid.(int64), ip, geetestClientType(userAgent))
  242. c.JSON(resp, err)
  243. }
  244. func realnameCaptchaRefresh(c *bm.Context) {
  245. var (
  246. err error
  247. mid, _ = c.Get("mid")
  248. userAgent = c.Request.UserAgent()
  249. ip = metadata.String(c, metadata.RemoteIP)
  250. v = &model.ParamRealnameCaptchaGTRefresh{}
  251. )
  252. if err = c.Bind(v); err != nil {
  253. return
  254. }
  255. var resp struct {
  256. CaptureType int `json:"captcha_type"`
  257. CaptureInfo struct {
  258. Success int `json:"success"`
  259. GT string `json:"gt"`
  260. Challenge string `json:"challenge"`
  261. } `json:"captcha_info"`
  262. }
  263. resp.CaptureType = 1
  264. resp.CaptureInfo.Challenge, resp.CaptureInfo.GT, resp.CaptureInfo.Success, err = realnameSvc.CaptchaGTRefresh(c, mid.(int64), ip, geetestClientType(userAgent), v.Hash)
  265. c.JSON(resp, err)
  266. }
  267. func realnameCaptchaConfirm(c *bm.Context) {
  268. var (
  269. err error
  270. mid, _ = c.Get("mid")
  271. userAgent = c.Request.UserAgent()
  272. ip = metadata.String(c, metadata.RemoteIP)
  273. v = &model.ParamRealnameCaptchaGTCheck{}
  274. )
  275. if err = c.Bind(v); err != nil {
  276. return
  277. }
  278. if v.Remote == 1 && len(v.Challenge) != 34 {
  279. err = ecode.RequestErr
  280. return
  281. }
  282. c.JSON(realnameSvc.CaptchaGTValidate(c, mid.(int64), ip, geetestClientType(userAgent), v))
  283. }
  284. func realnameAlipayApply(c *bm.Context) {
  285. var (
  286. err error
  287. mid, _ = c.Get("mid")
  288. v = &model.ParamRealnameAlipayApply{}
  289. )
  290. if err = c.Bind(v); err != nil {
  291. return
  292. }
  293. c.JSON(realnameSvc.AlipayApply(c, mid.(int64), v))
  294. }
  295. func realnameAlipayConfirm(c *bm.Context) {
  296. var (
  297. mid, _ = c.Get("mid")
  298. )
  299. c.JSON(realnameSvc.AlipayConfirm(c, mid.(int64)))
  300. }
  301. func geetestClientType(userAgent string) string {
  302. return "h5"
  303. }