accounts.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package model
  2. import (
  3. "crypto/md5"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "time"
  8. )
  9. type Accounts struct {
  10. Model
  11. }
  12. type AccountsRow struct {
  13. Id int64
  14. Account string
  15. Name string
  16. Password string
  17. LastLogin int64
  18. IsSuper bool
  19. Disabled bool
  20. }
  21. func NewAccounts(db *sql.DB) *Accounts {
  22. return &Accounts{Model{db: db}}
  23. }
  24. func PasswordConvert(pwd string) string {
  25. pwdMd5 := fmt.Sprintf("%x", md5.Sum([]byte(pwd)))
  26. return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("^redisdog|%s|pwd$", pwdMd5))))
  27. }
  28. func (m *Accounts) Insert(row *AccountsRow) (int64, error) {
  29. if row.Account == "" {
  30. return 0, errors.New("账号不能为空")
  31. }
  32. if len(row.Password) < 6 {
  33. return 0, errors.New("密码长度不能小于6")
  34. }
  35. disabledInt := 0
  36. if row.Disabled {
  37. disabledInt = 1
  38. }
  39. passwordEnc := PasswordConvert(row.Password)
  40. ret, err := m.db.Exec(`INSERT INTO accounts(account,name,password,is_super,disabled) VALUES(?,?,?,?,?)`, row.Account, row.Name, passwordEnc, row.IsSuper, disabledInt)
  41. if err != nil {
  42. return 0, nil
  43. }
  44. return ret.LastInsertId()
  45. }
  46. func (m *Accounts) Delete(id int64) (int64, error) {
  47. if id <= 0 {
  48. return 0, errors.New("ID必须是大于0的整数")
  49. }
  50. ret, err := m.db.Exec("DELETE FROM accounts WHERE id=?", id)
  51. if err != nil {
  52. return 0, err
  53. }
  54. return ret.RowsAffected()
  55. }
  56. func (m *Accounts) Update(row *AccountsRow) (int64, error) {
  57. if row.Id <= 0 {
  58. return 0, errors.New("ID必须是大于0的整数")
  59. }
  60. if row.Account == "" {
  61. return 0, errors.New("账号不能为空")
  62. }
  63. disabledInt := 0
  64. if row.Disabled {
  65. disabledInt = 1
  66. }
  67. ret, err := m.db.Exec(`UPDATE accounts SET account=?,name=?,is_super=?,disabled=? WHERE id=?`, row.Account, row.Name, row.IsSuper, disabledInt, row.Id)
  68. if err != nil {
  69. return 0, err
  70. }
  71. return ret.RowsAffected()
  72. }
  73. func (m *Accounts) UpdatePassword(id int64, password string) (int64, error) {
  74. if id <= 0 {
  75. return 0, errors.New("ID必须是大于0的整数")
  76. }
  77. if len(password) < 6 {
  78. return 0, errors.New("密码长度不能小于6")
  79. }
  80. passwordEnc := PasswordConvert(password)
  81. ret, err := m.db.Exec(`UPDATE accounts SET password=? WHERE id=?`, passwordEnc, id)
  82. if err != nil {
  83. return 0, err
  84. }
  85. return ret.RowsAffected()
  86. }
  87. func (m *Accounts) Get(id int64) (*AccountsRow, error) {
  88. if id <= 0 {
  89. return nil, errors.New("ID必须是大于0的整数")
  90. }
  91. result := m.db.QueryRow("SELECT id,account,name,last_login,is_super,disabled FROM accounts WHERE id=?", id)
  92. row := AccountsRow{}
  93. err := result.Scan(
  94. &row.Id,
  95. &row.Account,
  96. &row.Name,
  97. &row.LastLogin,
  98. &row.IsSuper,
  99. &row.Disabled,
  100. )
  101. if err != nil {
  102. if err == sql.ErrNoRows {
  103. return nil, errors.New("记录不存在")
  104. } else {
  105. return nil, err
  106. }
  107. } else {
  108. return &row, nil
  109. }
  110. }
  111. func (m *Accounts) GetAll() ([]*AccountsRow, error) {
  112. result, err := m.db.Query(`SELECT id,account,name,last_login,is_super,disabled FROM accounts ORDER BY id`)
  113. if err != nil {
  114. return nil, err
  115. }
  116. rows := make([]*AccountsRow, 0)
  117. for result.Next() {
  118. row := AccountsRow{}
  119. err = result.Scan(
  120. &row.Id,
  121. &row.Account,
  122. &row.Name,
  123. &row.LastLogin,
  124. &row.IsSuper,
  125. &row.Disabled,
  126. )
  127. if err != nil {
  128. continue
  129. }
  130. rows = append(rows, &row)
  131. }
  132. return rows, nil
  133. }
  134. func (m *Accounts) Login(account, password string) (*AccountsRow, error) {
  135. if account == "" {
  136. return nil, errors.New("账号不能为空")
  137. }
  138. if len(password) < 6 {
  139. return nil, errors.New("密码长度不能小于6")
  140. }
  141. passwordEnc := PasswordConvert(password)
  142. result := m.db.QueryRow("SELECT id,account,name,last_login,is_super,disabled FROM accounts WHERE account=? AND password=? LIMIT 1", account, passwordEnc)
  143. row := AccountsRow{}
  144. err := result.Scan(
  145. &row.Id,
  146. &row.Account,
  147. &row.Name,
  148. &row.LastLogin,
  149. &row.IsSuper,
  150. &row.Disabled,
  151. )
  152. if err != nil {
  153. if err == sql.ErrNoRows {
  154. return nil, errors.New("用户名或密码错误")
  155. } else {
  156. return nil, err
  157. }
  158. } else if row.Disabled {
  159. return nil, errors.New("账号已被禁用")
  160. } else {
  161. m.db.Exec("UPDATE last_login=? WHERE id=?", time.Now().Unix(), row.Id)
  162. return &row, nil
  163. }
  164. }