@@ -111,15 +111,13 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
111
111
updateCodeTheme ( )
112
112
var urlString = urlTF. stringValue
113
113
guard !urlString. isBlank else { return }
114
-
115
114
urlString = urlString. urlEncoding ( )
116
115
UserDefaults . standard. setValue ( urlString, forKey: CacheKeys . lastInputURL)
117
116
118
117
guard let url = URL ( string: urlString) else {
119
118
showAlertInfoWith ( " 无效的URL格式 " , . warning)
120
119
return
121
120
}
122
-
123
121
Task {
124
122
do {
125
123
let jsonString = try await fetchJsonData ( from: url)
@@ -133,24 +131,20 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
133
131
/// 使用async/await获取JSON数据
134
132
private func fetchJsonData( from url: URL ) async throws -> String {
135
133
var request = URLRequest ( url: url, cachePolicy: . useProtocolCachePolicy, timeoutInterval: 30 )
136
-
137
134
// 处理POST请求
138
135
if reqTypeBtn. indexOfSelectedItem == 1 {
139
136
request = try configurePostRequest ( originalUrl: url)
140
137
}
141
-
142
138
let ( data, _) = try await URLSession . shared. data ( for: request)
143
139
let jsonObj = try JSONSerialization . jsonObject ( with: data, options: . mutableContainers)
144
140
145
141
guard JSONSerialization . isValidJSONObject ( jsonObj) else {
146
142
throw NSError ( domain: " JSONError " , code: 1 , userInfo: [ NSLocalizedDescriptionKey: " 无效的JSON响应 " ] )
147
143
}
148
-
149
144
let formatJsonData = try JSONSerialization . data ( withJSONObject: jsonObj, options: . prettyPrinted)
150
145
guard let jsonString = String ( data: formatJsonData, encoding: . utf8) else {
151
146
throw NSError ( domain: " JSONError " , code: 2 , userInfo: [ NSLocalizedDescriptionKey: " 无法将JSON数据转换为字符串 " ] )
152
147
}
153
-
154
148
return jsonString
155
149
}
156
150
@@ -159,49 +153,40 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
159
153
guard let query = originalUrl. query else {
160
154
return URLRequest ( url: originalUrl, cachePolicy: . useProtocolCachePolicy, timeoutInterval: 30 )
161
155
}
162
-
163
156
let urlString = originalUrl. absoluteString
164
157
var urlWithoutQuery = urlString. replacingOccurrences ( of: query, with: " " )
165
158
if urlWithoutQuery. hasSuffix ( " ? " ) {
166
159
urlWithoutQuery. removeLast ( )
167
160
}
168
-
169
161
guard let newUrl = URL ( string: urlWithoutQuery) else {
170
162
throw NSError ( domain: " URLError " , code: 3 , userInfo: [ NSLocalizedDescriptionKey: " 无法创建不带查询参数的URL " ] )
171
163
}
172
-
173
164
var request = URLRequest ( url: newUrl, cachePolicy: . useProtocolCachePolicy, timeoutInterval: 30 )
174
165
request. httpMethod = " POST "
175
166
request. setValue ( " application/x-www-form-urlencoded " , forHTTPHeaderField: " Content-Type " )
176
167
request. httpBody = query. data ( using: . utf8)
177
-
178
168
return request
179
169
}
180
170
181
171
/// 开始生成代码
182
172
@IBAction func startMakeCode( _ sender: NSButton ) {
183
173
guard let jsonString = jsonTextView. textStorage? . string, !jsonString. isBlank else { return }
184
-
185
174
// 处理JSON字符串
186
175
let trimmedStr = jsonString. trimmingCharacters ( in: . whitespacesAndNewlines)
187
176
let attriStr = NSMutableString ( string: trimmedStr)
188
-
189
177
// 处理注释
190
178
var commentDicts : [ String : String ] = [ : ]
191
179
parseComments ( from: trimmedStr, commentDicts: & commentDicts, attriStr: attriStr)
192
-
193
180
do {
194
181
guard let jsonData = attriStr. data ( using: String . Encoding. utf8. rawValue) else {
195
182
showAlertInfoWith ( " 警告: 请输入有效的JSON字符串! " , . warning)
196
183
return
197
184
}
198
-
199
185
let jsonObj = try JSONSerialization . jsonObject ( with: jsonData, options: . mutableContainers)
200
186
guard JSONSerialization . isValidJSONObject ( jsonObj) else {
201
187
showAlertInfoWith ( " 警告: 不是有效的JSON格式!!! " , . warning)
202
188
return
203
189
}
204
-
205
190
// 保存用户输入内容并更新主题
206
191
saveUserInputContent ( )
207
192
updateCodeTheme ( )
@@ -216,7 +201,6 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
216
201
configJsonTextView ( text: formattedJsonString, textView: jsonTextView, color: NSColor . blue)
217
202
}
218
203
}
219
-
220
204
Task {
221
205
await generateModelCode ( from: jsonObj)
222
206
}
@@ -256,7 +240,6 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
256
240
257
241
@objc private func caculateInputContentWidth( ) {
258
242
guard let tf = currentInputTF else { return }
259
-
260
243
let constraints = tf. constraints
261
244
let attributes = [ NSAttributedString . Key. font: tf. font as Any ]
262
245
let string = NSString ( string: tf. stringValue)
@@ -265,9 +248,7 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
265
248
options: [ . usesLineFragmentOrigin, . usesFontLeading] ,
266
249
attributes: attributes
267
250
) . width + 10
268
-
269
251
strWidth = max ( strWidth, 114 )
270
-
271
252
for constraint in constraints {
272
253
if constraint. firstAttribute == . width {
273
254
constraint. constant = strWidth
@@ -342,7 +323,6 @@ private extension ViewController {
342
323
/// 处理生成的代码
343
324
func handleGeneratedCode( _ hString: NSMutableString , _ mString: NSMutableString ) {
344
325
var multiplier : CGFloat = 3 / 5.0
345
-
346
326
switch builder. config. codeType {
347
327
case . objectiveC:
348
328
configJsonTextView ( text: mString as String , textView: mTextView, color: codeTextColor)
@@ -351,7 +331,6 @@ private extension ViewController {
351
331
case . dart:
352
332
configJsonTextView ( text: mString as String , textView: mTextView, color: codeTextColor)
353
333
}
354
-
355
334
hTextViewHeightPriority = modifyConstraint ( hTextViewHeightPriority, multiplier)
356
335
configJsonTextView ( text: hString as String , textView: hTextView, color: codeTextColor)
357
336
@@ -414,49 +393,40 @@ private extension ViewController {
414
393
if let lastUrl = UserDefaults . standard. string ( forKey: CacheKeys . lastInputURL) {
415
394
urlTF. stringValue = lastUrl
416
395
}
417
-
418
396
// 父类名或协议名
419
397
if let superClassName = UserDefaults . standard. string ( forKey: CacheKeys . superClassName) {
420
398
superClassNameTF. stringValue = superClassName
421
399
}
422
-
423
400
// 模型名前缀
424
401
if let modelNamePrefix = UserDefaults . standard. string ( forKey: CacheKeys . modelNamePrefix) {
425
402
modelNamePrefixTF. stringValue = modelNamePrefix
426
403
}
427
-
428
404
// 根模型名
429
405
if let rootModelName = UserDefaults . standard. string ( forKey: CacheKeys . rootModelName) {
430
406
rootModelNameTF. stringValue = rootModelName
431
407
}
432
-
433
408
// 作者名
434
409
if let authorName = UserDefaults . standard. string ( forKey: CacheKeys . authorName) {
435
410
authorNameTF. stringValue = authorName
436
411
}
437
-
438
412
// 输出文件路径
439
413
if let outFilePath = UserDefaults . standard. string ( forKey: CacheKeys . generateFilePath) {
440
414
outputFilePath = outFilePath
441
415
}
442
-
443
416
// 代码类型
444
417
let codeTypeIndex = UserDefaults . standard. integer ( forKey: CacheKeys . buildCodeType)
445
418
if codeTypeIndex > 0 , codeTypeIndex <= SKCodeType . allCases. count {
446
419
builder. config. codeType = SKCodeType . allCases [ codeTypeIndex - 1 ]
447
420
codeTypeBtn. selectItem ( at: codeTypeIndex - 1 )
448
421
}
449
-
450
422
// JSON模型类型
451
423
let jsonTypeIndex = UserDefaults . standard. integer ( forKey: CacheKeys . supportJSONModelType)
452
424
if jsonTypeIndex >= 0 , jsonTypeIndex < SKJSONModelType . allCases. count {
453
425
builder. config. jsonType = SKJSONModelType . allCases [ jsonTypeIndex]
454
426
jsonTypeBtn. selectItem ( at: jsonTypeIndex)
455
427
}
456
-
457
428
// 是否生成文件
458
429
generateFileBtn. state = UserDefaults . standard. bool ( forKey: CacheKeys . shouldGenerateFile) ? . on : . off
459
-
460
430
// 是否生成注释
461
431
generateComment. state = UserDefaults . standard. bool ( forKey: CacheKeys . shouldGenerateComment) ? . on : . off
462
432
}
@@ -469,7 +439,6 @@ private extension ViewController {
469
439
builder. config. codeType = SKCodeType . allCases [ codeTypeIndex]
470
440
UserDefaults . standard. set ( codeTypeIndex + 1 , forKey: CacheKeys . buildCodeType)
471
441
}
472
-
473
442
// 父类名或协议名
474
443
var superClassName = " "
475
444
if builder. config. codeType == . dart || builder. config. codeType == . typeScript {
0 commit comments