{"id":19136944,"url":"https://github.com/singcl/xcx_scan_login","last_synced_at":"2026-06-12T23:31:50.105Z","repository":{"id":94727303,"uuid":"580701224","full_name":"singcl/xcx_scan_login","owner":"singcl","description":"微信小程序扫码授权DEMO","archived":false,"fork":false,"pushed_at":"2022-12-21T08:23:24.000Z","size":178,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-03T13:17:50.272Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/singcl.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-21T08:20:13.000Z","updated_at":"2023-01-29T02:18:24.000Z","dependencies_parsed_at":"2023-07-29T04:45:17.318Z","dependency_job_id":null,"html_url":"https://github.com/singcl/xcx_scan_login","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fxcx_scan_login","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fxcx_scan_login/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fxcx_scan_login/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/singcl%2Fxcx_scan_login/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/singcl","download_url":"https://codeload.github.com/singcl/xcx_scan_login/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240215478,"owners_count":19766423,"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-09T06:35:56.836Z","updated_at":"2025-11-12T23:03:24.513Z","avatar_url":"https://github.com/singcl.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 微信小程序扫码授权\n\n## 背景\n\n想要使用微信扫码登录自己的网址，通过授权快速获取用户的昵称，头像功能\n由于没有企业认证账号，故只能通过微信小程序实现,\n\n## 体验地址\n[https://api.nnnnzs.cn/screen-demo.html?env=release](https://api.nnnnzs.cn/screen-demo.html?env=release)\n\n## 源码\n[https://github.com/NNNNzs/wechat-screen-tools](https://github.com/NNNNzs/wechat-screen-tools)\n\n## 原理\n\n利用微信小程序的动态创建场景码接口 ([https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html)),创建短token的信息存储在服务端，而不需要大量信息存储在二维码里，实现授权\n\n## 流程\n\n1. 客户端在请求图片,服务生成 token，注册到认证服务\n2. 前端根据 token，获取带场景值的二维码图片，并轮训该 token 状态\n3. 使用微信扫码，根据 token 获取应用信息并展示，在小程序授权登录，获取 openid,nickname,avatorImg 等信息\n4. 确认允许登录后，发送 confirm 请求，发送 userInfo 值\n5. 网络获取到该 token 对应的用户信息\n\n## 流程图\n\n![流程图](https://static.nnnnzs.cn/upload/142449493212df2e8d42bc3807e7e4d8.png)\n\n## 核心接口\n\n| 接口           | 传参                    | 返回值             | 说明                                              |\n| -------------- | ----------------------- | ------------------ | ------------------------------------------------- |\n| getToken       | -                       | token              | 获取 token                                        |\n| getImgByToken  | token                   | 图片的 arrayBuffer | 获取小程序码                                      |\n| getInfoByToken | token                   | userInfo           | 获取用户信息                                      |\n| status         | token                   | -1\\| 0\\| 1         | -1 表示为扫码，0 表示已扫码未授权，1 表示已经授权 |\n| confirm        | nickName、openid,头像等 | -                  | 小程序的允许授权时确认的接口                      |\n\n## 踩坑点\n\n### scene 长度为最大 32 字符，利用 nodejs 的 uuid.v4 生成的值超过 32 位\n\u003e解决方案\n\n```js\nuuid.v4().replace(/-/g, \"\").toUpperCase();\n```\n\n### 生成二维码时，如果没有指定 page 字段可能会在页面里获取不到场景值(scene)\n\n\u003e解决方案\n\u003e指定`page: \"pages/index/index\"`\n\n### getwxacodeunlimit 接口成功直接返回 buffer 错误返回的是 json，在 axios 中需要根据 response 的 contentType 来判断成功还是失败\n\n\u003e 解决方案\n\n```js\nrouter.get(\"/getImgByToken\", async (req, res) =\u003e {\n  const { access_token, expires_in } = await wechat.getAccessToken();\n  const ua = req.headers[\"user-agent\"];\n  const token = req.query.token;\n  const env = req.query.env;\n  // env区分  release 发行版本   trial 体验版    develop 开发版\n  // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html\n  axios({\n    url: \"https://api.weixin.qq.com/wxa/getwxacodeunlimit\",\n    method: \"post\",\n    params: {\n      access_token: access_token,\n    },\n    responseType: \"arraybuffer\",\n    data: {\n      scene: token,\n      env_version: env || \"trial\",\n      check_path: false,\n      page: \"pages/index/index\",\n    },\n  }).then((response) =\u003e {\n    const isSuccess = response.headers[\"content-type\"].includes(\"image\");\n    if (!isSuccess) {\n      console.log(response.data.toString(\"utf-8\"));\n    } else {\n      res.type(\"png\").send(response.data);\n    }\n  });\n});\n```\n\n### 无论是`wx.getUserProfile`还是 `wx.getUserInfo` 无法静默获取 userInfo，都需要授权弹窗登录，不过可以缓存信息\n\u003e解决方案\n\u003e扫码进入之后onLoad就弹窗，并且存储场景值，允许拒绝授权之后再次\n\n### 开发的时候场景值每次提交代码生成预览版本比较麻烦\n\u003e解决方案\n\u003e在微信小程序开发工具里面添加条件编译，通过二维码编辑![在微信小程序开发工具里面添加条件编译，通过二维码编辑](https://static.nnnnzs.cn/upload/aebd25e424b7238b1938d829e980d0e5.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingcl%2Fxcx_scan_login","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsingcl%2Fxcx_scan_login","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsingcl%2Fxcx_scan_login/lists"}