realname.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package model
  2. import (
  3. "crypto/md5"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/x509"
  7. "encoding/base64"
  8. "encoding/pem"
  9. "fmt"
  10. "strings"
  11. "time"
  12. "go-common/app/job/main/member/conf"
  13. "go-common/library/log"
  14. "github.com/pkg/errors"
  15. )
  16. var (
  17. realnameSalt = "biliidentification@#$%^&*()(*&^%$#"
  18. )
  19. //RealnamePersonMessage is.
  20. type RealnamePersonMessage struct {
  21. MID int64 `json:"mid"`
  22. Realname string `json:"realname"`
  23. IdentifyCard string `json:"identify_card"`
  24. }
  25. //RealnameApplyMessage is.
  26. type RealnameApplyMessage struct {
  27. ID int `json:"id"`
  28. MID int64 `json:"mid"`
  29. Realname string `json:"realname"`
  30. Type int `json:"type"`
  31. CardDataCanal string `json:"card_data"`
  32. CardForSearch string `json:"card_for_search"`
  33. FrontIMG int `json:"front_img"`
  34. BackIMG int `json:"back_img"`
  35. FrontIMG2 int `json:"front_img2"`
  36. ApplyTimeUnix int64 `json:"apply_time"`
  37. Operater string `json:"operater"`
  38. OperaterTimeUnix int64 `json:"operater_time"`
  39. Status int8 `json:"status"`
  40. Remark string `json:"remark"`
  41. RemarkStatus int8 `json:"remark_status"`
  42. }
  43. // CardMD5 is.
  44. func (r *RealnameApplyMessage) CardMD5() (res string) {
  45. return cardReMD5(r.CardData(), r.CardType(), r.Country())
  46. }
  47. // cardReMD5 is.
  48. func cardReMD5(encrypedCard string, cardType int, country int) (res string) {
  49. card, err := cardDecrypt([]byte(encrypedCard))
  50. if err != nil {
  51. log.Error("cardNewMD5 decrypt err : %+v", err)
  52. return
  53. }
  54. return cardMD5(string(card), cardType, country)
  55. }
  56. func cardMD5(card string, cardType int, country int) (res string) {
  57. var (
  58. lowerCode = strings.ToLower(card)
  59. key = fmt.Sprintf("%s_%s_%d_%d", realnameSalt, lowerCode, cardType, country)
  60. )
  61. return fmt.Sprintf("%x", md5.Sum([]byte(key)))
  62. }
  63. func cardDecrypt(data []byte) (text []byte, err error) {
  64. var (
  65. decryptedData []byte
  66. size int
  67. )
  68. decryptedData = make([]byte, base64.StdEncoding.DecodedLen(len(data)))
  69. if size, err = base64.StdEncoding.Decode(decryptedData, data); err != nil {
  70. err = errors.Wrapf(err, "base decode %s", data)
  71. return
  72. }
  73. if text, err = rsaDecrypt(decryptedData[:size]); err != nil {
  74. err = errors.Wrapf(err, "rsa decrypt %s , data : %s", decryptedData, data)
  75. return
  76. }
  77. return
  78. }
  79. func rsaDecrypt(text []byte) (content []byte, err error) {
  80. block, _ := pem.Decode(conf.Conf.RealnameRsaPriv)
  81. if block == nil {
  82. err = errors.New("private key error")
  83. return
  84. }
  85. var (
  86. privateKey *rsa.PrivateKey
  87. )
  88. if privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
  89. err = errors.WithStack(err)
  90. return
  91. }
  92. if content, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, text); err != nil {
  93. err = errors.WithStack(err)
  94. return
  95. }
  96. return
  97. }
  98. //ApplyTime is.
  99. func (r *RealnameApplyMessage) ApplyTime() (t time.Time) {
  100. t = time.Unix(r.ApplyTimeUnix, 0)
  101. return
  102. }
  103. //OperaterTime is.
  104. func (r *RealnameApplyMessage) OperaterTime() (t time.Time) {
  105. t = time.Unix(r.OperaterTimeUnix, 0)
  106. return
  107. }
  108. //CardType is.
  109. func (r *RealnameApplyMessage) CardType() (t int) {
  110. if r.Type > 6 {
  111. return 6
  112. }
  113. return r.Type
  114. }
  115. //Country is.
  116. func (r *RealnameApplyMessage) Country() (t int) {
  117. if r.Type > 6 {
  118. return r.Type
  119. }
  120. return 0
  121. }
  122. //CardData is.
  123. func (r *RealnameApplyMessage) CardData() (data string) {
  124. if r.CardDataCanal == "" {
  125. log.Warn("card data empty (+v)", r)
  126. return ""
  127. }
  128. bytes, err := base64.StdEncoding.DecodeString(r.CardDataCanal)
  129. if err != nil {
  130. err = errors.Wrapf(err, "decode (%+v) failed", r)
  131. log.Error("+v", err)
  132. return ""
  133. }
  134. return string(bytes)
  135. }
  136. //RealnameApplyImgMessage is.
  137. type RealnameApplyImgMessage struct {
  138. ID int `json:"id"`
  139. IMGData string `json:"img_data"`
  140. AddTimeStr string `json:"add_time"`
  141. AddTimeDB time.Time
  142. }
  143. //AddTime is.
  144. func (r *RealnameApplyImgMessage) AddTime() (t time.Time) {
  145. if r.AddTimeDB.IsZero() {
  146. var err error
  147. if t, err = time.ParseInLocation("2006-01-02 15:04:05", r.AddTimeStr, time.Local); err != nil {
  148. log.Error("%+v", err)
  149. t = time.Now()
  150. }
  151. return
  152. }
  153. return r.AddTimeDB
  154. }
  155. // RealnameApplyStatus is.
  156. type RealnameApplyStatus int8
  157. const (
  158. // RealnameApplyStatusPending is.
  159. RealnameApplyStatusPending RealnameApplyStatus = iota
  160. // RealnameApplyStatusPass is.
  161. RealnameApplyStatusPass
  162. // RealnameApplyStatusBack is.
  163. RealnameApplyStatusBack
  164. // RealnameApplyStatusNone is.
  165. RealnameApplyStatusNone
  166. )
  167. // IsPass return is apply passed
  168. func (r RealnameApplyStatus) IsPass() bool {
  169. switch r {
  170. case RealnameApplyStatusPass:
  171. return true
  172. default:
  173. return false
  174. }
  175. }
  176. // RealnameChannel is
  177. type RealnameChannel int8
  178. // RealnameChannel enum
  179. const (
  180. RealnameChannelMain RealnameChannel = iota
  181. RealnameChannelAlipay
  182. )
  183. const (
  184. // RealnameCountryChina is.
  185. RealnameCountryChina = 0
  186. // RealnameCardTypeIdentity is.
  187. RealnameCardTypeIdentity = 0
  188. )
  189. // RealnameInfo is user realname status info
  190. type RealnameInfo struct {
  191. ID int64 `json:"id"`
  192. MID int64 `json:"mid"`
  193. Channel RealnameChannel `json:"channel"`
  194. Realname string `json:"realname"`
  195. Country int `json:"country"`
  196. CardType int `json:"card_type"`
  197. Card string `json:"card"`
  198. CardMD5 string `json:"card_md5"`
  199. Status RealnameApplyStatus `json:"status"`
  200. Reason string `json:"reason"`
  201. CTime time.Time `json:"ctime"`
  202. MTime time.Time `json:"mtime"`
  203. }
  204. // DecryptedCard is
  205. func (r *RealnameInfo) DecryptedCard() (string, error) {
  206. raw, err := cardDecrypt([]byte(r.Card))
  207. if err != nil {
  208. return "", err
  209. }
  210. return string(raw), nil
  211. }
  212. // RealnameApply is user realname apply info from default channel.
  213. type RealnameApply struct {
  214. ID int `json:"id"`
  215. MID int64 `json:"mid"`
  216. Realname string `json:"realname"`
  217. Country int16 `json:"country"`
  218. CardType int8 `json:"card_type"`
  219. CardNum string `json:"card_num"`
  220. CardMD5 string `json:"card_md5"`
  221. HandIMG int `json:"hand_img"`
  222. FrontIMG int `json:"front_img"`
  223. BackIMG int `json:"back_img"`
  224. Status int `json:"status"`
  225. Operator string `json:"operator"`
  226. OperatorID int64 `json:"operator_id"`
  227. OperatorTime time.Time `json:"operator_time"`
  228. Remark string `json:"remark"`
  229. RemarkStatus int8 `json:"remark_status"`
  230. CTime time.Time `json:"ctime"`
  231. MTime time.Time `json:"mtime"`
  232. }
  233. // RealnameAlipayApply is user alipay apply info from alipay channle.
  234. type RealnameAlipayApply struct {
  235. ID int64 `json:"id"`
  236. MID int64 `json:"mid"`
  237. Realname string `json:"realname"`
  238. Card string `json:"card"`
  239. IMG string `json:"img"`
  240. Status RealnameApplyStatus `json:"status"`
  241. Reason string `json:"reason"`
  242. Bizno string `json:"bizno"`
  243. CTime time.Time `json:"ctime"`
  244. MTime time.Time `json:"mtime"`
  245. }