Beispiel #1
0
/* 删除分类信息 */
func (this *GameController) DeleteCategory() {
	ok, data := func() (bool, interface{}) {
		member := &models.Member{}

		var session interface{}
		if session = this.GetSession("WOKUID"); session == nil {
			return false, "未登录"
		}

		if ok := member.FindOne(session.(string)); !ok {
			return false, "用户不存在"
		}

		// 查找游戏
		game := &models.Game{}
		if _ok := game.FindPath(this.GetString("game")); !_ok {
			return false, "板块不存在"
		}

		// 分类不能少于1
		if game.Categorys <= 1 {
			return false, "至少有1个分类"
		}

		// 检查权限
		if member.Id != game.Manager {
			return false, "需要管理员权限"
		}

		// 如果该分类下有文章,则不能删除
		topic := &models.Topic{}
		if _ok := topic.HasTopic(game.Id, this.GetString("id")); _ok {
			return false, "删除分类前请清空帖子"
		}

		// 根据id删除分类
		gameCategory := &models.GameCategory{}
		if _ok, _data := gameCategory.Delete(game.Id, this.GetString("id")); !_ok {
			return false, _data
		}

		// 游戏分类-1并保存
		game.Categorys--
		game.SaveAll()

		// 删除获取游戏的缓存(刷新game.分类数量和分类信息)
		models.DeleteCache("game-FindPath-" + game.Id)

		return true, nil
	}()

	this.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	this.ServeJson()
}
Beispiel #2
0
/* 管理-基本信息保存 */
func (this *GameController) BaseSave() {
	ok, data := func() (bool, interface{}) {
		member := &models.Member{}

		var session interface{}
		if session = this.GetSession("WOKUID"); session == nil {
			return false, "未登录"
		}

		if _ok := member.FindOne(session.(string)); !_ok {
			return false, "用户不存在"
		}

		game := &models.Game{}
		if _ok := game.FindPath(this.GetString("game")); !_ok {
			return false, "板块不存在"
		}

		if member.Id != game.Manager {
			return false, "没有权限"
		}

		size, _ := this.GetFloat("size")
		version, _ := this.GetFloat("version")
		game.Size = float32(size)
		game.Version = float32(version)
		game.Need = this.GetString("need")
		game.Description = this.GetString("description")
		game.Download = this.GetString("download")
		game.SaveAll() //保存数据

		//刷新缓存
		models.DeleteCache("game-FindPath-" + game.Id)
		return true, nil
	}()

	this.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	this.ServeJson()
}
Beispiel #3
0
/* 新增分类 */
func (this *GameController) AddCategory() {
	ok, data := func() (bool, interface{}) {
		member := &models.Member{}

		var session interface{}
		if session = this.GetSession("WOKUID"); session == nil {
			return false, "未登录"
		}

		if ok := member.FindOne(session.(string)); !ok {
			return false, "用户不存在"
		}

		if len([]rune(this.GetString("name"))) < 1 || len([]rune(this.GetString("name"))) > 10 {
			return false, "名称长度1-10"
		}

		if len([]rune(this.GetString("path"))) < 1 || len([]rune(this.GetString("path"))) > 20 {
			return false, "路径长度1-20"
		}

		re := regexp.MustCompile("[a-z]+")
		result := re.FindString(this.GetString("path"))
		if this.GetString("path") != result {
			return false, "路径只能输入字母"
		}

		//路径名不能等于 admin
		if this.GetString("path") == "admin" {
			return false, "路径不能为admin"
		}

		//路径名不能等于 tag
		if this.GetString("path") == "tag" {
			return false, "路径不能为tag"
		}

		add, _ := this.GetInt("add")
		if add != 0 && add != 1 {
			return false, "发帖限制不在范围内"
		}

		number, _ := this.GetInt("number") //推荐数量
		if number < 0 || number > 20 {
			return false, "推荐数量0-20"
		}

		reply, _ := this.GetInt("reply")
		if reply != 0 && reply != 1 {
			return false, "回帖限制不在范围内"
		}

		pri, _ := this.GetInt("pri")
		if pri < 0 || pri > 20 {
			return false, "优先级0-20"
		}

		_type, _ := this.GetInt("_type")
		if _type < 0 || _type > 1 {
			return false, "类型不在范围内"
		}

		// 查找游戏
		game := &models.Game{}
		if _ok := game.FindPath(this.GetString("game")); !_ok {
			return false, "板块不存在"
		}

		// 检查权限
		if member.Id != game.Manager {
			return false, "需要管理员权限"
		}

		if game.Categorys >= 20 {
			return false, "超过20个模块"
		}

		// 路径是否重复
		gameCategory := &models.GameCategory{}
		if _ok := gameCategory.Find(this.GetString("game"), this.GetString("path")); _ok {
			return false, "路径重复"
		}

		// 游戏分类+1并保存
		game.Categorys++
		game.SaveAll()

		// 新增分类
		gameCategory.Game = game.Id
		gameCategory.Category = this.GetString("path")
		gameCategory.CategoryName = this.GetString("name")
		gameCategory.RecommendPri = pri
		gameCategory.Recommend = number
		gameCategory.Add = add
		gameCategory.Reply = reply
		gameCategory.Type = _type
		gameCategory.Insert()

		// 删除获取游戏的缓存(刷新当前分类数量)
		models.DeleteCache("game-findpath-" + game.Id)

		return true, nil
	}()

	this.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	this.ServeJson()
}
Beispiel #4
0
/* 申请创建游戏 */
func (this *GameController) CreateGame() {
	ok, data := func() (bool, interface{}) {
		member := &models.Member{}

		var session interface{}
		if session = this.GetSession("WOKUID"); session == nil {
			return false, "未登录"
		}

		if ok := member.FindOne(session.(string)); !ok {
			return false, "用户不存在"
		}

		if time.Now().Sub(member.LastTime).Seconds() < 2.9 { //3秒内不能连续操作
			return false, "请继续等待"
		}

		if len([]rune(this.GetString("name"))) < 2 || len([]rune(this.GetString("name"))) > 20 { //名称不能超过10个字符
			return false, "名称长度2-20"
		}

		if this.GetString("name") == "" {
			return false, "名称不能为空"
		}

		if len([]rune(this.GetString("path"))) < 3 || len([]rune(this.GetString("path"))) > 20 { //路径不能超过10个字符
			return false, "域名长度3-20"
		}

		if this.GetString("path") == "" {
			return false, "域名不能为空"
		}

		if this.GetString("path") == "post" || this.GetString("path") == "api" {
			return false, "该域名不能使用"
		}

		//正则验证
		re := regexp.MustCompile("[`~!!@#$%^&*()_+<>?:\"”{},.,。/;;‘'[\\]]")
		result := re.FindString(this.GetString("name"))
		if result != "" {
			return false, "名称不能有特殊字符"
		}

		re = regexp.MustCompile("[a-z]+")
		result = re.FindString(this.GetString("path"))
		if this.GetString("path") != result {
			return false, "域名只能输入字母"
		}

		game := &models.Game{}
		//查询名称是否重复
		if game.FindRepeatName(this.GetString("name")) {
			return false, "名称已存在"
		}

		//查询域名是否重复
		if game.FindRepeat(this.GetString("path")) {
			return false, "域名已存在"
		}

		//检测type只能是0~4
		_type, _ := this.GetInt("type")
		if _type != 0 && _type != 1 && _type != 2 && _type != 3 && _type != 4 {
			return false, "分类不在范围内"
		}

		//一个用户最多建立20个游戏
		if member.GameNumber >= 20 {
			return false, "抱歉,您最多创建20个游戏"
		}

		member.GameNumber++
		member.Save()

		//注册新游戏
		game.Name = this.GetString("name")
		game.Id = this.GetString("path")
		game.Type = uint8(_type)
		game.Manager = member.Id
		game.Categorys = 3
		game.Insert()

		//给游戏注册默认分类
		defaultCategory := map[string]string{
			"topic": "公告",
			"plan":  "攻略",
			"bbs":   "论坛",
		}

		// 存储层级
		layer := 0

		for k, _ := range defaultCategory {
			gameCategory := &models.GameCategory{}
			gameCategory.Game = game.Id
			gameCategory.Category = k
			gameCategory.CategoryName = defaultCategory[k]
			gameCategory.RecommendPri = layer
			gameCategory.Recommend = 5
			gameCategory.Add = 1
			gameCategory.Reply = 1
			gameCategory.Insert()

			layer++
		}

		// 删除此游戏查询缓存
		models.DeleteCache("game-findpath-" + this.GetString("path"))

		// 删除查询游戏列表缓存
		models.DeleteCaches("game-find-" + this.GetString("type"))
		models.DeleteCaches("game-findcount-" + this.GetString("type"))

		return true, nil
	}()

	this.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	this.ServeJson()
}
Beispiel #5
0
/* 上传图片 */
func (this *GameController) UploadHandle() {
	ok, data := func() (bool, interface{}) {
		member := &models.Member{}

		var session interface{}
		if session = this.GetSession("WOKUID"); session == nil {
			return false, "未登录"
		}

		if ok := member.FindOne(session.(string)); !ok {
			return false, "用户不存在"
		}

		game := &models.Game{}
		if ok := game.FindPath(this.GetString("game")); !ok {
			return false, "板块不存在"
		}

		// 获取图片信息
		qiniu := &QiniuController{}
		__ok, info := qiniu.GetInfo("woku", this.GetString("name"))
		if !__ok {
			return false, "文件不存在"
		}

		var remotePath string

		//根据类别检查权限
		switch this.GetString("type") {
		case "gameImage": //应用图标
			if member.Id != game.Manager {
				return false, "没有权限"
			}

			//如果已有图片,删除旧图片
			if game.GameImage != "" {
				qiniu.DeleteFile("woku", game.GameImage)
			} else { //活跃度加2
				game.Hot += 2
			}

			game.GameImage = "game/" + game.Id + "/admin/image" + this.GetString("ext")
			//保存游戏信息
			game.SaveAll()

			// 移动文件
			qiniu.MoveFile("woku", this.GetString("name"), "woku", game.GameImage)

			remotePath = game.GameImage
		case "gameIcon": //应用ico
			if member.Id != game.Manager {
				return false, "没有权限"
			}

			//如果已有图片,删除旧图片
			if game.Icon != "" {
				qiniu.DeleteFile("woku", game.Icon)
			} else { //活跃度加2
				game.Hot += 2
			}

			game.Icon = "game/" + game.Id + "/admin/icon" + this.GetString("ext")
			//保存游戏信息
			game.SaveAll()

			// 移动文件
			qiniu.MoveFile("woku", this.GetString("name"), "woku", game.Icon)

			remotePath = game.Icon
		case "gameScreenShot": //截图展示
			if member.Id != game.Manager {
				return false, "没有权限"
			}

			//获取插入位置
			position, _ := this.GetInt("position")
			if position < 0 || position >= 6 { //不在范围内
				return false, "截图序号错误,不在范围内"
			}

			// 设置移动后路径
			remotePath = "game/" + game.Id + "/admin/screenShot" + this.GetString("position") + this.GetString("ext")

			//如果超过长度,增加
			if int(position) >= len(game.Image) {
				game.Image = append(game.Image, remotePath)
				game.Hot += 2
			} else {
				//如果已有图片,删除旧图片
				if game.Image[position] != "" {
					qiniu.DeleteFile("woku", game.Image[position])
				} else { //没有图片
					game.Hot += 2
				}
				//保存新图片信息
				game.Image[position] = remotePath
			}

			//移动图片
			qiniu.MoveFile("woku", this.GetString("name"), "woku", remotePath)

			//保存游戏信息
			game.SaveAll()
		default: //请求类型不在范围内
			return false, "无效的请求"
		}

		//清空game缓存
		models.DeleteCache("game-findpath-" + game.Id)

		//如果用户最后上传日期不等于今天,重新赋值
		if member.UploadDate.Year() < time.Now().Year() || member.UploadDate.YearDay() < time.Now().YearDay() { //天数小于今天
			member.UploadDate = time.Now()
			member.UploadSize = info.Fsize
		} else { //是今天则累加
			member.UploadSize += info.Fsize
		}
		member.Save()

		return true, remotePath
	}()

	this.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	this.ServeJson()
}
Beispiel #6
0
/* 更新分类信息 */
func (this *GameController) UpdateCategory() {
	ok, data := func() (bool, interface{}) {
		member := &models.Member{}

		var session interface{}
		if session = this.GetSession("WOKUID"); session == nil {
			return false, "未登录"
		}

		if ok := member.FindOne(session.(string)); !ok {
			return false, "用户不存在"
		}

		if len([]rune(this.GetString("name"))) < 1 || len([]rune(this.GetString("name"))) > 10 {
			return false, "名称长度1-10"
		}

		if len([]rune(this.GetString("path"))) < 1 || len([]rune(this.GetString("path"))) > 20 {
			return false, "路径长度1-20"
		}

		re := regexp.MustCompile("[a-z]+")
		result := re.FindString(this.GetString("path"))
		if this.GetString("path") != result {
			return false, "路径只能输入字母"
		}

		//路径名不能等于 admin
		if this.GetString("path") == "admin" {
			return false, "路径不能为admin"
		}

		add, _ := this.GetInt("add")
		if add != 0 && add != 1 {
			return false, "发帖限制不在范围内"
		}

		number, _ := this.GetInt("number") //推荐数量
		if number < 0 || number > 20 {
			return false, "推荐数量0-20"
		}

		reply, _ := this.GetInt("reply")
		if reply != 0 && reply != 1 {
			return false, "回帖限制不在范围内"
		}

		// 查找游戏
		game := &models.Game{}
		if _ok := game.FindPath(this.GetString("game")); !_ok {
			return false, "板块不存在"
		}

		// 检查权限
		if member.Id != game.Manager {
			return false, "需要管理员权限"
		}

		// 路径是否重复
		gameCategory := &models.GameCategory{}
		if _ok := gameCategory.Find(game.Id, this.GetString("path")); _ok {
			// 路径重复,并且不是当前修改的路径
			if gameCategory.Id.Hex() != this.GetString("id") {
				return false, "路径重复"
			}
		}

		_type, _ := this.GetInt("_type")
		if _type < 0 || _type > 1 {
			return false, "类型不在范围内"
		}

		// 查询分类信息
		if _ok := gameCategory.Find(game.Id, this.GetString("id")); !_ok {
			return false, "分类不存在"
		}

		// 切换分类需要清空其下文章
		if gameCategory.Type != _type {
			topic := &models.Topic{}
			if _ok := topic.HasTopic(game.Id, gameCategory.Id.Hex()); _ok {
				return false, "修改分类前请清空文章"
			}
		}

		// 根据id更新分类
		if _ok, _data := gameCategory.Update(this.GetString("id"), this.GetString("path"), this.GetString("name"), number, add, reply, _type); !_ok {
			return false, _data
		}

		// 删除获取游戏的缓存(刷新game.分类数量和分类信息)
		models.DeleteCache("game-FindPath-" + game.Id)

		return true, nil
	}()

	this.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	this.ServeJson()
}