first commit
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// PlatformSiteSettingsController 租户站点设置(站点基本信息)
|
||||
// 对应前端 normalSettings.vue 的:
|
||||
// - GET /backend/normalInfos
|
||||
// - POST /backend/saveNormalInfos
|
||||
// - GET /platform/normalInfos
|
||||
// - POST /platform/saveNormalInfos
|
||||
type PlatformSiteSettingsController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *PlatformSiteSettingsController) 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 *PlatformSiteSettingsController) claimsByPath() (*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")
|
||||
}
|
||||
|
||||
path := strings.ToLower(c.Ctx.Request.URL.Path)
|
||||
if strings.HasPrefix(path, "/platform/") {
|
||||
if claims.UserType != "platform" {
|
||||
return nil, fmt.Errorf("无权访问")
|
||||
}
|
||||
} else if strings.HasPrefix(path, "/backend/") {
|
||||
if claims.UserType != "backend" {
|
||||
return nil, fmt.Errorf("无权访问")
|
||||
}
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func parseUint64Flexible(v interface{}) uint64 {
|
||||
if v == nil {
|
||||
return 0
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case float64:
|
||||
if x <= 0 {
|
||||
return 0
|
||||
}
|
||||
return uint64(x)
|
||||
case string:
|
||||
s := strings.TrimSpace(x)
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
n, err := strconv.ParseUint(s, 10, 64)
|
||||
if err != nil || n == 0 {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
type normalInfosOutput struct {
|
||||
Sitename string `json:"sitename"`
|
||||
Companyintroduction string `json:"companyintroduction"`
|
||||
Description string `json:"description"`
|
||||
Copyright string `json:"copyright"`
|
||||
Companyname string `json:"companyname"`
|
||||
Icp string `json:"icp"`
|
||||
Logo string `json:"logo"`
|
||||
Logow string `json:"logow"`
|
||||
Ico string `json:"ico"`
|
||||
}
|
||||
|
||||
// GetNormalInfos GET /backend/normalInfos 或 /platform/normalInfos
|
||||
func (c *PlatformSiteSettingsController) GetNormalInfos() {
|
||||
claims, err := c.claimsByPath()
|
||||
if err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 优先使用 token 中的租户 id;若为 0,则允许前端通过查询参数传入(兼容历史/平台端)。
|
||||
tid := uint64(claims.TenantId)
|
||||
if tid == 0 {
|
||||
tidStr := strings.TrimSpace(c.GetString("tid"))
|
||||
if tidStr != "" {
|
||||
if n, err := strconv.ParseUint(tidStr, 10, 64); err == nil {
|
||||
tid = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out := normalInfosOutput{
|
||||
Sitename: "",
|
||||
Companyintroduction: "",
|
||||
Description: "",
|
||||
Copyright: "",
|
||||
Companyname: "",
|
||||
Icp: "",
|
||||
Logo: "",
|
||||
Logow: "",
|
||||
Ico: "",
|
||||
}
|
||||
|
||||
// tid 缺失时不报错,直接返回空对象给前端渲染(避免 UI 直接崩)。
|
||||
if tid == 0 {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": out}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var rows []models.TenantSiteSetting
|
||||
_, err = models.Orm.QueryTable(new(models.TenantSiteSetting)).
|
||||
Filter("tid", tid).
|
||||
Filter("delete_time__isnull", true).
|
||||
Limit(1).
|
||||
All(&rows)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "获取失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if len(rows) > 0 {
|
||||
r := rows[0]
|
||||
out.Sitename = r.Sitename
|
||||
out.Companyintroduction = r.Companyintroduction
|
||||
out.Logo = r.Logo
|
||||
out.Logow = r.Logow
|
||||
out.Ico = r.Ico
|
||||
out.Description = r.Description
|
||||
out.Copyright = r.Copyright
|
||||
out.Companyname = r.Companyname
|
||||
out.Icp = r.Icp
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": out}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
type normalInfosPayload struct {
|
||||
// 前端会传 tid(但我们仍优先使用 token 的 tenant_id)
|
||||
Tid interface{} `json:"tid"`
|
||||
|
||||
Sitename string `json:"sitename"`
|
||||
Companyintroduction string `json:"companyintroduction"`
|
||||
Logo string `json:"logo"`
|
||||
Logow string `json:"logow"`
|
||||
Ico string `json:"ico"`
|
||||
Description string `json:"description"`
|
||||
Copyright string `json:"copyright"`
|
||||
Companyname string `json:"companyname"`
|
||||
Icp string `json:"icp"`
|
||||
}
|
||||
|
||||
// SaveNormalInfos POST /backend/saveNormalInfos 或 /platform/saveNormalInfos
|
||||
func (c *PlatformSiteSettingsController) SaveNormalInfos() {
|
||||
claims, err := c.claimsByPath()
|
||||
if err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
raw, err := io.ReadAll(c.Ctx.Request.Body)
|
||||
if err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
var p normalInfosPayload
|
||||
if uerr := json.Unmarshal(raw, &p); uerr != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
tid := uint64(claims.TenantId)
|
||||
if tid == 0 {
|
||||
tid = parseUint64Flexible(p.Tid)
|
||||
}
|
||||
if tid == 0 {
|
||||
c.jsonErr(400, 400, "tid不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
sitename := strings.TrimSpace(p.Sitename)
|
||||
if sitename == "" {
|
||||
c.jsonErr(400, 400, "站点名称不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
up := map[string]interface{}{
|
||||
"tid": tid,
|
||||
"sitename": sitename,
|
||||
"companyintroduction": strings.TrimSpace(p.Companyintroduction),
|
||||
"logo": strings.TrimSpace(p.Logo),
|
||||
"logow": strings.TrimSpace(p.Logow),
|
||||
"ico": strings.TrimSpace(p.Ico),
|
||||
"description": strings.TrimSpace(p.Description),
|
||||
"copyright": strings.TrimSpace(p.Copyright),
|
||||
"companyname": strings.TrimSpace(p.Companyname),
|
||||
"icp": strings.TrimSpace(p.Icp),
|
||||
"update_time": now,
|
||||
}
|
||||
|
||||
cnt, err := models.Orm.QueryTable(new(models.TenantSiteSetting)).
|
||||
Filter("tid", tid).
|
||||
Filter("delete_time__isnull", true).
|
||||
Count()
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "保存失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if cnt == 0 {
|
||||
row := &models.TenantSiteSetting{
|
||||
Tid: tid,
|
||||
Sitename: sitename,
|
||||
Companyintroduction: strings.TrimSpace(p.Companyintroduction),
|
||||
Logo: strings.TrimSpace(p.Logo),
|
||||
Logow: strings.TrimSpace(p.Logow),
|
||||
Ico: strings.TrimSpace(p.Ico),
|
||||
Description: strings.TrimSpace(p.Description),
|
||||
Copyright: strings.TrimSpace(p.Copyright),
|
||||
Companyname: strings.TrimSpace(p.Companyname),
|
||||
Icp: strings.TrimSpace(p.Icp),
|
||||
CreateTime: now,
|
||||
UpdateTime: &now,
|
||||
}
|
||||
_, err = models.Orm.Insert(row)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "保存失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = models.Orm.QueryTable(new(models.TenantSiteSetting)).
|
||||
Filter("tid", tid).
|
||||
Filter("delete_time__isnull", true).
|
||||
Update(up)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "保存失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "保存成功"}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
Reference in New Issue
Block a user