accounts.go 4.0 KB

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