Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
func SaveRule(form url.Values, opUser string) (errMsg string, err error) {
	rule := model.NewCrawlRule()
	err = util.ConvertAssign(rule, form)
	if err != nil {
		logger.Errorln("rule ConvertAssign error", err)
		errMsg = err.Error()
		return
	}

	rule.OpUser = opUser

	if rule.Id != 0 {
		err = rule.Persist(rule)
	} else {
		_, err = rule.Insert()
	}

	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln("rule save:", errMsg, ":", err)
		return
	}

	return
}
Ejemplo n.º 3
0
func PublishDynamic(form url.Values) error {
	dynamic := model.NewDynamic()

	util.ConvertAssign(dynamic, form)
	_, err := dynamic.Insert()
	return err
}
Ejemplo n.º 4
0
func SaveAuthority(form url.Values, opUser string) (errMsg string, err error) {
	authority := model.NewAuthority()
	err = util.ConvertAssign(authority, form)
	if err != nil {
		logger.Errorln("authority ConvertAssign error", err)
		errMsg = err.Error()
		return
	}

	authority.OpUser = opUser

	if authority.Aid != 0 {
		err = authority.Persist(authority)
	} else {
		authority.Ctime = util.TimeNow()

		_, err = authority.Insert()
	}

	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln(errMsg, ":", err)
		return
	}

	global.AuthorityChan <- struct{}{}

	return
}
Ejemplo n.º 5
0
// 保存晨读
func SaveReading(form url.Values, username string) (errMsg string, err error) {
	reading := model.NewMorningReading()
	err = util.ConvertAssign(reading, form)
	if err != nil {
		logger.Errorln("reading SaveReading error", err)
		errMsg = err.Error()
		return
	}

	reading.Moreurls = strings.TrimSpace(reading.Moreurls)
	if strings.Contains(reading.Moreurls, "\n") {
		reading.Moreurls = strings.Join(strings.Split(reading.Moreurls, "\n"), ",")
	}

	reading.Username = username

	logger.Infoln(reading.Rtype, "id=", reading.Id)
	if reading.Id != 0 {
		err = reading.Persist(reading)
	} else {
		_, err = reading.Insert()
	}

	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln("reading save:", errMsg, ":", err)
		return
	}

	return
}
Ejemplo n.º 6
0
// 增加资源
func PublishResource(uid int, form url.Values) bool {
	resource := model.NewResource()
	err := util.ConvertAssign(resource, form)
	if err != nil {
		logger.Errorln("user ConvertAssign error", err)
		return false
	}
	resource.Ctime = time.Now().Format("2006-01-02 15:04:05")
	resource.Uid = uid
	id, err := resource.Insert()
	if err != nil {
		logger.Errorln("PublishResource error:", err)
		return false
	}

	// 发布资源,活跃度+10
	go IncUserWeight("uid="+strconv.Itoa(uid), 10)

	// 入扩展表
	resourceEx := model.NewResourceEx()
	resourceEx.Id = int(id)
	if _, err := resourceEx.Insert(); err != nil {
		logger.Errorln("PublishResource Ex error:", err)
		return false
	}

	return true
}
Ejemplo n.º 7
0
// 创建一个wiki页面
func CreateWiki(uid int, form url.Values) bool {
	wiki := model.NewWiki()
	err := util.ConvertAssign(wiki, form)
	if err != nil {
		logger.Errorln("wiki ConvertAssign error", err)
		return false
	}
	wiki.Uid = uid
	if _, err := wiki.Insert(); err != nil {
		logger.Errorln("wiki service CreateWiki error:", err)
		return false
	}

	// 创建一个wiki页面,活跃度+10
	go IncUserWeight("uid="+strconv.Itoa(uid), 10)

	return true
}
Ejemplo n.º 8
0
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
}
Ejemplo n.º 9
0
// 增加(修改)资源
func PublishResource(user map[string]interface{}, form url.Values) (err error) {
	uid := user["uid"].(int)

	resource := model.NewResource()

	if form.Get("id") != "" {
		err = resource.Where("id=?", form.Get("id")).Find()
		if err != nil {
			logger.Errorln("Publish Resource find error:", err)
			return
		}

		isAdmin := false
		if _, ok := user["isadmin"]; ok {
			isAdmin = user["isadmin"].(bool)
		}
		if resource.Uid != uid && !isAdmin {
			err = NotModifyAuthorityErr
			return
		}

		fields := []string{"title", "catid", "form", "url", "content"}
		if form.Get("form") == model.LinkForm {
			form.Set("content", "")
		} else {
			form.Set("url", "")
		}

		id := form.Get("id")
		query, args := updateSetClause(form, fields)
		err = resource.Set(query, args...).Where("id=?", id).Update()
		if err != nil {
			logger.Errorf("更新資源 【%s】 信息失败:%s\n", id, err)
			return
		}

		// 修改資源,活跃度+2
		go IncUserWeight("uid="+strconv.Itoa(uid), 2)
	} else {

		util.ConvertAssign(resource, form)

		resource.Uid = uid
		resource.Ctime = util.TimeNow()

		var id int64
		id, err = resource.Insert()

		if err != nil {
			logger.Errorln("Publish Resource error:", err)
			return
		}

		// 存扩展信息
		resourceEx := model.NewResourceEx()
		resourceEx.Id = int(id)
		if _, err = resourceEx.Insert(); err != nil {
			logger.Errorln("PublishResource Ex error:", err)
			return
		}

		// 给 被@用户 发系统消息
		/*
			ext := map[string]interface{}{
				"objid":   id,
				"objtype": model.TYPE_RESOURCE,
				"uid":     user["uid"],
				"msgtype": model.MsgtypePublishAtMe,
			}
			go SendSysMsgAtUsernames(form.Get("usernames"), ext)
		*/

		// 发布主题,活跃度+10
		go IncUserWeight("uid="+strconv.Itoa(uid), 10)
	}

	return
}
Ejemplo n.º 10
0
func PublishProject(user map[string]interface{}, form url.Values) (err error) {
	id := form.Get("id")
	isModify := id != ""

	if !isModify && ProjectUriExists(form.Get("uri")) {
		err = errors.New("uri存在")
		return
	}

	username := user["username"].(string)

	project := model.NewOpenProject()

	if isModify {
		err = project.Where("id=?", id).Find()
		if err != nil {
			logger.Errorln("Publish Project find error:", err)
			return
		}
		isAdmin := false
		if _, ok := user["isadmin"]; ok {
			isAdmin = user["isadmin"].(bool)
		}
		if project.Username != username && !isAdmin {
			err = NotModifyAuthorityErr
			return
		}

		util.ConvertAssign(project, form)
	} else {
		util.ConvertAssign(project, form)

		project.Username = username
		project.Ctime = util.TimeNow()
	}

	project.Uri = strings.ToLower(project.Uri)

	github := "github.com"
	pos := strings.Index(project.Src, github)
	if pos != -1 {
		project.Repo = project.Src[pos+len(github)+1:]
	}

	if !isModify {
		_, err = project.Insert()
	} else {
		err = project.Persist(project)
	}

	if err != nil {
		logger.Errorln("Publish Project error:", err)
	}

	// 发布項目,活跃度+10
	if uid, ok := user["uid"].(int); ok {
		weight := 10
		if isModify {
			weight = 2
		}
		go IncUserWeight("uid="+strconv.Itoa(uid), weight)
	}

	return
}
Ejemplo n.º 11
0
// 发布主题。入topics和topics_ex库
func PublishTopic(user map[string]interface{}, form url.Values) (err error) {
	uid := user["uid"].(int)

	topic := model.NewTopic()

	if form.Get("tid") != "" {
		err = topic.Where("tid=?", form.Get("tid")).Find()
		if err != nil {
			logger.Errorln("Publish Topic find error:", err)
			return
		}

		isAdmin := false
		if _, ok := user["isadmin"]; ok {
			isAdmin = user["isadmin"].(bool)
		}
		if topic.Uid != uid && !isAdmin {
			err = NotModifyAuthorityErr
			return
		}

		_, err = ModifyTopic(user, form)
		if err != nil {
			logger.Errorln("Publish Topic error:", err)
			return
		}
	} else {

		util.ConvertAssign(topic, form)

		topic.Uid = uid
		topic.Ctime = util.TimeNow()

		var tid int
		tid, err = topic.Insert()

		if err != nil {
			logger.Errorln("Publish Topic error:", err)
			return
		}

		// 存扩展信息
		topicEx := model.NewTopicEx()
		topicEx.Tid = tid
		_, err = topicEx.Insert()
		if err != nil {
			logger.Errorln("Insert TopicEx error:", err)
			return
		}

		// 给 被@用户 发系统消息
		ext := map[string]interface{}{
			"objid":   tid,
			"objtype": model.TYPE_TOPIC,
			"uid":     user["uid"],
			"msgtype": model.MsgtypePublishAtMe,
		}
		go SendSysMsgAtUsernames(form.Get("usernames"), ext)

		// 发布主题,活跃度+10
		go IncUserWeight("uid="+strconv.Itoa(uid), 10)
	}

	return
}