From 2cf7798754324278c0d93737094d4956793a6067 Mon Sep 17 00:00:00 2001 From: Cheliangzhao Date: Thu, 19 Sep 2024 19:41:59 +0800 Subject: [PATCH] feat: http; refactor: chat source type --- .../cangjie/src/components/chat/ChatList.cj | 6 +++- .../src/components/chat/ChatMessage.cj | 20 +++--------- .../cangjie/src/support/support_net/Http.cj | 32 +++++++++++++++++++ .../src/main/cangjie/src/types/EChatSource.cj | 27 ++++++++++++++++ entry/src/main/module.json5 | 5 +++ oh-package-lock.json5 | 16 +++++----- 6 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 entry/src/main/cangjie/src/support/support_net/Http.cj create mode 100644 entry/src/main/cangjie/src/types/EChatSource.cj diff --git a/entry/src/main/cangjie/src/components/chat/ChatList.cj b/entry/src/main/cangjie/src/components/chat/ChatList.cj index a4b3c61..7ff5ad3 100644 --- a/entry/src/main/cangjie/src/components/chat/ChatList.cj +++ b/entry/src/main/cangjie/src/components/chat/ChatList.cj @@ -12,6 +12,7 @@ import ohos_app_cangjie_entry.mock.* import ohos_app_cangjie_entry.types.ChatData import ohos_app_cangjie_entry.utils.Logger import ohos_app_cangjie_entry.view_model.ChatListControl +import ohos_app_cangjie_entry.types.EChatSource @Component public class ChatList { @@ -34,10 +35,13 @@ public class ChatList { item: ChatData, _: Int => ListItem { ChatMessage( message: item.message, - source: item.source, + source: EChatSource.fromString(item.source), ) } }, + keyGeneratorFunc: { + item, _ => item.id + } ) } }.height(100.percent).width(100.percent).padding(top: 5.vp) diff --git a/entry/src/main/cangjie/src/components/chat/ChatMessage.cj b/entry/src/main/cangjie/src/components/chat/ChatMessage.cj index 33330d1..a984b45 100644 --- a/entry/src/main/cangjie/src/components/chat/ChatMessage.cj +++ b/entry/src/main/cangjie/src/components/chat/ChatMessage.cj @@ -9,15 +9,7 @@ import ohos.state_macro_manage.* import ohos.state_manage.* import ohos.base.* import ohos_app_cangjie_entry.components.chat.chatMessages.* - -public enum EChatSource { - | ME - | YOU - - operator func ==(right: EChatSource) { - // fixme: enum无法比较 - } -} +import ohos_app_cangjie_entry.types.EChatSource /** * 只限定UI @@ -27,17 +19,13 @@ public class ChatMessage { @Prop var message: String @Prop - var source: String - - func upperSource() { - return source.toAsciiUpper() - } + var source: EChatSource @Builder func matchSource() { - if (this.upperSource() == 'ME') { + if (let EChatSource.ME <- this.source) { ChatMessageMe(message: this.message) - } else if (this.upperSource() == 'YOU') { + } else if (let EChatSource.YOU <- this.source) { ChatMessageYou(message: this.message) } else {} } diff --git a/entry/src/main/cangjie/src/support/support_net/Http.cj b/entry/src/main/cangjie/src/support/support_net/Http.cj new file mode 100644 index 0000000..b5d72e4 --- /dev/null +++ b/entry/src/main/cangjie/src/support/support_net/Http.cj @@ -0,0 +1,32 @@ +/** + * Created on 2024/9/19 + */ +package ohos_app_cangjie_entry.support.support_net + +import net.http.* +import std.time.* +import std.sync.* + +public enum EHttpMethod { + | GET + | POST +} + +public class Http { + public func get(url!: String): ?String { + let client = ClientBuilder().build() + try { + let response = client.get(url) + let responseHeaders = response.headers + let reponseBody = response.body + let dataBuffer = Array(8 * 1024 * 1024 * 10, item: 0) + let dataLength = reponseBody.read(dataBuffer) + let str = String.fromUtf8(dataBuffer[..dataLength]) + return str + } catch (e: Error) { + return None + } finally { + client.close() + } + } +} diff --git a/entry/src/main/cangjie/src/types/EChatSource.cj b/entry/src/main/cangjie/src/types/EChatSource.cj new file mode 100644 index 0000000..5dd35bd --- /dev/null +++ b/entry/src/main/cangjie/src/types/EChatSource.cj @@ -0,0 +1,27 @@ +/** + * Created on 2024/9/19 + */ +package ohos_app_cangjie_entry.types + +public enum EChatSource { + | ME + | YOU + | SYS + + public func toString() { + match (this) { + case EChatSource.ME => 'ME' + case EChatSource.YOU => 'YOU' + case EChatSource.SYS => 'SYS' + } + } + + public static func fromString(str: String) { + match (str) { + case 'ME' => EChatSource.ME + case 'YOU' => EChatSource.YOU + case 'SYS' => EChatSource.SYS + case _ => EChatSource.ME + } + } +} diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 index 4b71f19..cf28891 100644 --- a/entry/src/main/module.json5 +++ b/entry/src/main/module.json5 @@ -31,6 +31,11 @@ } ] } + ], + "requestPermissions": [ + { + "name": "ohos.permission.INTERNET" + } ] } } \ No newline at end of file diff --git a/oh-package-lock.json5 b/oh-package-lock.json5 index 205c6cf..16cf806 100644 --- a/oh-package-lock.json5 +++ b/oh-package-lock.json5 @@ -1,6 +1,6 @@ { "meta": { - "stableOrder": true + "stableOrder": false }, "lockfileVersion": 3, "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", @@ -9,19 +9,19 @@ "@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19" }, "packages": { - "@ohos/hamock@1.0.0": { - "name": "@ohos/hamock", - "version": "1.0.0", - "integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==", - "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har", - "registryType": "ohpm" - }, "@ohos/hypium@1.0.19": { "name": "@ohos/hypium", "version": "1.0.19", "integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==", "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har", "registryType": "ohpm" + }, + "@ohos/hamock@1.0.0": { + "name": "@ohos/hamock", + "version": "1.0.0", + "integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==", + "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har", + "registryType": "ohpm" } } } \ No newline at end of file