func CreateUser(form url.Values) (errMsg string, err error) { // 存用户基本信息,产生自增长UID user := model.NewUser() err = util.ConvertAssign(user, form) if err != nil { logger.Errorln("user ConvertAssign error", err) errMsg = err.Error() return } uid, err := user.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } // 存用户登录信息 userLogin := model.NewUserLogin() err = util.ConvertAssign(userLogin, form) if err != nil { errMsg = err.Error() logger.Errorln("CreateUser error:", err) return } userLogin.Uid = uid _, err = userLogin.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } // 存用户角色信息 userRole := model.NewUserRole() // 默认为初级会员 roleId := model.AllRoleId[len(model.AllRoleId)-1] if form.Get("roleid") != "" { tmpId, err := strconv.Atoi(form.Get("roleid")) if err == nil { roleId = tmpId } } userRole.Roleid = roleId userRole.Uid = uid if _, err = userRole.Insert(); err != nil { logger.Errorln("userRole insert Error:", err) } // 存用户活跃信息,初始活跃+2 userActive := model.NewUserActive() userActive.Uid = uid userActive.Username = user.Username userActive.Email = user.Email userActive.Weight = 2 if _, err = userActive.Insert(); err != nil { logger.Errorln("UserActive insert Error:", err) } return }
// 获得活跃用户 func FindActiveUsers(start, num int) []*model.UserActive { activeUsers, err := model.NewUserActive().Order("weight DESC").Limit(strconv.Itoa(start) + "," + strconv.Itoa(num)).FindAll() if err != nil { logger.Errorln("user service FindActiveUsers error:", err) return nil } return activeUsers }
// 更换头像 func ChangeAvatar(uid int, avatar string) (err error) { err = model.NewUser().Set("avatar=?", avatar).Where("uid=?", uid).Update() if err == nil { err = model.NewUserActive().Set("avatar=?", avatar).Where("uid=?", uid).Update() } return }
func DecrUserWeight(where string, divide int) { if divide <= 0 { return } strSql := "UPDATE user_active SET weight=weight/" + strconv.Itoa(divide) + " WHERE " + where if result, err := model.NewUserActive().Exec(strSql); err != nil { logger.Errorln("UserActive update Error:", err) } else { n, _ := result.RowsAffected() logger.Debugln(strSql, "affected num:", n) } }
// 获取 @ 的 suggest 列表 func GetUserMentions(term string, limit int) []map[string]string { term = "%" + term + "%" userActives, err := model.NewUserActive().Where("username like ?", term).Limit(strconv.Itoa(limit)).Order("mtime DESC").FindAll("email", "username", "avatar") if err != nil { logger.Errorln("user service GetUserMentions Error:", err) return nil } users := make([]map[string]string, len(userActives)) for i, userActive := range userActives { user := make(map[string]string, 2) user["username"] = userActive.Username user["avatar"] = util.Gravatar(userActive.Avatar, userActive.Email, 20) users[i] = user } return users }
// 增加或减少用户活跃度 func IncUserWeight(where string, weight int) { if err := model.NewUserActive().Where(where).Increment("weight", weight); err != nil { logger.Errorln("UserActive update Error:", err) } }
func CreateUser(form url.Values) (errMsg string, err error) { if EmailExists(form.Get("email")) { err = errors.New("该邮箱已注册过") return } if UsernameExists(form.Get("username")) { err = errors.New("用户名已存在") return } // 存用户基本信息,产生自增长UID user := model.NewUser() err = util.ConvertAssign(user, form) if err != nil { logger.Errorln("user ConvertAssign error", err) errMsg = err.Error() return } user.Ctime = util.TimeNow() // 随机给一个默认头像 user.Avatar = DefaultAvatars[rand.Intn(len(DefaultAvatars))] uid, err := user.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } // 存用户登录信息 userLogin := model.NewUserLogin() err = util.ConvertAssign(userLogin, form) if err != nil { errMsg = err.Error() logger.Errorln("CreateUser error:", err) return } userLogin.Uid = uid _, err = userLogin.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } // 存用户角色信息 userRole := model.NewUserRole() // 默认为初级会员 userRole.Roleid = Roles[len(Roles)-1].Roleid userRole.Uid = uid if _, err = userRole.Insert(); err != nil { logger.Errorln("userRole insert Error:", err) } // 存用户活跃信息,初始活跃+2 userActive := model.NewUserActive() userActive.Uid = uid userActive.Username = user.Username userActive.Avatar = user.Avatar userActive.Email = user.Email userActive.Weight = 2 if _, err = userActive.Insert(); err != nil { logger.Errorln("UserActive insert Error:", err) } return }