Coder Social home page Coder Social logo

co-wechat-api's Issues

上传视频素材时会报错

错误现象:

上传视频素材时会报错:

{"data":"socket hang upPOST https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=16_ronA4R5gRJYkl-OYm3GQEvf1Hv4DXB4ud04EhM7aUu8IWMJOL4IRW78Va7uy_fy8_-odPboLBbQJ-RjH-E5HsAtU_sQiLBVpFpgdyAy1EuUwDKfqQLWxmoKE3_n1qcOwevZPXiAJAKAI&type=video failed."}

解决方法:

将源码中的 stream 改为 data

stream: form

[ERROR] api.batchGetUser() NOT A FUNCTION

  const user  = [ 
    'o3gdewT5m6HzRGZrGr726Y3M20AE', 
    'o3gdewZPmhIQlCnatI3LI9D55-cc',
    'o3gdewYlR32p_zrHH76O7DsJOn7I',
    'o3gdewUVPwuExcS9XAjQkOY-8LPU'
  ]

  const userList = await api.batchGetUser(user);
  console.log(userList);
// got as below
(node:6042) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: api.batchGetUser is not a function
(node:6042) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

npm 默认版本问题

使用npm i co-wechat-api时,默认导入的是2.7.2版本
现在最新的版本已经是^3了
最大的差异就在于2版本的是使用的generator,3版本的使用的是async await。
导致在koa中引用的时候一直返回空值。查看源码才发现问题

就算npm默认使用2版本号,希望能至少在文档里提一下注意事项

另外,谢谢,封装的代码很好用

sendTemplate 接口返回 msgid 精度丢失

返回结果:

 {"errcode":0,"errmsg":"ok","msgid":271781022721654784}

由于数字太大了,JSON.parse 之后精度就丢失了,变成了 271781022721654800,目前request接口没提供预处理结果的方法,不好自己修正。

模板消息sendTemplate官方增加了小程序配置

参数 是否必填 说明
touser 接收者openid
template_id 模板ID
url 模板跳转链接
miniprogram 跳小程序所需数据,不需跳小程序可不用传该数据
appid 所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
pagepath 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
data 模板数据

注:url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。

现在的接口不能实现小程序的配置,并且topColor这个参数也去掉了。

Typescript支持

希望可以添加index.d.ts描述文件支持Typescript的开发。

使用上传永久素材接口uploadMeterial时,一直提示Error: socket hang up

测试代码:

var filePath = path.resolve('./public/upload');
var filename = path.join(filePath, '1.jpeg');
var type = 'image';
var result = yield api.uploadMaterial(filename, type);
this.body = result;

已创建好api对象,执行时,返回:

Error: socket hang up
at createHangUpError (_http_client.js:203:15)
at TLSSocket.socketOnEnd (_http_client.js:288:23)
at emitNone (events.js:72:20)
at TLSSocket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at doNTCallback2 (node.js:441:9)
at process._tickCallback (node.js:355:17)

测试环境:
MACOS:10.11.4
node:v4.2.2

换为express+wechat-api则无问题。

(node v0.12.3)no any response when yield httpx.request(url, opts)

as the title said...
when I used the api to get the qrcode, I get stuck at the codes below.
api_common.js

var res = yield httpx.request(url, options);     
console.log(res);//couldn't get the res utill time was out

but when I changed the codes, It worked fine..

var res = yield httpx.request("www.baidu.com", options);
console.log(res);//got it!

then I used the "koa-request" instead of "httpx", both of the above worked fine!
So I was wondering if I should count on the version of node?

async 返回错误

API: batchGetUsers
环境: node v8.1.3
问题描述:
同样的openid 在使 co + generator 的时候 返回结果正常
修改为 anync + await 之后 返回结果始终为一个 空对象
表示重构失败,完全不知所以然,忘解答

获取微信用户信息json parse error问题

{ WeChatAPIError: JSON.parse error. buffer is {"subscribe":1,"openid":"ogW8rt6i8BmyHaXvleP5SBULysSk","nickname":"Íõ¾ü","sex":1,"language":"zh_CN","city":"y?v\","province":"l?S","country":"","headimgurl":"http://wx.qlogo.cn/mmopen/Q3auHgzwzM4nXzLJEGv1SUt5ibMeibUjmaK45y06UOtaKDc2NgjvjjnsiccgwMPyKOwvstIKA85bM7zziac5m9zmfw/0","subscribe_time":1504246660,"unionid":"oKfK5s5qiqgWMHaeJ6f3sy5TYvx8","remark":"","groupid":0,"tagid_list":[]}

现在很多这种错误,我看了下wechat-api的issues说是解决了,co的这个能解决一下吗?

不定期的token失效

错误日志如下:

WeChatAPIError: invalid credential, access_token is invalid or not latest hint: [j0563vr23]
      at API.request (/xxxxx/node_modules/co-wechat-api/lib/api_common.js:122:17)
      at next (native)
      at exports.getTicket (/xxxxx/node_modules/co-wechat-api/lib/api_js.js:74:26)
      at next (native)
      at exports.ensureTicket (/xxxxx/node_modules/co-wechat-api/lib/api_js.js:160:26)
      at next (native)
      at exports.getJsConfig (/xxxxx/node_modules/co-wechat-api/lib/api_js.js:179:28)
      at next (native)
      at onFulfilled (/xxxxx/node_modules/co/index.js:65:19)
      at process._tickDomainCallback (node.js:409:9)

uploadMedia的Shortcut中的uploadImage被后面的同名方法覆盖掉了

['image', 'voice', 'video', 'thumb'].forEach(function (type) {
  var method = 'upload' + type[0].toUpperCase() + type.substring(1);
  exports[method] = function* (filepath) {
    return yield this.uploadMedia(filepath, type);
  };
});

这里定义了一个exports.uploadImage
被下面这个方法覆盖了

exports.uploadImage = function* (filepath) {
  var token = yield this.ensureAccessToken();
  var stat = yield fs.stat(filepath);
  var form = formstream();
  form.file('media', filepath, path.basename(filepath), stat.size);
  var url = this.prefix + 'media/uploadimg?access_token=' + token.accessToken;
  var opts = {
    method: 'POST',
    timeout: 60000, // 60秒超时
    headers: form.headers(),
    data: form
  };
  opts.headers.Accept = 'application/json';
  return yield this.request(url, opts);
};

应该想办法用 bluebird 之类的把 api 包装成 Promise

koa@1 实在是一种误导, 许多项目跟风基于 co 构建.
现在 koa@2 全面切换到 Promise 和 async function 了.

与其把所有方法都写成 generator, 不如全部返回 Promise 适用性更广.
比如用 bluebird 把 wechat-api 包装一下.

不知道是什么错

/root/aasd/node_modules/co-wechat-api/lib/api_common.js:59
this.getToken = getToken || async function () {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/root/51work/node_modules/co-wechat-api/index.js:3:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
/root/51work/node_modules/co-wechat-api/lib/api_common.js:59

getToken 函数的调用应该传入当前的 appid

at line 202:

var token = await this.getToken() => var token = await this.getToken(this.appid)

这样在一个系统维护多个 api 时,可以知道是哪个 api 的缓存被调用了,否则这个函数的实现必须针对每个 api 都用闭包来包装。

上传永久素材时无返回信息,上传临时素材时返回buffer

调用 uploadMaterial(filepath, 'TYPE) 时,无反应,即无成功result,也无errmsg,程序也无报错。
调用 uploadMedia(filename, TYPE)时, 返回buffer,如下,即非腾讯返回示例里的四种类型。

mediaresult: {"type":"Buffer","data":[123,34,118,111,105,99,101,95,99,111,117,11
0,116,34,58,49,44,34,118,105,100,101,111,95,99,111,117,110,116,34,58,48,44,34,10
5,109,97,103,101,95,99,111,117,110,116,34,58,51,44,34,110,101,119,115,95,99,111,
117,110,116,34,58,50,125]}

似乎所有 POST 请求都有 Bug?

co-wechat-api 版本: 2.4.2
httpx 版本: 2.0.0

我使用uploadMedia时一直都是提示"missing media data hint",看了下 co-wechat-api 的代码,似乎所有的 POST API 都可能有问题?

先看 lib/api_media.js
https://github.com/node-webot/co-wechat-api/blob/master/lib/api_media.js#L32

它给 this.request 传的 opts 中 http method 是用 type 来指定的。
在 api_common.js 中,它原样传给 httpx。

再看 httpx 的 index.js:
https://github.com/JacksonTian/httpx/blob/master/index.js#L29

它接受的是method属性名,而不是type

这个是 bug ?

Thanks

shark_around 少了很多的方法

exports.getPageStatistics = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/statistics/page?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

exports.listBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/getlist?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

exports.queryGroupBeacons = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/getdetail?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

exports.addBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/add?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

exports.updateBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/update?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

exports.deleteBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/delete?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

exports.addGroupBeacons = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/adddevice?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

exports.deleteGroupBeacons = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/deletedevice?access_token=' + accessToken;
return this.request(url, postJSON(options));
};

token全局变量的存储,在存储的时候是否还需要判断过期呢

  • 当多进程时,token 需要全局维护,以下为保存 token 的接口。
    • var api = new API('appid', 'secret', async function () {
    • // 传入一个获取全局 token 的方法
    • var txt = await fs.readFile('access_token.txt', 'utf8');
      ========= 在这里是否还需要做token 7200毫秒的过期判断呢?
    • return JSON.parse(txt);
    • }, async function (token) {
    • // 请将 token 存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
    • // 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
    • await fs.writeFile('access_token.txt', JSON.stringify(token));
    • });

异常抛出没有捕获

sendTemplate 接口调用的 request方法里面抛出了异常,但是在sendTemplate接口中并没有捕获异常,导致外部调用时不能捕获该异常。
请问,您的设计就是不让外部捕获这个异常吗?或者关于捕获这个异常您有什么好办法呢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.