Skip to content

integrateWechatLogin

集成微信登录到Android项目,自动配置相关文件和依赖。

前置依赖

依赖参数

参数名类型说明
deps.existsSyncFileSystem['existsSync']检查文件是否存在
deps.readFileSyncFileSystem['readFileSync']读取文件内容
deps.writeFileSyncFileSystem['writeFileSync']写入文件内容
deps.copyFileSyncFileSystem['copyFileSync']复制文件
deps.joinPath['join']路径拼接
deps.xmlParserXMLParserConstructorXML 解析器构造函数
deps.xmlSerializerXMLSerializerConstructorXML 序列化器构造函数
deps.xpathXPathXPath 查询对象

环境要求

  • @xmldom/xmldom: XML DOM 解析库
  • xpath: XPath 查询库
bash
npm install @xmldom/xmldom xpath

函数签名

typescript
function integrateWechatLogin(
  projectPath: string,
  moduleDir: string,
  packageName: string,
  config: WechatLoginConfig,
  deps: IntegrateThirdPartyModuleDeps
): Promise<{ success: boolean; message?: string; error?: string; logs: string[] }>

interface WechatLoginConfig {
  appId: string              // 微信AppID
  appSecret: string          // 微信AppSecret
  oauthAarPath: string       // oauth-weixin-release.aar文件路径
  wxEntryActivityPath: string // WXEntryActivity.java文件路径
}

参数

参数名类型必填说明
projectPathstringAndroid项目路径
moduleDirstring模块目录(如 'app' 或 'HBuilder-uniPlugin')
packageNamestring应用包名
config.appIdstring微信AppID
config.appSecretstring微信AppSecret
config.oauthAarPathstringoauth-weixin-release.aar文件路径
config.wxEntryActivityPathstringWXEntryActivity.java文件路径
depsIntegrateThirdPartyModuleDeps依赖注入对象

返回值

类型说明
Promise<{ success: boolean; message?: string; error?: string; logs: string[] }>Promise,解析为集成结果对象

工作原理

  1. 构建模块配置:根据微信登录配置构建第三方模块配置对象
  2. 拷贝库文件:将 oauth-weixin-release.aar 拷贝到 libs 目录
  3. 添加Gradle依赖:在 build.gradle 中添加微信SDK依赖
  4. 拷贝Activity文件:将 WXEntryActivity.java 拷贝到 wxapi 目录,并替换包名
  5. 修改AndroidManifest.xml
    • 添加 MODIFY_AUDIO_SETTINGS 权限
    • 添加 WX_APPIDWX_SECRET meta-data
    • 注册 WXEntryActivity,配置 intent-filter 和 scheme
  6. 配置dcloud_properties.xml:添加 OAuth 功能配置
  7. 返回结果:包含成功状态、消息、错误信息和详细日志

自动处理文件拷贝、依赖配置和清单文件修改,确保微信登录功能正确集成。