From f22c4b9230808fed4fec80c435eccb4c833349a0 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期五, 21 十月 2022 18:36:38 +0800
Subject: [PATCH] 2022.10.21 1.环境守法小程序后台功能首发版本完成

---
 src/main/kotlin/cn/flightfeather/supervision/common/net/WXHttpService.kt |   88 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 83 insertions(+), 5 deletions(-)

diff --git a/src/main/kotlin/cn/flightfeather/supervision/common/net/WXHttpService.kt b/src/main/kotlin/cn/flightfeather/supervision/common/net/WXHttpService.kt
index bb20893..39bd1b8 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/common/net/WXHttpService.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/common/net/WXHttpService.kt
@@ -1,5 +1,13 @@
 package cn.flightfeather.supervision.common.net
 
+import cn.flightfeather.supervision.common.wx.MessageWxVo
+import cn.flightfeather.supervision.common.wx.WxConfig
+import com.alibaba.fastjson.JSON
+import com.alibaba.fastjson.JSONObject
+import com.google.gson.Gson
+import com.google.gson.JsonObject
+import com.google.gson.JsonParser
+
 /**
  * @author riku
  * Date: 2020/10/15
@@ -7,17 +15,87 @@
  */
 object WXHttpService {
 
-    private const val APP_ID=""
-    private const val SECRET = ""
     private val httpMethod = HttpMethod("api.weixin.qq.com", 443, true)
 
     /**
      * 寰俊鐧诲綍鍑瘉鏍¢獙
      */
-    fun code2Session(code: String): HttpMethod.MyResponse =
-        httpMethod.get(
+    fun code2Session(code: String): Pair<String, String>? {
+        val res =httpMethod.get(
             "/sns/jscode2session", listOf(
-                Pair("appid", APP_ID), Pair("secret", SECRET), Pair("js_code", code), Pair("grant_type", "authorization_code")
+                Pair("appid", WxConfig.APP_ID), Pair("secret", WxConfig.SECRET), Pair("js_code", code), Pair("grant_type", "authorization_code")
             )
         )
+        return if (res.success) {
+            val json = JSONObject.parseObject(res.m.responseBodyAsString)
+            val errCode = json["errcode"]
+            if (errCode == 0 || errCode == null) {
+                val openid = json["openid"] as String
+                val unionid = json["session_key"] as String
+                Pair(openid, unionid)
+            } else {
+                null
+            }
+        } else {
+            null
+        }
+    }
+
+    /**
+     * 寰俊鍙戦�佽闃呮秷鎭�
+     */
+    fun sendMsg(accessToken:String, msgWxV0: MessageWxVo): Boolean {
+        val data = Gson().toJson(msgWxV0)
+        val response = httpMethod.post("/cgi-bin/message/subscribe/send?access_token=${accessToken}", data)
+        if (response.success) {
+            val json = JsonParser.parseString(response.m.responseBodyAsString)
+            if (json.isJsonObject) {
+                val jo = json.asJsonObject
+                val eMsg = jo["errmsg"]?.asString
+                when (jo["errcode"]?.asInt) {
+                    40003 -> {
+                        throw IllegalStateException("寰俊璁㈤槄娑堟伅锛宼ouser瀛楁openid涓虹┖鎴栬�呬笉姝g‘锛岄敊璇俊鎭細${eMsg}")
+                    }
+                    40037 -> {
+                        throw IllegalStateException("寰俊璁㈤槄娑堟伅锛岃闃呮ā鏉縤d涓虹┖涓嶆纭紝閿欒淇℃伅锛�${eMsg}")
+                    }
+                    43101 -> {
+                        throw IllegalStateException("寰俊璁㈤槄娑堟伅锛岀敤鎴锋嫆缁濇帴鏀舵秷鎭紝閿欒淇℃伅锛�${eMsg}")
+                    }
+                    47003 -> {
+                        throw IllegalStateException("寰俊璁㈤槄娑堟伅锛屾ā鏉垮弬鏁颁笉鍑嗙‘锛屽叿浣撻敊璇細${eMsg}")
+                    }
+                    41030 -> {
+                        throw IllegalStateException("寰俊璁㈤槄娑堟伅锛宲age璺緞涓嶆纭紝閿欒淇℃伅锛�${eMsg}")
+                    }
+                }
+            }
+            return true
+        } else {
+            return false
+        }
+    }
+
+    /**
+     * 鑾峰彇灏忕▼搴忓叏灞�鍚庡彴鎺ュ彛璋冪敤鍑嵁
+     */
+    fun getAccessToken(): JsonObject? {
+        val res = httpMethod.get(
+            "/cgi-bin/token", listOf(
+                Pair("grant_type", "client_credential"),
+                Pair("appid", WxConfig.APP_ID),
+                Pair("secret", WxConfig.SECRET)
+            )
+        )
+        return if (res.success) {
+            val json = JsonParser.parseString(res.m.responseBodyAsString)
+            if (json.isJsonObject) {
+                json.asJsonObject
+            } else {
+                null
+            }
+        } else {
+            null
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3