View Source WeChat.WebPage (wechat v0.15.1)

网页开发

API Docs

Summary

Types

JS API的临时票据类型

授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理

授权范围

重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节

Types

@type access_token() :: String.t()
@type card_config() :: %{cardId: WeChat.Card.card_id(), cardExt: card_ext()}
@type card_ext() :: String.t()
@type code() :: String.t()
@type js_api_ticket() :: ticket()
@type js_api_ticket_type() :: String.t()

JS API的临时票据类型

  • "jsapi" - JS-SDK Config
  • "wx_card" - 微信卡券
@type js_sdk_config() :: %{
  appId: WeChat.appid(),
  signature: signature(),
  timestamp: timestamp(),
  nonceStr: nonce_str()
}
@type nonce_str() :: String.t()
@type redirect_uri() :: String.t()

授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理

@type refresh_token() :: String.t()
@type scope() :: String.t()

授权范围

  • "snsapi_base" - 静默授权
  • "snsapi_userinfo" - 用户的基本信息(无须关注)
@type signature() :: String.t()
@type state() :: String.t()

重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节

@type ticket() :: String.t()
@type timestamp() :: non_neg_integer()
@type url() :: String.t()
@type wx_card_ticket() :: ticket()

Functions

Link to this function

add_card_config(client, card_id, outer_str)

View Source
@spec add_card_config(WeChat.client(), WeChat.Card.card_id(), outer_str :: String.t()) ::
  card_config()

生成微信卡券配置 - 添加卡券

API Docs

Link to this function

add_card_config(client, card_id, outer_str, openid)

View Source
@spec add_card_config(
  WeChat.client(),
  WeChat.Card.card_id(),
  outer_str :: String.t(),
  WeChat.openid()
) ::
  map()

生成微信卡券配置 - 添加卡券(绑定openid)

API Docs

Link to this function

auth(requester \\ Requester.OfficialAccount, openid, access_token)

View Source

网页授权 - 检验授权凭证(access_token)是否有效 - 官方文档

Link to this function

code2access_token(client, code)

View Source
@spec code2access_token(WeChat.client(), code()) :: WeChat.response()

网页授权 - 通过code换取access_token

官方文档:

Link to this function

get_ticket(client, type)

View Source
@spec get_ticket(WeChat.client(), js_api_ticket_type()) :: WeChat.response()

获取api_ticket - 官方文档

Link to this function

js_sdk_config(client, url)

View Source
@spec js_sdk_config(WeChat.client(), url()) :: js_sdk_config()

生成 JS-SDK 配置 - 官方文档

返回 maps 包含: appId/signature/timestamp/nonceStr

Link to this function

oauth2_authorize_url(client, redirect_uri, scope \\ "snsapi_base", state \\ "")

View Source
@spec oauth2_authorize_url(WeChat.client(), redirect_uri(), scope(), state()) :: url()

网页授权 - 请求code

官方文档:

关于网页授权回调域名的说明

  • 在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的 “开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息” 的配置选项中, 修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;
  • 授权回调域名配置规范为全域名,比如需要网页授权的域名为:www.qq.com,配置以后此域名下面的页面 http://www.qq.com/music.htmlhttp://www.qq.com/login.html 都可以进行 OAuth2.0 鉴权。但 http://pay.qq.comhttp://music.qq.comhttp://qq.com 无法进行 OAuth2.0 鉴权
  • 如果公众号登录授权给了第三方开发者来进行管理,则不必做任何设置,由第三方代替公众号实现网页授权即可
Link to this function

refresh_token(client, refresh_token)

View Source
@spec refresh_token(WeChat.client(), refresh_token()) :: WeChat.response()

网页授权 - 刷新access_token

由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,

refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。

官方文档:

@spec sign_card(list :: [String.t()]) :: map()

卡券签名 - 官方文档

Link to this function

sign_card(wx_card_ticket, card_id)

View Source
@spec sign_card(wx_card_ticket(), WeChat.Card.card_id()) :: map()

See sign_card/1

Link to this function

sign_card(wx_card_ticket, card_id, openid)

View Source
@spec sign_card(wx_card_ticket(), WeChat.Card.card_id(), WeChat.openid()) :: map()

See sign_card/1

Link to this function

sign_js_sdk(jsapi_ticket, url, appid)

View Source
@spec sign_js_sdk(js_api_ticket(), url(), WeChat.appid()) :: js_sdk_config()

生成 JS-SDK 配置(by ticket) - 官方文档

返回 maps 包含: appId/signature/timestamp/nonceStr

Link to this function

user_info(openid, access_token)

View Source
@spec user_info(WeChat.openid(), access_token()) :: WeChat.response()

网页授权 - 拉取用户信息(需scopesnsapi_userinfo) - 官方文档

如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了.

Link to this function

user_info(requester, openid, access_token)

View Source

See user_info/2

Link to this function

user_info(requester, openid, access_token, lang)

View Source

See user_info/2