Просмотр исходного кода

get GroupAnnouncementList is working now

luo123 6 лет назад
Родитель
Сommit
aa61e9441a

+ 0 - 5
mirai-core-qqandroid/build.gradle.kts

@@ -72,11 +72,6 @@ kotlin {
                 api(kotlinx("io", kotlinXIoVersion))
                 api(kotlinx("coroutines-io", coroutinesIoVersion))
                 api(kotlinx("coroutines-core", coroutinesVersion))
-
-                api(ktor("client-core", ktorVersion))
-
-                implementation(ktor("client-json",ktorVersion))
-                implementation(ktor("client-serialization",ktorVersion))
             }
         }
         commonMain {

+ 22 - 11
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt

@@ -10,14 +10,17 @@
 package net.mamoe.mirai.qqandroid
 
 import io.ktor.client.HttpClient
-import io.ktor.client.features.json.JsonFeature
-import io.ktor.client.features.json.serializer.KotlinxSerializer
 import io.ktor.client.request.*
+import io.ktor.client.request.forms.MultiPartFormDataContent
 import io.ktor.client.request.forms.formData
 import kotlinx.coroutines.async
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withTimeoutOrNull
 import kotlinx.io.core.Closeable
+import kotlinx.serialization.MissingFieldException
+import kotlinx.serialization.json.Json
+import kotlinx.serialization.json.JsonConfiguration
+import kotlinx.serialization.json.int
 import net.mamoe.mirai.LowLevelAPI
 import net.mamoe.mirai.contact.*
 import net.mamoe.mirai.data.*
@@ -567,25 +570,33 @@ internal class GroupImpl(
     }
 
     @MiraiExperimentalAPI
-    override suspend fun getAnnouncements(page: Int, amount: Int): GroupAnnouncementList {
+    override suspend fun getAnnouncements(page: Int, amount: Int): GroupAnnouncementList? {
+        val json = Json(JsonConfiguration(ignoreUnknownKeys = true))
         val data = bot.network.async {
-            HttpClient { install(JsonFeature) { serializer = KotlinxSerializer() } }.post<GroupAnnouncementList> {
+            HttpClient().post<String> {
                 url("https://web.qun.qq.com/cgi-bin/announce/list_announce")
-                formData {
+                body = MultiPartFormDataContent(formData {
                     append("qid", id)
                     append("bkn", getBkn())
                     append("ft", 23)  //好像是一个用来识别应用的参数
-                    append("s", -page)  // 第一页这里的参数应该是-1
+                    append("s", if (page ==1) 0 else -(page*amount+1))  // 第一页这里的参数应该是-1
                     append("n", amount)
+                    append("ni",if (page ==1) 1 else 0)
                     append("format", "json")
+                })
+                headers {
+                    append(
+                        "cookie",
+                        "uin=o${bot.selfQQ.id}; skey=${bot.client.wLoginSigInfo.sKey.data.encodeToString()}; p_uin=o${bot.selfQQ.id};"
+                    )
                 }
-                header(
-                    "cookie",
-                    "uin=o${bot.selfQQ.id}; skey=${bot.client.wLoginSigInfo.sKey.data.encodeToString()}; p_uin=o${bot.selfQQ.id};"
-                )
             }
         }
-        return data.await()
+
+        val rep = data.await()
+        bot.network.logger.error(rep)
+
+            return json.parse(GroupAnnouncementList.serializer(), rep)
     }
 
 

+ 1 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt

@@ -157,7 +157,7 @@ expect abstract class Group() : Contact, CoroutineScope {
      *
      * */
     @MiraiExperimentalAPI
-    abstract suspend fun getAnnouncements(page: Int = 1, amount: Int = 10):GroupAnnouncementList
+    abstract suspend fun getAnnouncements(page: Int = 1, amount: Int = 10):GroupAnnouncementList?
 
     /**
      * 让机器人退出这个群. 机器人必须为非群主才能退出. 否则将会失败

+ 5 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/data/GroupAnnouncement.kt

@@ -5,12 +5,16 @@ import kotlinx.serialization.Serializable
 
 /**
  * 群公告数据类
+ * getGroupAnnouncementList时,如果page=1,那么你可以在inst里拿到一些置顶公告
  *
  *
  */
 @Serializable
 data class GroupAnnouncementList(
-    val feeds: List<GroupAnnouncement>
+    val ec: Int,  //状态码 0 是正常的
+    @SerialName("em") val msg:String,   //信息
+    val feeds: List<GroupAnnouncement>?,   //群公告列表
+    val inst: List<GroupAnnouncement>?  //置顶列表?
 )
 
 @Serializable

+ 1 - 1
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt

@@ -153,7 +153,7 @@ actual abstract class Group : Contact(), CoroutineScope {
      *
      * */
     @MiraiExperimentalAPI
-    actual suspend abstract fun getAnnouncements(page: Int, amount: Int ):GroupAnnouncementList
+    actual suspend abstract fun getAnnouncements(page: Int, amount: Int ):GroupAnnouncementList?
 
     /**
      * 检查此 id 的群成员是否存在