132 lines
3.3 KiB
Go
132 lines
3.3 KiB
Go
package controllers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"strings"
|
||
|
||
"server/pkg/jwtutil"
|
||
"server/services/wechatmp"
|
||
"server/services/wechatmini"
|
||
|
||
beego "github.com/beego/beego/v2/server/web"
|
||
)
|
||
|
||
// =============================================================
|
||
// 平台端「微信小程序」配置:AppID / AppSecret / 启用开关 / 自动绑定
|
||
//
|
||
// 配置就绪后,uniapp 小程序端的登录页才会展示「微信手机号一键登录」入口
|
||
// (GET /auth/wechat/miniapp/config 返回 enabled)。
|
||
// AppSecret 掩码回显,提交空串或掩码表示不修改。
|
||
// =============================================================
|
||
|
||
// PlatformWechatMiniController 平台端微信小程序配置
|
||
type PlatformWechatMiniController struct {
|
||
beego.Controller
|
||
}
|
||
|
||
func (c *PlatformWechatMiniController) claims() (*jwtutil.Claims, error) {
|
||
auth := c.Ctx.Request.Header.Get("Authorization")
|
||
if auth == "" {
|
||
return nil, fmt.Errorf("未登录")
|
||
}
|
||
parts := strings.SplitN(auth, " ", 2)
|
||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||
return nil, fmt.Errorf("认证信息格式错误")
|
||
}
|
||
claims, err := jwtutil.ParseToken(parts[1])
|
||
if err != nil {
|
||
return nil, fmt.Errorf("无效的token")
|
||
}
|
||
if claims.UserType != "platform" {
|
||
return nil, fmt.Errorf("无权访问")
|
||
}
|
||
return claims, nil
|
||
}
|
||
|
||
func (c *PlatformWechatMiniController) jsonErr(httpStatus, bizCode int, msg string) {
|
||
c.Ctx.Output.SetStatus(httpStatus)
|
||
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
||
_ = c.ServeJSON()
|
||
}
|
||
|
||
func (c *PlatformWechatMiniController) ok(data interface{}, msg string) {
|
||
if msg == "" {
|
||
msg = "success"
|
||
}
|
||
resp := map[string]interface{}{"code": 200, "msg": msg}
|
||
if data != nil {
|
||
resp["data"] = data
|
||
}
|
||
c.Data["json"] = resp
|
||
_ = c.ServeJSON()
|
||
}
|
||
|
||
// GetConfig GET /platform/wechatMini/config
|
||
func (c *PlatformWechatMiniController) GetConfig() {
|
||
if _, err := c.claims(); err != nil {
|
||
c.jsonErr(401, 401, err.Error())
|
||
return
|
||
}
|
||
data := map[string]interface{}{
|
||
"app_id": "",
|
||
"app_secret": "",
|
||
"enabled": 0,
|
||
"auto_bind": 1,
|
||
"remark": "",
|
||
"configured": false,
|
||
}
|
||
if cfg, err := wechatmini.LoadConfig(); err == nil {
|
||
data["app_id"] = cfg.AppID
|
||
data["app_secret"] = wechatmp.MaskSecret(cfg.AppSecret)
|
||
data["remark"] = cfg.Remark
|
||
data["configured"] = cfg.AppID != "" && cfg.AppSecret != ""
|
||
if cfg.Enabled {
|
||
data["enabled"] = 1
|
||
}
|
||
if cfg.AutoBind {
|
||
data["auto_bind"] = 1
|
||
} else {
|
||
data["auto_bind"] = 0
|
||
}
|
||
}
|
||
c.ok(data, "获取成功")
|
||
}
|
||
|
||
// SaveConfig POST /platform/wechatMini/config
|
||
func (c *PlatformWechatMiniController) SaveConfig() {
|
||
if _, err := c.claims(); err != nil {
|
||
c.jsonErr(401, 401, err.Error())
|
||
return
|
||
}
|
||
var p struct {
|
||
AppID string `json:"app_id"`
|
||
AppSecret string `json:"app_secret"`
|
||
Enabled int `json:"enabled"`
|
||
AutoBind int `json:"auto_bind"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
raw, err := io.ReadAll(c.Ctx.Request.Body)
|
||
if err != nil {
|
||
c.jsonErr(400, 400, "参数错误")
|
||
return
|
||
}
|
||
if err := json.Unmarshal(raw, &p); err != nil {
|
||
c.jsonErr(400, 400, "参数错误")
|
||
return
|
||
}
|
||
|
||
if err := wechatmini.SaveConfig(wechatmini.SaveInput{
|
||
AppID: p.AppID,
|
||
AppSecret: p.AppSecret,
|
||
Enabled: p.Enabled == 1,
|
||
AutoBind: p.AutoBind == 1,
|
||
Remark: p.Remark,
|
||
}); err != nil {
|
||
c.jsonErr(400, 400, err.Error())
|
||
return
|
||
}
|
||
c.ok(nil, "保存成功")
|
||
}
|