Giter Site home page Giter Site logo

meteor-accounts-wechat-mp's Introduction

zhaoyao91:accounts-wechat-mp

Meteor accounts package for wechat-mp. Because this package is generally used in China, this doc will be written in chinese.

简介

用法

1. 添加包

meteor add zhaoyao91:accounts-wechat-mp
meteor add zhaoyao91:short-oauth-state
meteor add service-configuration

2. 配置

server端:

ServiceConfiguration.configurations.upsert({
    service: WeChatMP.serviceName // 可以通过Meteor.settings.public.wechatMPServiceName来修改这个值
}, {
    $set: {
        appId: '...',
        secret: '...',
        scope: 'base_userinfo',
        loginStyle: 'redirect',
        mainId: 'openId'
    }
});

3. 登录

client端:

Meteor.loginWithWeChatMP(function(err, res){
   ... 
})

注意,本服务仅能在微信浏览器中使用,且只能按redirect的方式打开

如何绑定微信开放平台登录?

微信开放平台和微信公众平台对于meteor来说,是两个不同的服务,对微信来说,登录的接口也有所不同。所以如果一个meteor同时支持开放 平台登录和公众平台登录,那么同一微信用户若使用两种不同的方式登录,会分别进入到不同的meteor账号。

微信提供了unionId机制来打通开放平台和公众平台(详情请查看微信相关文档)。在meteor应用中,如果所使用的开放平台账号和公众平台账号 已经绑定,那么登录应用后不仅可以获得openId,还可以获得unionId,此时,只要应用使用unionId作为服务微信服务的mainId,就可以打通两 种方式登录。

配置指南

注意,该配置仅供参考,表达原理,具体方式还需要根据应用实际情况确定。

  1. 应用使用的开放平台账号和公众平台账号要进行绑定
  2. meteor应用同时安装accounts-wechat和accounts-wechat-mp
  3. 配置服务时,mainId均使用unionId(而非openId)
  4. 同步meteor用户的两种服务的id字段,比如,若用户A绑定到wechat时,则设置A.services['wechat-mp'].id = A.services['wechat'].id。 再比如,若用户系统设计为用户只能使用微信登录,而没有其他方式变更用户绑定的微信,则可以
Accounts.onCreateUser(function (options, user) {
    if (options.profile) {
        user.profile = options.profile;
    }

    const wechatUnionId = _.get(user, 'services.wechat.id') || _.get(user, 'services.wechat-mp.id');
    if (wechatUnionId) {
        _.set(user, 'services.wechat.id', wechatUnionId);
        _.set(user, 'services.wechat-mp.id', wechatUnionId);
    }

    return user;
});

Note:

微信开放平台相关应用的授权回调域、对应Meteor应用的ROOT_URL以及用户访问该应用的实际url必须保持一致。

在绑定微信所有平台,需要先进行开发者认证,微信需要你有自己的服务器资源,填写的URL需要正确响应微信发送的Token验证,具体的js 在这里可以找到:如何接入微信公众平台(meteor)

为什么要安装zhaoyao91:short-oauth-state包?
因为Meteor默认将auth state编码到url中,导致url比较长,而微信对url的长度有限制,导致认证失败。该包仅提供一种workaround方案:将auth state保存到服务端,而仅在url中保存该state的id,从而缩短url长度。如果您有更好的解决方案,欢迎向社区做贡献!

参考

meteor-accounts-wechat-mp's People

Contributors

seanjsong avatar xyx2011 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

meteor-accounts-wechat-mp's Issues

cordova integration

Have you been able to integrate the plugin with Cordova ?
You give reference to boxfish:meteor-wechat (which seems totally broken), so I thought maybe you tried it before.

Meteor.loginWithWechatMP callback is never called

I have been playing around with the lib and found a couple of problems.
First of all, the package zhaoyao91:short-oauth-state should be made mandatory, I knocked my head on the wall to find out why weixin was refusing my requests.

Another problem is that my callback in Meteor.loginWithWechatMP(function callback(err) {
// never executed...
});

I does work for well for the open platform with loginWithWechat().

At the moment I directly hacked the Oauth package overwritting OAuth.getDataAfterRedirect to detect the return from weixin. It works but it's definitely not a good practice...

Is the callback working for you ?

server.js中对HTTP.get的返回值没有做好出错处理

在getTokenResponse()和getIdentity()中都有HTTP.get查询微信接口,返回值是这样处理的:

        if (response.error) // if the http response was an error
            throw response.error;
        if (typeof response.content === "string")
            response.content = JSON.parse(response.content);
        if (response.content.error)
            throw response.content;

Meteor的文档是这样说的:

When run in synchronous mode, either result is returned from the function, or error is thrown.

Contents of the result object:

statusCode Number
Numeric HTTP result status code, or null on error.
content String
The body of the HTTP response as a string.
data Object or null
If the response headers indicate JSON content, this contains the body of the document parsed as a JSON object.
headers Object
A dictionary of HTTP headers from the response.

所以:

  • response应该没有error属性
  • response.content应该总是String类型的,不需要判断
  • response.content.error不存在

比如我用一个过期的access token查询https://api.weixin.qq.com/sns/userinfo接口,结果如下:

curl -v https://api.weixin.qq.com/sns/userinfo\?access_token\=w1INhgRMe11Y_MEXe5_BBgYrxFNzBy9nXgfhaEqtz5n1AVjj7CAB0zwIp08dahb5zTA40yJFE7yFptzFt11WvPcdL-rjP9chlwWfEnRdihQ\&openid\=owKOmuAhaxPcr8Vq0eWJ6H2tCgu0\&lang\=zh-CN
*   Trying 103.7.30.34...
* TCP_NODELAY set
* Connected to api.weixin.qq.com (103.7.30.34) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: mp.weixin.qq.com
* Server certificate: GeoTrust SSL CA - G3
* Server certificate: GeoTrust Global CA
> GET /sns/userinfo?access_token=w1INhgRMe11Y_MEXe5_BBgYrxFNzBy9nXgfhaEqtz5n1AVjj7CAB0zwIp08dahb5zTA40yJFE7yFptzFt11WvPcdL-rjP9chlwWfEnRdihQ&openid=owKOmuAhaxPcr8Vq0eWJ6H2tCgu0&lang=zh-CN HTTP/1.1
> Host: api.weixin.qq.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain
< Date: Fri, 28 Apr 2017 01:11:07 GMT
< Content-Length: 84
<
* Curl_http_done: called premature == 0
* Connection #0 to host api.weixin.qq.com left intact
{"errcode":42001,"errmsg":"access_token expired, hints: [ req_id: O.VrUa0867ns88 ]"}%

在getIdentity()的代码中,response.content就是{"errcode":42001,"errmsg":"access_token expired, hints: [ req_id: O.VrUa0867ns88 ]"},经过JSON.parse后正常返回。从这样parse得到的对象里再去取nicknameheadimgurl等属性,结果都是undefined。

这段对HTTP.get返回值的处理代码,在一切正常时确实能返回正确的结果,出错时却并不能抛出异常。

我们在实际部署中遇到过几次这样的情况,用户登录后创建的账户中profile里只有openId等属性,而没有用户个人信息的nickname等属性。

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.