Skip to content
/ wechat Public

WeChat SDK for Elixir,目前 Elixir 中支持最完善的微信SDK

License

Apache-2.0, Apache-2.0 licenses found

Licenses found

Apache-2.0
LICENSE
Apache-2.0
LICENSE.md
Notifications You must be signed in to change notification settings

feng19/wechat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

WeChat

Module Version Hex Docs Total Download License Last Updated

WeChat SDK for Elixir

Links

Installation

You can use wechat in your projects by adding it to your mix.exs dependencies:

def deps do
  [
    {:wechat, "~> 0.15", hex: :wechat_sdk}
  ]
end

Usage

定义公众号 Client 模块

defmodule YourApp.WeChatAppCodeName do
  @moduledoc "CodeName"
  use WeChat,
    appid: "wx-appid",
    appsecret: "appsecret"
end

其他类型定义请看 WeChat

定义参数说明请看 Options

自动刷新 AccessToken

在调用接口之前,必须先获取 AccessToken 才能 调用接口, 官方说明

通过下面的方式激活 AccessToken 自动刷新器:

config.exs 中设置:

config :wechat, :refresh_settings, [YourApp.WeChatAppCodeName, ...]

动态添加:

WeChat.add_to_refresher(YourApp.WeChatAppCodeName)

调用接口

定义的 client,支持以下两种调用形式:

  • 调用 client 方法:

    YourApp.WeChatAppCodeName.Material.batch_get_material(:image, 2)

  • 原生调用方法

    WeChat.Material.batch_get_material(YourApp.WeChatAppCodeName, :image, 2)

更多详情请见:WeChat模块

网页授权

官方文档

把下面的代码放入到 router:

pipeline :oauth2_checker do
  plug WeChat.Plug.OAuth2Checker, clients: [YourApp.WeChatAppCodeName, ...]
end

scope "/wx/:app" do
  pipe_through :oauth2_checker
  get "/path", YourController, :your_action
end

访问 /wx/:app/path 会自动完成网页授权

其中 :app 可以是 code_name 或者 appid

JS-SDK setup

设置指南

如果网页内用到了 JS-SDK,如:

wx.config({
  debug: true, // 开启调试模式,调用的所有 api 的返回值会在客户端 alert 出来,若要查看传入的参数,可以在 pc 端打开,参数信息会通过 log 打出,仅在 pc 端时才会打印。
  appId: '', // 必填,公众号的唯一标识
  timestamp: , // 必填,生成签名的时间戳
  nonceStr: '', // 必填,生成签名的随机串
  signature: '',// 必填,签名
  jsApiList: [] // 必填,需要使用的 JS 接口列表
});

Setup 参数有 5 个必填项,其中,jsApiList 根据业务需要填写即可, 其余 4 个参数可以通过接口 WeChat.WebPage.js_sdk_config/2 生成

微信推送消息

接入指南

  • 填写服务器配置, 填入 https://host/wx/event
  • 定义 client 时必须设置: encoding_aes_key & token
forward "/wx/event", WeChat.Plug.EventHandler,
  client: YourApp.WeChatAppCodeName,
  event_handler: &HandleModule.handle_event/3

event_handler 的值为 3 参数的函数,详细请看: 函数定义

快速测试

微信提供申请测试号,用于快速测试

Contributing

如果有接口未覆盖到,欢迎提交 PR,感谢。

Copyright and License

Copyright (c) 2024 feng19

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

WeChat SDK for Elixir,目前 Elixir 中支持最完善的微信SDK

Topics

Resources

License

Apache-2.0, Apache-2.0 licenses found

Licenses found

Apache-2.0
LICENSE
Apache-2.0
LICENSE.md

Stars

Watchers

Forks

Languages