{"id":19035507,"url":"https://github.com/node-webot/wechat-corp-service","last_synced_at":"2025-04-23T18:23:19.090Z","repository":{"id":25313580,"uuid":"28740279","full_name":"node-webot/wechat-corp-service","owner":"node-webot","description":"微信企业版第三方应用接口","archived":false,"fork":false,"pushed_at":"2015-11-09T02:38:47.000Z","size":266,"stargazers_count":24,"open_issues_count":0,"forks_count":23,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-12T08:48:49.560Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-webot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-03T09:51:38.000Z","updated_at":"2021-12-17T18:26:47.000Z","dependencies_parsed_at":"2022-08-06T03:15:31.078Z","dependency_job_id":null,"html_url":"https://github.com/node-webot/wechat-corp-service","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-corp-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-corp-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-corp-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-corp-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-webot","download_url":"https://codeload.github.com/node-webot/wechat-corp-service/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250488227,"owners_count":21438744,"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","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":[],"created_at":"2024-11-08T21:51:03.195Z","updated_at":"2025-04-23T18:23:19.072Z","avatar_url":"https://github.com/node-webot.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"wechat corp service\n====================\n\n微信公共平台企业号版(第三方企业套件)SDK－主动调用接口\n\n\n## 功能说明\n\n企业第三方应用套件开发商管理已经托管的套件应用，以及提供应用授权接口。\n\n## 安装方法\n\n```sh\n$ npm install wechat-corp-service\n```\n\n## 使用方法\n\n- 回调接口请移步: [微信公共平台企业号版(第三方企业套件)SDK－回调接口](https://github.com/node-webot/wechat-corp-service-callback)\n\n### 前提\n\n- 首先，你要有一个企业号。\n- 然后，你要申请成为第三方企业套件的供应商。\n- 接下来才可以创建套件，并且设置套件应用。\n- 基于本SDK开发具体的套件应用。\n\n### 用法\n\n其中的token，encodingAESKey，suite_id可以在套件的信息配置界面获取。\n\n```js\nvar APICorp = require('wechat-corp-service');\n\nvar get_token = function(cb) {\n  var self = this;\n  SuiteConfig\n    .findOne({\n      suite_id: self.suiteId,\n    })\n    .exec(function(err, result) {\n      if (result \u0026\u0026 (new Date().getTime()) \u003c (new Date(result.suite_access_token_expire)).getTime()) {\n        // 有效期内，直接返回\n        cb(null, {\n          suite_access_token: result.suite_access_token,\n          expires_in: result.suite_access_token_expire\n        });\n      } else {\n        cb(null, null);\n      }\n    });\n}\n\nvar save_token = function(token, cb) {\n  var self = this;\n  async.waterfall([\n    function(cb) {\n      SuiteConfig\n        .findOne({\n          suite_id: self.suiteId,\n        }).exec(cb);\n    },\n    function(sc, cb) {\n      if (sc) {\n        //已经存在了，就更新一下token，否则创建一条新的\n        sc.suite_access_token = token.suite_access_token;\n        sc.suite_access_token_expire = new Date((new Date()).getTime() + 7190000);\n        sc.save(cb);\n      } else {\n        cb(null, null);\n      }\n    }\n  ], cb);\n}\n\nvar apicorp = new APICorp(sc.suite_id, sc.suite_secert, sc.suite_ticket, get_token, save_token),\n  // 授权完成后跳转的URL，一般返回套件开发商自己的页面，并且获取用户授权的信息。\n  redirect_uri = 'http://xxx.xxx.xxx/auth_callback_url',\n  auth_url = '';\n\n// 获取临时授权码，生成授权页面（带一个授权的按钮）\napicorp.getPreAuthCode(apps, function(err, result) {\n  auth_url = apicorp.generateAuthUrl(result.pre_auth_code, encodeURIComponent(redirect_uri), 'OK');\n});\n\n// 授权后，跳转回来的URL，可以获取auth_code，然后换取永久授权码。得到永久授权码之后就能知道是那个用户的企业号了。\nvar auth_code = req.query.auth_code;\napicorp.getPermanentCode(auth_code, cb);\n\n//企业号登录授权API\nvar apiProvider = new APICorp.Provider(sc.corpid, sc.providersecret, get_token, save_token);\n\n//生成登录授权URL\nvar login_auth_url = apiProvider.generateAuthUrl('http://youhost/callback_url', 'youstate');\n\n// 授权后，跳转回来的URL，可以获取auth_code.然后可获取用户信息.\nvar auth_code = req.query.auth_code;\napiProvider.getLoginInfo(auth_code, cb);\n```\n\n### 通过代理服务器访问\n\n#### 场景\n\n对于大规模的集群部署模式，为了安全和速度，会有一些负载均衡的节点放在内网的服务器上（即负载均衡的节点与主结点通过内网连接，并且内网服务器上没有外网的IP）。这时，就需要配置代理服务器来使内网的机器可以有限度的访问外网的资源。例如：微信套件中的各种主动调用接口。\n\n如何架设代理服务器在这里不做赘述，一般推荐使用squid 3，免费、快速、配置简单。\n\n#### 技术原理\n\n由于需要访问的微信API服务器是https协议，所以普通的http代理模式不能使用。\n而一般都是http协议的代理服务器。\n我们要实现的就是通过http代理通道来走https的请求。\n\n基本的步骤是2步：\n\n- 连接到代理服务器，发送CONNECT命令，打开一个TCP连接。\n- 使用上一步打开的TCP连接，发送https的请求。\n\n#### 实现步骤\n\n一、下载[node-tunnel](https://github.com/koichik/node-tunnel) 注意：npm上的版本较老，不支持node v0.10以上的版本。\n\n二、使用 httpsOverHttp 这个agent。\n\n三、将agent配置给urllib，通过urllib的beforeRequest这个方法。\n\n```js\nvar tunnel = require('tunnel');\nvar APICorp = require('wechat-corp-service');\n\nvar agent = tunnel.httpsOverHttp({\n  proxy: {\n    host: 'proxy_host_ip',\n    port: 3128\n  }\n});\n\nvar apicorp = new APICorp(sc.suite_id, sc.suite_secert, sc.suite_ticket, get_token, save_token);\n\napicorp.setOpts({\n    beforeRequest:function(options){\n        options.agent = agent;\n    }\n});\n\n```\n\n## 相关文档\n- [微信企业号－第三方应用授权](http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%88%E6%9D%83)\n\n\n## License\nThe MIT license.\n\n## 交流群\nQQ群：157964097，使用疑问，开发，贡献代码请加群。\n\n## 感谢\n感谢以下贡献者：\n```\n project  : wechat-corp-service\n repo age : 10 months\n active   : 7 days\n commits  : 16\n files    : 13\n authors  :\n     6  Jackson Tian  37.5%\n     6  Nick Ma       37.5%\n     3  hezedu        18.8%\n     1  马剑          6.2%\n\n```\n\n## 捐赠\n如果您觉得Wechat企业号版本对您有帮助，欢迎请作者一杯咖啡\n\n![捐赠wechat](https://cloud.githubusercontent.com/assets/327019/2941591/2b9e5e58-d9a7-11e3-9e80-c25aba0a48a1.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-webot%2Fwechat-corp-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-webot%2Fwechat-corp-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-webot%2Fwechat-corp-service/lists"}