{"id":17826426,"url":"https://github.com/eggmantv/wechat_gate","last_synced_at":"2025-07-25T13:10:43.582Z","repository":{"id":56898039,"uuid":"83650467","full_name":"eggmantv/wechat_gate","owner":"eggmantv","description":"隔壁老李的微信开发Ruby Gem","archived":false,"fork":false,"pushed_at":"2018-04-16T14:42:21.000Z","size":28,"stargazers_count":57,"open_issues_count":1,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-25T22:49:12.739Z","etag":null,"topics":["gem","rails","ruby","wechat"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eggmantv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-02T07:54:06.000Z","updated_at":"2025-01-04T14:31:03.000Z","dependencies_parsed_at":"2022-08-20T17:40:30.229Z","dependency_job_id":null,"html_url":"https://github.com/eggmantv/wechat_gate","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/eggmantv/wechat_gate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggmantv%2Fwechat_gate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggmantv%2Fwechat_gate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggmantv%2Fwechat_gate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggmantv%2Fwechat_gate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eggmantv","download_url":"https://codeload.github.com/eggmantv/wechat_gate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggmantv%2Fwechat_gate/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267010407,"owners_count":24020819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","rails","ruby","wechat"],"created_at":"2024-10-27T18:47:46.423Z","updated_at":"2025-07-25T13:10:43.522Z","avatar_url":"https://github.com/eggmantv.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WechatGate\n\n**微信公众平台开发库**\n\n支持的接口:\n\n- access_token(后端API使用)\n- 用户授权信息获取(OAuth2)\n- JS-SDK\n- 回复消息封装\n- 菜单接口\n- 素材接口\n\n功能特点:\n\n- 自动管理access_token和JS-SDK的ticket刷新和过期\n- 多微信公众号支持\n- 多环境支持(development, production)，方便本地测试\n- Controler和helper方法(微信session管理等等)\n- 接口简单，方便定制\n\n使用视频教程: [微信公众号开发](https://eggman.tv/c/s-wechat-development-using-ruby-on-rails)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'wechat-gate'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install wechat-gate\n\n## 公众号\n\n在开工之前你需要在微信公众账号平台做以下配置:\n\n1. 开通你的公众号（服务号），并开通微信认证（300元认证服务费）\n2. 在公众号后台“公众号设置” － “功能设置”中设置你的JS接口安全域名，就是你的公众号调用的网站的域名\n3. 在“接口权限” － “网页授权获取用户基本信息”中设置你的授权回调页面域名，这个用于OAuth2的回调域名认证\n4. 在“基本配置”中查看并配置你的AppID和AppSecret\n\n## 配置\n\n在Rails项目config目录下建立文件wechat.yml，并配置你的公众号信息.\n\n```\n# 区分不同的环境\neggman:\n  development:\n    host: http://wechat-test1.eggman.tv\n\n    wechat_id: xxxxxxxxxx\n    app_id: xxxxxxxxxx\n    app_secret: xxxxxxxxxx\n\n    oauth2_redirect_uri: \"http://wechat-test1.eggman.tv/wechat/users/callback\"\n\n    push_url: \"http://wechat-test1.eggman.tv/wechat/push\"\n    push_token: xxxxxxxxxxxxxxxxxxxx\n  production:\n    host: https://eggman.tv\n\n    wechat_id: xxxxxxxxxx\n    app_id: xxxxxxxxxxxxxxxxxxxx\n    app_secret: xxxxxxxxxxxxxxxxxxxxxxxxxx\n\n# 如果不需要多环境支持，也可以这样\napp_name:\n  app_id: \u003c%= ENV['WECHAT_APP_NAME_APP_ID'] %\u003e\n  app_secret: \u003c%= ENV['WECHAT_APP_NAME_APP_SECRET'] %\u003e\n  oauth2_redirect_uri: \u003c%= ENV['WECHAT_APP_NAME_OAUTH2_REDIRECT_URI'] %\u003e\n```\n\n然后在ApplicationController中指定当前要读取的公众号名称：\n\n```\nself.wechat_gate_app_name = 'eggman'\n```\n\n## 后端调用\n\n后台API操作(比如微信用户信息获取等等操作)。\n\n默认情况下在controller中已经初始化了配置，方法为**wechat_gate_config**，直接使用就行。\n\n\n```ruby\nwechat_gate_config.users # 获取用户列表\nwechat_gate_config.user('ONE_OPEN_ID') # 获取一个用户的详细信息\nwechat_gate_config.access_token # 获取当前access_token\n\n# OAuth 2\nwechat_gate_config.oauth2_entrance_url(scope: \"snsapi_userinfo\", state: \"CURENT_STATE\") # 获取当前OAuth2授权入口URL\nwechat_gate_config.oauth2_access_token(\"TOKEN\") # 根据OAuth2返回的TOKEN获取access_token\nwechat_gate_config.oauth2_user(\"ACCESS_TOKEN\", \"ONE_OPEN_ID\") # 获取一个用户的信息\n\nwechat_gate_config.medias # 获取素材列表, 参数type: image | video | voice | news (图文)\n\nwechat_gate_config.menu_get # 获取菜单\nwechat_gate_config.menu_create(MENU_HASH) # 创建菜单\n\nwechat_gate_config.generate_js_request_params(REFERER_URL) # 返回JS-SDK的验证参数，供前端JS-SDK使用\n```\n\n当然你也可以手工来初始化配置，甚至指定配置文件的路径：\n\n```\nconfig = WechatGate::Config.new('eggman', '/path/to/what/ever/you/want.yml')\n```\n\naccess_token和JS_SDK中ticket都有过期时间和刷新次数限制，这里已经考虑了，你可以不用管，如果你想手工刷新，可以这样:\n\n```\nconfig.refresh_access_token\nconfig.refresh_jsapi_ticket\n```\n\n**配置文件支持erb**\n\n\u003e 更多接口和文档请直接看源码，写的很详细\n\n## JS-SDK\n\n```ruby\n  def ticket\n    url = CGI.unescape(params[:url]) # 微信中用户访问的页面\n    @data = wechat_gate_config.generate_js_request_params(url) # 生成微信JS-SDK所需的jsapi_ticket，signature等等参数供前段js使用\n    render content_type: \"application/javascript\"\n  end\n```\n\nticket.js.erb:\n\n```\nvar wxServerConfig = \u003c%= @data.to_json.html_safe %\u003e;\n\u003c%= params[:callback] %\u003e();\n```\n\n然后在微信端页面引入以下代码:\n\n```js\n(function() {\n  var ticket = document.createElement(\"script\");\n  ticket.src = \"http://localhost/api/wechat_ticket/ticket.js?url=\" + encodeURIComponent(window.location.href.split('#')[0]) + \"\u0026callback=wxCallback\";\n  var s = document.getElementsByTagName(\"script\")[0];\n  s.parentNode.insertBefore(ticket, s);\n})();\n```\n\n## 其他功能\n\n### 自定义菜单\n\n首先设置菜单配置文件，config/wechat_menu.yml，支持erb，格式请参考[微信自定义菜单文档](https://mp.weixin.qq.com/wiki):\n\n```\nbutton:\n  - type: view\n    name: 我的2\n    url: \u003c%= @config.oauth2_entrance_url(scope: 'snsapi_userinfo', state: 'profile') %\u003e\n  - type: view\n    name: 课程\n    sub_button:\n      - type: view\n        name: 免费课程\n        url:  \u003c%= @config.oauth2_entrance_url(scope: 'snsapi_userinfo', state: 'free') %\u003e\n      - type: view\n        name: 付费课程\n        url:  \u003c%= @config.oauth2_entrance_url(scope: 'snsapi_userinfo', state: 'paid') %\u003e\n```\n\n\u003e 其中的**@config**变量为当前微信公众号实例，请不要修改，直接使用\n\n然后执行rake任务:\n\n```shell\n$rails wechat_gate:create_menu APP_NAME=eggman CONFIG=/path/to/wechat.yml MENU=/path/to/wechat_menu.yml\n```\n\n其中，CONFIG默认为config/wechat.yml，MENU默认为config/wechat_menu.yml，APP_NAME必须指定\n\n## TODO\n\n添加测试\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggmantv%2Fwechat_gate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feggmantv%2Fwechat_gate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggmantv%2Fwechat_gate/lists"}