mysql_author.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. artmdl "go-common/app/interface/openplatform/article/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. xtime "go-common/library/time"
  12. )
  13. const (
  14. _verifyAPI = "http://account.bilibili.co/api/internal/identify/info"
  15. )
  16. // Authors loads author list who are permitted to post articles.
  17. func (d *Dao) Authors(c context.Context) (res map[int64]*artmdl.AuthorLimit, err error) {
  18. var rows *sql.Rows
  19. if rows, err = d.authorsStmt.Query(c); err != nil {
  20. PromError("db:作者列表查询")
  21. log.Error("db.authorsStmt.Query error(%+v)", err)
  22. return
  23. }
  24. defer rows.Close()
  25. res = make(map[int64]*artmdl.AuthorLimit)
  26. for rows.Next() {
  27. var (
  28. mid int64
  29. author = &artmdl.AuthorLimit{}
  30. )
  31. if err = rows.Scan(&mid, &author.Limit, &author.State); err != nil {
  32. PromError("作者列表scan")
  33. log.Error("rows.Authors.Scan error(%+v)", err)
  34. return
  35. }
  36. res[mid] = author
  37. }
  38. err = rows.Err()
  39. promErrorCheck(err)
  40. return
  41. }
  42. // ApplyCount get today apply count
  43. func (d *Dao) ApplyCount(c context.Context) (count int64, err error) {
  44. var t = time.Now().Truncate(24 * time.Hour)
  45. if err = d.applyCountStmt.QueryRow(c, t).Scan(&count); err != nil {
  46. PromError("db:查询申请总数")
  47. log.Error("db.ApplyCountStmt.Query(%v) error(%+v)", t, err)
  48. }
  49. return
  50. }
  51. // AddApply add new apply
  52. func (d *Dao) AddApply(c context.Context, mid int64, content, category string) (err error) {
  53. var t = time.Now()
  54. if _, err = d.applyStmt.Exec(c, mid, t, content, category, t, content, category); err != nil {
  55. PromError("db:申请作者权限")
  56. log.Error("db.applyStmt.Query(mid: %v, t: %v, category: %v) error(%+v)", mid, t, category, err)
  57. }
  58. return
  59. }
  60. // AddAuthor add author
  61. func (d *Dao) AddAuthor(c context.Context, mid int64) (err error) {
  62. if _, err = d.addAuthorStmt.Exec(c, mid); err != nil {
  63. PromError("db:增加作者权限")
  64. log.Error("mysql: db.addAuthorStmt.Query(mid: %v) error(%+v)", mid, err)
  65. }
  66. return
  67. }
  68. // RawAuthor get author's info.
  69. func (d *Dao) RawAuthor(c context.Context, mid int64) (res *artmdl.AuthorLimit, err error) {
  70. res = new(artmdl.AuthorLimit)
  71. if err = d.authorStmt.QueryRow(c, mid).Scan(&res.State, &res.Rtime, &res.Limit); err != nil {
  72. if err == sql.ErrNoRows {
  73. res = nil
  74. err = nil
  75. return
  76. }
  77. PromError("db:RawAuthor scan")
  78. log.Error("RawAuthor row.Scan(%d) error(%+v)", mid, err)
  79. }
  80. if int64(res.Rtime) < 0 {
  81. res.Rtime = xtime.Time(0)
  82. }
  83. return
  84. }
  85. // Identify gets user verify info.
  86. func (d *Dao) Identify(c context.Context, mid int64) (res *artmdl.Identify, err error) {
  87. params := url.Values{}
  88. params.Set("mid", strconv.FormatInt(mid, 10))
  89. var resp struct {
  90. Code int `json:"code"`
  91. Data *artmdl.Identify `json:"data"`
  92. }
  93. if err = d.httpClient.Get(c, _verifyAPI, "", params, &resp); err != nil {
  94. log.Error("d.httpClient.Get(%s) error(%+v)", _verifyAPI+"?"+params.Encode(), err)
  95. PromError("http:获取用户实名认证信息")
  96. return
  97. }
  98. if resp.Code != ecode.OK.Code() {
  99. log.Error("d.httpClient.Get(%s) code(%d)", _verifyAPI+"?"+params.Encode(), resp.Code)
  100. PromError("http:获取用户实名认证信息状态码异常")
  101. err = ecode.Int(resp.Code)
  102. return
  103. }
  104. res = resp.Data
  105. return
  106. }