当前位置: 首页>>代码示例>>Golang>>正文


Golang Model.NewUser函数代码示例

本文整理汇总了Golang中Model.NewUser函数的典型用法代码示例。如果您正苦于以下问题:Golang NewUser函数的具体用法?Golang NewUser怎么用?Golang NewUser使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewUser函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: findUserByUniq

// 通过唯一键(uid或username)获取用户信息
func findUserByUniq(field, val string) *model.User {
	user := model.NewUser()
	err := user.Where(field + "=" + val).Find()
	if err != nil {
		logger.Errorf("获取用户 %s 信息失败:%s", val, err)
		return nil
	}
	if user.Uid == 0 {
		return nil
	}

	// 获取用户角色信息
	userRoleList, err := model.NewUserRole().
		Order("roleid ASC").Where("uid="+strconv.Itoa(user.Uid)).FindAll("uid", "roleid")
	if err != nil {
		logger.Errorf("获取用户 %s 角色 信息失败:%s", val, err)
		return nil
	}

	if roleNum := len(userRoleList); roleNum > 0 {
		user.Roleids = make([]int, roleNum)
		user.Rolenames = make([]string, roleNum)

		for i, userRole := range userRoleList {
			user.Roleids[i] = userRole.Roleid
			user.Rolenames[i] = Roles[userRole.Roleid-1].Name
		}
	}

	return user
}
开发者ID:NeoChow,项目名称:studygolang,代码行数:32,代码来源:user.go

示例2: FindUsers

func FindUsers() (map[int]*model.User, error) {
	userList, err := model.NewUser().FindAll()
	if err != nil {
		logger.Errorln("user service FindUsers Error:", err)
		return nil, err
	}
	userCount := len(userList)
	userMap := make(map[int]*model.User, userCount)
	uids := make([]int, userCount)
	for i, user := range userList {
		userMap[user.Uid] = user
		uids[i] = user.Uid
	}
	// 获得角色信息
	userRoleList, err := model.NewUserRole().Where("uid in (" + util.Join(uids, ",") + ")").FindAll()
	if err != nil {
		logger.Errorln("user service FindUsers Error:", err)
		return nil, err
	}
	for _, userRole := range userRoleList {
		user := userMap[userRole.Uid]
		if len(user.Roleids) == 0 {
			user.Roleids = []int{userRole.Roleid}
			user.Rolenames = []string{model.AllRole[userRole.Roleid].Name}
		} else {
			user.Roleids = append(user.Roleids, userRole.Roleid)
			user.Rolenames = append(user.Rolenames, model.AllRole[userRole.Roleid].Name)
		}
	}
	return userMap, nil
}
开发者ID:jimmykuu,项目名称:studygolang,代码行数:31,代码来源:user.go

示例3: FindUserByUsername

// 获取单个用户信息
func FindUserByUsername(username string) *model.User {
	user := model.NewUser()
	err := user.Where("username=" + username).Find()
	if err != nil {
		logger.Errorf("获取用户 %s 信息失败:%s", username, err)
		return nil
	}
	if user.Uid == 0 {
		return nil
	}

	// 获取角色信息
	userRoleList, err := model.NewUserRole().Where("uid=" + strconv.Itoa(user.Uid)).FindAll()
	if err != nil {
		logger.Errorf("获取用户 %s 角色 信息失败:%s", username, err)
		return nil
	}
	for _, userRole := range userRoleList {
		if len(user.Roleids) == 0 {
			user.Rolenames = []string{model.AllRole[userRole.Roleid].Name}
		} else {
			user.Rolenames = append(user.Rolenames, model.AllRole[userRole.Roleid].Name)
		}
	}
	return user
}
开发者ID:philsong,项目名称:studygolang,代码行数:27,代码来源:user.go

示例4: CreateUser

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
}
开发者ID:sndnvaps,项目名称:studygolang,代码行数:59,代码来源:user.go

示例5: FindNewUsers

func FindNewUsers(start, num int) []*model.User {
	users, err := model.NewUser().Order("ctime DESC").Limit(strconv.Itoa(start) + "," + strconv.Itoa(num)).FindAll([]string{"uid", "username", "email", "avatar", "ctime"}...)
	if err != nil {
		logger.Errorln("user service FindNewUsers error:", err)
		return nil
	}
	return users
}
开发者ID:philsong,项目名称:studygolang,代码行数:8,代码来源:user.go

示例6: ChangeAvatar

// 更换头像
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
}
开发者ID:NeoChow,项目名称:studygolang,代码行数:9,代码来源:user.go

示例7: FindUserByEmail

// 通过邮箱获取用户信息
func FindUserByEmail(email string) *model.User {
	user := model.NewUser()
	err := user.Where("email=?", email).Find()
	if err != nil {
		logger.Errorln("FindUserByEmail error:", err)
	}

	return user
}
开发者ID:NeoChow,项目名称:studygolang,代码行数:10,代码来源:user.go

示例8: UpdateUserStatus

// UpdateUserStatus 更新用户状态
func UpdateUserStatus(uid, status int) {
	setClause := "status=" + strconv.Itoa(status)
	err := model.NewUser().Set(setClause).Where("uid=?", uid).Update()
	if err != nil {
		logger.Errorf("更新用户 【%s】 状态失败:%s", uid, err)
		return
	}

	return
}
开发者ID:bluefchen,项目名称:studygolang,代码行数:11,代码来源:user.go

示例9: UpdateUser

func UpdateUser(form url.Values) (errMsg string, err error) {
	fields := []string{"name", "open", "city", "company", "github", "weibo", "website", "status", "introduce"}
	setClause := GenSetClause(form, fields)
	username := form.Get("username")
	err = model.NewUser().Set(setClause).Where("username=" + username).Update()
	if err != nil {
		logger.Errorf("更新用户 【%s】 信息失败:%s", username, err)
		errMsg = "对不起,服务器内部错误,请稍后再试!"
		return
	}
	return
}
开发者ID:jimmykuu,项目名称:studygolang,代码行数:12,代码来源:user.go

示例10: FindUsernameByUid

// 通过UID获取用户名
func FindUsernameByUid(uid int) string {
	user := model.NewUser()
	err := user.Where("uid=" + strconv.Itoa(uid)).Find()
	if err != nil {
		logger.Errorf("获取用户 %s 信息失败:%s", uid, err)
		return ""
	}
	if user.Uid == 0 {
		return ""
	}

	return user.Username
}
开发者ID:NeoChow,项目名称:studygolang,代码行数:14,代码来源:user.go

示例11: getUserInfos

// 获取用户信息
func getUserInfos(uids map[int]int) map[int]*model.User {
	// 获取用户信息
	inUids := util.Join(util.MapIntKeys(uids), ",")
	users, err := model.NewUser().Where("uid in(" + inUids + ")").FindAll()
	if err != nil {
		logger.Errorln("user service getUserInfos Error:", err)
		return map[int]*model.User{}
	}
	userMap := make(map[int]*model.User, len(users))
	for _, user := range users {
		userMap[user.Uid] = user
	}
	return userMap
}
开发者ID:sndnvaps,项目名称:studygolang,代码行数:15,代码来源:user.go

示例12: UpdateUserReminder

func UpdateUserReminder(form url.Values) (errMsg string, err error) {
	fields := []string{"lowestPrice", "highestPrice", "Emailnotice"}
	setClause := GenSetClause(form, fields)
	username := form.Get("username")
	err = model.NewUser().Set(setClause).Where("username=" + username).Update()
	if err != nil {
		logger.Errorf("更新用户 【%s】 reminder信息失败:%s", username, err)
		errMsg = "对不起,服务器内部错误,请稍后再试!"
		return
	}

	// 修改用户资料,活跃度+1
	go IncUserWeight("username="+username, 1)

	return
}
开发者ID:jackyxie,项目名称:btcrobot,代码行数:16,代码来源:user.go

示例13: parseAtUser

// @某人
func parseAtUser(content string) string {
	user := model.NewUser()

	reg := regexp.MustCompile(`@([^\[email protected]]{4,20})`)
	return reg.ReplaceAllStringFunc(content, func(matched string) string {
		username := matched[1:]

		// 校验 username 是否存在
		err := user.Where("username=?", username).Find()
		if err != nil {
			return matched
		}

		if user.Username != username {
			return matched
		}
		return fmt.Sprintf(`<a href="/user/%s" title="%s">%s</a>`, username, matched, matched)
	})
}
开发者ID:bluefchen,项目名称:studygolang,代码行数:20,代码来源:common.go

示例14: FindCurrentUser

// 获取当前登录用户信息(常用信息)
func FindCurrentUser(username string) (user map[string]interface{}, err error) {
	userInfo := model.NewUser()
	err = userInfo.Where("username=" + username).Find()
	if err != nil {
		logger.Errorf("获取用户 %s 信息失败:%s", username, err)
		return
	}
	if userInfo.Uid == 0 {
		logger.Infof("用户 %s 不存在!", username)
		return
	}
	user = map[string]interface{}{
		"uid":      userInfo.Uid,
		"username": userInfo.Username,
		"email":    userInfo.Email,
		"avatar":   userInfo.Avatar,
		"status":   userInfo.Status,
	}

	// 获取未读消息数
	user["msgnum"] = FindNotReadMsgNum(userInfo.Uid)

	// 获取角色信息
	userRoleList, err := model.NewUserRole().Where("uid=" + strconv.Itoa(userInfo.Uid)).FindAll()
	if err != nil {
		logger.Errorf("获取用户 %s 角色 信息失败:%s", username, err)
		return
	}
	for _, userRole := range userRoleList {
		if userRole.Roleid <= model.AdminMinRoleId {
			// 是管理员
			user["isadmin"] = true
		}
	}

	RecordLoginTime(username)

	return
}
开发者ID:NeoChow,项目名称:studygolang,代码行数:40,代码来源:user.go

示例15: FindUsersByPage

func FindUsersByPage(conds map[string]string, curPage, limit int) ([]*model.User, int) {
	conditions := make([]string, 0, len(conds))
	for k, v := range conds {
		conditions = append(conditions, k+"="+v)
	}

	user := model.NewUser()

	limitStr := strconv.Itoa((curPage-1)*limit) + "," + strconv.Itoa(limit)
	userList, err := user.Where(strings.Join(conditions, " AND ")).Limit(limitStr).
		FindAll()
	if err != nil {
		logger.Errorln("user service FindUsersByPage Error:", err)
		return nil, 0
	}

	total, err := user.Count()
	if err != nil {
		logger.Errorln("user service FindUsersByPage COUNT Error:", err)
		return nil, 0
	}

	return userList, total
}
开发者ID:NeoChow,项目名称:studygolang,代码行数:24,代码来源:user.go


注:本文中的Model.NewUser函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。