183 lines
5.7 KiB
Swift
183 lines
5.7 KiB
Swift
//
|
||
// LeanCloud+.swift
|
||
// 对leancloud平台的扩展代码
|
||
//
|
||
// Created by CC-star on 2025/7/2.
|
||
//
|
||
|
||
import Foundation
|
||
import LeanCloud
|
||
|
||
|
||
extension LCQuery {
|
||
func getFirst() async throws -> LCObject {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
_ = self.getFirst { result in
|
||
switch result {
|
||
case .success(object: let lcObject):
|
||
continuation.resume(returning: lcObject)
|
||
case .failure(let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// 异步获取匹配条件的对象数量
|
||
func count() async throws -> Int {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
self.count { result in
|
||
switch result {
|
||
case .success(let count):
|
||
continuation.resume(returning: count)
|
||
case .failure(let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//返回lcobject数组
|
||
func find() async throws -> [LCObject] {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
self.find { result in
|
||
switch result {
|
||
case .success(let objects):
|
||
continuation.resume(returning: objects)
|
||
case .failure(let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//此方法不用,仅供学习
|
||
func find<T: Decodable>(as type: T.Type) async throws -> [T] {
|
||
let lcObjects = try await withCheckedThrowingContinuation { continuation in
|
||
self.find { result in
|
||
switch result {
|
||
case .success(objects: let lcObjects):
|
||
continuation.resume(returning: lcObjects)
|
||
case .failure(error: let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
var res: [T] = []
|
||
for lcObject in lcObjects {
|
||
//dictionaryValue失效,无法转化成字典类型
|
||
guard let lcDic = lcObject.dictionaryValue else { throw UserError.lcObjToDicError }
|
||
let data = try JSONSerialization.data(withJSONObject: lcDic)
|
||
let object = try JSONDecoder().decode(T.self, from: data)
|
||
res.append(object)
|
||
}
|
||
return res
|
||
}
|
||
}
|
||
|
||
|
||
extension LCObject {
|
||
//将 LeanCloud 的保存方法操作保存包转为 async 方法
|
||
func save() async throws {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
_ = self.save() { result in
|
||
switch result {
|
||
case .success:
|
||
//保存成功
|
||
continuation.resume()
|
||
case .failure(let error):
|
||
//保存失败,抛出错误
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 异步删除方法
|
||
func delete() async throws {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
_ = self.delete { result in
|
||
switch result {
|
||
case .success:
|
||
continuation.resume()
|
||
case .failure(let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// 批量删除对象的异步方法
|
||
static func delete(_ objects: [LCObject]) async throws {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
self.delete(objects) { result in
|
||
switch result {
|
||
case .success:
|
||
continuation.resume()
|
||
case .failure(let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
extension LCSMSClient {
|
||
//把requestShortMessage(请求验证码)扩展成async方法
|
||
static func requestShortMessage(
|
||
mobilePhoneNumber: String,
|
||
templateName: String,
|
||
signatureName: String
|
||
) async throws {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
LCSMSClient.requestShortMessage(
|
||
mobilePhoneNumber: mobilePhoneNumber,
|
||
templateName: templateName,
|
||
signatureName: signatureName
|
||
// ,variables: ["name": LCString("BOSS大直招"), "ttl": LCNumber("5")]
|
||
) { result in
|
||
switch result {
|
||
case .success:
|
||
continuation.resume()
|
||
case .failure(let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
extension LCUser {
|
||
//把signUpOrLogIn(验证码注册或登录)扩展成async方法
|
||
static func signUpOrLogIn(mobilePhoneNumber: String, verificationCode: String) async throws -> LCUser {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
_ = LCUser.signUpOrLogIn(mobilePhoneNumber: mobilePhoneNumber, verificationCode: verificationCode) { result in
|
||
switch result {
|
||
case .success(object: let user):
|
||
continuation.resume(returning: user)
|
||
case .failure(error: let error):
|
||
continuation.resume(throwing: error)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
extension LCFile {
|
||
func save() async throws -> String {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
_ = self.save { result in
|
||
switch result {
|
||
case .success:
|
||
if let url = self.url?.value {
|
||
continuation.resume(returning: url) // 保存成功,返回文件 URL
|
||
} else {
|
||
let error = UserError.saveFileSuccessButNoURL // 自定义错误
|
||
continuation.resume(throwing: error)
|
||
}
|
||
case .failure(let error):
|
||
continuation.resume(throwing: error) // 直接抛出 LeanCloud 的错误
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|