Skip to content

Commit 93fefb5

Browse files
committed
修复上传视频的bug
1 parent 12c2d19 commit 93fefb5

File tree

6 files changed

+95
-48
lines changed

6 files changed

+95
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
lib-cov
33
coverage.html
44
example
5+
.DS_Store

lib/common.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,43 @@ API.prototype._getGroups = function (callback) {
383383
urllib.request(url, {dataType: 'json'}, wrapper(callback));
384384
};
385385

386+
/**
387+
* 查询用户在哪个分组
388+
* 详情请见:<http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口>
389+
* Examples:
390+
* ```
391+
* api.getWhichGroup(openid, callback);
392+
* ```
393+
* Callback:
394+
*
395+
* - `err`, 调用失败时得到的异常
396+
* - `result`, 调用正常时得到的对象
397+
*
398+
* Result:
399+
* ```
400+
* {
401+
* "groupid": 102
402+
* }
403+
* ```
404+
* @param {String} openid Open ID
405+
* @param {Function} callback 回调函数
406+
*/
407+
API.prototype.getWhichGroup = function (openid, callback) {
408+
this.preRequest(this._getWhichGroup, arguments);
409+
};
410+
411+
/*!
412+
* 查询用户在哪个分组未分组版本
413+
*/
414+
API.prototype._getWhichGroup = function (openid, callback) {
415+
// https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
416+
var url = this.prefix + 'groups/getid?access_token=' + this.token.accessToken;
417+
var data = {
418+
"openid": openid
419+
};
420+
urllib.request(url, postJSON(data), wrapper(callback));
421+
};
422+
386423
/**
387424
* 创建分组
388425
* 详情请见:<http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口>
@@ -750,7 +787,7 @@ API.prototype._sendVideo = function (openid, mediaId, thumbMediaId, callback) {
750787
var data = {
751788
"touser": openid,
752789
"msgtype":"video",
753-
"image": {
790+
"video": {
754791
"media_id": mediaId,
755792
"thumb_media_id": thumbMediaId
756793
}

test/common.test.js

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var API = require('../').API;
88

99
var puling = 'ofL4cs7hr04cJIcu600_W-ZwwxHg';
1010
var imageId = 'XDZxzuRWBPqI4R9n_nNR5uRVZVQCSneMoELyWKflwM2qF9K38vnVFzgaD97uCTUu';
11+
var voiceId = '9R5BhAum7AEaGhwku0WhgvtO4C_7Xs78NoiRvm6v7IyoTljE4HH5o8E_UfnPrL0p';
12+
var thumbId = 'BHxGDVy7WY6BCOcv3AwbywUE630Vw0tAV_V8bzBaCZid4Km5fwXrVOso3X0zas4n';
13+
var movieId = 'b4F8SfaZZQwalDxwPjd923ACV5IUeYvZ9-dYKf5ytXrS-IImXEkl2U8Fl5EH-jCF';
1114

1215
describe('common.js', function () {
1316
describe('getAccessToken', function () {
@@ -493,6 +496,14 @@ describe('common.js', function () {
493496
});
494497
});
495498

499+
it('getWhichGroup should ok', function (done) {
500+
api.getWhichGroup(puling, function (err, data, res) {
501+
expect(err).not.to.be.ok();
502+
expect(data).to.have.property('groupid');
503+
done();
504+
});
505+
});
506+
496507
it('createGroup should ok', function (done) {
497508
api.createGroup('new group', function (err, data, res) {
498509
expect(err).not.to.be.ok();
@@ -566,7 +577,7 @@ describe('common.js', function () {
566577
});
567578

568579
it('sendVoice should ok', function (done) {
569-
api.sendVoice(puling, 'imageId', function (err, data, res) {
580+
api.sendVoice(puling, voiceId, function (err, data, res) {
570581
expect(err).not.to.be.ok();
571582
expect(data).to.have.property('errcode', 0);
572583
expect(data).to.have.property('errmsg', 'ok');
@@ -575,10 +586,10 @@ describe('common.js', function () {
575586
});
576587

577588
it('sendVideo should ok', function (done) {
578-
api.sendVideo(puling, 'mediaId', 'thumbMediaId', function (err, data, res) {
579-
should.exist(err);
580-
err.name.should.be.equal('WeChatAPIError');
581-
err.message.should.be.equal('invalid appid');
589+
api.sendVideo(puling, movieId, thumbId, function (err, data, res) {
590+
expect(err).not.to.be.ok();
591+
expect(data).to.have.property('errcode', 0);
592+
expect(data).to.have.property('errmsg', 'ok');
582593
done();
583594
});
584595
});
@@ -589,13 +600,13 @@ describe('common.js', function () {
589600
"description":"MUSIC_DESCRIPTION", // 可选
590601
"musicurl":"MUSIC_URL",
591602
"hqmusicurl":"HQ_MUSIC_URL",
592-
"thumb_media_id":"THUMB_MEDIA_ID"
603+
"thumb_media_id": thumbId
593604
};
594605

595606
api.sendMusic(puling, music, function (err, data, res) {
596-
should.exist(err);
597-
err.name.should.be.equal('WeChatAPIError');
598-
err.message.should.be.equal('invalid appid');
607+
expect(err).not.to.be.ok();
608+
expect(data).to.have.property('errcode', 0);
609+
expect(data).to.have.property('errmsg', 'ok');
599610
done();
600611
});
601612
});
@@ -618,48 +629,25 @@ describe('common.js', function () {
618629
});
619630
});
620631

621-
it('deliverNotify should ok', function (done) {
622-
api.deliverNotify('{}', function (err, menu) {
623-
should.exist(err);
624-
err.name.should.be.equal('WeChatAPIError');
625-
err.message.should.be.equal('invalid appid');
626-
done();
627-
});
628-
});
629-
630-
it('orderQuery should ok', function (done) {
631-
api.orderQuery('{}', function (err, menu) {
632-
should.exist(err);
633-
err.name.should.be.equal('WeChatAPIError');
634-
err.message.should.be.equal('invalid appid');
635-
done();
636-
});
637-
});
638-
639632
describe('upload media', function () {
633+
var fixture = {
634+
'Image': path.join(__dirname, './fixture/image.jpg'),
635+
'Voice': path.join(__dirname, './fixture/test.mp3'),
636+
'Video': path.join(__dirname, './fixture/movie.mp4'),
637+
'Thumb': path.join(__dirname, './fixture/pic.jpg')
638+
};
640639
['Image', 'Voice', 'Video', 'Thumb'].forEach(function (method) {
641-
// before(function () {
642-
// muk(urllib, 'request', function (url, args, callback) {
643-
// var resp = {
644-
// "type":"image",
645-
// "media_id":"usr5xL_gcxapoRjwH3bQZw_zKvcXL-DU4tRJtLtrtN71-3bXL52p3xX63ebp7tqA",
646-
// "created_at":1383233542
647-
// };
648-
// process.nextTick(function () {
649-
// callback(null, resp);
650-
// });
651-
// });
652-
// });
653-
654-
// after(function () {
655-
// muk.restore();
656-
// });
657640
it('upload' + method + ' should ok', function (done) {
658-
api['upload' + method](path.join(__dirname, './fixture/image.jpg'), function (err, data, res) {
641+
// 上传文件比较慢
642+
this.timeout(60000);
643+
api['upload' + method](fixture[method], function (err, data, res) {
659644
should.not.exist(err);
660-
console.log(data);
661-
data.should.have.property('type', 'image');
662-
data.should.have.property('media_id');
645+
data.should.have.property('type', method.toLowerCase());
646+
if (method === 'Thumb') {
647+
data.should.have.property('thumb_media_id');
648+
} else {
649+
data.should.have.property('media_id');
650+
}
663651
data.should.have.property('created_at');
664652
done();
665653
});
@@ -759,4 +747,25 @@ describe('common.js', function () {
759747
});
760748
});
761749
});
750+
751+
describe('pay', function () {
752+
var api = new API('appid', 'secret');
753+
it('deliverNotify should ok', function (done) {
754+
api.deliverNotify('{}', function (err, menu) {
755+
should.exist(err);
756+
err.name.should.be.equal('WeChatAPIError');
757+
err.message.should.be.equal('invalid appid');
758+
done();
759+
});
760+
});
761+
762+
it('orderQuery should ok', function (done) {
763+
api.orderQuery('{}', function (err, menu) {
764+
should.exist(err);
765+
err.name.should.be.equal('WeChatAPIError');
766+
err.message.should.be.equal('invalid appid');
767+
done();
768+
});
769+
});
770+
});
762771
});

test/fixture/movie.mp4

2.33 MB
Binary file not shown.

test/fixture/pic.jpg

27.7 KB
Loading

test/fixture/test.mp3

74.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)