Skip to content

Commit 22fca50

Browse files
committed
Merge branch 'ifeiteng-add-semantic-api'
2 parents 368c65f + 1b5d6f9 commit 22fca50

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ API.mixin(require('./lib/api_payment'));
4343
API.mixin(require('./lib/api_feedback'));
4444
// 短网址接口
4545
API.mixin(require('./lib/api_url'));
46+
// 语义查询接口
47+
API.mixin(require('./lib/api_semantic'));
4648

4749
wechat.API = API;
4850
wechat.OAuth = require('./lib/oauth');

lib/api_semantic.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var urllib = require('urllib');
2+
var util = require('./util');
3+
var wrapper = util.wrapper;
4+
var postJSON = util.postJSON;
5+
6+
/**
7+
* 发送语义理解请求
8+
* 详细请看:http://mp.weixin.qq.com/wiki/index.php?title=%E8%AF%AD%E4%B9%89%E7%90%86%E8%A7%A3
9+
*
10+
* Opts:
11+
* ```
12+
* {
13+
* "query":"查一下明天从北京到上海的南航机票",
14+
* "city":"北京",
15+
* "category": "flight,hotel"
16+
* }
17+
* ```
18+
* Examples:
19+
* ```
20+
* api.semantic(uid, opts, callback);
21+
* ```
22+
* Callback:
23+
*
24+
* - `err`, 调用失败时得到的异常
25+
* - `result`, 调用正常时得到的对象
26+
*
27+
* Result:
28+
* ```
29+
* {
30+
* "errcode":0,
31+
* "query":"查一下明天从北京到上海的南航机票",
32+
* "type":"flight",
33+
* "semantic":{
34+
* "details":{
35+
* "start_loc":{
36+
* "type":"LOC_CITY",
37+
* "city":"北京市",
38+
* "city_simple":"北京",
39+
* "loc_ori":"北京"
40+
* },
41+
* "end_loc": {
42+
* "type":"LOC_CITY",
43+
* "city":"上海市",
44+
* "city_simple":"上海",
45+
* "loc_ori":"上海"
46+
* },
47+
* "start_date": {
48+
* "type":"DT_ORI",
49+
* "date":"2014-03-05",
50+
* "date_ori":"明天"
51+
* },
52+
* "airline":"中国南方航空公司"
53+
* },
54+
* "intent":"SEARCH"
55+
* }
56+
* ```
57+
* @param {String} openid 用户ID
58+
* @param {Object} opts 查询条件
59+
* @param {Function} callback 回调函数
60+
*/
61+
exports.semantic = function (uid, opts, callback) {
62+
this.preRequest(this._semantic, arguments);
63+
};
64+
65+
/*!
66+
* 发送语义理解请求的未封装版本
67+
*/
68+
exports._semantic = function (uid, opts, callback) {
69+
// https://api.weixin.qq.com/semantic/semproxy/search?access_token=YOUR_ACCESS_TOKEN
70+
var url = 'https://api.weixin.qq.com/semantic/semproxy/search?access_token=' + this.token.accessToken;
71+
opts.appid = this.appid;
72+
opts.uid = uid;
73+
urllib.request(url, postJSON(opts), wrapper(callback));
74+
};

test/api_semantic.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var config = require('./config');
2+
var API = require('../').API;
3+
var expect = require('expect.js');
4+
5+
describe('api_semantic', function () {
6+
var api = new API(config.appid, config.appsecret);
7+
it('should ok', function (done) {
8+
var condition = {
9+
"query":"查一下明天从北京到上海的南航机票",
10+
"city":"北京",
11+
"category": "flight,hotel"
12+
};
13+
14+
var uid = "123456";
15+
16+
api.semantic(uid, condition, function (err, data, res) {
17+
expect(err).to.be.ok();
18+
expect(data).to.have.property('errcode');
19+
expect(data).to.have.property('errmsg');
20+
done();
21+
});
22+
});
23+
});

0 commit comments

Comments
 (0)