wechat-media
v1.0.4
Published
manage wechat-enterprise media
Downloads
6
Readme
注意: 该npm包需要依赖node后台模板中的 ../libs/utils.js 和 ../config.dev/serverConfig.json
Download resource
var Wechat_media = require('wechat_media');
var company_id = "561f72fbe419be013b8b4zyx";
var option = {
url: media_url, //需要下载的资源url
name: filename, //要存储在本地的文件名
type: media_type, //文件类型,可以是"voice"或"image"
savePath: savePath //文件存储的相对路径
};
Wechat_media.download(company_id, option).then(function(ret){
if(ret.result){
var save_url = ret.data.media_url; //下载后的媒体文件可以访问的url地址,你必须首先在serverConfig中设置 static_url,这个将会是 static_url+option.name
var type = ret.data.type; //type为"voice"或"image"
var file = ret.data.file; //本地存储的文件地址
}else{
throw new Error("下载文件时出错");
}
}).catch(function(e){
console.log(e||e.stack);
})
Upload resource to Wechat
var Wechat_media = require('wechat_media');
var company_id = "561f72fbe419be013b8b4zyx";
var option = {
media_url: media_url, //非必须参数,由download方法返回
file: filepath, //本地存储文件的地址
type: media_type //可以是"type"或"image"
};
Wechat_media.upload(company_id, option).then(function(ret){
if(ret.result){
var media_id = ret.data.media_id, //上传到微信后返回的media_id
}else{
throw new Error("上传文件时出错");
}
}).catch(function(e){
console.log(e||e.stack);
})
Save resource from Wechat
var Wechat_media = require('wechat_media');
var company_id = "561f72fbe419be013b8b4zyx";
var option = {
media_id: media_id, //要从微信获取的素材的media_id
name: filename, //要存储在本地的文件名
type: media_type, //文件类型,可以是"voice"或"image"
savePath: savePath //文件存储的相对路径
};
Wechat_media.saveMedia(company_id, option).then(function(ret){
if(ret.result){
var media_url = ret.data.media_url; //下载后的媒体文件可以访问的url地址,你必须首先在serverConfig中设置 static_url,这个将会是 static_url+option.name
var type = ret.data.type; //type为"voice"或"image"
var file = ret.data.file; //本地存储的文件地址
}else{
throw new Error("下载文件时出错");
}
}).catch(function(e){
console.log(e||e.stack);
})
Installation
$ npm install wechat-media