Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. 1. 判定是否在微信中打开
  2. ``` ruby
  3. def wechat?
  4. request.user_agent =~ /MicroMessenger/i
  5. end
  6. ```
  7. 2. 分别调用 _微信网页扫码_ 或 _公众号授权_
  8. ``` ruby
  9. def wechat_cli
  10. @wechat_cli ||= if wechat?
  11. WechatMp.new('key', 'secret') # 微信公众号
  12. else
  13. Wechat.new('key', 'secret') # 微信扫码
  14. end
  15. end
  16. ```
  17. 3. 跳转页
  18. ``` ruby
  19. redirect wechat_cli.authorize_uri, permanent: true
  20. ```
  21. 3. 回调处理
  22. ``` ruby
  23. unauthorized('未获得授权') if params[:code].blank?
  24. token = wechat_cli.token(params[:code])
  25. if token && token['unionid']
  26. # 通过 token['unionid'] 查询是否已存在
  27. # 通过 wechat_cli.userinfo(token) 获取用户信息
  28. end
  29. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement