View Source LibWechat (lib_wechat v0.1.1)

LibWechat

This SDK provides an Elixir interface to interact with Wechat's APIs.

Head to the API reference for usage details.

installation

Installation

Add the dependency to your mix.exs file:

def deps do
  [
    {:lib_wechat, "~> 0.1.1"}
  ]
end

usage

Usage

  1. Create a new instance of LibWechat with your appid and secret.
wechat =
  LibWechat.new(
    appid: "Your APP ID",
    secret: "Your APP SECRET"
  )
  1. Add LibWechat to your supervision tree.
children = [
  {LibWechat, wechat: wechat}
]

Supervisor.init(children, strategy: :one_for_one)
  1. 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

License

This project is licensed under the MIT License - see the LICENSE file for details

Link to this section Summary

Link to this section Types

@type err_t() :: {:error, any()}
@type json_t() :: %{required(bitstring()) => any()}
@type ok_t(ret) :: {:ok, ret}
@type options_t() ::
  keyword(
    {:name, atom()}
    | {:client_module, atom()}
    | {:client, term()}
    | {:appid, binary()}
    | {:secret, binary()}
    | {:json_module, atom()}
  )
@type t() :: %LibWechat{
  appid: bitstring(),
  client: LibWechat.Client.t(),
  client_module: module(),
  json_module: module(),
  name: GenServer.name(),
  secret: bitstring()
}

Link to this section Functions

Link to this function

generate_scheme(wechat, token, body)

View Source
@spec generate_scheme(t(), String.t(), json_t()) :: ok_t(json_t()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html

examples

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
  }
)
Link to this function

get_access_token(wechat)

View Source
@spec get_access_token(t()) :: ok_t(json_t()) | err_t()

获取access_token https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

examples

Examples

{:ok, %{"access_token"=>"xxx"}} = LibWechat.get_access_token(wechat)
Link to this function

get_phone_number(wechat, token, code)

View Source
@spec get_phone_number(t(), String.t(), String.t()) :: ok_t(json_t()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html

examples

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)
Link to this function

get_unlimited_wxacode(wechat, token, body)

View Source
@spec get_unlimited_wxacode(t(), String.t(), json_t()) :: ok_t(binary()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

examples

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
  }
)
Link to this function

get_urllink(wechat, token, body)

View Source
@spec get_urllink(t(), String.t(), json_t()) :: ok_t(json_t()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html

examples

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
  }
)
Link to this function

jscode_to_session(wechat, code)

View Source
@spec jscode_to_session(t(), String.t()) :: ok_t(json_t()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html

@spec new(options_t()) :: t()

create an instance of LibWechat

options

options

  • :name (atom/0) - name of this process The default value is :wechat.

  • :client_module (atom/0) - module that implements LibWechat.Client behavior The default value is LibWechat.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 is Jason.

@spec start_link(keyword()) :: GenServer.on_start()
Link to this function

subscribe_send(wechat, token, body)

View Source
@spec subscribe_send(t(), String.t(), json_t()) :: ok_t(json_t()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html

examples

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号"}
    }
  }
)
Link to this function

uniform_send(wechat, token, body)

View Source
@spec uniform_send(t(), String.t(), json_t()) :: ok_t(json_t()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html

examples

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"}
      }
    }
  })