All Projects → eggmantv → wechat_gate

eggmantv / wechat_gate

Licence: MIT license
隔壁老李的微信开发Ruby Gem

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to wechat gate

zengin-rb
💎 The ruby implementation of ZenginCode.
Stars: ✭ 83 (+56.6%)
Mutual labels:  gem
sixarm ruby unaccent
SixArm.com » Ruby » Unaccent replaces a string's accented characters with ASCII characters.
Stars: ✭ 15 (-71.7%)
Mutual labels:  gem
zapier-REST-hooks
Rails engine that provides all the functionality/structure for Zapier REST hooks pattern.
Stars: ✭ 23 (-56.6%)
Mutual labels:  gem
modular routes
Dedicated controllers for each of your Rails route actions.
Stars: ✭ 45 (-15.09%)
Mutual labels:  gem
annoy.rb
annoy-rb provides Ruby bindings for the Annoy (Approximate Nearest Neighbors Oh Yeah).
Stars: ✭ 23 (-56.6%)
Mutual labels:  gem
best buy ruby
Ruby library for the BestBuy API. https://www.bestbuy.com
Stars: ✭ 21 (-60.38%)
Mutual labels:  gem
make model searchable
Adds simlpe search functionality to models
Stars: ✭ 27 (-49.06%)
Mutual labels:  gem
visual-call-graph
Visual Call Graph will help you visualize how a method works by creating a call graph of all the other methods called.
Stars: ✭ 46 (-13.21%)
Mutual labels:  gem
webpay rails
WebpayRails is an easy solution for integrate Transbank Webpay in Rails applications
Stars: ✭ 16 (-69.81%)
Mutual labels:  gem
profiling
Non-discriminatory profiling of Ruby code leveraging the ruby-prof gem
Stars: ✭ 12 (-77.36%)
Mutual labels:  gem
jsonapi-serializer-formats
💎 Gem to enrich jsonapi-serializer with multiple formats
Stars: ✭ 20 (-62.26%)
Mutual labels:  gem
ascii85gem
A simple gem that provides methods for encoding/decoding Adobe’s binary-to-text encoding of the same name.
Stars: ✭ 18 (-66.04%)
Mutual labels:  gem
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (-60.38%)
Mutual labels:  gem
activerecord-shard for
Database Sharding Library for ActiveRecord
Stars: ✭ 16 (-69.81%)
Mutual labels:  gem
rudash
Rudash - Lodash for Ruby Apps
Stars: ✭ 27 (-49.06%)
Mutual labels:  gem
nanook
Ruby library for making and receiving payments and managing a nano currency node
Stars: ✭ 17 (-67.92%)
Mutual labels:  gem
secs4java8
This library is SEMI-SECS-communicate implementation on Java8.
Stars: ✭ 46 (-13.21%)
Mutual labels:  gem
historical-bank-ruby
A Ruby Bank that serves historical exchange rates
Stars: ✭ 14 (-73.58%)
Mutual labels:  gem
rails-countries
Integration between Rails and countries gem.
Stars: ✭ 17 (-67.92%)
Mutual labels:  gem
icodi
Deterministic Random SVG Icon Generator
Stars: ✭ 18 (-66.04%)
Mutual labels:  gem

WechatGate

微信公众平台开发库

支持的接口:

  • access_token(后端API使用)
  • 用户授权信息获取(OAuth2)
  • JS-SDK
  • 回复消息封装
  • 菜单接口
  • 素材接口

功能特点:

  • 自动管理access_token和JS-SDK的ticket刷新和过期
  • 多微信公众号支持
  • 多环境支持(development, production),方便本地测试
  • Controler和helper方法(微信session管理等等)
  • 接口简单,方便定制

使用视频教程: 微信公众号开发

Installation

Add this line to your application's Gemfile:

gem 'wechat-gate'

And then execute:

$ bundle

Or install it yourself as:

$ gem install wechat-gate

公众号

在开工之前你需要在微信公众账号平台做以下配置:

  1. 开通你的公众号(服务号),并开通微信认证(300元认证服务费)
  2. 在公众号后台“公众号设置” - “功能设置”中设置你的JS接口安全域名,就是你的公众号调用的网站的域名
  3. 在“接口权限” - “网页授权获取用户基本信息”中设置你的授权回调页面域名,这个用于OAuth2的回调域名认证
  4. 在“基本配置”中查看并配置你的AppID和AppSecret

配置

在Rails项目config目录下建立文件wechat.yml,并配置你的公众号信息.

# 区分不同的环境
eggman:
  development:
    host: http://wechat-test1.eggman.tv

    wechat_id: xxxxxxxxxx
    app_id: xxxxxxxxxx
    app_secret: xxxxxxxxxx

    oauth2_redirect_uri: "http://wechat-test1.eggman.tv/wechat/users/callback"

    push_url: "http://wechat-test1.eggman.tv/wechat/push"
    push_token: xxxxxxxxxxxxxxxxxxxx
  production:
    host: https://eggman.tv

    wechat_id: xxxxxxxxxx
    app_id: xxxxxxxxxxxxxxxxxxxx
    app_secret: xxxxxxxxxxxxxxxxxxxxxxxxxx

# 如果不需要多环境支持,也可以这样
app_name:
  app_id: <%= ENV['WECHAT_APP_NAME_APP_ID'] %>
  app_secret: <%= ENV['WECHAT_APP_NAME_APP_SECRET'] %>
  oauth2_redirect_uri: <%= ENV['WECHAT_APP_NAME_OAUTH2_REDIRECT_URI'] %>

然后在ApplicationController中指定当前要读取的公众号名称:

self.wechat_gate_app_name = 'eggman'

后端调用

后台API操作(比如微信用户信息获取等等操作)。

默认情况下在controller中已经初始化了配置,方法为wechat_gate_config,直接使用就行。

wechat_gate_config.users # 获取用户列表
wechat_gate_config.user('ONE_OPEN_ID') # 获取一个用户的详细信息
wechat_gate_config.access_token # 获取当前access_token

# OAuth 2
wechat_gate_config.oauth2_entrance_url(scope: "snsapi_userinfo", state: "CURENT_STATE") # 获取当前OAuth2授权入口URL
wechat_gate_config.oauth2_access_token("TOKEN") # 根据OAuth2返回的TOKEN获取access_token
wechat_gate_config.oauth2_user("ACCESS_TOKEN", "ONE_OPEN_ID") # 获取一个用户的信息

wechat_gate_config.medias # 获取素材列表, 参数type: image | video | voice | news (图文)

wechat_gate_config.menu_get # 获取菜单
wechat_gate_config.menu_create(MENU_HASH) # 创建菜单

wechat_gate_config.generate_js_request_params(REFERER_URL) # 返回JS-SDK的验证参数,供前端JS-SDK使用

当然你也可以手工来初始化配置,甚至指定配置文件的路径:

config = WechatGate::Config.new('eggman', '/path/to/what/ever/you/want.yml')

access_token和JS_SDK中ticket都有过期时间和刷新次数限制,这里已经考虑了,你可以不用管,如果你想手工刷新,可以这样:

config.refresh_access_token
config.refresh_jsapi_ticket

配置文件支持erb

更多接口和文档请直接看源码,写的很详细

JS-SDK

  def ticket
    url = CGI.unescape(params[:url]) # 微信中用户访问的页面
    @data = wechat_gate_config.generate_js_request_params(url) # 生成微信JS-SDK所需的jsapi_ticket,signature等等参数供前段js使用
    render content_type: "application/javascript"
  end

ticket.js.erb:

var wxServerConfig = <%= @data.to_json.html_safe %>;
<%= params[:callback] %>();

然后在微信端页面引入以下代码:

(function() {
  var ticket = document.createElement("script");
  ticket.src = "http://localhost/api/wechat_ticket/ticket.js?url=" + encodeURIComponent(window.location.href.split('#')[0]) + "&callback=wxCallback";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(ticket, s);
})();

其他功能

自定义菜单

首先设置菜单配置文件,config/wechat_menu.yml,支持erb,格式请参考微信自定义菜单文档:

button:
  - type: view
    name: 我的2
    url: <%= @config.oauth2_entrance_url(scope: 'snsapi_userinfo', state: 'profile') %>
  - type: view
    name: 课程
    sub_button:
      - type: view
        name: 免费课程
        url:  <%= @config.oauth2_entrance_url(scope: 'snsapi_userinfo', state: 'free') %>
      - type: view
        name: 付费课程
        url:  <%= @config.oauth2_entrance_url(scope: 'snsapi_userinfo', state: 'paid') %>

其中的**@config**变量为当前微信公众号实例,请不要修改,直接使用

然后执行rake任务:

$rails wechat_gate:create_menu APP_NAME=eggman CONFIG=/path/to/wechat.yml MENU=/path/to/wechat_menu.yml

其中,CONFIG默认为config/wechat.yml,MENU默认为config/wechat_menu.yml,APP_NAME必须指定

TODO

添加测试

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].