Skip to content

coderyqy/strapi-plugin-wechat

Repository files navigation

Strapi Plugin WeChat

一个用于微信小程序用户认证和管理的 Strapi 插件。

功能特性

  • 微信小程序登录认证
  • 用户信息管理(头像、昵称)
  • 支持扩展字段存储
  • 自动创建和更新微信用户

安装

系统要求

  • Strapi v5.0.0 或更高版本
  • Node.js 18.x 或 20.x
  • NPM 6.0.0 或更高版本
# 使用 npm
npm install strapi-plugin-wechat

# 使用 yarn
yarn add strapi-plugin-wechat

配置

  1. 在项目根目录的 .env 文件中添加以下配置:
WECHAT_MINI_APPID=你的小程序AppID
WECHAT_MINI_SECRET=你的小程序AppSecret
  1. config/plugins.ts 中启用插件:
export default {
  // ...
  'strapi-plugin-wechat': {
    enabled: true,
  },
  // ...
};

API 接口

小程序登录

POST /api/strapi-plugin-wechat/mini/login

请求体:

{
  "code": "微信登录code"
}

响应:

{
  "data": {
    "id": 1,
    "openid": "用户openid",
    "nickname": "用户昵称",
    "avatar": {
      "url": "头像URL"
    },
    "type": "mini-program",
    "extend": {}
  }
}

获取用户信息

GET /api/strapi-plugin-wechat/mini/users/:openid

响应:

{
  "data": {
    "id": 1,
    "openid": "用户openid",
    "nickname": "用户昵称",
    "avatar": {
      "url": "头像URL"
    },
    "type": "mini-program",
    "extend": {}
  }
}

更新用户头像

POST /api/strapi-plugin-wechat/mini/users/:openid/avatar

请求体:

  • Content-Type: multipart/form-data
  • 文件字段名:files

响应:

{
  "data": {
    "id": 1,
    "openid": "用户openid",
    "avatar": {
      "url": "新头像URL"
    }
  }
}

更新用户昵称

PUT /api/strapi-plugin-wechat/mini/users/:openid/nickname

请求体:

{
  "nickname": "新昵称"
}

响应:

{
  "data": {
    "id": 1,
    "openid": "用户openid",
    "nickname": "新昵称"
  }
}

更新扩展信息

POST /api/strapi-plugin-wechat/mini/users/:openid/extend

请求体:

{
  "extend": {
    "key1": "value1",
    "key2": "value2"
  }
}

响应:

{
  "data": {
    "id": 1,
    "openid": "用户openid",
    "extend": {
      "key1": "value1",
      "key2": "value2"
    }
  }
}

数据结构

WeChat User

字段 类型 描述
openid string 微信用户唯一标识
nickname string 用户昵称
avatar media 用户头像
type enum 用户类型 (mini-program/h5)
extend json 扩展信息

注意事项

  1. 头像上传大小限制为 2MB
  2. extend 字段支持存储任意 JSON 格式数据

License

MIT

About

一个用于微信小程序用户认证和管理的 Strapi 插件。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published