delete_account.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. im "github.com/inkrtech/tencent-im"
  7. "log"
  8. "os"
  9. "time"
  10. "github.com/sirupsen/logrus"
  11. "gorm.io/driver/mysql"
  12. "gorm.io/gorm"
  13. "gorm.io/gorm/logger"
  14. "gorm.io/gorm/schema"
  15. )
  16. func main() {
  17. var appID, isPrint int
  18. var secret, userID, dsn string
  19. flag.IntVar(&appID, "app_id", 0, "tim app id")
  20. flag.IntVar(&isPrint, "is_print", 1, "1 is print, 2 is execute")
  21. flag.StringVar(&secret, "secret", "", "tim app secret")
  22. flag.StringVar(&userID, "user_id", "", "tim admin user")
  23. flag.StringVar(&dsn, "dsn", "", "mysql dsn")
  24. flag.Parse()
  25. if appID == 0 || isPrint == 0 || secret == "" || userID == "" || dsn == "" {
  26. logrus.Errorf("app_id, secret, user_id is required")
  27. return
  28. }
  29. utcZone := time.FixedZone("UTC", 0)
  30. time.Local = utcZone
  31. // 连接mysql
  32. newLogger := logger.New(
  33. log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
  34. logger.Config{
  35. SlowThreshold: time.Second, // 慢 SQL 阈值
  36. LogLevel: logger.Silent, // Log level
  37. Colorful: false, // 禁用彩色打印
  38. },
  39. )
  40. dsn = fmt.Sprintf("%s?charset=utf8mb4&parseTime=True&loc=Local", dsn)
  41. mysqlDb, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: newLogger, NamingStrategy: schema.NamingStrategy{
  42. TablePrefix: "st_", // 表前缀
  43. SingularTable: true, // 复数形式
  44. }},
  45. )
  46. if err != nil {
  47. logrus.WithError(err).Error("init db failed")
  48. return
  49. }
  50. var userList []UserInfo
  51. err = mysqlDb.Model(&UserInfo{}).Where("deleted in (2,3)").Find(&userList).Error
  52. if err != nil {
  53. logrus.WithError(err).Error("query user info list failed")
  54. return
  55. }
  56. if isPrint == 1 {
  57. fmt.Println("List of users to be processed (only print)")
  58. for _, user := range userList {
  59. fmt.Printf("%d\n", user.UserNo)
  60. }
  61. return
  62. }
  63. if isPrint == 2 {
  64. fmt.Println("List of users to be processed")
  65. for _, user := range userList {
  66. fmt.Printf("%d\n", user.UserNo)
  67. }
  68. tim := NewTIMClient(appID, secret, userID)
  69. for _, user := range userList {
  70. err = tim.DeleteAccount(context.Background(), fmt.Sprintf("%d", user.UserNo))
  71. if err != nil {
  72. logrus.WithField("user_no", user.UserNo).WithError(err).Error("delete account failed")
  73. continue
  74. }
  75. }
  76. }
  77. }
  78. type UserInfo struct {
  79. ID int `gorm:"column:id;primaryKey;autoIncrement:true;comment:用户ID" json:"id"` // 用户ID
  80. UserNo int `gorm:"column:user_no;not null;comment:用户编号" json:"user_no"` // 用户编号
  81. Deleted int `gorm:"column:deleted;not null;comment:(0:初始化用户,1:正常,2:注销账号可撤销,3:账号已删除,4:后管封禁)" json:"deleted"`
  82. }
  83. type TIMClient struct {
  84. im.IM
  85. }
  86. func NewTIMClient(appID int, secret, userID string) *TIMClient {
  87. tim := im.NewIM(&im.Options{
  88. AppId: appID,
  89. AppSecret: secret,
  90. UserId: userID,
  91. })
  92. return &TIMClient{IM: tim}
  93. }
  94. func (tim *TIMClient) DeleteAccount(ctx context.Context, userId string) error {
  95. err := tim.IM.Account().DeleteAccount(userId)
  96. if err != nil {
  97. logrus.WithField("user_no", userId).WithError(err).Error("delete account failed")
  98. return err
  99. }
  100. return nil
  101. }