新增项目、排期与邀请模块,并接入首页路由
将项目管理、排期管理与摄影师邀请流程从占位页迁移为完整实现,包含项目图片 OSS 上传与共享业务模型。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
178
suixinkan/Features/Projects/API/ProjectAPI.swift
Normal file
178
suixinkan/Features/Projects/API/ProjectAPI.swift
Normal file
@ -0,0 +1,178 @@
|
||||
//
|
||||
// ProjectAPI.swift
|
||||
// suixinkan
|
||||
//
|
||||
// Created by Codex on 2026/6/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
/// 项目服务协议,抽象摄影师项目和店铺项目接口以便 ViewModel 测试替换。
|
||||
@MainActor
|
||||
protocol ProjectServing {
|
||||
/// 获取摄影师项目列表。
|
||||
func projectList(scenicId: Int, name: String?, page: Int, pageSize: Int) async throws -> ListPayload<PhotographerProjectItem>
|
||||
|
||||
/// 获取摄影师项目详情。
|
||||
func projectDetail(id: Int) async throws -> PhotographerProjectDetailResponse
|
||||
|
||||
/// 创建摄影师项目。
|
||||
func createProject(_ request: ProjectCreateRequest) async throws
|
||||
|
||||
/// 编辑摄影师项目。
|
||||
func editProject(_ request: ProjectCreateRequest) async throws
|
||||
|
||||
/// 删除摄影师项目。
|
||||
func deleteProject(id: Int) async throws
|
||||
|
||||
/// 获取店铺项目可管理景区。
|
||||
func storeManagerScenicList(userId: String) async throws -> ListPayload<StoreManagerScenicItem>
|
||||
|
||||
/// 获取店铺项目列表。
|
||||
func storeManagerProjectList(userId: String?, page: Int, pageSize: Int) async throws -> ListPayload<PhotographerProjectItem>
|
||||
|
||||
/// 获取店铺项目详情。
|
||||
func storeManagerProjectDetail(id: Int) async throws -> PhotographerProjectDetailResponse
|
||||
|
||||
/// 创建店铺多点位项目。
|
||||
func storeManagerCreate(_ request: StoreManagerCreateRequest) async throws
|
||||
|
||||
/// 创建店铺押金项目。
|
||||
func storeManagerOfflineCreate(_ request: StoreManagerOfflineCreateRequest) async throws
|
||||
|
||||
/// 更新店铺多点位项目。
|
||||
func storeManagerUpdate(_ request: StoreManagerUpdateRequest) async throws
|
||||
|
||||
/// 更新店铺押金项目。
|
||||
func storeManagerOfflineUpdate(_ request: StoreManagerOfflineUpdateRequest) async throws
|
||||
|
||||
/// 删除店铺项目。
|
||||
func storeManagerDeleteProject(id: Int) async throws
|
||||
|
||||
/// 获取全部门店,用于店铺押金项目选择门店。
|
||||
func storeAll() async throws -> ListPayload<StoreItem>
|
||||
}
|
||||
|
||||
/// 项目 API,封装项目管理、店铺项目管理相关接口。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class ProjectAPI: ProjectServing {
|
||||
@ObservationIgnored private let client: APIClient
|
||||
|
||||
/// 初始化项目 API,并注入共享网络客户端。
|
||||
init(client: APIClient) {
|
||||
self.client = client
|
||||
}
|
||||
|
||||
/// 获取摄影师项目列表。
|
||||
func projectList(scenicId: Int, name: String? = nil, page: Int = 1, pageSize: Int = 10) async throws -> ListPayload<PhotographerProjectItem> {
|
||||
var query = [
|
||||
URLQueryItem(name: "scenic_id", value: "\(scenicId)"),
|
||||
URLQueryItem(name: "page", value: "\(max(page, 1))"),
|
||||
URLQueryItem(name: "page_size", value: "\(max(pageSize, 1))")
|
||||
]
|
||||
if let name = name?.trimmingCharacters(in: .whitespacesAndNewlines), !name.isEmpty {
|
||||
query.append(URLQueryItem(name: "name", value: name))
|
||||
}
|
||||
return try await client.send(
|
||||
APIRequest(method: .get, path: "/api/yf-handset-app/photog/project/list", queryItems: query)
|
||||
)
|
||||
}
|
||||
|
||||
/// 获取摄影师项目详情。
|
||||
func projectDetail(id: Int) async throws -> PhotographerProjectDetailResponse {
|
||||
try await client.send(
|
||||
APIRequest(
|
||||
method: .get,
|
||||
path: "/api/yf-handset-app/photog/project/info-view",
|
||||
queryItems: [URLQueryItem(name: "id", value: "\(id)")]
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/// 创建摄影师项目。
|
||||
func createProject(_ request: ProjectCreateRequest) async throws {
|
||||
_ = try await client.send(
|
||||
APIRequest(method: .post, path: "/api/yf-handset-app/photog/project/create", body: request)
|
||||
) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 编辑摄影师项目。
|
||||
func editProject(_ request: ProjectCreateRequest) async throws {
|
||||
_ = try await client.send(
|
||||
APIRequest(method: .post, path: "/api/yf-handset-app/photog/project/edit", body: request)
|
||||
) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 删除摄影师项目。
|
||||
func deleteProject(id: Int) async throws {
|
||||
_ = try await client.send(
|
||||
APIRequest(method: .post, path: "/api/yf-handset-app/photog/project/delete", body: ProjectDeleteRequest(id: id))
|
||||
) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 获取店铺项目可管理景区。
|
||||
func storeManagerScenicList(userId: String) async throws -> ListPayload<StoreManagerScenicItem> {
|
||||
try await client.send(
|
||||
APIRequest(
|
||||
method: .get,
|
||||
path: "/api/app/store-manager/scenic-list",
|
||||
queryItems: [URLQueryItem(name: "user_id", value: userId)]
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/// 获取店铺项目列表。
|
||||
func storeManagerProjectList(userId: String?, page: Int = 1, pageSize: Int = 20) async throws -> ListPayload<PhotographerProjectItem> {
|
||||
var query = [
|
||||
URLQueryItem(name: "page", value: "\(max(page, 1))"),
|
||||
URLQueryItem(name: "page_size", value: "\(max(pageSize, 1))")
|
||||
]
|
||||
if let userId = userId?.trimmingCharacters(in: .whitespacesAndNewlines), !userId.isEmpty {
|
||||
query.append(URLQueryItem(name: "user_id", value: userId))
|
||||
}
|
||||
return try await client.send(
|
||||
APIRequest(method: .get, path: "/api/app/store-manager/list", queryItems: query)
|
||||
)
|
||||
}
|
||||
|
||||
/// 获取店铺项目详情。
|
||||
func storeManagerProjectDetail(id: Int) async throws -> PhotographerProjectDetailResponse {
|
||||
try await client.send(
|
||||
APIRequest(method: .get, path: "/api/app/store-manager/detail", queryItems: [URLQueryItem(name: "id", value: "\(id)")])
|
||||
)
|
||||
}
|
||||
|
||||
/// 创建店铺多点位项目。
|
||||
func storeManagerCreate(_ request: StoreManagerCreateRequest) async throws {
|
||||
_ = try await client.send(APIRequest(method: .post, path: "/api/app/store-manager/create", body: request)) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 创建店铺押金项目。
|
||||
func storeManagerOfflineCreate(_ request: StoreManagerOfflineCreateRequest) async throws {
|
||||
_ = try await client.send(APIRequest(method: .post, path: "/api/app/store-manager/offline-create", body: request)) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 更新店铺多点位项目。
|
||||
func storeManagerUpdate(_ request: StoreManagerUpdateRequest) async throws {
|
||||
_ = try await client.send(APIRequest(method: .post, path: "/api/app/store-manager/update", body: request)) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 更新店铺押金项目。
|
||||
func storeManagerOfflineUpdate(_ request: StoreManagerOfflineUpdateRequest) async throws {
|
||||
_ = try await client.send(APIRequest(method: .post, path: "/api/app/store-manager/offline-update", body: request)) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 删除店铺项目。
|
||||
func storeManagerDeleteProject(id: Int) async throws {
|
||||
_ = try await client.send(
|
||||
APIRequest(method: .post, path: "/api/app/store-manager/delete", body: ProjectDeleteRequest(id: id))
|
||||
) as EmptyPayload
|
||||
}
|
||||
|
||||
/// 获取全部门店。
|
||||
func storeAll() async throws -> ListPayload<StoreItem> {
|
||||
try await client.send(APIRequest(method: .get, path: "/api/app/store/all"))
|
||||
}
|
||||
}
|
||||
440
suixinkan/Features/Projects/Models/ProjectModels.swift
Normal file
440
suixinkan/Features/Projects/Models/ProjectModels.swift
Normal file
@ -0,0 +1,440 @@
|
||||
//
|
||||
// ProjectModels.swift
|
||||
// suixinkan
|
||||
//
|
||||
// Created by Codex on 2026/6/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// 项目详情实体,表示摄影师项目或店铺项目详情页需要展示的完整业务字段。
|
||||
struct PhotographerProjectDetailResponse: Decodable, Identifiable, Hashable {
|
||||
let id: Int
|
||||
let scenicId: Int
|
||||
let cover: String
|
||||
let coverProject: String
|
||||
let coverCarousel: [String]
|
||||
let name: String
|
||||
let type: Int
|
||||
let typeName: String
|
||||
let status: Int
|
||||
let statusName: String
|
||||
let price: String
|
||||
let otPrice: String
|
||||
let priceDeposit: String
|
||||
let description: String
|
||||
let attrLabel: [String]
|
||||
let projectContent: String
|
||||
let sold: Int
|
||||
let unitName: String
|
||||
let materialNum: Int
|
||||
let photoNum: Int
|
||||
let videoNum: Int
|
||||
let createdAt: String
|
||||
let updatedAt: String
|
||||
let creatorName: String
|
||||
let creatorPhone: String
|
||||
let creatorRoleName: String
|
||||
let auditRemark: String
|
||||
let photogList: [ProjectPhotographerBrief]
|
||||
let scenicList: [ProjectScenicSpotBrief]
|
||||
|
||||
/// 字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case scenicId = "scenic_id"
|
||||
case cover
|
||||
case coverProject = "cover_project"
|
||||
case coverCarousel = "cover_carousel"
|
||||
case name
|
||||
case type
|
||||
case typeName = "type_name"
|
||||
case status
|
||||
case statusName = "status_name"
|
||||
case price
|
||||
case otPrice = "ot_price"
|
||||
case priceDeposit = "price_deposit"
|
||||
case description
|
||||
case attrLabel = "attr_label"
|
||||
case projectContent = "project_content"
|
||||
case sold
|
||||
case unitName = "unit_name"
|
||||
case materialNum = "material_num"
|
||||
case photoNum = "photo_num"
|
||||
case videoNum = "video_num"
|
||||
case createdAt = "created_at"
|
||||
case updatedAt = "updated_at"
|
||||
case creatorName = "creator_name"
|
||||
case creatorPhone = "creator_phone"
|
||||
case creatorRoleName = "creator_role_name"
|
||||
case auditRemark = "audit_remark"
|
||||
case photogList = "photog_list"
|
||||
case scenicList = "scenic_list"
|
||||
}
|
||||
|
||||
/// 宽松解码项目详情字段。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decodeProjectLossyInt(forKey: .id) ?? 0
|
||||
scenicId = try container.decodeProjectLossyInt(forKey: .scenicId) ?? 0
|
||||
cover = try container.decodeProjectLossyString(forKey: .cover)
|
||||
coverProject = try container.decodeProjectLossyString(forKey: .coverProject)
|
||||
coverCarousel = try container.decodeProjectLossyStringArray(forKey: .coverCarousel)
|
||||
name = try container.decodeProjectLossyString(forKey: .name)
|
||||
type = try container.decodeProjectLossyInt(forKey: .type) ?? 0
|
||||
typeName = try container.decodeProjectLossyString(forKey: .typeName)
|
||||
status = try container.decodeProjectLossyInt(forKey: .status) ?? 0
|
||||
statusName = try container.decodeProjectLossyString(forKey: .statusName)
|
||||
price = try container.decodeProjectLossyString(forKey: .price)
|
||||
otPrice = try container.decodeProjectLossyString(forKey: .otPrice)
|
||||
priceDeposit = try container.decodeProjectLossyString(forKey: .priceDeposit)
|
||||
description = try container.decodeProjectLossyString(forKey: .description)
|
||||
attrLabel = try container.decodeProjectLossyStringArray(forKey: .attrLabel)
|
||||
projectContent = try container.decodeProjectLossyString(forKey: .projectContent)
|
||||
sold = try container.decodeProjectLossyInt(forKey: .sold) ?? 0
|
||||
unitName = try container.decodeProjectLossyString(forKey: .unitName)
|
||||
materialNum = try container.decodeProjectLossyInt(forKey: .materialNum) ?? 0
|
||||
photoNum = try container.decodeProjectLossyInt(forKey: .photoNum) ?? 0
|
||||
videoNum = try container.decodeProjectLossyInt(forKey: .videoNum) ?? 0
|
||||
createdAt = try container.decodeProjectLossyString(forKey: .createdAt)
|
||||
updatedAt = try container.decodeProjectLossyString(forKey: .updatedAt)
|
||||
creatorName = try container.decodeProjectLossyString(forKey: .creatorName)
|
||||
creatorPhone = try container.decodeProjectLossyString(forKey: .creatorPhone)
|
||||
creatorRoleName = try container.decodeProjectLossyString(forKey: .creatorRoleName)
|
||||
auditRemark = try container.decodeProjectLossyString(forKey: .auditRemark)
|
||||
photogList = (try? container.decode([ProjectPhotographerBrief].self, forKey: .photogList)) ?? []
|
||||
scenicList = (try? container.decode([ProjectScenicSpotBrief].self, forKey: .scenicList)) ?? []
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目摄影师摘要实体,表示项目详情中的摄影师信息。
|
||||
struct ProjectPhotographerBrief: Decodable, Identifiable, Hashable {
|
||||
let id: Int
|
||||
let photogUid: Int
|
||||
let nickname: String
|
||||
let avatar: String
|
||||
let orderNum: Int
|
||||
let completedOrderCount: Int
|
||||
|
||||
/// 字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case photogUid = "photog_uid"
|
||||
case nickname
|
||||
case avatar
|
||||
case orderNum = "order_num"
|
||||
case completedOrderCount = "completed_order_count"
|
||||
}
|
||||
|
||||
/// 宽松解码摄影师摘要。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decodeProjectLossyInt(forKey: .id) ?? 0
|
||||
photogUid = try container.decodeProjectLossyInt(forKey: .photogUid) ?? 0
|
||||
nickname = try container.decodeProjectLossyString(forKey: .nickname)
|
||||
avatar = try container.decodeProjectLossyString(forKey: .avatar)
|
||||
orderNum = try container.decodeProjectLossyInt(forKey: .orderNum) ?? 0
|
||||
completedOrderCount = try container.decodeProjectLossyInt(forKey: .completedOrderCount) ?? 0
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目打卡点摘要实体,表示项目关联的景点或打卡点。
|
||||
struct ProjectScenicSpotBrief: Decodable, Identifiable, Hashable {
|
||||
let id: Int
|
||||
let name: String
|
||||
|
||||
/// 字段映射。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case name
|
||||
}
|
||||
|
||||
/// 宽松解码打卡点摘要。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decodeProjectLossyInt(forKey: .id) ?? 0
|
||||
name = try container.decodeProjectLossyString(forKey: .name)
|
||||
}
|
||||
}
|
||||
|
||||
/// 摄影师项目创建或编辑请求实体。
|
||||
struct ProjectCreateRequest: Encodable, Equatable {
|
||||
let id: Int?
|
||||
let type: Int
|
||||
let scenicId: Int
|
||||
let name: String
|
||||
let price: String
|
||||
let otPrice: String?
|
||||
let coverProject: String
|
||||
let coverCarousel: [String]?
|
||||
let description: String
|
||||
let attrLabel: [String]?
|
||||
let extra: ProjectCreateExtra
|
||||
let allowNFC: Int = 1
|
||||
|
||||
/// 请求字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case type
|
||||
case scenicId = "scenic_id"
|
||||
case name
|
||||
case price
|
||||
case otPrice = "ot_price"
|
||||
case coverProject = "cover_project"
|
||||
case coverCarousel = "cover_carousel"
|
||||
case description
|
||||
case attrLabel = "attr_label"
|
||||
case extra
|
||||
case allowNFC = "allow_nfc"
|
||||
}
|
||||
}
|
||||
|
||||
/// 摄影师项目扩展配置实体,表示交付数量、押金和关联打卡点/摄影师。
|
||||
struct ProjectCreateExtra: Encodable, Equatable {
|
||||
let priceDeposit: String
|
||||
let materialNum: Int
|
||||
let photoNum: Int
|
||||
let videoNum: Int
|
||||
let scenicSpotId: [Int]
|
||||
let photogUid: [Int]
|
||||
|
||||
/// 请求字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case priceDeposit = "price_deposit"
|
||||
case materialNum = "material_num"
|
||||
case photoNum = "photo_num"
|
||||
case videoNum = "video_num"
|
||||
case scenicSpotId = "scenic_spot_id"
|
||||
case photogUid = "photog_uid"
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目删除请求实体。
|
||||
struct ProjectDeleteRequest: Encodable, Equatable {
|
||||
let id: Int
|
||||
}
|
||||
|
||||
/// 店铺项目可管理景区实体。
|
||||
struct StoreManagerScenicItem: Decodable, Identifiable, Hashable {
|
||||
let id: Int
|
||||
let name: String
|
||||
|
||||
/// 字段映射。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case name
|
||||
}
|
||||
|
||||
/// 宽松解码店铺项目景区。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decodeProjectLossyInt(forKey: .id) ?? 0
|
||||
name = try container.decodeProjectLossyString(forKey: .name)
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺项目套餐实体,表示多点位项目的套餐价格配置。
|
||||
struct StoreManagerPackageItem: Codable, Hashable {
|
||||
let materialNum: Int
|
||||
let price: String
|
||||
|
||||
/// 请求字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case materialNum = "material_num"
|
||||
case price
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺项目创建请求实体,用于多点位项目。
|
||||
struct StoreManagerCreateRequest: Encodable, Equatable {
|
||||
let name: String
|
||||
let storeId: Int?
|
||||
let type: Int
|
||||
let description: String
|
||||
let coverProject: String?
|
||||
let coverCarousel: [String]?
|
||||
let projectRule: String?
|
||||
let scenicId: [Int]
|
||||
let settleSpotNum: Int
|
||||
let price: Double
|
||||
let priceMaterial: Double
|
||||
let pricePhoto: Double
|
||||
let priceVideo: Double
|
||||
let priceMaterialAll: Double?
|
||||
let packageList: [StoreManagerPackageItem]?
|
||||
let userId: Int
|
||||
let singleSpotMaterialNum: Int
|
||||
let singleSpotPhotoNum: Int
|
||||
let singleSpotVideoNum: Int
|
||||
|
||||
/// 请求字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case name
|
||||
case storeId = "store_id"
|
||||
case type
|
||||
case description
|
||||
case coverProject = "cover_project"
|
||||
case coverCarousel = "cover_carousel"
|
||||
case projectRule = "project_rule"
|
||||
case scenicId = "scenic_id"
|
||||
case settleSpotNum = "settle_spot_num"
|
||||
case price
|
||||
case priceMaterial = "price_material"
|
||||
case pricePhoto = "price_photo"
|
||||
case priceVideo = "price_video"
|
||||
case priceMaterialAll = "price_material_all"
|
||||
case packageList = "package"
|
||||
case userId = "user_id"
|
||||
case singleSpotMaterialNum = "single_spot_material_num"
|
||||
case singleSpotPhotoNum = "single_spot_photo_num"
|
||||
case singleSpotVideoNum = "single_spot_video_num"
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺项目更新请求实体,用于多点位项目。
|
||||
struct StoreManagerUpdateRequest: Encodable, Equatable {
|
||||
let id: Int
|
||||
let name: String
|
||||
let type: Int
|
||||
let description: String
|
||||
let coverProject: String?
|
||||
let coverCarousel: [String]?
|
||||
let projectRule: String?
|
||||
let scenicId: [Int]
|
||||
let settleSpotNum: Int
|
||||
let price: Double
|
||||
let priceMaterial: Double
|
||||
let pricePhoto: Double
|
||||
let priceVideo: Double
|
||||
let priceMaterialAll: Double?
|
||||
let packageList: [StoreManagerPackageItem]?
|
||||
let userId: Int
|
||||
let singleSpotMaterialNum: Int
|
||||
let singleSpotPhotoNum: Int
|
||||
let singleSpotVideoNum: Int
|
||||
|
||||
/// 请求字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case name
|
||||
case type
|
||||
case description
|
||||
case coverProject = "cover_project"
|
||||
case coverCarousel = "cover_carousel"
|
||||
case projectRule = "project_rule"
|
||||
case scenicId = "scenic_id"
|
||||
case settleSpotNum = "settle_spot_num"
|
||||
case price
|
||||
case priceMaterial = "price_material"
|
||||
case pricePhoto = "price_photo"
|
||||
case priceVideo = "price_video"
|
||||
case priceMaterialAll = "price_material_all"
|
||||
case packageList = "package"
|
||||
case userId = "user_id"
|
||||
case singleSpotMaterialNum = "single_spot_material_num"
|
||||
case singleSpotPhotoNum = "single_spot_photo_num"
|
||||
case singleSpotVideoNum = "single_spot_video_num"
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺押金项目创建请求实体。
|
||||
struct StoreManagerOfflineCreateRequest: Encodable, Equatable {
|
||||
let name: String
|
||||
let scenicId: Int
|
||||
let storeId: Int
|
||||
let description: String
|
||||
let price: Double
|
||||
let coverProject: String
|
||||
let coverCarousel: [String]
|
||||
|
||||
/// 请求字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case name
|
||||
case scenicId = "scenic_id"
|
||||
case storeId = "store_id"
|
||||
case description
|
||||
case price
|
||||
case coverProject = "cover_project"
|
||||
case coverCarousel = "cover_carousel"
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺押金项目更新请求实体。
|
||||
struct StoreManagerOfflineUpdateRequest: Encodable, Equatable {
|
||||
let id: Int
|
||||
let name: String
|
||||
let scenicId: Int
|
||||
let description: String
|
||||
let price: Double
|
||||
let coverProject: String?
|
||||
let coverCarousel: [String]?
|
||||
let storeId: Int?
|
||||
|
||||
/// 请求字段映射,兼容后端下划线命名。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case name
|
||||
case scenicId = "scenic_id"
|
||||
case description
|
||||
case price
|
||||
case coverProject = "cover_project"
|
||||
case coverCarousel = "cover_carousel"
|
||||
case storeId = "store_id"
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目本地待上传图片实体,保存 PhotosPicker 读取后的图片数据。
|
||||
struct ProjectLocalImage: Identifiable, Equatable {
|
||||
let id = UUID()
|
||||
let data: Data
|
||||
let fileName: String
|
||||
let previewURL: String?
|
||||
}
|
||||
|
||||
private extension KeyedDecodingContainer {
|
||||
/// 将 String、数字和 Bool 宽松解码为字符串。
|
||||
func decodeProjectLossyString(forKey key: Key) throws -> String {
|
||||
if let value = try? decodeIfPresent(String.self, forKey: key) {
|
||||
return value
|
||||
}
|
||||
if let value = try? decodeIfPresent(Int.self, forKey: key) {
|
||||
return String(value)
|
||||
}
|
||||
if let value = try? decodeIfPresent(Double.self, forKey: key) {
|
||||
return String(value)
|
||||
}
|
||||
if let value = try? decodeIfPresent(Bool.self, forKey: key) {
|
||||
return value ? "1" : "0"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
/// 将 String、Double 和 Int 宽松解码为 Int。
|
||||
func decodeProjectLossyInt(forKey key: Key) throws -> Int? {
|
||||
if let value = try? decodeIfPresent(Int.self, forKey: key) {
|
||||
return value
|
||||
}
|
||||
if let value = try? decodeIfPresent(Double.self, forKey: key) {
|
||||
return Int(value)
|
||||
}
|
||||
if let value = try? decodeIfPresent(String.self, forKey: key) {
|
||||
let text = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return Int(text) ?? Double(text).map(Int.init)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// 将数组或逗号分隔字符串宽松解码为字符串数组。
|
||||
func decodeProjectLossyStringArray(forKey key: Key) throws -> [String] {
|
||||
if let values = try? decodeIfPresent([String].self, forKey: key) {
|
||||
return values
|
||||
}
|
||||
if let value = try? decodeIfPresent(String.self, forKey: key) {
|
||||
return value
|
||||
.components(separatedBy: CharacterSet(charactersIn: ",,\n"))
|
||||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||
.filter { !$0.isEmpty }
|
||||
}
|
||||
return []
|
||||
}
|
||||
}
|
||||
31
suixinkan/Features/Projects/Projects.md
Normal file
31
suixinkan/Features/Projects/Projects.md
Normal file
@ -0,0 +1,31 @@
|
||||
# Projects 模块业务逻辑
|
||||
|
||||
## 模块职责
|
||||
|
||||
Projects 模块负责首页项目相关入口:
|
||||
- `pm`、`project_edit`:摄影师项目管理。
|
||||
- `pm_manager`:店铺项目管理。
|
||||
|
||||
模块只保存项目列表、详情、编辑表单和上传中的临时状态,不写入 `AppSession`、`AccountContext`、TabBar 或首页全局状态。
|
||||
|
||||
## 摄影师项目
|
||||
|
||||
`ProjectManagementViewModel` 管理摄影师项目列表、搜索、分页、详情入口和删除动作。列表请求依赖当前景区 ID,缺少景区时清空本模块列表并停止请求。
|
||||
|
||||
`ProjectEditorViewModel` 管理新建和编辑表单。项目封面、轮播图先通过 `OSSUploadService.uploadProjectImage` 上传到 `project/yyyyMMdd/scenicId/...` 路径,再把最终 URL 提交给项目创建或编辑接口。
|
||||
|
||||
## 店铺项目
|
||||
|
||||
`StoreProjectManagementViewModel` 管理店铺项目列表和可管理景区。店铺项目按业务账号 ID 查询,缺少用户 ID 时不请求。
|
||||
|
||||
`StoreProjectEditorViewModel` 支持多点位项目和押金项目两种提交路径。多点位项目提交景区、点位、展示图和项目套餐;押金项目提交门店、押金金额、底片数量等字段。
|
||||
|
||||
## 接口边界
|
||||
|
||||
`ProjectAPI` 只封装项目模块需要的接口,包括摄影师项目列表/详情/创建/编辑/删除,以及店铺项目列表/详情/创建/更新/删除和可管理景区列表。
|
||||
|
||||
样片上传需要的轻量项目选择复用共享 `PhotographerProjectItem`,但不反向依赖 Projects 页面状态。
|
||||
|
||||
## 缓存边界
|
||||
|
||||
项目图片临时数据、OSS STS、编辑表单和分页状态都不落盘。网络图片展示继续交给 `RemoteImage` / Kingfisher。
|
||||
609
suixinkan/Features/Projects/ViewModels/ProjectViewModels.swift
Normal file
609
suixinkan/Features/Projects/ViewModels/ProjectViewModels.swift
Normal file
@ -0,0 +1,609 @@
|
||||
//
|
||||
// ProjectViewModels.swift
|
||||
// suixinkan
|
||||
//
|
||||
// Created by Codex on 2026/6/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
/// 项目表单模式,区分新建和编辑。
|
||||
enum ProjectEditorMode: Equatable {
|
||||
case create
|
||||
case edit(PhotographerProjectDetailResponse)
|
||||
}
|
||||
|
||||
/// 店铺项目表单模式,区分新建和编辑。
|
||||
enum StoreProjectEditorMode: Equatable {
|
||||
case create
|
||||
case edit(PhotographerProjectDetailResponse)
|
||||
}
|
||||
|
||||
/// 项目表单错误实体,表示校验、上传或提交失败。
|
||||
enum ProjectEditorError: LocalizedError, Equatable {
|
||||
case missingScenic
|
||||
case missingUser
|
||||
case missingName
|
||||
case missingDescription
|
||||
case missingPrice
|
||||
case missingSpot
|
||||
case missingImage
|
||||
case missingStore
|
||||
|
||||
/// 错误文案。
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .missingScenic: return "当前缺少景区信息"
|
||||
case .missingUser: return "当前缺少用户信息"
|
||||
case .missingName: return "请输入项目名称"
|
||||
case .missingDescription: return "请输入项目简介"
|
||||
case .missingPrice: return "请填写有效价格"
|
||||
case .missingSpot: return "请至少选择一个打卡点"
|
||||
case .missingImage: return "请至少上传项目封面"
|
||||
case .missingStore: return "请选择所属门店"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 摄影师项目管理 ViewModel,负责项目列表、分页、详情和删除。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class ProjectManagementViewModel {
|
||||
private(set) var items: [PhotographerProjectItem] = []
|
||||
private(set) var loading = false
|
||||
private(set) var loadingMore = false
|
||||
private(set) var hasMore = false
|
||||
private(set) var selectedDetail: PhotographerProjectDetailResponse?
|
||||
var searchText = ""
|
||||
var errorMessage: String?
|
||||
|
||||
private var page = 1
|
||||
private var total = 0
|
||||
private let pageSize = 10
|
||||
|
||||
/// 重新加载摄影师项目列表。
|
||||
func reload(api: any ProjectServing, scenicId: Int?) async {
|
||||
guard let scenicId else {
|
||||
resetList()
|
||||
return
|
||||
}
|
||||
loading = true
|
||||
errorMessage = nil
|
||||
defer { loading = false }
|
||||
do {
|
||||
page = 1
|
||||
let response = try await api.projectList(
|
||||
scenicId: scenicId,
|
||||
name: normalizedSearch,
|
||||
page: page,
|
||||
pageSize: pageSize
|
||||
)
|
||||
total = response.total
|
||||
items = sorted(response.list)
|
||||
hasMore = items.count < total
|
||||
page = 2
|
||||
} catch {
|
||||
resetList()
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 加载下一页摄影师项目。
|
||||
func loadMore(api: any ProjectServing, scenicId: Int?) async {
|
||||
guard let scenicId, hasMore, !loadingMore else { return }
|
||||
loadingMore = true
|
||||
defer { loadingMore = false }
|
||||
do {
|
||||
let response = try await api.projectList(scenicId: scenicId, name: normalizedSearch, page: page, pageSize: pageSize)
|
||||
total = response.total
|
||||
items = sorted(deduplicated(items + response.list))
|
||||
hasMore = items.count < total
|
||||
page += 1
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 加载项目详情。
|
||||
func loadDetail(id: Int, api: any ProjectServing) async {
|
||||
do {
|
||||
selectedDetail = try await api.projectDetail(id: id)
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 删除摄影师项目并从本地列表移除。
|
||||
func delete(id: Int, api: any ProjectServing) async {
|
||||
do {
|
||||
try await api.deleteProject(id: id)
|
||||
items.removeAll { $0.id == id }
|
||||
total = max(total - 1, 0)
|
||||
hasMore = items.count < total
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
private var normalizedSearch: String? {
|
||||
let value = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return value.isEmpty ? nil : value
|
||||
}
|
||||
|
||||
private func resetList() {
|
||||
items = []
|
||||
loading = false
|
||||
loadingMore = false
|
||||
hasMore = false
|
||||
page = 1
|
||||
total = 0
|
||||
}
|
||||
|
||||
private func sorted(_ values: [PhotographerProjectItem]) -> [PhotographerProjectItem] {
|
||||
values.sorted { lhs, rhs in
|
||||
if lhs.status != rhs.status { return lhs.status < rhs.status }
|
||||
return lhs.id > rhs.id
|
||||
}
|
||||
}
|
||||
|
||||
private func deduplicated(_ values: [PhotographerProjectItem]) -> [PhotographerProjectItem] {
|
||||
var result: [Int: PhotographerProjectItem] = [:]
|
||||
values.forEach { result[$0.id] = $0 }
|
||||
return Array(result.values)
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目编辑 ViewModel,负责摄影师项目表单、图片上传和提交。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class ProjectEditorViewModel {
|
||||
var name = ""
|
||||
var descriptionText = ""
|
||||
var price = ""
|
||||
var otPrice = ""
|
||||
var deposit = ""
|
||||
var attrLabelText = ""
|
||||
var materialNum = "1"
|
||||
var photoNum = "1"
|
||||
var videoNum = "1"
|
||||
var selectedSpotIds: Set<Int> = []
|
||||
var coverImage: ProjectLocalImage?
|
||||
var carouselImages: [ProjectLocalImage] = []
|
||||
var existingCoverURL = ""
|
||||
var existingCarouselURLs: [String] = []
|
||||
private(set) var submitting = false
|
||||
private(set) var uploadProgress = 0
|
||||
var errorMessage: String?
|
||||
|
||||
private let mode: ProjectEditorMode
|
||||
|
||||
/// 初始化摄影师项目表单,并在编辑模式下回填详情。
|
||||
init(mode: ProjectEditorMode) {
|
||||
self.mode = mode
|
||||
if case let .edit(detail) = mode {
|
||||
name = detail.name
|
||||
descriptionText = detail.description
|
||||
price = detail.price
|
||||
otPrice = detail.otPrice
|
||||
deposit = detail.priceDeposit
|
||||
attrLabelText = detail.attrLabel.joined(separator: ",")
|
||||
materialNum = "\(max(detail.materialNum, 0))"
|
||||
photoNum = "\(max(detail.photoNum, 0))"
|
||||
videoNum = "\(max(detail.videoNum, 0))"
|
||||
selectedSpotIds = Set(detail.scenicList.map(\.id))
|
||||
existingCoverURL = detail.coverProject
|
||||
existingCarouselURLs = detail.coverCarousel
|
||||
}
|
||||
}
|
||||
|
||||
/// 提交摄影师项目,必要时先上传封面和轮播图。
|
||||
func submit(
|
||||
scenicId: Int?,
|
||||
userId: Int?,
|
||||
api: any ProjectServing,
|
||||
uploadService: any OSSUploadServing
|
||||
) async -> Bool {
|
||||
guard !submitting else { return false }
|
||||
guard let scenicId else { return fail(.missingScenic) }
|
||||
guard let userId, userId > 0 else { return fail(.missingUser) }
|
||||
let normalizedName = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let normalizedDescription = descriptionText.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !normalizedName.isEmpty else { return fail(.missingName) }
|
||||
guard !normalizedDescription.isEmpty else { return fail(.missingDescription) }
|
||||
guard Double(price) ?? 0 > 0 else { return fail(.missingPrice) }
|
||||
guard !selectedSpotIds.isEmpty else { return fail(.missingSpot) }
|
||||
|
||||
submitting = true
|
||||
errorMessage = nil
|
||||
uploadProgress = 0
|
||||
defer { submitting = false }
|
||||
|
||||
do {
|
||||
let coverURL = try await resolveCoverURL(scenicId: scenicId, uploadService: uploadService)
|
||||
guard !coverURL.isEmpty else { throw ProjectEditorError.missingImage }
|
||||
let carouselURLs = try await resolveCarouselURLs(scenicId: scenicId, uploadService: uploadService)
|
||||
let request = ProjectCreateRequest(
|
||||
id: editID,
|
||||
type: 11,
|
||||
scenicId: scenicId,
|
||||
name: normalizedName,
|
||||
price: normalizedMoney(price) ?? "0.00",
|
||||
otPrice: normalizedMoney(otPrice),
|
||||
coverProject: coverURL,
|
||||
coverCarousel: carouselURLs.isEmpty ? nil : carouselURLs,
|
||||
description: normalizedDescription,
|
||||
attrLabel: normalizedLabels,
|
||||
extra: ProjectCreateExtra(
|
||||
priceDeposit: normalizedMoney(deposit) ?? "0.00",
|
||||
materialNum: max(Int(materialNum) ?? 0, 0),
|
||||
photoNum: max(Int(photoNum) ?? 0, 0),
|
||||
videoNum: max(Int(videoNum) ?? 0, 0),
|
||||
scenicSpotId: selectedSpotIds.sorted(),
|
||||
photogUid: [userId]
|
||||
)
|
||||
)
|
||||
if editID == nil {
|
||||
try await api.createProject(request)
|
||||
} else {
|
||||
try await api.editProject(request)
|
||||
}
|
||||
uploadProgress = 100
|
||||
return true
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private var editID: Int? {
|
||||
if case let .edit(detail) = mode {
|
||||
return detail.id
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private var normalizedLabels: [String]? {
|
||||
let labels = attrLabelText
|
||||
.components(separatedBy: CharacterSet(charactersIn: ",,"))
|
||||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||
.filter { !$0.isEmpty }
|
||||
return labels.isEmpty ? nil : labels
|
||||
}
|
||||
|
||||
private func resolveCoverURL(scenicId: Int, uploadService: any OSSUploadServing) async throws -> String {
|
||||
if let coverImage {
|
||||
return try await uploadService.uploadProjectImage(data: coverImage.data, fileName: coverImage.fileName, scenicId: scenicId) { [weak self] progress in
|
||||
self?.uploadProgress = progress
|
||||
}
|
||||
}
|
||||
return existingCoverURL
|
||||
}
|
||||
|
||||
private func resolveCarouselURLs(scenicId: Int, uploadService: any OSSUploadServing) async throws -> [String] {
|
||||
var urls = existingCarouselURLs
|
||||
for image in carouselImages {
|
||||
let url = try await uploadService.uploadProjectImage(data: image.data, fileName: image.fileName, scenicId: scenicId) { [weak self] progress in
|
||||
self?.uploadProgress = progress
|
||||
}
|
||||
urls.append(url)
|
||||
}
|
||||
return urls
|
||||
}
|
||||
|
||||
private func normalizedMoney(_ value: String) -> String? {
|
||||
let amount = Double(value.trimmingCharacters(in: .whitespacesAndNewlines)) ?? 0
|
||||
return amount > 0 ? String(format: "%.2f", amount) : nil
|
||||
}
|
||||
|
||||
private func fail(_ error: ProjectEditorError) -> Bool {
|
||||
errorMessage = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺项目管理 ViewModel,负责店铺项目列表、筛选、详情和删除。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class StoreProjectManagementViewModel {
|
||||
private(set) var items: [PhotographerProjectItem] = []
|
||||
private(set) var loading = false
|
||||
private(set) var selectedDetail: PhotographerProjectDetailResponse?
|
||||
var searchText = ""
|
||||
var selectedType = 0
|
||||
var errorMessage: String?
|
||||
|
||||
/// 根据本地筛选条件返回展示项目。
|
||||
var filteredItems: [PhotographerProjectItem] {
|
||||
let keyword = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return items.filter { item in
|
||||
let typeMatched = selectedType == 0 || item.type == selectedType
|
||||
let keywordMatched = keyword.isEmpty || item.name.localizedCaseInsensitiveContains(keyword)
|
||||
return typeMatched && keywordMatched
|
||||
}
|
||||
}
|
||||
|
||||
/// 重新加载店铺项目列表。
|
||||
func reload(api: any ProjectServing, userId: String?) async {
|
||||
let normalizedUserId = userId?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||
guard !normalizedUserId.isEmpty else {
|
||||
items = []
|
||||
return
|
||||
}
|
||||
loading = true
|
||||
errorMessage = nil
|
||||
defer { loading = false }
|
||||
do {
|
||||
let response = try await api.storeManagerProjectList(userId: normalizedUserId, page: 1, pageSize: 100)
|
||||
items = response.list
|
||||
} catch {
|
||||
items = []
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 加载店铺项目详情。
|
||||
func loadDetail(id: Int, api: any ProjectServing) async {
|
||||
do {
|
||||
selectedDetail = try await api.storeManagerProjectDetail(id: id)
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 删除店铺项目。
|
||||
func delete(id: Int, api: any ProjectServing) async {
|
||||
do {
|
||||
try await api.storeManagerDeleteProject(id: id)
|
||||
items.removeAll { $0.id == id }
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺项目编辑 ViewModel,负责多点位和押金项目表单提交。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class StoreProjectEditorViewModel {
|
||||
var projectType = 19
|
||||
var scenicList: [StoreManagerScenicItem] = []
|
||||
var storeList: [StoreItem] = []
|
||||
var selectedScenicIds: Set<Int> = []
|
||||
var selectedStoreId: Int?
|
||||
var name = ""
|
||||
var descriptionText = ""
|
||||
var coverImage: ProjectLocalImage?
|
||||
var carouselImages: [ProjectLocalImage] = []
|
||||
var existingCoverURL = ""
|
||||
var existingCarouselURLs: [String] = []
|
||||
var settleSpotNum = "1"
|
||||
var projectPrice = ""
|
||||
var priceMaterial = ""
|
||||
var pricePhoto = ""
|
||||
var priceVideo = ""
|
||||
var priceDeposit = ""
|
||||
var materialNum = "1"
|
||||
var photoNum = "1"
|
||||
var videoNum = "1"
|
||||
private(set) var submitting = false
|
||||
var errorMessage: String?
|
||||
|
||||
private let mode: StoreProjectEditorMode
|
||||
|
||||
/// 初始化店铺项目表单,并在编辑模式下回填详情。
|
||||
init(mode: StoreProjectEditorMode) {
|
||||
self.mode = mode
|
||||
if case let .edit(detail) = mode {
|
||||
projectType = detail.type == 4 ? 4 : 19
|
||||
name = detail.name
|
||||
descriptionText = detail.description
|
||||
existingCoverURL = detail.coverProject
|
||||
existingCarouselURLs = detail.coverCarousel
|
||||
selectedScenicIds = Set(detail.scenicList.map(\.id))
|
||||
projectPrice = detail.price
|
||||
priceDeposit = detail.priceDeposit
|
||||
materialNum = "\(max(detail.materialNum, 0))"
|
||||
photoNum = "\(max(detail.photoNum, 0))"
|
||||
videoNum = "\(max(detail.videoNum, 0))"
|
||||
}
|
||||
}
|
||||
|
||||
/// 加载店铺项目可管理景区。
|
||||
func loadScenicList(api: any ProjectServing, userId: Int) async {
|
||||
guard scenicList.isEmpty, userId > 0 else { return }
|
||||
do {
|
||||
scenicList = try await api.storeManagerScenicList(userId: "\(userId)").list
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 加载当前景区下门店列表。
|
||||
func loadStoreList(api: any ProjectServing) async {
|
||||
guard projectType == 4, let scenicId = selectedScenicIds.first else { return }
|
||||
do {
|
||||
storeList = try await api.storeAll().list.filter { $0.scenicId == scenicId }
|
||||
if selectedStoreId == nil {
|
||||
selectedStoreId = storeList.first?.id
|
||||
}
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 切换店铺项目景区选择。
|
||||
func toggleScenic(_ id: Int) {
|
||||
if projectType == 4 {
|
||||
selectedScenicIds = [id]
|
||||
} else if selectedScenicIds.contains(id) {
|
||||
selectedScenicIds.remove(id)
|
||||
} else {
|
||||
selectedScenicIds.insert(id)
|
||||
}
|
||||
}
|
||||
|
||||
/// 提交店铺项目。
|
||||
func submit(userId: Int, api: any ProjectServing, uploadService: any OSSUploadServing) async -> Bool {
|
||||
guard !submitting else { return false }
|
||||
guard userId > 0 else { return fail(.missingUser) }
|
||||
let normalizedName = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let normalizedDescription = descriptionText.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !normalizedName.isEmpty else { return fail(.missingName) }
|
||||
guard !normalizedDescription.isEmpty else { return fail(.missingDescription) }
|
||||
guard !selectedScenicIds.isEmpty else { return fail(.missingScenic) }
|
||||
guard Double(projectType == 4 ? priceDeposit : projectPrice) ?? 0 > 0 else { return fail(.missingPrice) }
|
||||
if projectType == 4, selectedStoreId == nil { return fail(.missingStore) }
|
||||
|
||||
submitting = true
|
||||
errorMessage = nil
|
||||
defer { submitting = false }
|
||||
|
||||
do {
|
||||
let scenicIdForUpload = selectedScenicIds.first ?? 0
|
||||
let coverURL = try await resolveCoverURL(scenicId: scenicIdForUpload, uploadService: uploadService)
|
||||
guard !coverURL.isEmpty else { throw ProjectEditorError.missingImage }
|
||||
let carouselURLs = try await resolveCarouselURLs(scenicId: scenicIdForUpload, uploadService: uploadService)
|
||||
if projectType == 4 {
|
||||
try await submitOfflineProject(
|
||||
api: api,
|
||||
name: normalizedName,
|
||||
description: normalizedDescription,
|
||||
coverURL: coverURL,
|
||||
carouselURLs: carouselURLs
|
||||
)
|
||||
} else {
|
||||
try await submitMultiPointProject(
|
||||
userId: userId,
|
||||
api: api,
|
||||
name: normalizedName,
|
||||
description: normalizedDescription,
|
||||
coverURL: coverURL,
|
||||
carouselURLs: carouselURLs
|
||||
)
|
||||
}
|
||||
return true
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private var editID: Int? {
|
||||
if case let .edit(detail) = mode {
|
||||
return detail.id
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func resolveCoverURL(scenicId: Int, uploadService: any OSSUploadServing) async throws -> String {
|
||||
if let coverImage {
|
||||
return try await uploadService.uploadProjectImage(data: coverImage.data, fileName: coverImage.fileName, scenicId: scenicId) { _ in }
|
||||
}
|
||||
return existingCoverURL
|
||||
}
|
||||
|
||||
private func resolveCarouselURLs(scenicId: Int, uploadService: any OSSUploadServing) async throws -> [String] {
|
||||
var urls = existingCarouselURLs
|
||||
for image in carouselImages {
|
||||
let url = try await uploadService.uploadProjectImage(data: image.data, fileName: image.fileName, scenicId: scenicId) { _ in }
|
||||
urls.append(url)
|
||||
}
|
||||
return urls
|
||||
}
|
||||
|
||||
private func submitMultiPointProject(
|
||||
userId: Int,
|
||||
api: any ProjectServing,
|
||||
name: String,
|
||||
description: String,
|
||||
coverURL: String,
|
||||
carouselURLs: [String]
|
||||
) async throws {
|
||||
if let editID {
|
||||
try await api.storeManagerUpdate(
|
||||
StoreManagerUpdateRequest(
|
||||
id: editID,
|
||||
name: name,
|
||||
type: 19,
|
||||
description: description,
|
||||
coverProject: coverURL,
|
||||
coverCarousel: carouselURLs,
|
||||
projectRule: nil,
|
||||
scenicId: selectedScenicIds.sorted(),
|
||||
settleSpotNum: max(Int(settleSpotNum) ?? 1, 1),
|
||||
price: Double(projectPrice) ?? 0,
|
||||
priceMaterial: Double(priceMaterial) ?? 0,
|
||||
pricePhoto: Double(pricePhoto) ?? 0,
|
||||
priceVideo: Double(priceVideo) ?? 0,
|
||||
priceMaterialAll: nil,
|
||||
packageList: nil,
|
||||
userId: userId,
|
||||
singleSpotMaterialNum: max(Int(materialNum) ?? 0, 0),
|
||||
singleSpotPhotoNum: max(Int(photoNum) ?? 0, 0),
|
||||
singleSpotVideoNum: max(Int(videoNum) ?? 0, 0)
|
||||
)
|
||||
)
|
||||
} else {
|
||||
try await api.storeManagerCreate(
|
||||
StoreManagerCreateRequest(
|
||||
name: name,
|
||||
storeId: nil,
|
||||
type: 19,
|
||||
description: description,
|
||||
coverProject: coverURL,
|
||||
coverCarousel: carouselURLs,
|
||||
projectRule: nil,
|
||||
scenicId: selectedScenicIds.sorted(),
|
||||
settleSpotNum: max(Int(settleSpotNum) ?? 1, 1),
|
||||
price: Double(projectPrice) ?? 0,
|
||||
priceMaterial: Double(priceMaterial) ?? 0,
|
||||
pricePhoto: Double(pricePhoto) ?? 0,
|
||||
priceVideo: Double(priceVideo) ?? 0,
|
||||
priceMaterialAll: nil,
|
||||
packageList: nil,
|
||||
userId: userId,
|
||||
singleSpotMaterialNum: max(Int(materialNum) ?? 0, 0),
|
||||
singleSpotPhotoNum: max(Int(photoNum) ?? 0, 0),
|
||||
singleSpotVideoNum: max(Int(videoNum) ?? 0, 0)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func submitOfflineProject(api: any ProjectServing, name: String, description: String, coverURL: String, carouselURLs: [String]) async throws {
|
||||
let scenicId = selectedScenicIds.first ?? 0
|
||||
let storeId = selectedStoreId ?? 0
|
||||
if let editID {
|
||||
try await api.storeManagerOfflineUpdate(
|
||||
StoreManagerOfflineUpdateRequest(
|
||||
id: editID,
|
||||
name: name,
|
||||
scenicId: scenicId,
|
||||
description: description,
|
||||
price: Double(priceDeposit) ?? 0,
|
||||
coverProject: coverURL,
|
||||
coverCarousel: carouselURLs,
|
||||
storeId: storeId
|
||||
)
|
||||
)
|
||||
} else {
|
||||
try await api.storeManagerOfflineCreate(
|
||||
StoreManagerOfflineCreateRequest(
|
||||
name: name,
|
||||
scenicId: scenicId,
|
||||
storeId: storeId,
|
||||
description: description,
|
||||
price: Double(priceDeposit) ?? 0,
|
||||
coverProject: coverURL,
|
||||
coverCarousel: carouselURLs
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func fail(_ error: ProjectEditorError) -> Bool {
|
||||
errorMessage = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
}
|
||||
776
suixinkan/Features/Projects/Views/ProjectViews.swift
Normal file
776
suixinkan/Features/Projects/Views/ProjectViews.swift
Normal file
@ -0,0 +1,776 @@
|
||||
//
|
||||
// ProjectViews.swift
|
||||
// suixinkan
|
||||
//
|
||||
// Created by Codex on 2026/6/24.
|
||||
//
|
||||
|
||||
import PhotosUI
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
/// 摄影师项目管理页面,展示项目列表并提供新建、详情和删除入口。
|
||||
struct ProjectManagementView: View {
|
||||
@Environment(AccountContext.self) private var accountContext
|
||||
@Environment(AccountSnapshotStore.self) private var snapshotStore
|
||||
@Environment(ProjectAPI.self) private var projectAPI
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
@State private var viewModel = ProjectManagementViewModel()
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
filterBar
|
||||
if accountContext.currentScenic?.id == nil {
|
||||
ContentUnavailableView("缺少景区", systemImage: "map", description: Text("请选择景区后再管理项目。"))
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
} else {
|
||||
projectList
|
||||
}
|
||||
}
|
||||
.navigationTitle("项目管理")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
NavigationLink(value: HomeRoute.projectEditor(id: nil, storeMode: false)) {
|
||||
Image(systemName: "plus")
|
||||
}
|
||||
.accessibilityLabel("新建项目")
|
||||
}
|
||||
}
|
||||
.task(id: accountContext.currentScenic?.id) {
|
||||
await reload(showLoading: true)
|
||||
}
|
||||
}
|
||||
|
||||
private var filterBar: some View {
|
||||
HStack(spacing: AppMetrics.Spacing.small) {
|
||||
Image(systemName: "magnifyingglass")
|
||||
.foregroundStyle(AppDesign.textSecondary)
|
||||
TextField("搜索项目名称", text: $viewModel.searchText)
|
||||
.textInputAutocapitalization(.never)
|
||||
.autocorrectionDisabled(true)
|
||||
.submitLabel(.search)
|
||||
.onSubmit { Task { await reload(showLoading: true) } }
|
||||
Button("搜索") {
|
||||
Task { await reload(showLoading: true) }
|
||||
}
|
||||
.font(.system(size: AppMetrics.FontSize.subheadline, weight: .semibold))
|
||||
.foregroundStyle(AppDesign.primary)
|
||||
}
|
||||
.padding(.horizontal, AppMetrics.Spacing.medium)
|
||||
.frame(height: 52)
|
||||
.background(.white)
|
||||
}
|
||||
|
||||
private var projectList: some View {
|
||||
ScrollView {
|
||||
LazyVStack(spacing: AppMetrics.Spacing.medium) {
|
||||
if viewModel.items.isEmpty && !viewModel.loading {
|
||||
ContentUnavailableView("暂无项目", systemImage: "folder", description: Text("可点击右上角创建项目。"))
|
||||
.frame(maxWidth: .infinity, minHeight: 260)
|
||||
}
|
||||
ForEach(viewModel.items) { item in
|
||||
NavigationLink(value: HomeRoute.projectDetail(id: item.id, storeMode: false)) {
|
||||
ProjectCardView(item: item)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.contextMenu {
|
||||
Button(role: .destructive) {
|
||||
Task { await viewModel.delete(id: item.id, api: projectAPI) }
|
||||
} label: {
|
||||
Label("删除", systemImage: "trash")
|
||||
}
|
||||
}
|
||||
}
|
||||
if viewModel.hasMore {
|
||||
ProgressView()
|
||||
.padding(.vertical, AppMetrics.Spacing.medium)
|
||||
.onAppear {
|
||||
Task { await viewModel.loadMore(api: projectAPI, scenicId: accountContext.currentScenic?.id) }
|
||||
}
|
||||
} else if !viewModel.items.isEmpty {
|
||||
Text("没有更多")
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(AppDesign.textSecondary)
|
||||
.padding(.vertical, AppMetrics.Spacing.medium)
|
||||
}
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
}
|
||||
.background(Color(hex: 0xF5F7FA))
|
||||
.refreshable { await reload(showLoading: false) }
|
||||
}
|
||||
|
||||
private func reload(showLoading: Bool) async {
|
||||
await globalLoading.withOptionalLoading(showLoading && accountContext.currentScenic?.id != nil) {
|
||||
await viewModel.reload(api: projectAPI, scenicId: accountContext.currentScenic?.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺项目管理页面,展示店铺项目列表、筛选和新建入口。
|
||||
struct StoreProjectManagementView: View {
|
||||
@Environment(AccountSnapshotStore.self) private var snapshotStore
|
||||
@Environment(ProjectAPI.self) private var projectAPI
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
@State private var viewModel = StoreProjectManagementViewModel()
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(spacing: AppMetrics.Spacing.medium) {
|
||||
storeFilterCard
|
||||
storeSummaryCard
|
||||
ForEach(viewModel.filteredItems) { item in
|
||||
NavigationLink(value: HomeRoute.projectDetail(id: item.id, storeMode: true)) {
|
||||
ProjectCompactCardView(item: item)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
if viewModel.filteredItems.isEmpty && !viewModel.loading {
|
||||
ContentUnavailableView("暂无店铺项目", systemImage: "folder", description: Text("可点击右上角新建店铺项目。"))
|
||||
.frame(minHeight: 260)
|
||||
}
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
}
|
||||
.background(Color(hex: 0xF5F7FA))
|
||||
.navigationTitle("店铺项目管理")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
NavigationLink(value: HomeRoute.projectEditor(id: nil, storeMode: true)) {
|
||||
Image(systemName: "plus")
|
||||
}
|
||||
.accessibilityLabel("新建店铺项目")
|
||||
}
|
||||
}
|
||||
.task { await reload(showLoading: true) }
|
||||
.refreshable { await reload(showLoading: false) }
|
||||
}
|
||||
|
||||
private var storeFilterCard: some View {
|
||||
VStack(spacing: AppMetrics.Spacing.small) {
|
||||
Picker("项目类型", selection: $viewModel.selectedType) {
|
||||
Text("全部").tag(0)
|
||||
Text("多点位").tag(19)
|
||||
Text("押金").tag(4)
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
TextField("搜索项目名称", text: $viewModel.searchText)
|
||||
.textInputAutocapitalization(.never)
|
||||
.autocorrectionDisabled(true)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
.background(.white, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.card))
|
||||
}
|
||||
|
||||
private var storeSummaryCard: some View {
|
||||
HStack(spacing: AppMetrics.Spacing.small) {
|
||||
stat("总数", "\(viewModel.filteredItems.count)")
|
||||
stat("启用", "\(viewModel.filteredItems.filter { $0.status == 1 }.count)")
|
||||
stat("押金", "\(viewModel.filteredItems.filter { $0.type == 4 }.count)")
|
||||
}
|
||||
}
|
||||
|
||||
private func stat(_ title: String, _ value: String) -> some View {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(title)
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(AppDesign.textSecondary)
|
||||
Text(value)
|
||||
.font(.system(size: AppMetrics.FontSize.body, weight: .semibold))
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(AppMetrics.Spacing.small)
|
||||
.background(.white, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.card))
|
||||
}
|
||||
|
||||
private func reload(showLoading: Bool) async {
|
||||
await globalLoading.withOptionalLoading(showLoading) {
|
||||
await viewModel.reload(api: projectAPI, userId: snapshotStore.load()?.profile?.userId ?? snapshotStore.load()?.businessUserId.map(String.init))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目详情页面,展示项目基础信息、媒体、打卡点和摄影师摘要。
|
||||
struct ProjectDetailView: View {
|
||||
@Environment(ProjectAPI.self) private var projectAPI
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
let projectId: Int
|
||||
let storeMode: Bool
|
||||
@State private var detail: PhotographerProjectDetailResponse?
|
||||
@State private var errorMessage: String?
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(spacing: AppMetrics.Spacing.medium) {
|
||||
if let detail {
|
||||
ProjectDetailContentView(detail: detail)
|
||||
} else if let errorMessage {
|
||||
ContentUnavailableView("加载失败", systemImage: "exclamationmark.triangle", description: Text(errorMessage))
|
||||
.frame(minHeight: 360)
|
||||
} else {
|
||||
ProgressView()
|
||||
.frame(maxWidth: .infinity, minHeight: 360)
|
||||
}
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
}
|
||||
.background(Color(hex: 0xF5F7FA))
|
||||
.navigationTitle("项目详情")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
if detail != nil {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
NavigationLink(value: HomeRoute.projectEditor(id: projectId, storeMode: storeMode)) {
|
||||
Text("编辑")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.task { await load() }
|
||||
}
|
||||
|
||||
private func load() async {
|
||||
await globalLoading.withLoading {
|
||||
do {
|
||||
detail = storeMode ? try await projectAPI.storeManagerProjectDetail(id: projectId) : try await projectAPI.projectDetail(id: projectId)
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 摄影师项目编辑页面,支持新建和编辑项目。
|
||||
struct ProjectEditorView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(AccountContext.self) private var accountContext
|
||||
@Environment(AccountSnapshotStore.self) private var snapshotStore
|
||||
@Environment(ScenicSpotContext.self) private var scenicSpotContext
|
||||
@Environment(ProjectAPI.self) private var projectAPI
|
||||
@Environment(OSSUploadService.self) private var uploadService
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
let projectId: Int?
|
||||
@State private var viewModel = ProjectEditorViewModel(mode: .create)
|
||||
@State private var coverItem: PhotosPickerItem?
|
||||
@State private var carouselItems: [PhotosPickerItem] = []
|
||||
@State private var loadingDetail = false
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(spacing: AppMetrics.Spacing.medium) {
|
||||
basicForm
|
||||
projectImageForm
|
||||
countForm
|
||||
spotForm
|
||||
if let error = viewModel.errorMessage {
|
||||
Text(error)
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(Color(hex: 0xE5484D))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
.background(Color(hex: 0xE5484D).opacity(0.1), in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.card))
|
||||
}
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
}
|
||||
.background(Color(hex: 0xF5F7FA))
|
||||
.navigationTitle(projectId == nil ? "新建项目" : "编辑项目")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button(viewModel.submitting ? "保存中..." : "保存") {
|
||||
Task { await submit() }
|
||||
}
|
||||
.disabled(viewModel.submitting || loadingDetail)
|
||||
}
|
||||
}
|
||||
.task {
|
||||
await prepare()
|
||||
}
|
||||
.onChange(of: coverItem) { _, newValue in
|
||||
Task { await loadCover(newValue) }
|
||||
}
|
||||
.onChange(of: carouselItems) { _, newValue in
|
||||
Task { await loadCarousel(newValue) }
|
||||
}
|
||||
}
|
||||
|
||||
private var basicForm: some View {
|
||||
ProjectFormCard(title: "基础信息") {
|
||||
TextField("项目名称", text: $viewModel.name)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
TextField("项目简介", text: $viewModel.descriptionText, axis: .vertical)
|
||||
.lineLimit(3...5)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: 96)
|
||||
HStack(spacing: AppMetrics.Spacing.small) {
|
||||
TextField("优惠价", text: $viewModel.price)
|
||||
.keyboardType(.decimalPad)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
TextField("原价", text: $viewModel.otPrice)
|
||||
.keyboardType(.decimalPad)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
}
|
||||
TextField("押金", text: $viewModel.deposit)
|
||||
.keyboardType(.decimalPad)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
TextField("标签,多个用逗号分隔", text: $viewModel.attrLabelText)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
}
|
||||
}
|
||||
|
||||
private var projectImageForm: some View {
|
||||
ProjectFormCard(title: "项目图片") {
|
||||
PhotosPicker(selection: $coverItem, matching: .images) {
|
||||
ProjectImagePickerLabel(title: "项目封面", url: viewModel.coverImage?.previewURL ?? viewModel.existingCoverURL)
|
||||
}
|
||||
PhotosPicker(selection: $carouselItems, maxSelectionCount: 8, matching: .images) {
|
||||
Label("选择轮播图(\(viewModel.carouselImages.count + viewModel.existingCarouselURLs.count))", systemImage: "photo.stack")
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 44)
|
||||
.background(AppDesign.primarySoft, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.input))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
|
||||
private var countForm: some View {
|
||||
ProjectFormCard(title: "交付数量") {
|
||||
HStack(spacing: AppMetrics.Spacing.small) {
|
||||
TextField("底片", text: $viewModel.materialNum)
|
||||
TextField("精修", text: $viewModel.photoNum)
|
||||
TextField("视频", text: $viewModel.videoNum)
|
||||
}
|
||||
.keyboardType(.numberPad)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
}
|
||||
}
|
||||
|
||||
private var spotForm: some View {
|
||||
ProjectFormCard(title: "打卡点") {
|
||||
if scenicSpotContext.spots.isEmpty {
|
||||
Text("暂无打卡点数据")
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(AppDesign.textSecondary)
|
||||
} else {
|
||||
ForEach(scenicSpotContext.spots) { spot in
|
||||
Button {
|
||||
if viewModel.selectedSpotIds.contains(spot.id) {
|
||||
viewModel.selectedSpotIds.remove(spot.id)
|
||||
} else {
|
||||
viewModel.selectedSpotIds.insert(spot.id)
|
||||
}
|
||||
} label: {
|
||||
HStack {
|
||||
Text(spot.name)
|
||||
Spacer()
|
||||
Image(systemName: viewModel.selectedSpotIds.contains(spot.id) ? "checkmark.circle.fill" : "circle")
|
||||
.foregroundStyle(viewModel.selectedSpotIds.contains(spot.id) ? AppDesign.primary : AppDesign.textSecondary)
|
||||
}
|
||||
.font(.system(size: AppMetrics.FontSize.subheadline))
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
.padding(AppMetrics.Spacing.small)
|
||||
.background(Color(hex: 0xF8FAFC), in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.input))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func prepare() async {
|
||||
guard let projectId else { return }
|
||||
loadingDetail = true
|
||||
defer { loadingDetail = false }
|
||||
do {
|
||||
let detail = try await projectAPI.projectDetail(id: projectId)
|
||||
viewModel = ProjectEditorViewModel(mode: .edit(detail))
|
||||
} catch {
|
||||
viewModel.errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
private func submit() async {
|
||||
await globalLoading.withLoading {
|
||||
let success = await viewModel.submit(
|
||||
scenicId: accountContext.currentScenic?.id,
|
||||
userId: snapshotStore.load()?.businessUserId ?? Int(snapshotStore.load()?.profile?.userId ?? ""),
|
||||
api: projectAPI,
|
||||
uploadService: uploadService
|
||||
)
|
||||
if success { dismiss() }
|
||||
}
|
||||
}
|
||||
|
||||
private func loadCover(_ item: PhotosPickerItem?) async {
|
||||
guard let file = await ProjectPickerLoader.loadImage(from: item) else { return }
|
||||
viewModel.coverImage = file
|
||||
}
|
||||
|
||||
private func loadCarousel(_ items: [PhotosPickerItem]) async {
|
||||
viewModel.carouselImages = await ProjectPickerLoader.loadImages(from: items)
|
||||
}
|
||||
}
|
||||
|
||||
/// 店铺项目编辑页面,支持新建和编辑店铺项目。
|
||||
struct StoreProjectEditorView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(AccountSnapshotStore.self) private var snapshotStore
|
||||
@Environment(ProjectAPI.self) private var projectAPI
|
||||
@Environment(OSSUploadService.self) private var uploadService
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
let projectId: Int?
|
||||
@State private var viewModel = StoreProjectEditorViewModel(mode: .create)
|
||||
@State private var coverItem: PhotosPickerItem?
|
||||
@State private var carouselItems: [PhotosPickerItem] = []
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(spacing: AppMetrics.Spacing.medium) {
|
||||
storeBasicForm
|
||||
storeImageForm
|
||||
storeScenicForm
|
||||
storePriceForm
|
||||
if let error = viewModel.errorMessage {
|
||||
Text(error)
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(Color(hex: 0xE5484D))
|
||||
}
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
}
|
||||
.background(Color(hex: 0xF5F7FA))
|
||||
.navigationTitle(projectId == nil ? "新建店铺项目" : "编辑店铺项目")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button(viewModel.submitting ? "保存中..." : "保存") {
|
||||
Task { await submit() }
|
||||
}
|
||||
.disabled(viewModel.submitting)
|
||||
}
|
||||
}
|
||||
.task { await prepare() }
|
||||
.onChange(of: coverItem) { _, newValue in
|
||||
Task { viewModel.coverImage = await ProjectPickerLoader.loadImage(from: newValue) }
|
||||
}
|
||||
.onChange(of: carouselItems) { _, newValue in
|
||||
Task { viewModel.carouselImages = await ProjectPickerLoader.loadImages(from: newValue) }
|
||||
}
|
||||
}
|
||||
|
||||
private var storeBasicForm: some View {
|
||||
ProjectFormCard(title: "基础信息") {
|
||||
Picker("类型", selection: $viewModel.projectType) {
|
||||
Text("多点位").tag(19)
|
||||
Text("押金").tag(4)
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
TextField("项目名称", text: $viewModel.name)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
TextField("项目简介", text: $viewModel.descriptionText, axis: .vertical)
|
||||
.lineLimit(3...5)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: 96)
|
||||
}
|
||||
}
|
||||
|
||||
private var storeImageForm: some View {
|
||||
ProjectFormCard(title: "项目图片") {
|
||||
PhotosPicker(selection: $coverItem, matching: .images) {
|
||||
ProjectImagePickerLabel(title: "项目封面", url: viewModel.coverImage?.previewURL ?? viewModel.existingCoverURL)
|
||||
}
|
||||
PhotosPicker(selection: $carouselItems, maxSelectionCount: 8, matching: .images) {
|
||||
Label("选择轮播图(\(viewModel.carouselImages.count + viewModel.existingCarouselURLs.count))", systemImage: "photo.stack")
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 44)
|
||||
.background(AppDesign.primarySoft, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.input))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
|
||||
private var storeScenicForm: some View {
|
||||
ProjectFormCard(title: viewModel.projectType == 4 ? "景区与门店" : "景区") {
|
||||
ForEach(viewModel.scenicList) { scenic in
|
||||
Button {
|
||||
viewModel.toggleScenic(scenic.id)
|
||||
Task { await viewModel.loadStoreList(api: projectAPI) }
|
||||
} label: {
|
||||
HStack {
|
||||
Text(scenic.name)
|
||||
Spacer()
|
||||
Image(systemName: viewModel.selectedScenicIds.contains(scenic.id) ? "checkmark.circle.fill" : "circle")
|
||||
}
|
||||
.foregroundStyle(viewModel.selectedScenicIds.contains(scenic.id) ? AppDesign.primary : AppDesign.textPrimary)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
if viewModel.projectType == 4 {
|
||||
Picker("门店", selection: Binding(get: { viewModel.selectedStoreId ?? 0 }, set: { viewModel.selectedStoreId = $0 == 0 ? nil : $0 })) {
|
||||
Text("请选择").tag(0)
|
||||
ForEach(viewModel.storeList) { store in
|
||||
Text(store.name).tag(store.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var storePriceForm: some View {
|
||||
ProjectFormCard(title: "价格与数量") {
|
||||
if viewModel.projectType == 4 {
|
||||
TextField("押金金额", text: $viewModel.priceDeposit)
|
||||
.keyboardType(.decimalPad)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
} else {
|
||||
TextField("项目价格", text: $viewModel.projectPrice)
|
||||
.keyboardType(.decimalPad)
|
||||
.appInputFieldStyle(cornerRadius: AppMetrics.CornerRadius.input, minHeight: AppMetrics.ControlSize.inputHeight)
|
||||
HStack {
|
||||
TextField("底片价", text: $viewModel.priceMaterial)
|
||||
TextField("精修价", text: $viewModel.pricePhoto)
|
||||
TextField("视频价", text: $viewModel.priceVideo)
|
||||
}
|
||||
.keyboardType(.decimalPad)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func prepare() async {
|
||||
let userId = snapshotStore.load()?.businessUserId ?? Int(snapshotStore.load()?.profile?.userId ?? "") ?? 0
|
||||
if let projectId {
|
||||
do {
|
||||
let detail = try await projectAPI.storeManagerProjectDetail(id: projectId)
|
||||
viewModel = StoreProjectEditorViewModel(mode: .edit(detail))
|
||||
} catch {
|
||||
viewModel.errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
await viewModel.loadScenicList(api: projectAPI, userId: userId)
|
||||
await viewModel.loadStoreList(api: projectAPI)
|
||||
}
|
||||
|
||||
private func submit() async {
|
||||
let userId = snapshotStore.load()?.businessUserId ?? Int(snapshotStore.load()?.profile?.userId ?? "") ?? 0
|
||||
await globalLoading.withLoading {
|
||||
let success = await viewModel.submit(userId: userId, api: projectAPI, uploadService: uploadService)
|
||||
if success { dismiss() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目卡片视图,展示封面、名称、状态和价格。
|
||||
private struct ProjectCardView: View {
|
||||
let item: PhotographerProjectItem
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
RemoteImage(urlString: item.coverProject.isEmpty ? item.coverVideo : item.coverProject, contentMode: .fill) {
|
||||
Image(systemName: "photo")
|
||||
.font(.system(size: 30))
|
||||
.foregroundStyle(AppDesign.primary)
|
||||
}
|
||||
.frame(height: 180)
|
||||
.clipped()
|
||||
VStack(alignment: .leading, spacing: AppMetrics.Spacing.small) {
|
||||
HStack {
|
||||
Text(item.name.isEmpty ? "未命名项目" : item.name)
|
||||
.font(.system(size: AppMetrics.FontSize.body, weight: .semibold))
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
Spacer()
|
||||
Text(item.statusName.isEmpty ? "状态\(item.status)" : item.statusName)
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(AppDesign.primary)
|
||||
}
|
||||
Text("¥\(item.price.isEmpty ? "0.00" : item.price)")
|
||||
.font(.system(size: AppMetrics.FontSize.title3, weight: .bold))
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
}
|
||||
.background(.white, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.card))
|
||||
.clipShape(RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.card))
|
||||
}
|
||||
}
|
||||
|
||||
/// 紧凑项目卡片视图,展示店铺项目摘要。
|
||||
private struct ProjectCompactCardView: View {
|
||||
let item: PhotographerProjectItem
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: AppMetrics.Spacing.medium) {
|
||||
RemoteImage(urlString: item.coverProject.isEmpty ? item.coverVideo : item.coverProject, contentMode: .fill) {
|
||||
Image(systemName: "photo")
|
||||
.foregroundStyle(AppDesign.primary)
|
||||
}
|
||||
.frame(width: 76, height: 76)
|
||||
.clipShape(RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.input))
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
Text(item.name.isEmpty ? "未命名项目" : item.name)
|
||||
.font(.system(size: AppMetrics.FontSize.body, weight: .semibold))
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
Text(item.typeName.isEmpty ? "类型\(item.type)" : item.typeName)
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(AppDesign.textSecondary)
|
||||
Text("¥\(item.price.isEmpty ? item.priceDeposit : item.price)")
|
||||
.font(.system(size: AppMetrics.FontSize.subheadline, weight: .semibold))
|
||||
.foregroundStyle(AppDesign.primary)
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
.background(.white, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.card))
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目详情内容视图。
|
||||
private struct ProjectDetailContentView: View {
|
||||
let detail: PhotographerProjectDetailResponse
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: AppMetrics.Spacing.medium) {
|
||||
ProjectCardView(item: detail.asListItem)
|
||||
ProjectFormCard(title: "服务内容") {
|
||||
HStack {
|
||||
service("底片", "\(detail.materialNum)")
|
||||
service("精修", "\(detail.photoNum)")
|
||||
service("视频", "\(detail.videoNum)")
|
||||
}
|
||||
}
|
||||
ProjectFormCard(title: "基础信息") {
|
||||
row("项目类型", detail.typeName)
|
||||
row("状态", detail.statusName)
|
||||
row("创建人", detail.creatorName)
|
||||
row("简介", detail.description)
|
||||
if !detail.auditRemark.isEmpty {
|
||||
row("审核备注", detail.auditRemark)
|
||||
}
|
||||
}
|
||||
if !detail.scenicList.isEmpty {
|
||||
ProjectFormCard(title: "打卡点") {
|
||||
Text(detail.scenicList.map(\.name).joined(separator: "、"))
|
||||
.font(.system(size: AppMetrics.FontSize.subheadline))
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func service(_ title: String, _ value: String) -> some View {
|
||||
VStack(spacing: 4) {
|
||||
Text(value)
|
||||
.font(.system(size: AppMetrics.FontSize.title3, weight: .bold))
|
||||
.foregroundStyle(AppDesign.primary)
|
||||
Text(title)
|
||||
.font(.system(size: AppMetrics.FontSize.caption))
|
||||
.foregroundStyle(AppDesign.textSecondary)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
|
||||
private func row(_ title: String, _ value: String) -> some View {
|
||||
HStack(alignment: .top) {
|
||||
Text(title)
|
||||
.foregroundStyle(AppDesign.textSecondary)
|
||||
Spacer()
|
||||
Text(value.isEmpty ? "--" : value)
|
||||
.multilineTextAlignment(.trailing)
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
}
|
||||
.font(.system(size: AppMetrics.FontSize.subheadline))
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目表单通用卡片。
|
||||
private struct ProjectFormCard<Content: View>: View {
|
||||
let title: String
|
||||
@ViewBuilder let content: () -> Content
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: AppMetrics.Spacing.small) {
|
||||
Text(title)
|
||||
.font(.system(size: AppMetrics.FontSize.body, weight: .semibold))
|
||||
.foregroundStyle(AppDesign.textPrimary)
|
||||
content()
|
||||
}
|
||||
.padding(AppMetrics.Spacing.medium)
|
||||
.background(.white, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.card))
|
||||
}
|
||||
}
|
||||
|
||||
/// 项目图片选择标签。
|
||||
private struct ProjectImagePickerLabel: View {
|
||||
let title: String
|
||||
let url: String
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
RemoteImage(urlString: url, contentMode: .fill) {
|
||||
VStack(spacing: 8) {
|
||||
Image(systemName: "photo.badge.plus")
|
||||
Text(title)
|
||||
}
|
||||
.foregroundStyle(AppDesign.primary)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 160)
|
||||
.background(AppDesign.primarySoft, in: RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.input))
|
||||
.clipShape(RoundedRectangle(cornerRadius: AppMetrics.CornerRadius.input))
|
||||
}
|
||||
}
|
||||
|
||||
/// PhotosPicker 项目图片加载器。
|
||||
private enum ProjectPickerLoader {
|
||||
/// 从单个 PhotosPickerItem 读取项目图片。
|
||||
static func loadImage(from item: PhotosPickerItem?) async -> ProjectLocalImage? {
|
||||
guard let item, let data = try? await item.loadTransferable(type: Data.self) else { return nil }
|
||||
let fileName = fileName(for: item)
|
||||
return ProjectLocalImage(data: data, fileName: fileName, previewURL: dataURL(data: data, fileName: fileName))
|
||||
}
|
||||
|
||||
/// 从多个 PhotosPickerItem 读取项目图片。
|
||||
static func loadImages(from items: [PhotosPickerItem]) async -> [ProjectLocalImage] {
|
||||
var result: [ProjectLocalImage] = []
|
||||
for item in items {
|
||||
if let image = await loadImage(from: item) {
|
||||
result.append(image)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private static func fileName(for item: PhotosPickerItem) -> String {
|
||||
let ext = item.supportedContentTypes.first?.preferredFilenameExtension ?? "jpg"
|
||||
return "project_\(UUID().uuidString).\(ext)"
|
||||
}
|
||||
|
||||
private static func dataURL(data: Data, fileName: String) -> String? {
|
||||
let mime = UTType(filenameExtension: URL(fileURLWithPath: fileName).pathExtension)?.preferredMIMEType ?? "image/jpeg"
|
||||
return "data:\(mime);base64,\(data.base64EncodedString())"
|
||||
}
|
||||
}
|
||||
|
||||
private extension PhotographerProjectDetailResponse {
|
||||
/// 将详情转换为列表卡片展示项。
|
||||
var asListItem: PhotographerProjectItem {
|
||||
PhotographerProjectItem(
|
||||
id: id,
|
||||
type: type,
|
||||
typeName: typeName,
|
||||
status: status,
|
||||
statusName: statusName,
|
||||
name: name,
|
||||
coverProject: coverProject,
|
||||
coverVideo: cover,
|
||||
price: price,
|
||||
otPrice: otPrice,
|
||||
priceDeposit: priceDeposit,
|
||||
attrLabel: attrLabel
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user