Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
galaxy-iOS
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mobile-group
galaxy-iOS
Commits
f3c5ebb9
Commit
f3c5ebb9
authored
May 20, 2024
by
pete谢兆麟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
OSS上传私链转公链等相关问题
parent
2c8489eb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
17 deletions
+71
-17
YHBaseViewModel.swift
galaxy/galaxy/Classes/Base/C/YHBaseViewModel.swift
+28
-12
YHOSSManager.swift
galaxy/galaxy/Classes/Tools/Upload/YHOSSManager.swift
+43
-5
No files found.
galaxy/galaxy/Classes/Base/C/YHBaseViewModel.swift
View file @
f3c5ebb9
...
...
@@ -55,20 +55,36 @@ class YHBaseViewModel {
//换取一张
func
getPublicImageUrl
(
_
url
:
String
,
callBackBlock
:
@escaping
(
_
success
:
String
?,
_
error
:
YHErrorModel
?)
->
())
{
let
strUrl
=
YHBaseUrlManager
.
shared
.
curOssURL
()
+
YHAllApiName
.
OSS
.
getPublicApi
let
param
:
[
String
:
Any
]
=
[
"fileUrl"
:
url
]
let
_
=
YHNetRequest
.
getRequest
(
url
:
strUrl
,
params
:
param
)
{
[
weak
self
]
json
,
code
in
//1. json字符串 转 对象
guard
let
_
=
self
else
{
return
}
if
json
.
code
==
200
{
callBackBlock
(
json
.
data
as?
String
,
nil
)
}
else
{
let
error
:
YHErrorModel
=
YHErrorModel
(
errorCode
:
Int32
(
json
.
code
),
errorMsg
:
json
.
msg
)
callBackBlock
(
nil
,
error
)
// let strUrl = YHBaseUrlManager.shared.curOssURL() + YHAllApiName.OSS.getPublicApi
// let param: [String: Any] = ["fileUrl": url]
// let _ = YHNetRequest.getRequest(url: strUrl, params: param) { [weak self] json, code in
// //1. json字符串 转 对象
// guard let _ = self else { return }
// if json.code == 200 {
// callBackBlock(json.data as? String, nil)
// } else {
// let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
// callBackBlock(nil, error)
// }
// } failBlock: { err in
// callBackBlock(nil,err)
// }
if
YHNetworkStatusManager
.
shared
.
isNetWorkOK
==
false
{
DispatchQueue
.
main
.
async
{
YHHUD
.
flash
(
message
:
"网络异常"
)
let
errorModel
=
YHErrorModel
(
errorCode
:
0
,
errorMsg
:
"网络异常"
)
callBackBlock
(
nil
,
errorModel
)
return
}
}
failBlock
:
{
err
in
callBackBlock
(
nil
,
err
)
return
}
YHOSSManager
.
share
.
getPublic
(
url
:
url
,
callBackBlock
:
{
[
weak
self
]
success
,
error
in
guard
let
_
=
self
else
{
return
}
DispatchQueue
.
main
.
async
{
// 在这里执行需要在主线程上完成的任务
callBackBlock
(
success
,
error
)
}
})
}
//换取多张图片
...
...
galaxy/galaxy/Classes/Tools/Upload/YHOSSManager.swift
View file @
f3c5ebb9
...
...
@@ -47,8 +47,9 @@ class YHOSSManager: NSObject, URLSessionDelegate {
}
func
getOSSID
()
{
let
param
:
[
String
:
Any
]
=
[
"path"
:
"4001001"
]
let
strUrl
=
YHBaseUrlManager
.
shared
.
curOssURL
()
+
YHAllApiName
.
OSS
.
getID
let
_
=
YHNetRequest
.
getRequest
(
url
:
strUrl
)
{
[
weak
self
]
json
,
code
in
let
_
=
YHNetRequest
.
getRequest
(
url
:
strUrl
,
params
:
param
)
{
[
weak
self
]
json
,
code
in
//1. json字符串 转 对象
guard
let
self
=
self
else
{
return
}
let
dic
=
json
.
data
...
...
@@ -71,8 +72,12 @@ class YHOSSManager: NSObject, URLSessionDelegate {
let
timestamp
=
Date
()
.
timeIntervalSince1970
let
randomInt
=
Int
.
random
(
in
:
1
...
100
)
// 随机生成1到10之间的整数
let
name
=
"
\(
UInt64
(
timestamp
)
)
"
+
"
\(
randomInt
)
"
+
".jpg"
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
self
.
fileName
.
append
(
name
)
request
.
objectKey
=
self
.
model
.
bucketPath
+
"/"
+
"
\(
name
)
"
request
.
objectKey
=
self
.
model
.
bucketPath
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
name
)
"
request
.
contentDisposition
=
"inline"
request
.
uploadProgress
=
{
(
bytesSent
:
Int64
,
totalBytesSent
:
Int64
,
totalBytesExpectedToSend
:
Int64
)
->
Void
in
printLog
(
"bytesSent:
\(
bytesSent
)
,totalBytesSent:
\(
totalBytesSent
)
,totalBytesExpectedToSend:
\(
totalBytesExpectedToSend
)
"
)
}
...
...
@@ -84,7 +89,10 @@ class YHOSSManager: NSObject, URLSessionDelegate {
if
(
result
.
error
!=
nil
)
{
callBackBlock
(
nil
,
YHErrorModel
())
}
else
{
let
string
=
"https://"
+
self
.
model
.
bucket
+
"."
+
"oss-cn-shenzhen.aliyuncs.com/"
+
self
.
model
.
bucketPath
+
"/"
+
"
\(
self
.
fileName
.
first
??
""
)
"
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
let
string
=
"https://"
+
self
.
model
.
bucket
+
"."
+
"oss-cn-shenzhen.aliyuncs.com/"
+
self
.
model
.
bucketPath
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
self
.
fileName
.
first
??
""
)
"
self
.
fileName
.
removeFirst
()
print
(
"------------
\(
string
)
"
)
callBackBlock
(
string
,
YHErrorModel
())
...
...
@@ -102,7 +110,11 @@ class YHOSSManager: NSObject, URLSessionDelegate {
let
timestamp
=
Date
()
.
timeIntervalSince1970
let
name
=
"
\(
UInt64
(
timestamp
)
)
"
+
".
\(
fileExtension
)
"
self
.
fileName
.
append
(
name
)
request
.
objectKey
=
self
.
model
.
bucketPath
+
"/"
+
"
\(
name
)
"
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
request
.
objectKey
=
self
.
model
.
bucketPath
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
name
)
"
request
.
contentDisposition
=
"inline"
request
.
uploadProgress
=
{
(
bytesSent
:
Int64
,
totalBytesSent
:
Int64
,
totalBytesExpectedToSend
:
Int64
)
->
Void
in
printLog
(
"bytesSent:
\(
bytesSent
)
,totalBytesSent:
\(
totalBytesSent
)
,totalBytesExpectedToSend:
\(
totalBytesExpectedToSend
)
"
)
}
...
...
@@ -116,11 +128,37 @@ class YHOSSManager: NSObject, URLSessionDelegate {
let
result
=
error
.
description
callBackBlock
(
nil
,
YHErrorModel
())
}
else
{
let
string
=
"https://"
+
self
.
model
.
bucket
+
"."
+
"oss-cn-shenzhen.aliyuncs.com/"
+
self
.
model
.
bucketPath
+
"/"
+
"
\(
self
.
fileName
.
first
??
""
)
"
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
let
string
=
"https://"
+
self
.
model
.
bucket
+
"."
+
"oss-cn-shenzhen.aliyuncs.com/"
+
self
.
model
.
bucketPath
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
self
.
fileName
.
first
??
""
)
"
self
.
fileName
.
removeFirst
()
callBackBlock
(
string
,
YHErrorModel
())
}
return
})
.
waitUntilFinished
()
}
func
getPublic
(
url
:
String
,
callBackBlock
:
@escaping
(
_
success
:
String
?,
_
error
:
YHErrorModel
?)
->
())
{
let
bucketName
=
self
.
model
.
bucket
let
pathComponents
=
url
.
pathComponents
var
bucketPath
=
""
if
pathComponents
.
count
>=
4
{
bucketPath
=
pathComponents
[
2
]
+
"/"
+
pathComponents
[
3
]
}
let
objectKey
=
bucketPath
+
"/"
+
url
.
lastPathComponent
let
task
=
mClient
.
presignConstrainURL
(
withBucketName
:
bucketName
,
withObjectKey
:
objectKey
,
withExpirationInterval
:
60
*
5
)
task
.
continue
({
(
t
)
->
Any
?
in
let
result
=
t
if
(
result
.
error
!=
nil
)
{
let
error
:
NSError
=
(
task
.
error
)
!
as
NSError
_
=
error
.
description
callBackBlock
(
nil
,
YHErrorModel
())
}
else
{
let
string
=
result
.
result
as!
String
callBackBlock
(
string
,
YHErrorModel
())
}
return
})
.
waitUntilFinished
()
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment