Files
suixinkan_ios_new/suixinkan/Features/Projects/ViewModels/ProjectViewModels.swift
汉秋 311a70d610 新增项目、排期与邀请模块,并接入首页路由
将项目管理、排期管理与摄影师邀请流程从占位页迁移为完整实现,包含项目图片 OSS 上传与共享业务模型。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 15:57:15 +08:00

610 lines
22 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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
}
}