Module: JPSClient::API::Category
- Included in:
- Client
- Defined in:
- lib/jpsclient/api/category.rb
Overview
Category 相关 API 处理通用分类管理相关接口
Instance Method Summary collapse
-
#batch_create_categories(categories: []) ⇒ Array<Hash>
批量创建分类 注意:此方法会逐个调用 API,返回结果数组.
-
#create_category(params: {}) ⇒ Hash
创建分类.
-
#delete_category(id:) ⇒ Hash
删除分类.
-
#disable_category(category_id:) ⇒ Hash
停用分类.
-
#enable_category(category_id:) ⇒ Hash
启用分类.
-
#get_categories_by_type(type:, parent_id: nil) ⇒ Hash
按类型获取分类列表(便捷方法).
-
#get_category_list(params: {}) ⇒ Hash
获取分类列表.
-
#get_category_path(category_id:) ⇒ Array<Hash>
构建分类路径 根据分类ID获取从根到该分类的完整路径.
-
#get_category_statistics(category_id:) ⇒ Hash
获取分类统计信息 统计某个分类下的直接子分类数量.
-
#get_category_tree(params: {}) ⇒ Hash
获取分类树形结构.
-
#get_root_categories(type: nil) ⇒ Hash
获取根分类列表.
-
#get_subcategories(parent_id:) ⇒ Hash
获取某分类的所有子分类.
-
#move_category(category_id:, new_parent_id: nil) ⇒ Hash
移动分类到新的父分类.
-
#sort_categories(items: []) ⇒ Hash
分类排序.
-
#update_category(params: {}) ⇒ Hash
更新分类.
Instance Method Details
#batch_create_categories(categories: []) ⇒ Array<Hash>
批量创建分类注意:此方法会逐个调用 API,返回结果数组
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/jpsclient/api/category.rb', line 179 def batch_create_categories(categories: []) return [] if categories.empty? results = [] categories.each do |category_params| result = create_category(params: category_params) results << result end results end |
#create_category(params: {}) ⇒ Hash
创建分类
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jpsclient/api/category.rb', line 47 def create_category(params: {}) config = @request_config && @request_config["category_create"] raise JPSClient::ExceptionError, "Missing config for category_create" unless config && config["url"] # 参数验证 required_fields = [:name, :type] missing_fields = required_fields.select { |field| params[field].nil? || params[field].to_s.empty? } if missing_fields.any? raise JPSClient::ExceptionError, "Missing required fields: #{missing_fields.join(', ')}" end # 设置默认值 params[:sort_order] ||= 1 path = config["url"] return request_with_auth(:post, path, body: params) end |
#delete_category(id:) ⇒ Hash
删除分类
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/jpsclient/api/category.rb', line 93 def delete_category(id:) config = @request_config && @request_config["category_delete"] raise JPSClient::ExceptionError, "Missing config for category_delete" unless config && config["url"] path = config["url"] # POST请求,ID作为body参数 params = { id: id } return request_with_auth(:post, path, body: params) end |
#disable_category(category_id:) ⇒ Hash
停用分类
216 217 218 |
# File 'lib/jpsclient/api/category.rb', line 216 def disable_category(category_id:) update_category(params: { id: category_id, status: 0 }) end |
#enable_category(category_id:) ⇒ Hash
启用分类
208 209 210 |
# File 'lib/jpsclient/api/category.rb', line 208 def enable_category(category_id:) update_category(params: { id: category_id, status: 1 }) end |
#get_categories_by_type(type:, parent_id: nil) ⇒ Hash
按类型获取分类列表(便捷方法)
150 151 152 153 154 |
# File 'lib/jpsclient/api/category.rb', line 150 def get_categories_by_type(type:, parent_id: nil) params = { type: type } params[:parent_id] = parent_id if parent_id get_category_list(params: params) end |
#get_category_list(params: {}) ⇒ Hash
获取分类列表
13 14 15 16 17 18 19 20 |
# File 'lib/jpsclient/api/category.rb', line 13 def get_category_list(params: {}) config = @request_config && @request_config["category_list"] raise JPSClient::ExceptionError, "Missing config for category_list" unless config && config["url"] path = config["url"] return request_with_auth(:get, path, params: params) end |
#get_category_path(category_id:) ⇒ Array<Hash>
构建分类路径根据分类ID获取从根到该分类的完整路径
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/jpsclient/api/category.rb', line 225 def get_category_path(category_id:) response = get_category_list # 如果请求失败,返回空数组 return [] unless response && response['data'] categories = response['data']['list'] || response['data'][:list] || [] path = [] current_id = category_id.to_s while current_id category = categories.find { |c| (c['id'] || c[:id]).to_s == current_id } break unless category path.unshift(category) current_id = (category['parent_id'] || category[:parent_id])&.to_s break if current_id == "0" || current_id.nil? end path end |
#get_category_statistics(category_id:) ⇒ Hash
获取分类统计信息统计某个分类下的直接子分类数量
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/jpsclient/api/category.rb', line 254 def get_category_statistics(category_id:) response = get_subcategories(parent_id: category_id) # 如果请求失败,返回空统计 unless response && response['data'] return { category_id: category_id, direct_children_count: 0, children: [] } end child_list = response['data']['list'] || response['data'][:list] || [] { category_id: category_id, direct_children_count: child_list.size, children: child_list } end |
#get_category_tree(params: {}) ⇒ Hash
获取分类树形结构
28 29 30 31 32 33 34 35 |
# File 'lib/jpsclient/api/category.rb', line 28 def get_category_tree(params: {}) config = @request_config && @request_config["category_tree"] raise JPSClient::ExceptionError, "Missing config for category_tree" unless config && config["url"] path = config["url"] return request_with_auth(:get, path, params: params) end |
#get_root_categories(type: nil) ⇒ Hash
获取根分类列表
168 169 170 171 172 |
# File 'lib/jpsclient/api/category.rb', line 168 def get_root_categories(type: nil) params = { parent_id: 0 } # parent_id 为 0 或 null 表示根分类 params[:type] = type if type get_category_list(params: params) end |
#get_subcategories(parent_id:) ⇒ Hash
获取某分类的所有子分类
160 161 162 |
# File 'lib/jpsclient/api/category.rb', line 160 def get_subcategories(parent_id:) get_category_list(params: { parent_id: parent_id }) end |
#move_category(category_id:, new_parent_id: nil) ⇒ Hash
移动分类到新的父分类
196 197 198 199 200 201 202 |
# File 'lib/jpsclient/api/category.rb', line 196 def move_category(category_id:, new_parent_id: nil) params = { id: category_id, parent_id: new_parent_id || 0 } update_category(params: params) end |
#sort_categories(items: []) ⇒ Hash
分类排序
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/jpsclient/api/category.rb', line 112 def sort_categories(items: []) config = @request_config && @request_config["category_sort"] raise JPSClient::ExceptionError, "Missing config for category_sort" unless config && config["url"] if items.empty? raise JPSClient::ExceptionError, "Items array cannot be empty" end # 验证每个项都有必需的字段 items.each_with_index do |item, index| unless item[:id] || item["id"] raise JPSClient::ExceptionError, "Item at index #{index} missing 'id'" end unless item[:sort_order] || item["sort_order"] raise JPSClient::ExceptionError, "Item at index #{index} missing 'sort_order'" end end path = config["url"] params = { items: items } return request_with_auth(:post, path, body: params) end |
#update_category(params: {}) ⇒ Hash
更新分类
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/jpsclient/api/category.rb', line 77 def update_category(params: {}) config = @request_config && @request_config["category_update"] raise JPSClient::ExceptionError, "Missing config for category_update" unless config && config["url"] # ID 是必需的 raise JPSClient::ExceptionError, "Missing required field: id" unless params[:id] path = config["url"] return request_with_auth(:post, path, body: params) end |