Пример #1
0
func AddUser(username, password string, department_id, employment_type, gender, level int, name, phone, phone1, position, email string, join_date, birth_date time.Time, ip_address string) error {
	var u = &models.Users{Username: username}
	has, _ := app.Engine.Get(u)

	if has {
		return errors.New("用户名已被注册,不可用")
	}

	now := time.Now()
	u.IpAddress = strings.Split(ip_address, ":")[0]
	u.Salt = utils.Salt()
	u.Password = utils.EncryptPassword(u.Salt, password)
	u.IsActivited = 1
	u.ActivatedAt = now
	u.CreatedAt = now
	u.UpdatedAt = now

	_, err := app.Engine.Insert(u)
	if err != nil {
		return errors.New("添加用户失败" + err.Error())
	}

	profile := new(models.UserProfiles)
	profile.UserId = u.Id
	profile.DepartmentId = department_id
	profile.Gender = gender
	profile.Level = level
	profile.EmploymentType = employment_type
	profile.Name = name
	profile.Phone = phone
	profile.Phone1 = phone1
	profile.Position = position
	profile.JoinDate = join_date
	profile.Email = email
	profile.BirthDate = birth_date
	profile.CreatedAt = now
	profile.UpdatedAt = now
	_, err = app.Engine.Insert(profile)
	if err != nil {
		app.Engine.Id(u.Id).Delete(u)
		errors.New("添加用户失败" + err.Error())
	}
	return nil
}