Files
suixinkan_ios_new/suixinkan/Features/CooperationOrder/ViewModels/BindAcquirerViewModel.swift
汉秋 1970572edd 迁移合作订单完整能力,并统一 AppRoleCode 角色权限与首页菜单展示。
新增 CooperationOrder 模块(双 Tab 列表、获客员绑定、主 Tab 扫码)、订单来源/带客单绑定及配套 API 测试;同步引入 roleCode 权限匹配与相关单元测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 13:33:00 +08:00

71 lines
2.3 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.

//
// BindAcquirerViewModel.swift
// suixinkan
//
// Created by Codex on 2026/6/29.
//
import Foundation
import Combine
/// ViewModel
@MainActor
final class BindAcquirerViewModel: ObservableObject {
@Published var inviteInfo: SaleUserInviteInfo?
@Published var isLoading = false
@Published var isAlreadyBound = false
@Published var errorMessage: String?
private var saleUserId = 0
///
func load(api: CooperationOrderServing, saleUserId: Int) async {
guard saleUserId > 0 else {
errorMessage = "获客员信息无效"
return
}
if self.saleUserId == saleUserId && (inviteInfo != nil || isAlreadyBound) { return }
self.saleUserId = saleUserId
isAlreadyBound = false
isLoading = true
errorMessage = nil
defer { isLoading = false }
do {
var info = try await api.saleUserInviteInfo(saleUserId: saleUserId)
if info.saleUserId <= 0 {
info = SaleUserInviteInfo(
saleUserId: saleUserId,
isBound: info.isBound,
photographerName: info.photographerName,
photographerPhone: info.photographerPhone,
salerName: info.salerName,
salerPhone: info.salerPhone,
shareCode: info.shareCode,
subtitle: info.subtitle,
title: info.title
)
}
if info.isBound {
isAlreadyBound = true
}
inviteInfo = info
} catch {
handleBindError(error.localizedDescription.isEmpty ? "获取邀请信息失败" : error.localizedDescription)
}
}
///
func acceptBind(api: CooperationOrderServing) async throws {
guard saleUserId > 0, !isAlreadyBound else { return }
try await api.saleUserAcceptBind(saleUserId: saleUserId)
}
///
func handleBindError(_ message: String) {
errorMessage = message
if message.contains("已绑定") {
isAlreadyBound = true
}
}
}