View Source LibWechat (lib_wechat v0.1.2)
LibWechat
This SDK provides an Elixir interface to interact with Wechat's APIs.
Head to the API reference for usage details.
Installation
Add the dependency to your mix.exs
file:
def deps do
[
{:lib_wechat, "~> 0.1"}
]
end
Usage
- Create a new instance of
LibWechat
with your appid and secret.
wechat =
LibWechat.new(
appid: "Your APP ID",
secret: "Your APP SECRET"
)
- Add LibWechat to your supervision tree.
children = [
{LibWechat, wechat: wechat}
]
Supervisor.init(children, strategy: :one_for_one)
- Call the API you want to use.
# Get access token
LibWechat.get_access_token(wechat)
# Get miniapp session with code
LibWechat.jscode_to_session(wechat, "jscode")
# Get unlimited miniapp wxacode
LibWechat.get_unlimited_wxacode(wechat, token,
%{"scene" => "foo=bar",
"page" => "pages/index/index",
"width" => 430,
"auto_color" => false,
"line_color" => %{"r" => 0, "g" => 0, "b" => 0},
"is_hyaline" => false
})
#.... see more api in lib_wechat.ex
License
This project is licensed under the MIT License - see the LICENSE file for details
Summary
Types
@type err_t() :: {:error, LibWechat.Client.Error.t()}
@type ok_t(ret) :: {:ok, ret}
@type t() :: %LibWechat{ appid: bitstring(), client: LibWechat.Client.t(), client_module: module(), json_module: module(), name: GenServer.name(), secret: bitstring() }
Functions
Examples
{:ok, %{
"errcode" => 0,
"errmsg" => "ok",
"openlink" => "weixin://dl/business/?t=Akeatr890b"
}} = LibWechat.generate_scheme(wechat, token,
%{
"jump_wxa" => %{
"path" => "pages/index/index",
"query" => "foo=bar"
},
"is_expire" => false,
"expire_type" => 0,
"expire_time" => 0
}
)
获取access_token https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
Examples
{:ok, %{"access_token"=>"xxx"}} = LibWechat.get_access_token(wechat)
Examples
{:ok,
%{
"errcode":0,
"errmsg":"ok",
"phone_info": {
"phoneNumber":"xxxxxx",
"purePhoneNumber": "xxxxxx",
"countryCode": 86,
"watermark": {
"timestamp": 1637744274,
"appid": "xxxx"
}
}
}
} = get_phone_number(wechat, token, code)
Examples
{:ok, <<255, 216, ...>>} = LibWechat.get_unlimited_wxacode(wechat, token,
%{"scene" => "foo=bar",
"page" => "pages/index/index",
"width" => 430,
"auto_color" => false,
"line_color" => %{"r" => 0, "g" => 0, "b" => 0},
"is_hyaline" => false
}
)
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html
Examples
{:ok, %{
"errcode" => 0,
"errmsg" => "ok",
"url_link" => "https://wxaurl.cn/bz2LB4RMDVqq"
}} = LibWechat.get_urllink(wechat, token,
%{
"path" => "pages/index/index",
"query" => "foo=bar",
"is_expire" => false,
"expire_type" => 0,
"expire_time" => 0
}
)
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
create an instance of LibWechat
options
:name
(atom/0
) - name of this process The default value is:wechat
.:client_module
(atom/0
) - module that implementsLibWechat.Client
behavior The default value isLibWechat.Client.Finch
.:client
(term/0
) - client instance The default value is%LibWechat.Client.Finch{name: LibWechat.Client.Finch, addr: "https://api.weixin.qq.com", json_module: Jason}
.:appid
(String.t/0
) - Required. 第三方用户唯一凭证:secret
(String.t/0
) - Required. 第三方用户唯一凭证密钥,即appsecret:json_module
(atom/0
) - module that implements json's encode and decode behavior like Jason The default value isJason
.
@spec start_link(keyword()) :: GenServer.on_start()
Examples
{:ok, %{
"errcode" => 0,
"errmsg" => "ok",
"msgid" => 294402298110051942
}} = LibWechat.subscribe_send(wechat, token,
%{
"touser" => "OPENID",
"template_id" => "TEMPLATE_ID",
"page" => "index",
"miniprogram_state" => "developer",
"lang" => "zh_CN",
"data" => %{
"number01" => %{"value" => "339208499"},
"date01" => %{"value" => "2015年01月05日"},
"site01" => %{"value" => "TIT创意园"},
"site02" => %{"value" => "广州市新港中路397号"}
}
}
)
Examples
{:ok, %{"errcode" => 0, "errmsg" => "ok"}} = LibWechat.uniform_send(wechat, token,
%{
"touser" => "OPENID",
"weapp_template_msg" => %{
"template_id" => "TEMPLATE_ID",
"page" => "index",
"form_id" => "FORMID",
"data" => %{
"keyword1" => %{"value" => "339208499"},
"keyword2" => %{"value" => "2015年01月05日"},
"keyword3" => %{"value" => "粤海喜来登酒店"},
"keyword4" => %{"value" => "广州市天河区天河路208号"}
},
"emphasis_keyword" => "keyword1.DATA"
},
"mp_template_msg" => %{
"appid" => "APPID ",
"template_id" => "TEMPLATE_ID",
"url" => "http://weixin.qq.com/download",
"miniprogram" => %{
"appid" => "xiaochengxuappid12345",
"pagepath" => "index?foo=bar"
},
"data" => %{
"first" => %{"value" => "恭喜你购买成功!", "color" => "#173177"},
"keyword1" => %{"value" => "巧克力", "color" => "#173177"},
"keyword2" => %{"value" => "39.8元", "color" => "#173177"},
"keyword3" => %{"value" => "2014年9月22日", "color" => "#173177"},
"remark" => %{"value" => "欢迎再次购买!", "color" => "#173177"}
}
}
})