|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "one-api/common" |
| 7 | + "one-api/model" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + // "strings" |
| 11 | + |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/xuri/excelize/v2" |
| 14 | +) |
| 15 | + |
| 16 | +func ImportChannels(c *gin.Context) { |
| 17 | + // 从请求中获取上传的文件 |
| 18 | + file, err := c.FormFile("file") |
| 19 | + if err != nil { |
| 20 | + common.ApiError(c, fmt.Errorf("failed to get file: %w", err)) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + // 打开文件 |
| 25 | + src, err := file.Open() |
| 26 | + if err != nil { |
| 27 | + common.ApiError(c, fmt.Errorf("failed to open file: %w", err)) |
| 28 | + return |
| 29 | + } |
| 30 | + defer src.Close() |
| 31 | + |
| 32 | + // 读取Excel文件 |
| 33 | + f, err := excelize.OpenReader(src) |
| 34 | + if err != nil { |
| 35 | + common.ApiError(c, fmt.Errorf("failed to open Excel file: %w", err)) |
| 36 | + return |
| 37 | + } |
| 38 | + defer func() { |
| 39 | + if err := f.Close(); err != nil { |
| 40 | + common.SysError("Error closing Excel file: " + err.Error()) |
| 41 | + } |
| 42 | + }() |
| 43 | + |
| 44 | + // 获取第一个工作表 |
| 45 | + sheetName := f.GetSheetName(0) |
| 46 | + if sheetName == "" { |
| 47 | + common.ApiError(c, fmt.Errorf("no sheets found in Excel file")) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // 读取所有行 |
| 52 | + rows, err := f.GetRows(sheetName) |
| 53 | + if err != nil { |
| 54 | + common.ApiError(c, fmt.Errorf("failed to read rows: %w", err)) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + // 检查是否有数据 |
| 59 | + if len(rows) <= 1 { |
| 60 | + common.ApiError(c, fmt.Errorf("no data found in Excel file")) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + // 解析表头 |
| 65 | + headers := rows[0] |
| 66 | + headerMap := make(map[string]int) |
| 67 | + for i, header := range headers { |
| 68 | + headerMap[header] = i |
| 69 | + } |
| 70 | + |
| 71 | + // 解析数据行 |
| 72 | + channels := make([]model.Channel, 0, len(rows)-1) |
| 73 | + for i := 1; i < len(rows); i++ { |
| 74 | + row := rows[i] |
| 75 | + if len(row) == 0 { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + // 创建渠道对象 |
| 80 | + channel := model.Channel{} |
| 81 | + |
| 82 | + // 根据表头映射填充数据 |
| 83 | + for header, colIndex := range headerMap { |
| 84 | + if colIndex >= len(row) { |
| 85 | + continue |
| 86 | + } |
| 87 | + value := row[colIndex] |
| 88 | + |
| 89 | + switch header { |
| 90 | + case "ID": |
| 91 | + // ID由数据库自动生成,不需要设置 |
| 92 | + case "名称": |
| 93 | + channel.Name = value |
| 94 | + case "类型": |
| 95 | + if v, err := strconv.Atoi(value); err == nil { |
| 96 | + channel.Type = v |
| 97 | + } |
| 98 | + case "状态": |
| 99 | + if v, err := strconv.Atoi(value); err == nil { |
| 100 | + channel.Status = v |
| 101 | + } |
| 102 | + case "密钥": |
| 103 | + channel.Key = value |
| 104 | + case "组织": |
| 105 | + if value != "" { |
| 106 | + channel.OpenAIOrganization = &value |
| 107 | + } |
| 108 | + case "测试模型": |
| 109 | + if value != "" { |
| 110 | + channel.TestModel = &value |
| 111 | + } |
| 112 | + case "权重": |
| 113 | + if value != "" { |
| 114 | + if v, err := strconv.ParseUint(value, 10, 32); err == nil { |
| 115 | + weight := uint(v) |
| 116 | + channel.Weight = &weight |
| 117 | + } |
| 118 | + } |
| 119 | + case "创建时间": |
| 120 | + if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 121 | + channel.CreatedTime = v |
| 122 | + } |
| 123 | + case "测试时间": |
| 124 | + if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 125 | + channel.TestTime = v |
| 126 | + } |
| 127 | + case "响应时间": |
| 128 | + if v, err := strconv.Atoi(value); err == nil { |
| 129 | + channel.ResponseTime = v |
| 130 | + } |
| 131 | + case "基础URL": |
| 132 | + if value != "" { |
| 133 | + channel.BaseURL = &value |
| 134 | + } |
| 135 | + case "其他": |
| 136 | + channel.Other = value |
| 137 | + case "余额": |
| 138 | + if v, err := strconv.ParseFloat(value, 64); err == nil { |
| 139 | + channel.Balance = v |
| 140 | + } |
| 141 | + case "余额更新时间": |
| 142 | + if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 143 | + channel.BalanceUpdatedTime = v |
| 144 | + } |
| 145 | + case "模型": |
| 146 | + channel.Models = value |
| 147 | + case "分组": |
| 148 | + channel.Group = value |
| 149 | + case "已用配额": |
| 150 | + if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 151 | + channel.UsedQuota = v |
| 152 | + } |
| 153 | + case "模型映射": |
| 154 | + if value != "" { |
| 155 | + channel.ModelMapping = &value |
| 156 | + } |
| 157 | + case "状态码映射": |
| 158 | + if value != "" { |
| 159 | + channel.StatusCodeMapping = &value |
| 160 | + } |
| 161 | + case "优先级": |
| 162 | + if value != "" { |
| 163 | + if v, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 164 | + channel.Priority = &v |
| 165 | + } |
| 166 | + } |
| 167 | + case "自动禁用": |
| 168 | + if value != "" { |
| 169 | + if v, err := strconv.Atoi(value); err == nil { |
| 170 | + channel.AutoBan = &v |
| 171 | + } |
| 172 | + } |
| 173 | + case "标签": |
| 174 | + if value != "" { |
| 175 | + channel.Tag = &value |
| 176 | + } |
| 177 | + case "额外设置": |
| 178 | + if value != "" { |
| 179 | + channel.Setting = &value |
| 180 | + } |
| 181 | + case "参数覆盖": |
| 182 | + if value != "" { |
| 183 | + channel.ParamOverride = &value |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // 设置默认值 |
| 189 | + if channel.CreatedTime == 0 { |
| 190 | + channel.CreatedTime = common.GetTimestamp() |
| 191 | + } |
| 192 | + |
| 193 | + // 如果没有设置状态,默认为启用 |
| 194 | + if channel.Status == 0 { |
| 195 | + channel.Status = 1 |
| 196 | + } |
| 197 | + |
| 198 | + channels = append(channels, channel) |
| 199 | + } |
| 200 | + |
| 201 | + // 批量插入渠道 |
| 202 | + err = model.BatchInsertChannels(channels) |
| 203 | + if err != nil { |
| 204 | + common.ApiError(c, fmt.Errorf("failed to insert channels: %w", err)) |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + // 初始化渠道缓存 |
| 209 | + model.InitChannelCache() |
| 210 | + |
| 211 | + // 返回成功响应 |
| 212 | + c.JSON(http.StatusOK, gin.H{ |
| 213 | + "success": true, |
| 214 | + "message": fmt.Sprintf("成功导入 %d 个渠道", len(channels)), |
| 215 | + }) |
| 216 | +} |
0 commit comments