{"id":19585603,"url":"https://github.com/tencentyun/wafer2-node-sdk","last_synced_at":"2025-04-05T19:10:29.177Z","repository":{"id":57397007,"uuid":"100224042","full_name":"tencentyun/wafer2-node-sdk","owner":"tencentyun","description":"Wafer2 SDK for Node.js","archived":false,"fork":false,"pushed_at":"2018-10-24T11:01:19.000Z","size":51,"stargazers_count":268,"open_issues_count":9,"forks_count":57,"subscribers_count":55,"default_branch":"master","last_synced_at":"2024-10-16T09:32:36.701Z","etag":null,"topics":["tencent","wafer","weapp"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tencentyun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-14T03:21:01.000Z","updated_at":"2024-02-23T20:01:25.000Z","dependencies_parsed_at":"2022-09-13T10:21:57.386Z","dependency_job_id":null,"html_url":"https://github.com/tencentyun/wafer2-node-sdk","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fwafer2-node-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fwafer2-node-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fwafer2-node-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fwafer2-node-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tencentyun","download_url":"https://codeload.github.com/tencentyun/wafer2-node-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266476,"owners_count":20910831,"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":["tencent","wafer","weapp"],"created_at":"2024-11-11T07:56:12.714Z","updated_at":"2025-04-05T19:10:29.153Z","avatar_url":"https://github.com/tencentyun.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Wafer 服务端 SDK - Node.js\n\n## 介绍\n\nWafer 服务端 SDK 是腾讯云为微信小程序开发者提供的快速开发库，SDK 封装了以下功能供小程序开发者快速调用：\n\n- 用户登录与验证\n- 信道服务\n- 图片上传\n- 数据库\n- 客服消息\n\n开发者只需要根据文档对 SDK 进行初始化配置，就可以获得以上能力。你还可以直接到[腾讯云小程序控制台](https://console.qcloud.com/la)购买小程序解决方案，可以得到运行本示例所需的资源和服务，其中包括已部署好的相关程序、示例代码及自动下发的 SDK 配置文件 `/etc/qcloud/sdk.config`。\n\n## 安装\n\n```bash\nnpm install wafer-node-sdk --save\n```\n\n## 配置\n\n```javascript\nconst configs = {\n  appId: 'wx00dd00dd00dd00dd',\n  appSecret: 'abcdefghijkl',\n  useQcloudLogin: false,\n  cos: {\n    region: 'cn-south',\n    fileBucket: 'test',\n    uploadFolder: ''\n  },\n  serverHost: '1234567.qcloud.la',\n  tunnelServerUrl: '1234567.ws.qcloud.la',\n  tunnelSignatureKey: 'abcdefghijkl',\n  qcloudAppId: '121000000',\n  qcloudSecretId: 'ABCDEFG',\n  qcloudSecretKey: 'abcdefghijkl',\n  wxMessageToken: 'abcdefghijkl'\n}\nconst qcloud = require('qcloud-weapp-server-sdk')(configs)\n```\n\n具体配置项说明请查看：[API 文档](/API.md)。\n\n## API 文档\n\n具体查看 [API 文档](/API.md)。\n\n## 基本功能\n\n#### 用户登录与验证\n\n用户登录使用 `authorization` 接口：\n\n```javascript\nconst { auth: { authorization } } = qcloud\n\n// express\nmodule.exports = (req, res) =\u003e {\n  authorization(req).then(result =\u003e {\n    // result : {\n    //   loginState: 0  // 1表示登录成功，0表示登录失败\n    //   userinfo: { // 用户信息.. }\n    // }\n  })\n}\n```\n\n用户登录态校验使用 `validation` 接口：\n\n```javascript\nconst { auth: { validation } } = qcloud\n\n// express\nmodule.exports = (req, res) =\u003e {\n  validation(req).then(result =\u003e {\n    // result : {\n    //   loginState: 0  // 1表示登录成功，0表示登录失败\n    //   userinfo: { // 用户信息.. }\n    // }\n  })\n}\n```\n\n如果你使用 Koa 框架，则可以直接使用 SDK 导出的 `koaAuthorization` 和 `koaValidation` 中间件，登录信息将会被写进 `ctx.state.$wxInfo`：\n\n```javascript\nconst { auth: { authorizationMiddleware, validationMiddleware } } = qcloud\n\n// 颁发登录态\nrouter.get('/login', authorizationMiddleware, ctx =\u003e {\n  console.log(ctx.state.$wxInfo)\n  // {\n  //   loginState: 0  // 1表示登录成功，0表示登录失败\n  //   userinfo: { // 用户信息.. }\n  // }\n})\n\n// 校验登录态\nrouter.get('/user', validationMiddleware, ctx =\u003e {\n  console.log(ctx.state.$wxInfo)\n  // {\n  //   loginState: 0  // 1表示登录成功，0表示登录失败\n  //   userinfo: { // 用户信息.. }\n  // }\n})\n```\n\n#### 信道服务\n\n业务在一个路由上（如 `/tunnel`）提供信道服务，只需把该路由上的请求都交给 SDK 的信道服务处理即可。使用信道服务需要实现处理器，来获取处理信道的各种事件，具体可参考配套 Demo 中的 tunnel.js 的实现。\n\n#### 图片上传\n\nSDK 提供直接上传图片至腾讯云对象储存（COS）的接口，只需要将请求传入接口，即可自动上传文件到 COS 中，并返回数据：\n\n```javascript\nconst { uploader } = qcloud\n\nmodule.exports = async ctx =\u003e {\n  await uploader(ctx.req).then(data =\u003e {\n    console.log(data)\n    // {\n    //   imgUrl: 'http://test-121000000.cosgz.myqcloud.com/abcdef.jpg',\n    //   size: 1024,\n    //   mimeType: 'image/jpeg',\n    //   name: 'abcdef.jpg'\n    // }\n  })\n}\n```\n\n#### 数据库\n\nSDK 还暴露出了内部使用的 MySQL 连接，由于 SDK 内部使用 [Knex.js](http://knexjs.org/) 连接数据库，SDK 暴露的 MySQL 实例就是 Knex.js 连接实例，具体使用方法可以查看 [Knex.js 文档](http://knexjs.org/)：\n\n```javascript\nconst { mysql } = qcloud\n\nmysql('db_name').select('*').where({ id: 1 })\n// =\u003e { id:1, name: 'leo', age: 20 }\n```\n\n#### 客服消息\n\n微信提供一个[客服消息](https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/callback_help.html)处理能力，你可以使用 SDK 提供的接口快速部署一个接受客服信息的 API：\n\n```javascript\nconst { message: { checkSignature } } = require('../qcloud')\n\n/**\n * 响应 GET 请求（响应微信配置时的签名检查请求）\n */\nrouter.get('/message', ctx =\u003e {\n  const { signature, timestamp, nonce, echostr } = ctx.query\n  if (checkSignature(signature, timestamp, nonce)) ctx.body = echostr\n  else ctx.body = 'ERR_WHEN_CHECK_SIGNATURE'\n})\n\n// post 请求用来接收消息\nrouter.post('/message', (ctx, next) {\n    // 检查签名，确认是微信发出的请求\n    const { signature, timestamp, nonce } = ctx.query\n    if (!checkSignature(signature, timestamp, nonce)) ctx.body = 'ERR_WHEN_CHECK_SIGNATURE'\n\n    /**\n     * 解析微信发送过来的请求体\n     * 可查看微信文档：https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/receive.html#接收消息和事件\n     */\n    const body = ctx.request.body\n\n    ctx.body = 'success'\n})\n```\n\n## 示例 Demo\n\n腾讯云还提供了完整的示例代码，点击[这里](https://github.com/tencentyun/wafer2-quickstart-nodejs)下载。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftencentyun%2Fwafer2-node-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftencentyun%2Fwafer2-node-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftencentyun%2Fwafer2-node-sdk/lists"}