sns.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "hash/crc32"
  7. "go-common/app/job/main/passport-sns/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _getSnsUserByMidSQL = "SELECT mid,unionid,platform,expires FROM sns_user WHERE mid = ? and platform = ?"
  13. _getSnsUserByUnionIDSQL = "SELECT mid,unionid,platform FROM sns_user WHERE unionid = ? and platform = ?"
  14. _addSnsUserSQL = "INSERT INTO sns_user (mid,unionid,platform,expires) VALUES(?,?,?,?)"
  15. _addSnsOpenIDSQL = "INSERT IGNORE INTO sns_openid_%02d (openid,unionid,appid,platform) VALUES(?,?,?,?)"
  16. _addSnsTokenSQL = "INSERT INTO sns_token (mid,openid,unionid,platform,token,expires,appid) VALUES(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE mid=?, openid =?, token =?, expires =?, appid =?"
  17. _delSnsUserSQL = "DELETE FROM sns_user WHERE mid = ? and platform = ?"
  18. _updateSnsUserExpiresSQL = "UPDATE sns_user SET expires = ? where mid = ? and platform = ?"
  19. _updateSnsUserSQL = "UPDATE sns_user SET unionid = ?, expires = ? where mid = ? and platform = ?"
  20. _updateSnsTokenSQL = "UPDATE sns_token SET token =?, expires =? WHERE mid =? and platform = ?"
  21. )
  22. // SnsUserByMid get sns user by mid and platform
  23. func (d *Dao) SnsUserByMid(c context.Context, mid int64, platform int) (res *model.SnsUser, err error) {
  24. res = new(model.SnsUser)
  25. row := d.snsDB.QueryRow(c, _getSnsUserByMidSQL, mid, platform)
  26. if err = row.Scan(&res.Mid, &res.UnionID, &res.Platform, &res.Expires); err != nil {
  27. if err == xsql.ErrNoRows {
  28. err = nil
  29. res = nil
  30. return
  31. }
  32. log.Error("SnsUserByMid mid(%d) platform(%d) row.Scan() error(%+v)", mid, platform, err)
  33. return
  34. }
  35. return
  36. }
  37. // SnsUserByUnionID get sns user by unionID and platform
  38. func (d *Dao) SnsUserByUnionID(c context.Context, unionID string, platform int) (res *model.SnsUser, err error) {
  39. res = new(model.SnsUser)
  40. row := d.snsDB.QueryRow(c, _getSnsUserByUnionIDSQL, unionID, platform)
  41. if err = row.Scan(&res.Mid, &res.UnionID, &res.Platform); err != nil {
  42. if err == xsql.ErrNoRows {
  43. err = nil
  44. res = nil
  45. return
  46. }
  47. log.Error("SnsUserByUnionID unionID(%d) platform(%d) row.Scan() error(%+v)", unionID, platform, err)
  48. return
  49. }
  50. return
  51. }
  52. // AddSnsUser add sns user.
  53. func (d *Dao) AddSnsUser(c context.Context, mid, expires int64, unionID string, platform int) (affected int64, err error) {
  54. var res sql.Result
  55. if res, err = d.snsDB.Exec(c, _addSnsUserSQL, mid, unionID, platform, expires); err != nil {
  56. log.Error("AddSnsUser mid(%d) platform(%d) unionID(%s) expires(%d) d.snsDB.Exec() error(%+v)", mid, platform, unionID, platform, err)
  57. return
  58. }
  59. return res.RowsAffected()
  60. }
  61. // TxAddSnsUser add sns user.
  62. func (d *Dao) TxAddSnsUser(tx *xsql.Tx, a *model.SnsUser) (affected int64, err error) {
  63. var res sql.Result
  64. if res, err = tx.Exec(_addSnsUserSQL, a.Mid, a.UnionID, a.Platform, a.Expires); err != nil {
  65. log.Error("TxAddSnsUser(%+v) tx.Exec() error(%+v)", a, err)
  66. return
  67. }
  68. return res.RowsAffected()
  69. }
  70. // TxAddSnsOpenID add sns openid.
  71. func (d *Dao) TxAddSnsOpenID(tx *xsql.Tx, a *model.SnsOpenID) (affected int64, err error) {
  72. var res sql.Result
  73. if res, err = tx.Exec(fmt.Sprintf(_addSnsOpenIDSQL, openIDSuffix(a.OpenID)), a.OpenID, a.UnionID, a.AppID, a.Platform); err != nil {
  74. log.Error("TxAddSnsOpenID(%+v) tx.Exec() error(%+v)", a, err)
  75. return
  76. }
  77. return res.RowsAffected()
  78. }
  79. // TxAddSnsToken add sns token.
  80. func (d *Dao) TxAddSnsToken(tx *xsql.Tx, a *model.SnsToken) (affected int64, err error) {
  81. var res sql.Result
  82. if res, err = tx.Exec(_addSnsTokenSQL, a.Mid, a.OpenID, a.UnionID, a.Platform, a.Token, a.Expires, a.AppID, a.Mid, a.OpenID, a.Token, a.Expires, a.AppID); err != nil {
  83. log.Error("TxAddSnsToken(%+v) tx.Exec() error(%+v)", a, err)
  84. return
  85. }
  86. return res.RowsAffected()
  87. }
  88. // DelSnsUser del sns user.
  89. func (d *Dao) DelSnsUser(c context.Context, mid int64, platform int) (affected int64, err error) {
  90. var res sql.Result
  91. if res, err = d.snsDB.Exec(c, _delSnsUserSQL, mid, platform); err != nil {
  92. log.Error("DelSnsUser mid(%d) platform(%d) d.snsDB.Exec() error(%+v)", mid, platform, err)
  93. return
  94. }
  95. return res.RowsAffected()
  96. }
  97. // TxUpdateSnsUserExpires update sns user expires.
  98. func (d *Dao) TxUpdateSnsUserExpires(tx *xsql.Tx, a *model.SnsUser) (affected int64, err error) {
  99. var res sql.Result
  100. if res, err = tx.Exec(_updateSnsUserExpiresSQL, a.Expires, a.Mid, a.Platform); err != nil {
  101. log.Error("TxUpdateSnsUser(%+v) tx.Exec() error(%+v)", a, err)
  102. return
  103. }
  104. return res.RowsAffected()
  105. }
  106. // TxUpdateSnsUser update sns user.
  107. func (d *Dao) TxUpdateSnsUser(tx *xsql.Tx, mid, expires int64, unionID string, platform int) (affected int64, err error) {
  108. var res sql.Result
  109. if res, err = tx.Exec(_updateSnsUserSQL, unionID, expires, mid, platform); err != nil {
  110. log.Error("TxUpdateSnsUser mid(%d) platform(%d) unionID(%s) expires(%d) d.snsDB.Exec() error(%+v)", mid, platform, unionID, platform, err)
  111. return
  112. }
  113. return res.RowsAffected()
  114. }
  115. // UpdateSnsUser update sns user.
  116. func (d *Dao) UpdateSnsUser(c context.Context, mid, expires int64, unionID string, platform int) (affected int64, err error) {
  117. var res sql.Result
  118. if res, err = d.snsDB.Exec(c, _updateSnsUserSQL, unionID, expires, mid, platform); err != nil {
  119. log.Error("UpdateSnsUser mid(%d) platform(%d) unionID(%s) expires(%d) d.snsDB.Exec() error(%+v)", mid, platform, unionID, platform, err)
  120. return
  121. }
  122. return res.RowsAffected()
  123. }
  124. // TxUpdateSnsToken update sns token.
  125. func (d *Dao) TxUpdateSnsToken(tx *xsql.Tx, a *model.SnsToken) (affected int64, err error) {
  126. var res sql.Result
  127. if res, err = tx.Exec(_updateSnsTokenSQL, a.Token, a.Expires, a.Mid, a.Platform); err != nil {
  128. log.Error("TxUpdateSnsToken(%+v) tx.Exec() error(%+v)", a, err)
  129. return
  130. }
  131. return res.RowsAffected()
  132. }
  133. // UpdateSnsToken update sns token.
  134. func (d *Dao) UpdateSnsToken(c context.Context, a *model.SnsToken) (affected int64, err error) {
  135. var res sql.Result
  136. if res, err = d.snsDB.Exec(c, _updateSnsTokenSQL, a.Token, a.Expires, a.Mid, a.Platform); err != nil {
  137. log.Error("UpdateSnsToken(%+v) tx.Exec() error(%+v)", a, err)
  138. return
  139. }
  140. return res.RowsAffected()
  141. }
  142. func openIDSuffix(openID string) int {
  143. v := int(crc32.ChecksumIEEE([]byte(openID)))
  144. if v < 0 {
  145. v = -v
  146. }
  147. return v % 100
  148. }