Jelajahi Sumber

修改返回

wucan 9 bulan lalu
induk
melakukan
0f92a71f9e

+ 3 - 2
config/app.go

@@ -34,8 +34,9 @@ func App() *ConfigNode {
 		"black_list_table": env("BLACK_LIST_TABLE", "black_list_table:"), //黑名单表
 		"option_key":       env("OPTION_KEY", "option_key:"),             //用户配置表    option_key:openid
 
-		"admin_user":   env("ADMIN_USER", "chunhao"),  //默认管理员
-		"admin_secret": env("ADMIN_SECRET", "123456"), //默认管理密码
+		"admin_user":   env("ADMIN_USER", "chunhao"),    //默认管理员
+		"admin_secret": env("ADMIN_SECRET", "123456"),   //默认管理密码
+		"user_total":   env("USER_TOTAL", "user_total"), //所有用户的集合
 	}
 }
 

+ 16 - 11
controller/v1/blackList.go

@@ -3,9 +3,9 @@ package v1
 import (
 	"context"
 	"designs/app/common/request"
-	"designs/common"
 	"designs/config"
 	"designs/global"
+	"designs/response"
 	"github.com/gin-gonic/gin"
 	"github.com/go-redis/redis/v8"
 	"strconv"
@@ -33,7 +33,7 @@ func AddUserToBlackList(c *gin.Context) {
 	//存在用户 读取数据
 	userdata, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
 	if err != nil {
-		common.RetJson(1003, "用户数据不存在", err.Error(), c)
+		response.Fail(c, 1003, "用户数据不存在"+err.Error())
 		return
 	}
 	curHid := userdata["hid"]
@@ -69,11 +69,13 @@ func AddUserToBlackList(c *gin.Context) {
 	blackListKey := config.Get("app.black_list_table")
 	err = global.App.Redis.ZAdd(context.Background(), blackListKey, &redis.Z{Member: userKey}).Err()
 	if err != nil {
-		common.RetJson(1003, "加入黑名单失败", "参数错误2"+err.Error(), c)
+		response.Fail(c, 1003, "加入黑名单失败"+err.Error())
 		return
 	}
 
-	common.RetJson(0, "加入黑名单成功", "", c)
+	response.Success(c, gin.H{
+		"msg": "加入黑名单成功",
+	})
 }
 
 // DeleteUserFormBlackList 将玩家从从黑名单中去掉
@@ -89,7 +91,7 @@ func DeleteUserFormBlackList(c *gin.Context) {
 	//存在用户 读取数据
 	_, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
 	if err != nil {
-		common.RetJson(1003, "用户数据不存在", err.Error(), c)
+		response.Fail(c, 1003, "用户数据不存在"+err.Error())
 		return
 	}
 
@@ -97,11 +99,12 @@ func DeleteUserFormBlackList(c *gin.Context) {
 	blackListKey := config.Get("app.black_list_table")
 	err = global.App.Redis.ZRem(context.Background(), blackListKey, userKey).Err()
 	if err != nil {
-		common.RetJson(1003, "从黑名单中移除失败", "参数错误2", c)
+		response.Fail(c, 1003, "从黑名单中移除失败"+err.Error())
 		return
 	}
-
-	common.RetJson(0, "从黑名单中移除成功", "", c)
+	response.Success(c, gin.H{
+		"msg": "从黑名单中移除成功",
+	})
 }
 
 // ReadBlackList 查看目前黑名单有哪些人
@@ -110,7 +113,7 @@ func ReadBlackList(c *gin.Context) {
 
 	data, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
 	if err != nil {
-		common.RetJson(1003, "读取黑名单列表失败", "参数错误2", c)
+		response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
 		return
 	}
 
@@ -118,11 +121,13 @@ func ReadBlackList(c *gin.Context) {
 	for _, value := range data {
 		user, err := global.App.Redis.HGetAll(context.Background(), value).Result()
 		if err != nil {
-			common.RetJson(1003, "GetUserData err", "", c)
+			response.Fail(c, 1003, "GetUserData err"+err.Error())
 			return
 		}
 		userData = append(userData, user)
 	}
 
-	common.RetJson(0, "读取黑名单列表成功", userData, c)
+	response.Success(c, gin.H{
+		"msg": "读取黑名单列表成功",
+	})
 }

+ 11 - 6
controller/v1/gameConfig.go

@@ -3,9 +3,9 @@ package v1
 import (
 	"context"
 	"designs/app/common/request"
-	"designs/common"
 	"designs/config"
 	"designs/global"
+	"designs/response"
 	"github.com/gin-gonic/gin"
 )
 
@@ -17,7 +17,7 @@ func AddGidConfig(c *gin.Context) {
 
 	// 在这种情况下,将自动选择合适的绑定
 	if data.AppSecret != config.Get("app.app_secret") {
-		common.RetJson(1003, "fail", "密钥参数错误", c)
+		response.Fail(c, 1003, "密钥参数错误")
 		return
 	}
 
@@ -31,11 +31,13 @@ func AddGidConfig(c *gin.Context) {
 	gidKey := config.Get("app.gid") + form.Gid
 	err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
 	if err != nil {
-		common.RetJson(1003, "fail", "配置错误", c)
+		response.Fail(c, 1003, "配置错误")
 		return
 	}
 
-	common.RetJson(0, "success", gameConfigData, c)
+	response.Success(c, gin.H{
+		"data": gameConfigData,
+	})
 }
 
 /* 获取配置 */
@@ -43,7 +45,7 @@ func GetGidConfig(c *gin.Context) {
 	var data GetGameCfg
 	form := request.Check(c, &data)
 	if form.AppSecret != config.Get("app.app_secret") {
-		common.RetJson(1003, "fail", "密钥不对", c)
+		response.Fail(c, 1003, "密钥不对")
 	}
 
 	gidKey := config.Get("app.gid") + "*"
@@ -53,5 +55,8 @@ func GetGidConfig(c *gin.Context) {
 		res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
 		gameData = append(gameData, res)
 	}
-	common.RetJson(0, "success", gameData, c)
+
+	response.Success(c, gin.H{
+		"data": gameData,
+	})
 }

+ 3 - 2
controller/v1/poster.go

@@ -18,7 +18,7 @@ func AddUserOption(c *gin.Context) {
 		Option string `json:"option" binding:"required"`
 	}{})
 
-	optionKey := config.Get("app.option_key") + form.Gid + ":" + form.OpenId
+	optionKey := config.Get("app.option_key") + form.Gid + ":" + form.Pf + ":" + form.OpenId
 
 	data := map[string]interface{}{
 		"option": form.Option,
@@ -37,9 +37,10 @@ func GetUserOption(c *gin.Context) {
 	form := request.Check(c, &struct {
 		OpenId string `json:"openid" binding:"required"`
 		Gid    string `json:"gid" binding:"required"`
+		Pf     string `json:"pf" binding:"required"`
 	}{})
 
-	optionKey := config.Get("app.option_key") + form.Gid + ":" + form.OpenId
+	optionKey := config.Get("app.option_key") + form.Gid + ":" + form.Pf + ":" + form.OpenId
 	data, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
 	if err != nil {
 		response.Fail(c, 1003, err.Error())

+ 79 - 4
controller/v1/user.go

@@ -1,13 +1,15 @@
 package v1
 
 import (
+	"context"
 	"designs/app/common/request"
 	"designs/app/common/response"
 	"designs/common"
 	"designs/config"
 	"designs/global"
+	"designs/utils"
 	"errors"
-	"fmt"
+	"strings"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -91,7 +93,7 @@ func RefreshToken(c *gin.Context) {
 	token, err0 := common.GetJwtToken(userName, 0)
 	if err0 != nil {
 		global.App.Log.Info("RefreshToken err")
-		common.RetJson(1003, "RefreshToken err", "", c)
+		response.Fail(c, 1003, "RefreshToken err")
 		return
 	}
 
@@ -99,7 +101,10 @@ func RefreshToken(c *gin.Context) {
 		"token": token,
 	}
 	//返回数据
-	common.RetJson(0, "success", data, c)
+	response.Success(c, gin.H{
+		"data": data,
+	})
+
 }
 
 /* 获取时间 */
@@ -108,11 +113,81 @@ func GetSysTime(c *gin.Context) {
 	common.RetJson(0, "获取系统时间", time, c)
 }
 
+type User struct {
+	UserId   string `json:"userId"`
+	Gid      string `json:"gid"`
+	Pf       string `json:"pf"`
+	NickName string `json:"nickName"`
+	Head     string `json:"head"`
+	OpenId   string `json:"openId"`
+	InBlack  bool   `json:"inBlack"`
+	Option   string `json:"option"`
+}
+
 func UserList(c *gin.Context) {
 	form := request.Check(c, &struct {
 		Offset int `form:"offset" json:"offset" binding:""`
 		Limit  int `form:"limit" json:"limit" binding:"required"`
 	}{})
 
-	fmt.Print(form)
+	//读取表
+	userTotalKey := config.Get("app.user_total")
+	data, err := global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, int64(form.Offset), int64(form.Offset+form.Limit)).Result()
+	if err != nil {
+		response.Fail(c, 1001, err.Error())
+		return
+	}
+
+	//读取黑名单
+	blackListKey := config.Get("app.black_list_table")
+	black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
+	if err != nil {
+		response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
+		return
+	}
+
+	var res []User
+	for _, val := range data {
+		userKey := val.Member.(string)
+		userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
+		if err != nil {
+			global.App.Log.Error("GetUserData err")
+			response.Fail(c, 1003, "GetUserData err"+err.Error())
+			return
+		}
+
+		//查看是否在黑名单
+		var inBlack bool
+		if utils.InArray(userKey, black) {
+			inBlack = true
+		}
+
+		userMsg := strings.Split(userKey, ":")
+		openId := userMsg[len(userMsg)-1]
+		gid := userMsg[0]
+		pf := userMsg[1]
+
+		//查看是否加了权限
+		optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
+		option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
+		if err != nil {
+			response.Fail(c, 1003, err.Error())
+			return
+		}
+
+		res = append(res, User{
+			UserId:   userData["userId"],
+			Gid:      userData["gid"],
+			Pf:       userData["pf"],
+			NickName: userData["nickName"],
+			Head:     userData["head"],
+			OpenId:   openId,
+			InBlack:  inBlack,
+			Option:   option["option"],
+		})
+	}
+
+	response.Success(c, gin.H{
+		"data": res,
+	})
 }

+ 9 - 7
middleware/auth.go

@@ -5,6 +5,7 @@ import (
 	"designs/common"
 	"designs/config"
 	"designs/global"
+	"designs/response"
 	"fmt"
 	"net/http"
 	"time"
@@ -32,20 +33,20 @@ func TokenAuthMiddleware() gin.HandlerFunc {
 		if contentLength > config.GetInt64("app.max_content") {
 			// 输出请求体的大小
 			// fmt.Printf("Request body size: %d bytes\n", contentLength)
-			common.RetJson(1003, "ruquest too max", "", c)
+			response.Fail(c, 1003, "ruquest too max")
 			c.Abort()
 			return
 		}
 		token := c.GetHeader("Authorization")
 		//校验token
 		if token == "" {
-			common.RetJson(-2, "Unauthorized", "", c)
+			response.Fail(c, -2, "Unauthorized")
 			c.Abort()
 			return
 		}
 		ok, userName := isValidToken(token)
 		if !ok {
-			common.RetJson(-1, "authorized invalid!", "", c)
+			response.Fail(c, -1, "authorized invalid!")
 			c.Abort()
 			return
 		}
@@ -97,13 +98,14 @@ func RefreshTokenAuthMiddleware() gin.HandlerFunc {
 		token := c.GetHeader("Authorization")
 		//校验token
 		if token == "" {
-			common.RetJson(-2, "Unauthorized", "", c)
+			response.Fail(c, -2, "Unauthorized")
+
 			c.Abort()
 			return
 		}
 		ok, userName := isValidRefreshToken(token)
 		if !ok {
-			common.RetJson(-1, "authorized invalid!", "", c)
+			response.Fail(c, -1, "authorized invalid!")
 			c.Abort()
 			return
 		}
@@ -112,7 +114,7 @@ func RefreshTokenAuthMiddleware() gin.HandlerFunc {
 		key := fmt.Sprintf("%s:%s", config.Get("app.api_limit_key"), apiPath)
 		count, err := global.App.Redis.Incr(context.Background(), key).Result()
 		if err != nil {
-			common.RetJson(1001, "server error!", "", c)
+			response.Fail(c, 1001, "server error!")
 			c.Abort()
 			return
 		}
@@ -120,7 +122,7 @@ func RefreshTokenAuthMiddleware() gin.HandlerFunc {
 			global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
 		}
 		if count > config.GetInt64("app.api_limit_count") {
-			common.RetJson(1002, "too many requests!", "", c)
+			response.Fail(c, 1002, "too many requests!")
 			c.Abort()
 			return
 		}