Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. 判定是否在微信中打开
- ``` ruby
- def wechat?
- request.user_agent =~ /MicroMessenger/i
- end
- ```
- 2. 分别调用 _微信网页扫码_ 或 _公众号授权_
- ``` ruby
- def wechat_cli
- @wechat_cli ||= if wechat?
- WechatMp.new('key', 'secret') # 微信公众号
- else
- Wechat.new('key', 'secret') # 微信扫码
- end
- end
- ```
- 3. 跳转页
- ``` ruby
- redirect wechat_cli.authorize_uri, permanent: true
- ```
- 3. 回调处理
- ``` ruby
- unauthorized('未获得授权') if params[:code].blank?
- token = wechat_cli.token(params[:code])
- if token && token['unionid']
- # 通过 token['unionid'] 查询是否已存在
- # 通过 wechat_cli.userinfo(token) 获取用户信息
- end
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement