sso

register_service.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package service
  2. import (
  3. "time"
  4. "SSO/enums"
  5. "SSO/models"
  6. "SSO/utils"
  7. )
  8. func IsMobileRegister(mobile string) bool {
  9. var count int
  10. err := readUserDb.
  11. Model(&models.AdminUser{}).
  12. Where("mobile = ?", mobile).
  13. Count(&count).
  14. Error
  15. if err != nil {
  16. utils.ErrorLog("判断手机号是否被注册时失败: %v", err)
  17. return true
  18. }
  19. return count > 0
  20. }
  21. func RegisterSuperAdmin(mobile string, password string) (*models.AdminUser, *enums.SGJError) {
  22. tx := writeUserDb.Begin()
  23. now := time.Now().Unix()
  24. admin := models.AdminUser{
  25. Mobile: mobile,
  26. Password: password,
  27. IsSuperAdmin: true,
  28. Status: 1,
  29. CreateTime: now,
  30. ModifyTime: now,
  31. }
  32. registerSuccess := false
  33. if createAdminErr := tx.Create(&admin).Error; createAdminErr != nil {
  34. utils.ErrorLog("创建 AdminUser 失败:%v", createAdminErr)
  35. tx.Rollback()
  36. } else {
  37. registerSuccess = true
  38. }
  39. tx.Commit()
  40. if registerSuccess == true {
  41. return &admin, nil
  42. } else {
  43. return nil, &enums.SGJError{Code: enums.ErrorCodeRegisterFail}
  44. }
  45. }
  46. func ModifyPassword(mobile string, newPwd string) error {
  47. tx := writeUserDb.Begin()
  48. if err := tx.Exec("UPDATE sgj_user_admin SET password = ?, mtime = ? WHERE mobile = ?", newPwd, time.Now().Unix(), mobile).Error; err != nil {
  49. tx.Rollback()
  50. return err
  51. }
  52. return tx.Commit().Error
  53. }