|
@@ -0,0 +1,139 @@
|
|
|
|
|
+package com.mooxygen.user.wxapi
|
|
|
|
|
+
|
|
|
|
|
+import android.app.Activity
|
|
|
|
|
+import android.content.Intent
|
|
|
|
|
+import android.os.Bundle
|
|
|
|
|
+import com.alibaba.android.arouter.facade.annotation.Autowired
|
|
|
|
|
+import com.alibaba.android.arouter.launcher.ARouter
|
|
|
|
|
+import com.narutohuo.xindazhou.core.log.ILog
|
|
|
|
|
+import com.narutohuo.xindazhou.core.share.IShareCallback
|
|
|
|
|
+import com.tencent.mm.opensdk.constants.ConstantsAPI
|
|
|
|
|
+import com.tencent.mm.opensdk.modelbase.BaseReq
|
|
|
|
|
+import com.tencent.mm.opensdk.modelbase.BaseResp
|
|
|
|
|
+import com.tencent.mm.opensdk.openapi.IWXAPI
|
|
|
|
|
+import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
|
|
|
|
|
+import com.tencent.mm.opensdk.openapi.WXAPIFactory
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 微信分享回调 Activity
|
|
|
|
|
+ *
|
|
|
|
|
+ * 按照微信官方 SDK 文档,需要创建此 Activity 处理微信分享回调
|
|
|
|
|
+ *
|
|
|
|
|
+ * 注意:
|
|
|
|
|
+ * - 包名必须是应用包名(com.narutohuo.xindazhou.wxapi),不能是模块 namespace
|
|
|
|
|
+ * - 微信 SDK 要求此 Activity 必须在应用包名的 wxapi 子包下
|
|
|
|
|
+ * - 虽然文件在 base-core 模块中,但包名使用应用包名,这样 Android 清单合并后能正确识别
|
|
|
|
|
+ *
|
|
|
|
|
+ * 架构说明:
|
|
|
|
|
+ * - 使用 ARouter 依赖注入获取 IShareCallback 接口实现
|
|
|
|
|
+ * - share 模块实现 IShareCallback 接口并通过 ARouter 注册
|
|
|
|
|
+ * - base-core 不直接依赖 share 模块,实现解耦
|
|
|
|
|
+ * - base-core 只依赖微信官方 SDK,不依赖友盟
|
|
|
|
|
+ *
|
|
|
|
|
+ * 工作原理:
|
|
|
|
|
+ * 1. 用户在微信中完成分享操作后,微信会通过 Intent 回调到此 Activity
|
|
|
|
|
+ * 2. 通过微信 SDK 的 IWXAPIEventHandler 接口处理回调
|
|
|
|
|
+ * 3. 通过 ARouter 获取 IShareCallback 实现,调用 share 模块的处理方法
|
|
|
|
|
+ * 4. share 模块处理完成后,通过 ShareProxyActivity 的回调传递给业务层
|
|
|
|
|
+ */
|
|
|
|
|
+class WXEntryActivity : Activity(), IWXAPIEventHandler {
|
|
|
|
|
+
|
|
|
|
|
+ private var api: IWXAPI? = null
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通过 ARouter 依赖注入获取分享回调服务
|
|
|
|
|
+ *
|
|
|
|
|
+ * 注意:share 模块需要实现 IShareCallback 接口并通过 @Route 注册
|
|
|
|
|
+ */
|
|
|
|
|
+ @Autowired(name = "/share/callback")
|
|
|
|
|
+ @JvmField
|
|
|
|
|
+ var shareCallback: IShareCallback? = null
|
|
|
|
|
+
|
|
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
|
|
+
|
|
|
|
|
+ // ARouter 依赖注入
|
|
|
|
|
+ ARouter.getInstance().inject(this)
|
|
|
|
|
+
|
|
|
|
|
+ // 创建微信 API 实例(需要从 share 模块获取 AppId,这里先尝试处理回调)
|
|
|
|
|
+ // 注意:实际的微信 API 初始化应该在 share 模块中完成
|
|
|
|
|
+ // 这里只是接收回调并转发给 share 模块处理
|
|
|
|
|
+
|
|
|
|
|
+ ILog.d("WXEntryActivity", "onCreate: 收到微信回调")
|
|
|
|
|
+
|
|
|
|
|
+ // 调用 share 模块处理回调
|
|
|
|
|
+ shareCallback?.let {
|
|
|
|
|
+ ILog.d("WXEntryActivity", "通过 ARouter 获取到 IShareCallback 实现,调用 onWeChatCallback")
|
|
|
|
|
+ it.onWeChatCallback(intent)
|
|
|
|
|
+ } ?: run {
|
|
|
|
|
+ ILog.e("WXEntryActivity", "未找到 IShareCallback 实现,请确保 share 模块已注册")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理微信回调(通过 IWXAPIEventHandler)
|
|
|
|
|
+ handleIntent(intent)
|
|
|
|
|
+
|
|
|
|
|
+ // 关闭 Activity
|
|
|
|
|
+ finish()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onNewIntent(intent: Intent?) {
|
|
|
|
|
+ super.onNewIntent(intent)
|
|
|
|
|
+ setIntent(intent)
|
|
|
|
|
+ ILog.d("WXEntryActivity", "onNewIntent: 收到新的微信回调 Intent")
|
|
|
|
|
+
|
|
|
|
|
+ // 调用 share 模块处理回调
|
|
|
|
|
+ intent?.let {
|
|
|
|
|
+ shareCallback?.onWeChatCallback(it)
|
|
|
|
|
+ handleIntent(it)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 处理微信回调 Intent
|
|
|
|
|
+ */
|
|
|
|
|
+ private fun handleIntent(intent: Intent?) {
|
|
|
|
|
+ if (intent == null) return
|
|
|
|
|
+
|
|
|
|
|
+ // 尝试从 share 模块获取微信 API 实例
|
|
|
|
|
+ // 如果 share 模块已经初始化了微信 API,这里可以直接使用
|
|
|
|
|
+ // 否则,这里只转发 Intent 给 share 模块处理
|
|
|
|
|
+
|
|
|
|
|
+ // 注意:微信 SDK 要求通过 IWXAPI.handleIntent 处理回调
|
|
|
|
|
+ // 但这里为了解耦,将 Intent 转发给 share 模块处理
|
|
|
|
|
+ // share 模块会使用自己的 IWXAPI 实例处理回调
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 微信请求回调
|
|
|
|
|
+ */
|
|
|
|
|
+ override fun onReq(req: BaseReq?) {
|
|
|
|
|
+ ILog.d("WXEntryActivity", "onReq: ${req?.type}")
|
|
|
|
|
+ // 微信请求通常不需要处理,主要是响应回调
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 微信响应回调
|
|
|
|
|
+ */
|
|
|
|
|
+ override fun onResp(resp: BaseResp?) {
|
|
|
|
|
+ ILog.d("WXEntryActivity", "onResp: type=${resp?.type}, errCode=${resp?.errCode}, errStr=${resp?.errStr}")
|
|
|
|
|
+
|
|
|
|
|
+ when (resp?.type) {
|
|
|
|
|
+ ConstantsAPI.COMMAND_SENDAUTH -> {
|
|
|
|
|
+ // 微信登录回调
|
|
|
|
|
+ ILog.d("WXEntryActivity", "收到微信登录回调")
|
|
|
|
|
+ }
|
|
|
|
|
+ ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX -> {
|
|
|
|
|
+ // 微信分享回调
|
|
|
|
|
+ ILog.d("WXEntryActivity", "收到微信分享回调")
|
|
|
|
|
+ }
|
|
|
|
|
+ ConstantsAPI.COMMAND_PAY_BY_WX -> {
|
|
|
|
|
+ // 微信支付回调
|
|
|
|
|
+ ILog.d("WXEntryActivity", "收到微信支付回调")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 将回调转发给 share 模块处理
|
|
|
|
|
+ shareCallback?.onWeChatCallback(intent)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|