Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问可以获取联系人或群成员的性别、所属地域、头像吗? #121

Closed
wuhaoworld opened this issue Dec 7, 2016 · 13 comments

Comments

@wuhaoworld
Copy link

wuhaoworld commented Dec 7, 2016

看文档 Contact Class 中并没有提供性别和所属地域以及头像的支持,在 web 页面中可以获得用户的地域和性别属性,刚好一个小项目中需要用到,希望未来能支持,感谢!

@udbmnm
Copy link

udbmnm commented Dec 7, 2016

@wuhaoworld 在web上有的就是可以获取的

@wuhaoworld
Copy link
Author

@udbmnm 文档中并未提供相关 API

@wuhaoworld wuhaoworld changed the title 请问可以获取联系人或群成员的性别和所属地域吗? 请问可以获取联系人或群成员的性别、所属地域、头像吗? Dec 7, 2016
@huan
Copy link
Member

huan commented Dec 7, 2016

@wuhaoworld

We are planing to add Contact.sex() & Contact.province() & Contact.city() in the future. Before that, you can use Contact.get('sex') & Contact.get('province') & Contact.get('city') to get them.

For more details about the user contact information which is not exposed by API, you can try to console.log(contact.obj) & console.log(contact.rawObj), and any new API suggestion is welcome.

The harder part is to get the contact avatar. I do not know if you could get the avatar picture by URL directly, as the server might check the cookies. Please let me know if the url works for you, if not, I can add a new method to Contact for you, to get the avatar file.

Thanks.

@wuhaoworld
Copy link
Author

@zixia thank you very much. 获取头像时,微信确实会检查 Cookie,未登录状态下头像地址无效。
API 可以设计成下载头像文件到指定目录,这个需求倒是比较小众了。

性别接口建议使用 gender 这个词。

@huan
Copy link
Member

huan commented Dec 7, 2016

Ok.

And how do you think about the province() and city() method name? I'm wondering if we should combine them to one method?

@huan
Copy link
Member

huan commented Dec 7, 2016

@wuhaoworld

Some new methods that you required just added.

Could you please test them and let me know if they work as expected?

Thanks.

+  /**
+   * Contact gender
+   * @return Gender.Male(2) | Gender.Femail(1) | Gender.Unknown(0)
+   */
+  public gender()   { return this.obj ? this.obj.sex : Gender.Unknown }
+  public province() { return this.obj && this.obj.province }
+  public city()     { return this.obj && this.obj.city }
+
+  /**
+   * Get avatar picture file stream
+   */
+  public async avatar(): Promise<NodeJS.ReadableStream> {

@nolanlee
Copy link

nolanlee commented Dec 8, 2016

@zixia
gender(), province(), city() 可以正常工作, 但是我在执行 avatar() 的时候失败了,异常如下:

WARN Contact avatar() exception: Error: protocol unknown: null
    at Function.urlStream (/xxx/node_modules/wechaty/dist/src/util-lib.js:94:19)
    at Contact.<anonymous> (/xxx/node_modules/wechaty/dist/src/contact.js:90:43)
    at next (native)
    at fulfilled (/xxx/node_modules/wechaty/dist/src/contact.js:4:58)
    at process._tickCallback (internal/process/next_tick.js:103:7)
(node:25907) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: protocol unknown: null

我在调试的时候发现 rawObj.HeadImgUrl 是一个 pathname (形如: /cgi-bin/mmwebwx-bin/webwxgeticon?seq=xxx&username=xxx&skey=xxx), 并没有 protocolhost,这样导致 const protocol = u.protocol 赋值后 protocolnull

@huan
Copy link
Member

huan commented Dec 9, 2016

@nolanlee Thanks for your great feedback.

Then what we need to do is to add a new method like serverHost() to get the current wechat web server name, like wx.qq.com or wx2.qq.com/wx8.qq.com, and add it to the url.

I'll check it later.

@huan
Copy link
Member

huan commented Dec 13, 2016

@nolanlee avatar() should work now, I tested it without any problem.

Please let me know if it works for you. Thanks.

@nolanlee
Copy link

@zixia avatar() 可以正常工作,棒🖐

@huan
Copy link
Member

huan commented Dec 20, 2016

@nolanlee Great! 🖐

@LuKing4DB
Copy link

现在又获取不到头像了,相关的项目也是如此

@huan
Copy link
Member

huan commented Jul 8, 2017

@LuKing4DB Could you please submit a new issue, with the related "相关的项目也是如此" issues links? Because it must have others changes on the server side, and this issue is already closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants