user.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package service
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "strconv"
  6. "time"
  7. "go-common/app/job/main/passport-user/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _platformSINA = 1
  13. _platformQQ = 2
  14. )
  15. func (s *Service) convertAccountToUserBase(a *model.OriginAccount) (res *model.UserBase, err error) {
  16. pwdBytes, err := hex.DecodeString(a.Pwd)
  17. if err != nil {
  18. log.Error("fail to hex decode pwd(%+v) error(%+v)", a.Pwd, err)
  19. return
  20. }
  21. res = &model.UserBase{
  22. Mid: a.Mid,
  23. UserID: a.UserID,
  24. Pwd: pwdBytes,
  25. Salt: a.Salt,
  26. Status: a.Isleak,
  27. Deleted: 0,
  28. MTime: a.MTime,
  29. }
  30. return
  31. }
  32. func (s *Service) convertAccountToUserEmail(a *model.OriginAccount) (res *model.UserEmail) {
  33. if a.Email == "" {
  34. res = &model.UserEmail{
  35. Mid: a.Mid,
  36. EmailBindTime: time.Now().Unix(),
  37. MTime: a.MTime,
  38. }
  39. return
  40. }
  41. res = &model.UserEmail{
  42. Mid: a.Mid,
  43. Email: s.doEncrypt(a.Email),
  44. EmailBindTime: time.Now().Unix(),
  45. MTime: a.MTime,
  46. }
  47. return
  48. }
  49. func (s *Service) convertAccountToUserTel(a *model.OriginAccount) (res *model.UserTel) {
  50. if a.Tel == "" {
  51. res = &model.UserTel{
  52. Mid: a.Mid,
  53. TelBindTime: time.Now().Unix(),
  54. MTime: a.MTime,
  55. }
  56. return
  57. }
  58. res = &model.UserTel{
  59. Mid: a.Mid,
  60. Tel: s.doEncrypt(a.Tel),
  61. Cid: s.countryMap[a.CountryID],
  62. TelBindTime: time.Now().Unix(),
  63. MTime: a.MTime,
  64. }
  65. return
  66. }
  67. func (s *Service) convertAccountInfoToUserSafeQuestion(a *model.OriginAccountInfo) (res *model.UserSafeQuestion) {
  68. res = &model.UserSafeQuestion{
  69. Mid: a.Mid,
  70. SafeQuestion: a.SafeQuestion,
  71. SafeAnswer: s.doHash(a.SafeAnswer),
  72. SafeBindTime: time.Now().Unix(),
  73. }
  74. return
  75. }
  76. func (s *Service) convertAccountInfoToUserRegOrigin(a *model.OriginAccountInfo) (res *model.UserRegOrigin) {
  77. res = &model.UserRegOrigin{
  78. Mid: a.Mid,
  79. JoinIP: InetAtoN(a.JoinIP),
  80. JoinIPV6: a.JoinIPV6,
  81. JoinTime: a.JoinTime,
  82. }
  83. return
  84. }
  85. func (s *Service) convertAccountInfoToUserEmail(a *model.OriginAccountInfo) (res *model.UserEmail) {
  86. var (
  87. verified int32
  88. activeTime int64
  89. )
  90. if a.Spacesta >= 0 {
  91. verified = 1
  92. activeTime = a.ActiveTime
  93. }
  94. res = &model.UserEmail{
  95. Mid: a.Mid,
  96. Verified: verified,
  97. EmailBindTime: activeTime,
  98. }
  99. return
  100. }
  101. func (s *Service) convertAccountRegToUserRegOrigin(a *model.OriginAccountReg) (res *model.UserRegOrigin) {
  102. res = &model.UserRegOrigin{
  103. Mid: a.Mid,
  104. Origin: a.OriginType,
  105. RegType: a.RegType,
  106. AppID: a.AppID,
  107. CTime: a.MTime,
  108. MTime: a.CTime,
  109. }
  110. return
  111. }
  112. func (s *Service) convertAccountSnsToUserThirdBindQQ(a *model.OriginAccountSns) (res *model.UserThirdBind) {
  113. res = &model.UserThirdBind{
  114. Mid: a.Mid,
  115. OpenID: a.QQOpenid,
  116. PlatForm: _platformQQ,
  117. Token: a.QQAccessToken,
  118. Expires: a.QQAccessExpires,
  119. }
  120. return
  121. }
  122. func (s *Service) convertAccountSnsToUserThirdBindSina(a *model.OriginAccountSns) (res *model.UserThirdBind) {
  123. res = &model.UserThirdBind{
  124. Mid: a.Mid,
  125. OpenID: strconv.FormatInt(a.SinaUID, 10),
  126. PlatForm: _platformSINA,
  127. Token: a.SinaAccessToken,
  128. Expires: a.SinaAccessExpires,
  129. }
  130. return
  131. }
  132. func (s *Service) syncAsoAccount(a *model.OriginAccount) (err error) {
  133. var (
  134. tx *sql.Tx
  135. userBase *model.UserBase
  136. )
  137. userBase, err = s.convertAccountToUserBase(a)
  138. if err != nil {
  139. log.Error("fail to convert AsoAccount(%+v) to UserBase error(%+v)", a, err)
  140. return
  141. }
  142. tx, err = s.d.BeginTran(context.Background())
  143. if err != nil {
  144. log.Error("s.dao.Begin error(%+v)", err)
  145. return
  146. }
  147. if _, err = s.d.TxAddUserBase(tx, userBase); err != nil {
  148. log.Error("fail to add UserBase(%+v) error(%+v)", userBase, err)
  149. tx.Rollback()
  150. return
  151. }
  152. if a.Email != "" {
  153. userEmail := s.convertAccountToUserEmail(a)
  154. userEmail.EmailBindTime = 0
  155. if _, err = s.d.TxAddUserEmail(tx, userEmail); err != nil {
  156. log.Error("fail to add userEmail(%+v) error(%+v)", userEmail, err)
  157. tx.Rollback()
  158. return
  159. }
  160. }
  161. if a.Tel != "" {
  162. userTel := s.convertAccountToUserTel(a)
  163. userTel.TelBindTime = 0
  164. if _, err = s.d.TxAddUserTel(tx, userTel); err != nil {
  165. log.Error("fail to add userTel(%+v) error(%+v)", userTel, err)
  166. tx.Rollback()
  167. return
  168. }
  169. }
  170. if err = tx.Commit(); err != nil {
  171. log.Error("tx.Commit(), error(%v)", err)
  172. return
  173. }
  174. return
  175. }
  176. func (s *Service) syncAsoAccountInfo(a *model.OriginAccountInfo) (err error) {
  177. var (
  178. tx *sql.Tx
  179. userSafeQuestion *model.UserSafeQuestion
  180. userRegOrigin *model.UserRegOrigin
  181. userEmail *model.UserEmail
  182. )
  183. userRegOrigin = s.convertAccountInfoToUserRegOrigin(a)
  184. userEmail = s.convertAccountInfoToUserEmail(a)
  185. tx, err = s.d.BeginTran(context.Background())
  186. if err != nil {
  187. log.Error("s.dao.Begin error(%+v)", err)
  188. return
  189. }
  190. if a.SafeQuestion != 0 || a.SafeAnswer != "" {
  191. userSafeQuestion = s.convertAccountInfoToUserSafeQuestion(a)
  192. userSafeQuestion.SafeBindTime = 0
  193. if _, err = s.d.TxInsertIgnoreUserSafeQuestion(tx, userSafeQuestion); err != nil {
  194. tx.Rollback()
  195. return
  196. }
  197. }
  198. if _, err = s.d.TxInsertIgnoreUserRegOrigin(tx, userRegOrigin); err != nil {
  199. tx.Rollback()
  200. return
  201. }
  202. if err = tx.Commit(); err != nil {
  203. log.Error("tx.Commit(), error(%v)", err)
  204. return
  205. }
  206. if _, err = s.d.UpdateUserEmailBindTime(context.Background(), userEmail); err != nil {
  207. tx.Rollback()
  208. return
  209. }
  210. return
  211. }
  212. func (s *Service) syncAsoAccountReg(a *model.OriginAccountReg) (err error) {
  213. userRegOrigin := s.convertAccountRegToUserRegOrigin(a)
  214. if _, err = s.d.InsertUpdateUserRegOriginType(context.Background(), userRegOrigin); err != nil {
  215. log.Error("fail to insert update reg origin type userRegOrigin(%+v) error(%+v)", userRegOrigin, err)
  216. }
  217. return
  218. }
  219. func (s *Service) syncAsoAccountSns(a *model.OriginAccountSns) (err error) {
  220. var (
  221. tx *sql.Tx
  222. qq *model.UserThirdBind
  223. sina *model.UserThirdBind
  224. utb *model.UserThirdBind
  225. )
  226. sina = s.convertAccountSnsToUserThirdBindSina(a)
  227. qq = s.convertAccountSnsToUserThirdBindQQ(a)
  228. tx, err = s.d.BeginTran(context.Background())
  229. if err != nil {
  230. log.Error("s.dao.Begin error(%+v)", err)
  231. return
  232. }
  233. if a.QQOpenid != "" {
  234. if utb, err = s.d.GetUserThirdBindByMidAndPlatform(context.Background(), a.Mid, _platformQQ); err != nil {
  235. return
  236. }
  237. if utb == nil {
  238. if _, err = s.d.TxAddUserThirdBind(tx, qq); err != nil {
  239. log.Error("fail to add third bind qq userThirdBind(%+v) error(%+v)", qq, err)
  240. tx.Rollback()
  241. return
  242. }
  243. } else {
  244. log.Error("third bind qq is exist, userThirdBind(%+v)", utb)
  245. }
  246. }
  247. if a.SinaUID != 0 {
  248. if utb, err = s.d.GetUserThirdBindByMidAndPlatform(context.Background(), a.Mid, _platformSINA); err != nil {
  249. return
  250. }
  251. if utb == nil {
  252. if _, err = s.d.TxAddUserThirdBind(tx, sina); err != nil {
  253. log.Error("fail to add third bind sina userThirdBind(%+v) error(%+v)", sina, err)
  254. tx.Rollback()
  255. return
  256. }
  257. } else {
  258. log.Error("third bind weibo is exist, userThirdBind(%+v)", utb)
  259. }
  260. }
  261. if err = tx.Commit(); err != nil {
  262. log.Error("tx.Commit(), error(%v)", err)
  263. return
  264. }
  265. return
  266. }
  267. func (s *Service) syncAsoCountryCode() (err error) {
  268. res, err := s.d.AsoCountryCode(context.Background())
  269. if err != nil {
  270. log.Error("fail to get country code error(%+v)", err)
  271. return
  272. }
  273. for _, r := range res {
  274. s.d.AddCountryCode(context.Background(), r)
  275. }
  276. return
  277. }