Skip to content
jason edited this page Dec 23, 2016 · 16 revisions

Welcome to the wechat-jssdk API wiki for V3!

Initialize

//get your wechat info from wechat dashboard
const wechatInfo = {
  "wechatRedirectUrl": "http://yourdomain.com/wechat/oauth-callback",
  "wechatToken": "aaa", // used to pass the verification when save wechat config on wechat dashboard
  "appId": "bbb", //required for jssdk and oauth usage
  "appSecret": "ccc" //required
}
const WeChat = require('wechat-jssdk');
const wx = new WeChat(wechatInfo);

JSSDK APIs

Default Api usage: wx.jssdk.fun(params...).then(resolveFn, rejectFn), the wx.jssdk is a reference to new JSSDK(options) instance internally,
Instantiate your own JSSDK instance:

const JSSDK = WeChat.JSSDK;
const jssdk = new JSSDK(wechatInfo);
jssdk.fun(params...).then(resolveFn, rejectFn);
  • @constructor WeChat.JSSDK, use new JSSDK(options) to instantiate JSSDK instance, which is automatically called when you do new WeChat(options) above.
  • verifySignature, [query {object}], JSON object containing all the verification properties, including: timestamp, nonce, to generate the signature to compare with query.signature, you need to call this api when you first input the wechat config in wechat dashboard.
  • getSignature, [url {string}, forceNewSignature {boolean}], get the signature from cache or create a new one, usually this is the most used api. If forceNewSignature is true, generate a new signature instead of getting it from cache.
  • jssdk.store, the Store instance for the current jssdk instance.

Other apis are used internally, usually you don't need to call them directly...

OAuth APIs

Default Api usage: wx.oauth.fun(params...).then(resolveFn, rejectFn), the wx.oauth is a reference to new OAuth(options) instance internally,
Instantiate your own OAuth instance:

const OAuth = WeChat.OAuth;
const oauth = new OAuth(wechatInfo);
oauth.fun(params...).then(resolveFn, rejectFn);
  • @constructor WeChat.OAuth, use new OAuth(options) to instantiate OAuth instance, which is automatically called when you do new WeChat(options) above.
  • generateOAuthUrl, [redirectUrl {string}, scope {string}, state {string}], customize your own callback url, pass your custom scope and state(default userAuth) option to original wechat api. The default wx.oauth.snsUserInfoUrl(scope: snsapi_userinfo) and wx.oauth.snsUserBaseUrl(scope: snsapi_base) is generated with the wechatRedirectUrl when you pass to new Wechat(options).
  • getUserInfo, [code {string}, key {string}, returnToken {boolean}], get wechat user info, including nickname, openid, avatar, etc..., if code is a false value('', null, etc...), it will get the access token from cache based on the key parameter, the key(default use the openid from the userinfo result) is used as a unique key for access token info to be stored in Stores, which can be retrieved next time directly from cache instead of getting the code again, if returnToken is true, the result of the token api will be merged with the user profile, and be returned together.
  • getUserBaseInfo, [code {string}, key {string}], get wechat user base info, code param is included in the wechat redirect url.
  • oauth.store, the Store instance for the current oauth instance.

Clone this wiki locally