sns.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "hash/crc32"
  7. "go-common/app/service/main/passport-sns/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _getSnsAppsSQL = "SELECT appid,appsecret,platform,business FROM sns_apps"
  13. _getSnsUsersSQL = "SELECT mid,unionid,platform,expires FROM sns_user WHERE mid = ?"
  14. _getSnsTokensSQL = "SELECT mid,openid,unionid,platform,token,expires FROM sns_token WHERE mid = ?"
  15. _getSnsUserByMidSQL = "SELECT mid,unionid,platform FROM sns_user WHERE mid = ? and platform = ?"
  16. _getSnsUserByUnionIDSQL = "SELECT mid,unionid,platform FROM sns_user WHERE unionid = ? and platform = ?"
  17. _addSnsUserSQL = "INSERT INTO sns_user (mid,unionid,platform,expires) VALUES(?,?,?,?)"
  18. _addSnsOpenIDSQL = "INSERT IGNORE INTO sns_openid_%02d (openid,unionid,appid,platform) VALUES(?,?,?,?)"
  19. _addSnsTokenSQL = "INSERT INTO sns_token (mid,openid,unionid,platform,token,expires,appid) VALUES(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE mid=?, openid =?, token =?, expires =?, appid =?"
  20. _updateSnsTokenSQL = "UPDATE sns_token SET token =?, expires =? WHERE mid =? and platform = ?"
  21. _updateSnsUserSQL = "UPDATE sns_user SET expires =? WHERE mid =? and platform =?"
  22. _delSnsUserSQL = "DELETE FROM sns_user WHERE mid = ? and platform = ?"
  23. _delSnsUsersSQL = "DELETE FROM sns_user WHERE mid = ?"
  24. )
  25. // SnsApps get sns apps
  26. func (d *Dao) SnsApps(c context.Context) (res []*model.SnsApps, err error) {
  27. var rows *xsql.Rows
  28. if rows, err = d.db.Query(c, _getSnsAppsSQL); err != nil {
  29. log.Error("SnsApps dao.db.Query error(%+v)", err)
  30. return
  31. }
  32. res = make([]*model.SnsApps, 0)
  33. defer rows.Close()
  34. for rows.Next() {
  35. r := new(model.SnsApps)
  36. if err = rows.Scan(&r.AppID, &r.AppSecret, &r.Platform, &r.Business); err != nil {
  37. log.Error("SnsApps row.Scan() error(%+v)", err)
  38. res = nil
  39. return
  40. }
  41. res = append(res, r)
  42. }
  43. return
  44. }
  45. // SnsUsers get sns users
  46. func (d *Dao) SnsUsers(c context.Context, mid int64) (res []*model.SnsUser, err error) {
  47. var rows *xsql.Rows
  48. if rows, err = d.db.Query(c, _getSnsUsersSQL, mid); err != nil {
  49. log.Error("SnsUsers dao.db.Query error(%+v)", err)
  50. return
  51. }
  52. res = make([]*model.SnsUser, 0)
  53. defer rows.Close()
  54. for rows.Next() {
  55. r := new(model.SnsUser)
  56. if err = rows.Scan(&r.Mid, &r.UnionID, &r.Platform, &r.Expires); err != nil {
  57. log.Error("SnsUsers row.Scan() error(%+v)", err)
  58. res = nil
  59. return
  60. }
  61. res = append(res, r)
  62. }
  63. return
  64. }
  65. // SnsTokens get sns tokens
  66. func (d *Dao) SnsTokens(c context.Context, mid int64) (res []*model.SnsToken, err error) {
  67. var rows *xsql.Rows
  68. if rows, err = d.db.Query(c, _getSnsTokensSQL, mid); err != nil {
  69. log.Error("SnsTokens dao.db.Query error(%+v)", err)
  70. return
  71. }
  72. res = make([]*model.SnsToken, 0)
  73. defer rows.Close()
  74. for rows.Next() {
  75. r := new(model.SnsToken)
  76. if err = rows.Scan(&r.Mid, &r.OpenID, &r.UnionID, &r.Platform, &r.Token, &r.Expires); err != nil {
  77. log.Error("SnsTokens row.Scan() error(%+v)", err)
  78. res = nil
  79. return
  80. }
  81. res = append(res, r)
  82. }
  83. return
  84. }
  85. // SnsUserByMid get sns user by mid and platform
  86. func (d *Dao) SnsUserByMid(c context.Context, mid int64, platform int) (res *model.SnsUser, err error) {
  87. res = new(model.SnsUser)
  88. row := d.db.QueryRow(c, _getSnsUserByMidSQL, mid, platform)
  89. if err = row.Scan(&res.Mid, &res.UnionID, &res.Platform); err != nil {
  90. if err == xsql.ErrNoRows {
  91. err = nil
  92. res = nil
  93. return
  94. }
  95. log.Error("SnsUserByMid mid(%d) platform(%d) row.Scan() error(%+v)", mid, platform, err)
  96. return
  97. }
  98. return
  99. }
  100. // SnsUserByUnionID get sns user by unionID and platform
  101. func (d *Dao) SnsUserByUnionID(c context.Context, unionID string, platform int) (res *model.SnsUser, err error) {
  102. res = new(model.SnsUser)
  103. row := d.db.QueryRow(c, _getSnsUserByUnionIDSQL, unionID, platform)
  104. if err = row.Scan(&res.Mid, &res.UnionID, &res.Platform); err != nil {
  105. if err == xsql.ErrNoRows {
  106. err = nil
  107. res = nil
  108. return
  109. }
  110. log.Error("SnsUserByUnionID unionID(%s) platform(%d) row.Scan() error(%+v)", unionID, platform, err)
  111. return
  112. }
  113. return
  114. }
  115. // TxAddSnsUser add sns user.
  116. func (d *Dao) TxAddSnsUser(tx *xsql.Tx, a *model.SnsUser) (affected int64, err error) {
  117. var res sql.Result
  118. if res, err = tx.Exec(_addSnsUserSQL, a.Mid, a.UnionID, a.Platform, a.Expires); err != nil {
  119. log.Error("TxAddSnsUser(%+v) tx.Exec() error(%+v)", a, err)
  120. return
  121. }
  122. return res.RowsAffected()
  123. }
  124. // TxAddSnsOpenID add sns openid.
  125. func (d *Dao) TxAddSnsOpenID(tx *xsql.Tx, a *model.SnsOpenID) (affected int64, err error) {
  126. var res sql.Result
  127. if res, err = tx.Exec(fmt.Sprintf(_addSnsOpenIDSQL, openIDSuffix(a.OpenID)), a.OpenID, a.UnionID, a.AppID, a.Platform); err != nil {
  128. log.Error("TxAddSnsOpenID(%+v) tx.Exec() error(%+v)", a, err)
  129. return
  130. }
  131. return res.RowsAffected()
  132. }
  133. // TxAddSnsToken add sns token.
  134. func (d *Dao) TxAddSnsToken(tx *xsql.Tx, a *model.SnsToken) (affected int64, err error) {
  135. var res sql.Result
  136. 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 {
  137. log.Error("TxAddSnsToken(%+v) tx.Exec() error(%+v)", a, err)
  138. return
  139. }
  140. return res.RowsAffected()
  141. }
  142. // TxUpdateSnsUser update sns user expires.
  143. func (d *Dao) TxUpdateSnsUser(tx *xsql.Tx, a *model.SnsUser) (affected int64, err error) {
  144. var res sql.Result
  145. if res, err = tx.Exec(_updateSnsUserSQL, a.Expires, a.Mid, a.Platform); err != nil {
  146. log.Error("TxUpdateSnsUser(%+v) tx.Exec() error(%+v)", a, err)
  147. return
  148. }
  149. return res.RowsAffected()
  150. }
  151. // TxUpdateSnsToken update sns token.
  152. func (d *Dao) TxUpdateSnsToken(tx *xsql.Tx, a *model.SnsToken) (affected int64, err error) {
  153. var res sql.Result
  154. if res, err = tx.Exec(_updateSnsTokenSQL, a.Token, a.Expires, a.Mid, a.Platform); err != nil {
  155. log.Error("TxUpdateSnsToken(%+v) tx.Exec() error(%+v)", a, err)
  156. return
  157. }
  158. return res.RowsAffected()
  159. }
  160. // DelSnsUser del sns user.
  161. func (d *Dao) DelSnsUser(c context.Context, mid int64, platform int) (affected int64, err error) {
  162. var res sql.Result
  163. if res, err = d.db.Exec(c, _delSnsUserSQL, mid, platform); err != nil {
  164. log.Error("DelSnsUser mid(%d) platform(%d) d.db.Exec() error(%+v)", mid, platform, err)
  165. return
  166. }
  167. return res.RowsAffected()
  168. }
  169. // DelSnsUsers del sns user by mid.
  170. func (d *Dao) DelSnsUsers(c context.Context, mid int64) (affected int64, err error) {
  171. var res sql.Result
  172. if res, err = d.db.Exec(c, _delSnsUsersSQL, mid); err != nil {
  173. log.Error("DelAllSnsUser mid(%d) d.db.Exec() error(%+v)", mid, err)
  174. return
  175. }
  176. return res.RowsAffected()
  177. }
  178. func openIDSuffix(openID string) int {
  179. v := int(crc32.ChecksumIEEE([]byte(openID)))
  180. if v < 0 {
  181. v = -v
  182. }
  183. return v % 100
  184. }