// 创建会员 func (this *Member) create(m *member.ValueMember) (int, error) { if this.UsrIsExist() { return -1, errors.New("用户名已经被使用") } t := time.Now().Unix() m.State = 1 m.RegTime = t m.LastLoginTime = t m.Level = 1 m.Exp = 1 m.Avatar = "resource/no_avatar.gif" m.Birthday = "1970-01-01" m.DynamicToken = m.Pwd m.Exp = 0 if len(m.RegFrom) == 0 { m.RegFrom = "API-INTERNAL" } // 如果昵称为空,则跟用户名相同 if len(m.Name) == 0 { m.Name = m.Usr } m.InvitationCode = this.generateInvitationCode() // 创建一个邀请码 id, err := this._rep.SaveMember(m) if id != 0 { this._value.Id = id } return id, err }
func (this *Member) validate(v *member.ValueMember) error { v.Usr = strings.ToLower(strings.TrimSpace(v.Usr)) // 小写并删除空格 v.Name = strings.TrimSpace(v.Name) v.Email = strings.ToLower(strings.TrimSpace(v.Email)) v.Phone = strings.TrimSpace(v.Phone) if len([]rune(v.Usr)) < 6 { return member.ErrUserLength } if !userRegex.MatchString(v.Usr) { return member.ErrUserValidErr } if this.GetAggregateRootId() != 0 && len([]rune(v.Name)) < 2 { return member.ErrPersonName } if len(v.Email) != 0 && !emailRegex.MatchString(v.Email) { return member.ErrEmailValidErr } if len(v.Phone) != 0 && !phoneRegex.MatchString(v.Phone) { return member.ErrPhoneValidErr } if len(v.Qq) != 0 && !qqRegex.MatchString(v.Qq) { return member.ErrQqValidErr } return nil }