package main import ( "context" "flag" "fmt" im "github.com/inkrtech/tencent-im" "log" "os" "time" "github.com/sirupsen/logrus" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" ) func main() { var appID, isPrint int var secret, userID, dsn string flag.IntVar(&appID, "app_id", 0, "tim app id") flag.IntVar(&isPrint, "is_print", 1, "1 is print, 2 is execute") flag.StringVar(&secret, "secret", "", "tim app secret") flag.StringVar(&userID, "user_id", "", "tim admin user") flag.StringVar(&dsn, "dsn", "", "mysql dsn") flag.Parse() if appID == 0 || isPrint == 0 || secret == "" || userID == "" || dsn == "" { logrus.Errorf("app_id, secret, user_id is required") return } utcZone := time.FixedZone("UTC", 0) time.Local = utcZone // 连接mysql newLogger := logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer logger.Config{ SlowThreshold: time.Second, // 慢 SQL 阈值 LogLevel: logger.Silent, // Log level Colorful: false, // 禁用彩色打印 }, ) dsn = fmt.Sprintf("%s?charset=utf8mb4&parseTime=True&loc=Local", dsn) mysqlDb, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: newLogger, NamingStrategy: schema.NamingStrategy{ TablePrefix: "st_", // 表前缀 SingularTable: true, // 复数形式 }}, ) if err != nil { logrus.WithError(err).Error("init db failed") return } var userList []UserInfo err = mysqlDb.Model(&UserInfo{}).Where("deleted in (2,3)").Find(&userList).Error if err != nil { logrus.WithError(err).Error("query user info list failed") return } if isPrint == 1 { fmt.Println("List of users to be processed (only print)") for _, user := range userList { fmt.Printf("%d\n", user.UserNo) } return } if isPrint == 2 { fmt.Println("List of users to be processed") for _, user := range userList { fmt.Printf("%d\n", user.UserNo) } tim := NewTIMClient(appID, secret, userID) for _, user := range userList { err = tim.DeleteAccount(context.Background(), fmt.Sprintf("%d", user.UserNo)) if err != nil { logrus.WithField("user_no", user.UserNo).WithError(err).Error("delete account failed") continue } } } } type UserInfo struct { ID int `gorm:"column:id;primaryKey;autoIncrement:true;comment:用户ID" json:"id"` // 用户ID UserNo int `gorm:"column:user_no;not null;comment:用户编号" json:"user_no"` // 用户编号 Deleted int `gorm:"column:deleted;not null;comment:(0:初始化用户,1:正常,2:注销账号可撤销,3:账号已删除,4:后管封禁)" json:"deleted"` } type TIMClient struct { im.IM } func NewTIMClient(appID int, secret, userID string) *TIMClient { tim := im.NewIM(&im.Options{ AppId: appID, AppSecret: secret, UserId: userID, }) return &TIMClient{IM: tim} } func (tim *TIMClient) DeleteAccount(ctx context.Context, userId string) error { err := tim.IM.Account().DeleteAccount(userId) if err != nil { logrus.WithField("user_no", userId).WithError(err).Error("delete account failed") return err } return nil }