UNPKG

2.95 kBJavaScriptView Raw
1'use strict';
2
3var util = require('./util');
4var wrapper = util.wrapper;
5var postJSON = util.postJSON;
6
7/**
8 * 创建临时二维码
9 * 详细请看:<http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html>
10 * Examples:
11 * ```
12 * api.createTmpQRCode(10000, 1800, callback);
13 * ```
14 * Callback:
15 *
16 * - `err`, 调用失败时得到的异常
17 * - `result`, 调用正常时得到的对象
18 *
19 * Result:
20 * ```
21 * {
22 * "ticket":"gQG28DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0FuWC1DNmZuVEhvMVp4NDNMRnNRAAIEesLvUQMECAcAAA==",
23 * "expire_seconds":1800
24 * }
25 * ```
26 * @param {Number} sceneId 场景ID
27 * @param {Number} expire 过期时间,单位秒。最大不超过604800(即7天)
28 * @param {Function} callback 回调函数
29 */
30exports.createTmpQRCode = function (sceneId, expire, callback) {
31 this.preRequest(this._createTmpQRCode, arguments);
32};
33
34/*!
35 * 创建临时二维码的未封装版本
36 */
37exports._createTmpQRCode = function (sceneId, expire, callback) {
38 var url = this.endpoint + '/cgi-bin/qrcode/create?access_token=' + this.token.accessToken;
39 var data = {
40 'expire_seconds': expire,
41 'action_name': 'QR_SCENE',
42 'action_info': {'scene': {'scene_id': sceneId}}
43 };
44 this.request(url, postJSON(data), wrapper(callback));
45};
46
47/**
48 * 创建永久二维码
49 * 详细请看:<http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html>
50 * Examples:
51 * ```
52 * api.createLimitQRCode(100, callback);
53 * ```
54 * Callback:
55 *
56 * - `err`, 调用失败时得到的异常
57 * - `result`, 调用正常时得到的对象
58 *
59 * Result:
60 * ```
61 * {
62 * "ticket":"gQG28DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0FuWC1DNmZuVEhvMVp4NDNMRnNRAAIEesLvUQMECAcAAA=="
63 * }
64 * ```
65 * @param {Number|String} sceneId 场景ID。数字ID不能大于100000,字符串ID长度限制为1到64
66 * @param {Function} callback 回调函数
67 */
68exports.createLimitQRCode = function (sceneId, callback) {
69 this.preRequest(this._createLimitQRCode, arguments);
70};
71
72/*!
73 * 创建永久二维码的未封装版本
74 */
75exports._createLimitQRCode = function (sceneId, callback) {
76 var url = this.endpoint + '/cgi-bin/qrcode/create?access_token=' + this.token.accessToken;
77 var data = {
78 'action_name': 'QR_LIMIT_SCENE',
79 'action_info': {'scene': {'scene_id': sceneId}}
80 };
81 // 字符串
82 if (typeof sceneId === 'string') {
83 data.action_name = 'QR_LIMIT_STR_SCENE';
84 data.action_info.scene = {'scene_str': sceneId};
85 }
86 this.request(url, postJSON(data), wrapper(callback));
87};
88
89/**
90 * 生成显示二维码的链接。微信扫描后,可立即进入场景
91 * Examples:
92 * ```
93 * api.showQRCodeURL(titck);
94 * // => https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
95 * ```
96 * @param {String} ticket 二维码Ticket
97 * @return {String} 显示二维码的URL地址,通过img标签可以显示出来
98 */
99exports.showQRCodeURL = function (ticket) {
100 return this.mpPrefix + 'showqrcode?ticket=' + ticket;
101};