realname.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "time"
  7. "go-common/app/job/main/member/conf"
  8. "go-common/app/job/main/member/model"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "github.com/pkg/errors"
  12. )
  13. var (
  14. _selRealnameAlipayApplyList = "SELECT id,mid,realname,card,img,status,reason,bizno,ctime,mtime FROM realname_alipay_apply WHERE id>? AND status=? AND mtime>=? AND mtime<=? ORDER BY id ASC LIMIT ?"
  15. _selRealnameInfo = `SELECT id,mid,channel,realname,country,card_type,card,card_md5,status,reason,ctime,mtime FROM realname_info WHERE mid = ? LIMIT 1`
  16. _upsertRealnameInfo = `INSERT INTO realname_info (mid,channel,realname,country,card_type,card,card_md5,status,reason) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE channel=?,realname=?,country=?,card_type=?,card=?,card_md5=?,status=?,reason=?`
  17. _upsertRealnameApply = `INSERT INTO realname_apply (id,mid,realname,country,card_type,card_num,card_md5,hand_img,front_img,back_img,status,operator,operator_id,operator_time,remark,remark_status,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE realname=?,country=?,card_type=?,card_num=?,card_md5=?,hand_img=?,front_img=?,back_img=?,status=?,operator=?,operator_time=?,remark=?,remark_status=?,mtime=?`
  18. _upsertRealnameApplyIMG = `INSERT INTO realname_apply_img (id,img_data,ctime,mtime) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE img_data=?,mtime=?`
  19. )
  20. // UpdateRealnameFromMSG is
  21. func (d *Dao) UpdateRealnameFromMSG(c context.Context, ms *model.RealnameApplyMessage) (err error) {
  22. var (
  23. tx *xsql.Tx
  24. cardEncrpted = ms.CardData()
  25. cardMD5 = ms.CardMD5()
  26. )
  27. if tx, err = d.db.Begin(c); err != nil {
  28. return
  29. }
  30. if _, err = tx.Exec(_upsertRealnameApply, ms.ID, ms.MID, ms.Realname, ms.Country(), ms.CardType(), cardEncrpted, cardMD5, ms.FrontIMG, ms.FrontIMG2, ms.BackIMG, ms.Status, ms.Operater, 0, ms.OperaterTime(), ms.Remark, ms.RemarkStatus, ms.ApplyTime(), ms.ApplyTime(), ms.Realname, ms.Country(), ms.CardType(), cardEncrpted, cardMD5, ms.FrontIMG, ms.FrontIMG2, ms.BackIMG, ms.Status, ms.Operater, ms.OperaterTime(), ms.Remark, ms.RemarkStatus, time.Now()); err != nil {
  31. err = errors.WithStack(err)
  32. tx.Rollback()
  33. return
  34. }
  35. if _, err = tx.Exec(_upsertRealnameInfo, ms.MID, 0, ms.Realname, ms.Country(), ms.CardType(), cardEncrpted, cardMD5, ms.Status, ms.Remark, 0, ms.Realname, ms.Country(), ms.CardType(), cardEncrpted, cardMD5, ms.Status, ms.Remark); err != nil {
  36. err = errors.WithStack(err)
  37. tx.Rollback()
  38. return
  39. }
  40. if err = tx.Commit(); err != nil {
  41. return
  42. }
  43. return
  44. }
  45. // RealnameInfo is.
  46. func (d *Dao) RealnameInfo(c context.Context, mid int64) (info *model.RealnameInfo, err error) {
  47. row := d.db.QueryRow(c, _selRealnameInfo, mid)
  48. info = &model.RealnameInfo{}
  49. if err = row.Scan(&info.ID, &info.MID, &info.Channel, &info.Realname, &info.Country, &info.CardType, &info.Card, &info.CardMD5, &info.Status, &info.Reason, &info.CTime, &info.MTime); err != nil {
  50. if err == xsql.ErrNoRows {
  51. err = nil
  52. info = nil
  53. return
  54. }
  55. err = errors.Wrapf(err, "dao RealnameInfo mid(%d)", mid)
  56. return
  57. }
  58. return
  59. }
  60. // UpsertRealnameInfo is
  61. func (d *Dao) UpsertRealnameInfo(c context.Context, ms *model.RealnameInfo) (err error) {
  62. if _, err = d.db.Exec(c, _upsertRealnameInfo, ms.MID, 0, ms.Realname, ms.Country, ms.CardType, ms.Card, ms.CardMD5, ms.Status, ms.Reason, 0, ms.Realname, ms.Country, ms.CardType, ms.Card, ms.CardMD5, ms.Status, ms.Reason); err != nil {
  63. err = errors.WithStack(err)
  64. return
  65. }
  66. return
  67. }
  68. // UpsertRealnameApplyImg is
  69. func (d *Dao) UpsertRealnameApplyImg(c context.Context, ms *model.RealnameApplyImgMessage) (err error) {
  70. if _, err = d.db.Exec(c, _upsertRealnameApplyIMG, ms.ID, ms.IMGData, ms.AddTime(), ms.AddTime(), ms.IMGData, time.Now()); err != nil {
  71. err = errors.WithStack(err)
  72. return
  73. }
  74. return
  75. }
  76. // RealnameAlipayApplyList is
  77. func (d *Dao) RealnameAlipayApplyList(c context.Context, startID int64, status model.RealnameApplyStatus, fromTime, toTime time.Time, limit int) (maxID int64, list []*model.RealnameAlipayApply, err error) {
  78. var (
  79. rows *xsql.Rows
  80. )
  81. if rows, err = d.db.Query(c, _selRealnameAlipayApplyList, startID, status, fromTime, toTime, limit); err != nil {
  82. err = errors.WithStack(err)
  83. return
  84. }
  85. defer rows.Close()
  86. for rows.Next() {
  87. var (
  88. apply = &model.RealnameAlipayApply{}
  89. )
  90. if err = rows.Scan(&apply.ID, &apply.MID, &apply.Realname, &apply.Card, &apply.IMG, &apply.Status, &apply.Reason, &apply.Bizno, &apply.CTime, &apply.MTime); err != nil {
  91. err = errors.WithStack(err)
  92. return
  93. }
  94. if maxID < apply.ID {
  95. maxID = apply.ID
  96. }
  97. list = append(list, apply)
  98. }
  99. if err = rows.Err(); err != nil {
  100. err = errors.WithStack(err)
  101. return
  102. }
  103. return
  104. }
  105. // AlipayQuery .
  106. func (d *Dao) AlipayQuery(c context.Context, param url.Values) (pass bool, reason string, err error) {
  107. var (
  108. req *http.Request
  109. )
  110. url := conf.Conf.Biz.RealnameAlipayGateway + "?" + param.Encode()
  111. if req, err = http.NewRequest("GET", url, nil); err != nil {
  112. err = errors.Wrapf(err, "http.NewRequest(GET,%s)", url)
  113. return
  114. }
  115. var resp struct {
  116. Resp struct {
  117. respAlipay
  118. Passed string `json:"passed"`
  119. FailedReason string `json:"failed_reason"`
  120. IdentityInfo string `json:"identity_info"`
  121. AttributeInfo string `json:"attribute_info"`
  122. ChannelStatuses string `json:"channel_statuses"`
  123. } `json:"zhima_customer_certification_query_response"`
  124. Sign string `json:"sign"`
  125. }
  126. if err = d.client.Do(c, req, &resp); err != nil {
  127. return
  128. }
  129. log.Info("Realname alipay query \n\tparam : %+v \n\tresp : %+v", param, resp)
  130. if err = resp.Resp.Error(); err != nil {
  131. return
  132. }
  133. if resp.Resp.Passed == "true" {
  134. pass = true
  135. } else {
  136. pass = false
  137. }
  138. reason = resp.Resp.FailedReason
  139. return
  140. }
  141. type respAlipay struct {
  142. Code string `json:"code"`
  143. Msg string `json:"msg"`
  144. SubCode string `json:"sub_code"`
  145. SubMsg string `json:"sub_msg"`
  146. }
  147. func (r *respAlipay) Error() error {
  148. if r.Code == "10000" {
  149. return nil
  150. }
  151. return errors.Errorf("alipay response failed , code : %s, msg : %s, sub_code : %s, sub_msg : %s", r.Code, r.Msg, r.SubCode, r.SubMsg)
  152. }