wechat

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2025 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Answer 微信登录插件

License Go Version Answer Version

为 Apache Answer 开发的微信 OAuth 登录插件,支持微信扫码登录。


✨ 特性

  • 🔐 OAuth 2.0 标准登录
  • 🌍 多语言支持(中文/英文)
  • 🎨 微信官方品牌 Logo
  • ⚙️ 简单配置(AppID + AppSecret)
  • 🔒 CSRF 防护和安全验证
  • 📱 支持扫码和 PC 快速登录

🚀 快速安装

一条命令安装(推荐)
answer build \
    --with github.com/starvpn/answer-user-wxcom \
    --output ./new_answer

# 替换原有的 answer
sudo mv ./new_answer /usr/bin/answer

# 启动 Answer
answer run -C ./answer-data
Docker 部署
FROM answerdev/answer:latest

RUN answer build \
    --with github.com/starvpn/answer-user-wxcom \
    --output /usr/bin/answer
docker build -t answer-wechat .
docker run -d -p 80:80 -v ./data:/data answer-wechat
安装多个插件
answer build \
    --with github.com/starvpn/answer-user-wxcom \
    --with github.com/apache/answer-plugins/storage-s3 \
    --with github.com/apache/answer-plugins/search-elasticsearch \
    --output ./new_answer

⚙️ 配置

1. 微信开放平台准备
  1. 注册微信开放平台开发者账号
  2. 创建网站应用并通过审核
  3. 获取 AppID 和 AppSecret
  4. 配置授权回调域(如:yourdomain.com,不含协议和路径)
2. Answer 后台配置
  1. 登录 Answer 管理后台
  2. 导航到:设置 → 登录 → 微信
  3. 填写配置并保存:
配置项 必填 说明
AppID ✅ 微信开放平台应用 ID
AppSecret ✅ 微信开放平台应用密钥
Display Name ❌ 自定义按钮显示名称
  1. 启用插件
3. 测试登录

访问 Answer 登录页面,应该能看到"使用微信登录"按钮。点击后扫码即可登录。


📖 文档


🔧 开发

本地开发
# 克隆项目
git clone https://github.com/starvpn/answer-user-wxcom.git

# 链接到 Answer 项目
cd /path/to/answer
go mod edit -replace=github.com/starvpn/answer-user-wxcom=/path/to/answer-user-wxcom

# 在 cmd/answer/main.go 中导入
import _ "github.com/starvpn/answer-user-wxcom"

# 构建
go mod tidy
make build
项目结构
answer-user-wxcom/
├── wechat.go              # 核心实现
├── i18n.go                # 国际化
├── go.mod                 # Go 模块
├── i18n/                  # 翻译文件
├── docs/                  # 文档目录
└── config.example.yaml    # 配置示例

🐛 故障排查

常见问题

Q: "该链接无法访问"
A: 检查 AppID 是否正确,授权回调域是否配置正确(不要包含 https:// 和路径)

Q: 获取用户信息失败
A: 检查 AppSecret 是否正确,应用是否已通过审核

Q: 回调地址错误
A: 确保 Answer 的域名配置正确,且防火墙允许微信服务器访问

详细故障排查请查看:快速开始指南


🔒 安全说明

  • ⚠️ AppSecret 必须保密,不要提交到版本控制系统
  • ✅ 生产环境必须使用 HTTPS
  • ✅ 已实现 CSRF 防护(State 参数验证)
  • ✅ 使用安全的 Cookie 处理

🤝 贡献

欢迎贡献代码、报告问题、提出建议!

查看 贡献指南 了解详情。


📄 许可证

Apache License 2.0


🙏 致谢


📞 支持


Made with ❤️ for Answer Community

⭐ Star · 📖 Docs · 🐛 Issues

Documentation

Index

Constants

View Source
const (
	InfoName        = "plugin.wechat_connector.name"
	InfoDescription = "plugin.wechat_connector.description"

	ConnectorName = "plugin.wechat_connector.name"

	ConfigAppIDTitle             = "plugin.wechat_connector.config.app_id.title"
	ConfigAppIDDescription       = "plugin.wechat_connector.config.app_id.description"
	ConfigAppSecretTitle         = "plugin.wechat_connector.config.app_secret.title"
	ConfigAppSecretDescription   = "plugin.wechat_connector.config.app_secret.description"
	ConfigDisplayNameTitle       = "plugin.wechat_connector.config.display_name.title"
	ConfigDisplayNameDescription = "plugin.wechat_connector.config.display_name.description"
)

Translation keys for i18n

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	AppID       string `json:"app_id"`
	AppSecret   string `json:"app_secret"`
	DisplayName string `json:"display_name"`
}

Config WeChat connector configuration

type Connector

type Connector struct {
	Config *Config
}

Connector WeChat OAuth2 connector plugin

func (*Connector) ConfigFields

func (c *Connector) ConfigFields() []plugin.ConfigField

ConfigFields implements plugin.Config

func (*Connector) ConfigReceiver

func (c *Connector) ConfigReceiver(config []byte) error

ConfigReceiver implements plugin.Config

func (*Connector) ConnectorLogoSVG

func (c *Connector) ConnectorLogoSVG() string

ConnectorLogoSVG implements plugin.Connector

func (*Connector) ConnectorName

func (c *Connector) ConnectorName() plugin.Translator

ConnectorName implements plugin.Connector

func (*Connector) ConnectorReceiver

func (c *Connector) ConnectorReceiver(ctx *plugin.GinContext, receiverURL string) (userInfo plugin.ExternalLoginUserInfo, err error)

ConnectorReceiver implements plugin.Connector It handles the callback from WeChat and retrieves user information

func (*Connector) ConnectorSender

func (c *Connector) ConnectorSender(ctx *plugin.GinContext, receiverURL string) (redirectURL string)

ConnectorSender implements plugin.Connector It redirects user to WeChat OAuth authorization page

func (*Connector) ConnectorSlugName

func (c *Connector) ConnectorSlugName() string

ConnectorSlugName implements plugin.Connector

func (*Connector) Info

func (c *Connector) Info() plugin.Info

Info implements plugin.Base

func (*Connector) TranslateConfig added in v1.0.2

func (c *Connector) TranslateConfig(language string) string

TranslateConfig implements plugin.Translator

func (*Connector) TranslateConfigList added in v1.0.3

func (c *Connector) TranslateConfigList() []string

TranslateConfigList implements plugin.Translator

type WeChatAccessToken

type WeChatAccessToken struct {
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	OpenID       string `json:"openid"`
	Scope        string `json:"scope"`
	UnionID      string `json:"unionid"`
	ErrCode      int    `json:"errcode"`
	ErrMsg       string `json:"errmsg"`
}

WeChatAccessToken WeChat access token response

type WeChatUserInfo

type WeChatUserInfo struct {
	OpenID     string   `json:"openid"`
	UnionID    string   `json:"unionid"`
	Nickname   string   `json:"nickname"`
	Sex        int      `json:"sex"`
	Province   string   `json:"province"`
	City       string   `json:"city"`
	Country    string   `json:"country"`
	HeadImgURL string   `json:"headimgurl"`
	Privilege  []string `json:"privilege"`
	ErrCode    int      `json:"errcode"`
	ErrMsg     string   `json:"errmsg"`
}

WeChatUserInfo WeChat user information response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL