mirror of
				https://github.com/linux-do/new-api.git
				synced 2025-11-04 13:23:42 +08:00 
			
		
		
		
	Merge remote-tracking branch 'upstream/main'
This commit is contained in:
		@@ -22,6 +22,24 @@ func UpdateUserUsableGroupsByJSONString(jsonStr string) error {
 | 
				
			|||||||
	return json.Unmarshal([]byte(jsonStr), &UserUsableGroups)
 | 
						return json.Unmarshal([]byte(jsonStr), &UserUsableGroups)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetUserUsableGroups(userGroup string) map[string]string {
 | 
				
			||||||
 | 
						if userGroup == "" {
 | 
				
			||||||
 | 
							// 如果userGroup为空,返回UserUsableGroups
 | 
				
			||||||
 | 
							return UserUsableGroups
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 如果userGroup不在UserUsableGroups中,返回UserUsableGroups + userGroup
 | 
				
			||||||
 | 
						if _, ok := UserUsableGroups[userGroup]; !ok {
 | 
				
			||||||
 | 
							appendUserUsableGroups := make(map[string]string)
 | 
				
			||||||
 | 
							for k, v := range UserUsableGroups {
 | 
				
			||||||
 | 
								appendUserUsableGroups[k] = v
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							appendUserUsableGroups[userGroup] = "用户分组"
 | 
				
			||||||
 | 
							return appendUserUsableGroups
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 如果userGroup在UserUsableGroups中,返回UserUsableGroups
 | 
				
			||||||
 | 
						return UserUsableGroups
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GroupInUserUsableGroups(groupName string) bool {
 | 
					func GroupInUserUsableGroups(groupName string) bool {
 | 
				
			||||||
	_, ok := UserUsableGroups[groupName]
 | 
						_, ok := UserUsableGroups[groupName]
 | 
				
			||||||
	return ok
 | 
						return ok
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"one-api/common"
 | 
						"one-api/common"
 | 
				
			||||||
 | 
						"one-api/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetGroups(c *gin.Context) {
 | 
					func GetGroups(c *gin.Context) {
 | 
				
			||||||
@@ -20,10 +21,14 @@ func GetGroups(c *gin.Context) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func GetUserGroups(c *gin.Context) {
 | 
					func GetUserGroups(c *gin.Context) {
 | 
				
			||||||
	usableGroups := make(map[string]string)
 | 
						usableGroups := make(map[string]string)
 | 
				
			||||||
 | 
						userGroup := ""
 | 
				
			||||||
 | 
						userId := c.GetInt("id")
 | 
				
			||||||
 | 
						userGroup, _ = model.CacheGetUserGroup(userId)
 | 
				
			||||||
	for groupName, _ := range common.GroupRatio {
 | 
						for groupName, _ := range common.GroupRatio {
 | 
				
			||||||
		// UserUsableGroups contains the groups that the user can use
 | 
							// UserUsableGroups contains the groups that the user can use
 | 
				
			||||||
		if _, ok := common.UserUsableGroups[groupName]; ok {
 | 
							userUsableGroups := common.GetUserUsableGroups(userGroup)
 | 
				
			||||||
			usableGroups[groupName] = common.UserUsableGroups[groupName]
 | 
							if _, ok := userUsableGroups[groupName]; ok {
 | 
				
			||||||
 | 
								usableGroups[groupName] = userUsableGroups[groupName]
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	c.JSON(http.StatusOK, gin.H{
 | 
						c.JSON(http.StatusOK, gin.H{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,7 +42,7 @@ func Distribute() func(c *gin.Context) {
 | 
				
			|||||||
		tokenGroup := c.GetString("token_group")
 | 
							tokenGroup := c.GetString("token_group")
 | 
				
			||||||
		if tokenGroup != "" {
 | 
							if tokenGroup != "" {
 | 
				
			||||||
			// check common.UserUsableGroups[userGroup]
 | 
								// check common.UserUsableGroups[userGroup]
 | 
				
			||||||
			if _, ok := common.UserUsableGroups[tokenGroup]; !ok {
 | 
								if _, ok := common.GetUserUsableGroups(userGroup)[tokenGroup]; !ok {
 | 
				
			||||||
				abortWithOpenAiMessage(c, http.StatusForbidden, fmt.Sprintf("令牌分组 %s 已被禁用", tokenGroup))
 | 
									abortWithOpenAiMessage(c, http.StatusForbidden, fmt.Sprintf("令牌分组 %s 已被禁用", tokenGroup))
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -46,6 +46,7 @@ func SetApiRouter(router *gin.Engine) {
 | 
				
			|||||||
			selfRoute := userRoute.Group("/")
 | 
								selfRoute := userRoute.Group("/")
 | 
				
			||||||
			selfRoute.Use(middleware.UserAuth())
 | 
								selfRoute.Use(middleware.UserAuth())
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
 | 
									selfRoute.GET("/self/groups", controller.GetUserGroups)
 | 
				
			||||||
				selfRoute.GET("/self", controller.GetSelf)
 | 
									selfRoute.GET("/self", controller.GetSelf)
 | 
				
			||||||
				selfRoute.GET("/models", controller.GetUserModels)
 | 
									selfRoute.GET("/models", controller.GetUserModels)
 | 
				
			||||||
				selfRoute.PUT("/self", controller.UpdateSelf)
 | 
									selfRoute.PUT("/self", controller.UpdateSelf)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -250,7 +250,7 @@ const LogsTable = () => {
 | 
				
			|||||||
      title: '类型',
 | 
					      title: '类型',
 | 
				
			||||||
      dataIndex: 'type',
 | 
					      dataIndex: 'type',
 | 
				
			||||||
      render: (text, record, index) => {
 | 
					      render: (text, record, index) => {
 | 
				
			||||||
        return <div>{renderType(text)}</div>;
 | 
					        return <>{renderType(text)}</>;
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -258,7 +258,7 @@ const LogsTable = () => {
 | 
				
			|||||||
      dataIndex: 'model_name',
 | 
					      dataIndex: 'model_name',
 | 
				
			||||||
      render: (text, record, index) => {
 | 
					      render: (text, record, index) => {
 | 
				
			||||||
        return record.type === 0 || record.type === 2 ? (
 | 
					        return record.type === 0 || record.type === 2 ? (
 | 
				
			||||||
          <div>
 | 
					          <>
 | 
				
			||||||
            <Tag
 | 
					            <Tag
 | 
				
			||||||
              color={stringToColor(text)}
 | 
					              color={stringToColor(text)}
 | 
				
			||||||
              size='large'
 | 
					              size='large'
 | 
				
			||||||
@@ -269,7 +269,7 @@ const LogsTable = () => {
 | 
				
			|||||||
              {' '}
 | 
					              {' '}
 | 
				
			||||||
              {text}{' '}
 | 
					              {text}{' '}
 | 
				
			||||||
            </Tag>
 | 
					            </Tag>
 | 
				
			||||||
          </div>
 | 
					          </>
 | 
				
			||||||
        ) : (
 | 
					        ) : (
 | 
				
			||||||
          <></>
 | 
					          <></>
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
@@ -282,22 +282,22 @@ const LogsTable = () => {
 | 
				
			|||||||
        if (record.is_stream) {
 | 
					        if (record.is_stream) {
 | 
				
			||||||
          let other = getLogOther(record.other);
 | 
					          let other = getLogOther(record.other);
 | 
				
			||||||
          return (
 | 
					          return (
 | 
				
			||||||
            <div>
 | 
					            <>
 | 
				
			||||||
              <Space>
 | 
					              <Space>
 | 
				
			||||||
                {renderUseTime(text)}
 | 
					                {renderUseTime(text)}
 | 
				
			||||||
                {renderFirstUseTime(other.frt)}
 | 
					                {renderFirstUseTime(other.frt)}
 | 
				
			||||||
                {renderIsStream(record.is_stream)}
 | 
					                {renderIsStream(record.is_stream)}
 | 
				
			||||||
              </Space>
 | 
					              </Space>
 | 
				
			||||||
            </div>
 | 
					            </>
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
          return (
 | 
					          return (
 | 
				
			||||||
            <div>
 | 
					            <>
 | 
				
			||||||
              <Space>
 | 
					              <Space>
 | 
				
			||||||
                {renderUseTime(text)}
 | 
					                {renderUseTime(text)}
 | 
				
			||||||
                {renderIsStream(record.is_stream)}
 | 
					                {renderIsStream(record.is_stream)}
 | 
				
			||||||
              </Space>
 | 
					              </Space>
 | 
				
			||||||
            </div>
 | 
					            </>
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
@@ -307,7 +307,7 @@ const LogsTable = () => {
 | 
				
			|||||||
      dataIndex: 'prompt_tokens',
 | 
					      dataIndex: 'prompt_tokens',
 | 
				
			||||||
      render: (text, record, index) => {
 | 
					      render: (text, record, index) => {
 | 
				
			||||||
        return record.type === 0 || record.type === 2 ? (
 | 
					        return record.type === 0 || record.type === 2 ? (
 | 
				
			||||||
          <div>{<span> {text} </span>}</div>
 | 
					          <>{<span> {text} </span>}</>
 | 
				
			||||||
        ) : (
 | 
					        ) : (
 | 
				
			||||||
          <></>
 | 
					          <></>
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
@@ -319,7 +319,7 @@ const LogsTable = () => {
 | 
				
			|||||||
      render: (text, record, index) => {
 | 
					      render: (text, record, index) => {
 | 
				
			||||||
        return parseInt(text) > 0 &&
 | 
					        return parseInt(text) > 0 &&
 | 
				
			||||||
          (record.type === 0 || record.type === 2) ? (
 | 
					          (record.type === 0 || record.type === 2) ? (
 | 
				
			||||||
          <div>{<span> {text} </span>}</div>
 | 
					          <>{<span> {text} </span>}</>
 | 
				
			||||||
        ) : (
 | 
					        ) : (
 | 
				
			||||||
          <></>
 | 
					          <></>
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
@@ -330,7 +330,7 @@ const LogsTable = () => {
 | 
				
			|||||||
      dataIndex: 'quota',
 | 
					      dataIndex: 'quota',
 | 
				
			||||||
      render: (text, record, index) => {
 | 
					      render: (text, record, index) => {
 | 
				
			||||||
        return record.type === 0 || record.type === 2 ? (
 | 
					        return record.type === 0 || record.type === 2 ? (
 | 
				
			||||||
          <div>{renderQuota(text, 6)}</div>
 | 
					          <>{renderQuota(text, 6)}</>
 | 
				
			||||||
        ) : (
 | 
					        ) : (
 | 
				
			||||||
          <></>
 | 
					          <></>
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -83,7 +83,7 @@ const Playground = () => {
 | 
				
			|||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const loadGroups = async () => {
 | 
					  const loadGroups = async () => {
 | 
				
			||||||
    let res = await API.get(`/api/user/groups`);
 | 
					    let res = await API.get(`/api/user/self/groups`);
 | 
				
			||||||
    const { success, message, data } = res.data;
 | 
					    const { success, message, data } = res.data;
 | 
				
			||||||
    if (success) {
 | 
					    if (success) {
 | 
				
			||||||
      // return data is a map, key is group name, value is group description
 | 
					      // return data is a map, key is group name, value is group description
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -93,7 +93,7 @@ const EditToken = (props) => {
 | 
				
			|||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const loadGroups = async () => {
 | 
					  const loadGroups = async () => {
 | 
				
			||||||
    let res = await API.get(`/api/user/groups`);
 | 
					    let res = await API.get(`/api/user/self/groups`);
 | 
				
			||||||
    const { success, message, data } = res.data;
 | 
					    const { success, message, data } = res.data;
 | 
				
			||||||
    if (success) {
 | 
					    if (success) {
 | 
				
			||||||
      // return data is a map, key is group name, value is group description
 | 
					      // return data is a map, key is group name, value is group description
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user