Skip to content

Commit

Permalink
修复上传视频的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 1, 2014
1 parent 12c2d19 commit 93fefb5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
lib-cov
coverage.html
example
.DS_Store
39 changes: 38 additions & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,43 @@ API.prototype._getGroups = function (callback) {
urllib.request(url, {dataType: 'json'}, wrapper(callback));
};

/**
* 查询用户在哪个分组
* 详情请见:<http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口>
* Examples:
* ```
* api.getWhichGroup(openid, callback);
* ```
* Callback:
*
* - `err`, 调用失败时得到的异常
* - `result`, 调用正常时得到的对象
*
* Result:
* ```
* {
* "groupid": 102
* }
* ```
* @param {String} openid Open ID
* @param {Function} callback 回调函数
*/
API.prototype.getWhichGroup = function (openid, callback) {
this.preRequest(this._getWhichGroup, arguments);
};

/*!
* 查询用户在哪个分组未分组版本
*/
API.prototype._getWhichGroup = function (openid, callback) {
// https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
var url = this.prefix + 'groups/getid?access_token=' + this.token.accessToken;
var data = {
"openid": openid
};
urllib.request(url, postJSON(data), wrapper(callback));
};

/**
* 创建分组
* 详情请见:<http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口>
Expand Down Expand Up @@ -750,7 +787,7 @@ API.prototype._sendVideo = function (openid, mediaId, thumbMediaId, callback) {
var data = {
"touser": openid,
"msgtype":"video",
"image": {
"video": {
"media_id": mediaId,
"thumb_media_id": thumbMediaId
}
Expand Down
103 changes: 56 additions & 47 deletions test/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var API = require('../').API;

var puling = 'ofL4cs7hr04cJIcu600_W-ZwwxHg';
var imageId = 'XDZxzuRWBPqI4R9n_nNR5uRVZVQCSneMoELyWKflwM2qF9K38vnVFzgaD97uCTUu';
var voiceId = '9R5BhAum7AEaGhwku0WhgvtO4C_7Xs78NoiRvm6v7IyoTljE4HH5o8E_UfnPrL0p';
var thumbId = 'BHxGDVy7WY6BCOcv3AwbywUE630Vw0tAV_V8bzBaCZid4Km5fwXrVOso3X0zas4n';
var movieId = 'b4F8SfaZZQwalDxwPjd923ACV5IUeYvZ9-dYKf5ytXrS-IImXEkl2U8Fl5EH-jCF';

describe('common.js', function () {
describe('getAccessToken', function () {
Expand Down Expand Up @@ -493,6 +496,14 @@ describe('common.js', function () {
});
});

it('getWhichGroup should ok', function (done) {
api.getWhichGroup(puling, function (err, data, res) {
expect(err).not.to.be.ok();
expect(data).to.have.property('groupid');
done();
});
});

it('createGroup should ok', function (done) {
api.createGroup('new group', function (err, data, res) {
expect(err).not.to.be.ok();
Expand Down Expand Up @@ -566,7 +577,7 @@ describe('common.js', function () {
});

it('sendVoice should ok', function (done) {
api.sendVoice(puling, 'imageId', function (err, data, res) {
api.sendVoice(puling, voiceId, function (err, data, res) {
expect(err).not.to.be.ok();
expect(data).to.have.property('errcode', 0);
expect(data).to.have.property('errmsg', 'ok');
Expand All @@ -575,10 +586,10 @@ describe('common.js', function () {
});

it('sendVideo should ok', function (done) {
api.sendVideo(puling, 'mediaId', 'thumbMediaId', function (err, data, res) {
should.exist(err);
err.name.should.be.equal('WeChatAPIError');
err.message.should.be.equal('invalid appid');
api.sendVideo(puling, movieId, thumbId, function (err, data, res) {
expect(err).not.to.be.ok();
expect(data).to.have.property('errcode', 0);
expect(data).to.have.property('errmsg', 'ok');
done();
});
});
Expand All @@ -589,13 +600,13 @@ describe('common.js', function () {
"description":"MUSIC_DESCRIPTION", // 可选
"musicurl":"MUSIC_URL",
"hqmusicurl":"HQ_MUSIC_URL",
"thumb_media_id":"THUMB_MEDIA_ID"
"thumb_media_id": thumbId
};

api.sendMusic(puling, music, function (err, data, res) {
should.exist(err);
err.name.should.be.equal('WeChatAPIError');
err.message.should.be.equal('invalid appid');
expect(err).not.to.be.ok();
expect(data).to.have.property('errcode', 0);
expect(data).to.have.property('errmsg', 'ok');
done();
});
});
Expand All @@ -618,48 +629,25 @@ describe('common.js', function () {
});
});

it('deliverNotify should ok', function (done) {
api.deliverNotify('{}', function (err, menu) {
should.exist(err);
err.name.should.be.equal('WeChatAPIError');
err.message.should.be.equal('invalid appid');
done();
});
});

it('orderQuery should ok', function (done) {
api.orderQuery('{}', function (err, menu) {
should.exist(err);
err.name.should.be.equal('WeChatAPIError');
err.message.should.be.equal('invalid appid');
done();
});
});

describe('upload media', function () {
var fixture = {
'Image': path.join(__dirname, './fixture/image.jpg'),
'Voice': path.join(__dirname, './fixture/test.mp3'),
'Video': path.join(__dirname, './fixture/movie.mp4'),
'Thumb': path.join(__dirname, './fixture/pic.jpg')
};
['Image', 'Voice', 'Video', 'Thumb'].forEach(function (method) {
// before(function () {
// muk(urllib, 'request', function (url, args, callback) {
// var resp = {
// "type":"image",
// "media_id":"usr5xL_gcxapoRjwH3bQZw_zKvcXL-DU4tRJtLtrtN71-3bXL52p3xX63ebp7tqA",
// "created_at":1383233542
// };
// process.nextTick(function () {
// callback(null, resp);
// });
// });
// });

// after(function () {
// muk.restore();
// });
it('upload' + method + ' should ok', function (done) {
api['upload' + method](path.join(__dirname, './fixture/image.jpg'), function (err, data, res) {
// 上传文件比较慢
this.timeout(60000);
api['upload' + method](fixture[method], function (err, data, res) {
should.not.exist(err);
console.log(data);
data.should.have.property('type', 'image');
data.should.have.property('media_id');
data.should.have.property('type', method.toLowerCase());
if (method === 'Thumb') {
data.should.have.property('thumb_media_id');
} else {
data.should.have.property('media_id');
}
data.should.have.property('created_at');
done();
});
Expand Down Expand Up @@ -759,4 +747,25 @@ describe('common.js', function () {
});
});
});

describe('pay', function () {
var api = new API('appid', 'secret');
it('deliverNotify should ok', function (done) {
api.deliverNotify('{}', function (err, menu) {
should.exist(err);
err.name.should.be.equal('WeChatAPIError');
err.message.should.be.equal('invalid appid');
done();
});
});

it('orderQuery should ok', function (done) {
api.orderQuery('{}', function (err, menu) {
should.exist(err);
err.name.should.be.equal('WeChatAPIError');
err.message.should.be.equal('invalid appid');
done();
});
});
});
});
Binary file added test/fixture/movie.mp4
Binary file not shown.
Binary file added test/fixture/pic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixture/test.mp3
Binary file not shown.

0 comments on commit 93fefb5

Please sign in to comment.