290 lines
7.3 KiB
Go
290 lines
7.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"math/rand"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// PlatformTenantUserController 平台端租户-用户绑定管理
|
|
type PlatformTenantUserController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
type tenantUserPayload struct {
|
|
Tid uint64 `json:"tid"`
|
|
Uid uint64 `json:"uid"`
|
|
Account *string `json:"account"`
|
|
Name *string `json:"name"`
|
|
Phone *string `json:"phone"`
|
|
Email *string `json:"email"`
|
|
Password *string `json:"password"`
|
|
IsDefault *int8 `json:"is_default"`
|
|
Status *int8 `json:"status"`
|
|
Remark *string `json:"remark"`
|
|
}
|
|
|
|
// GetTenantUserList 获取绑定列表(支持按 tid / uid 过滤)
|
|
// GET /platform/tenantUser/list?tid=1&uid=2
|
|
func (c *PlatformTenantUserController) GetTenantUserList() {
|
|
tid, _ := c.GetUint64("tid")
|
|
uid, _ := c.GetUint64("uid")
|
|
|
|
qs := models.Orm.QueryTable(new(models.TenantUser))
|
|
if tid > 0 {
|
|
qs = qs.Filter("tid", tid)
|
|
}
|
|
if uid > 0 {
|
|
qs = qs.Filter("uid", uid)
|
|
}
|
|
|
|
var rows []models.TenantUser
|
|
_, err := qs.OrderBy("-is_default", "-id").All(&rows)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "查询失败: " + err.Error()}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": map[string]interface{}{
|
|
"list": rows,
|
|
"total": len(rows),
|
|
},
|
|
}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// GetTenantUsersByTid 兼容路径参数方式获取租户用户列表
|
|
// GET /platform/getTenantUsers/:tid
|
|
func (c *PlatformTenantUserController) GetTenantUsersByTid() {
|
|
tidStr := c.Ctx.Input.Param(":tid")
|
|
tid, _ := strconv.ParseUint(tidStr, 10, 64)
|
|
if tid == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "tid 不能为空"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
var rows []models.TenantUser
|
|
_, err := models.Orm.QueryTable(new(models.TenantUser)).
|
|
Filter("tid", tid).
|
|
OrderBy("-is_default", "-id").
|
|
All(&rows)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "查询失败: " + err.Error()}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": map[string]interface{}{"list": rows, "total": len(rows)},
|
|
}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// GetTenantUserDetail 获取绑定详情
|
|
// GET /platform/tenantUser/detail/:id
|
|
func (c *PlatformTenantUserController) GetTenantUserDetail() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "无效ID"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
var row models.TenantUser
|
|
err = models.Orm.QueryTable(new(models.TenantUser)).Filter("id", id).One(&row)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 404, "msg": "记录不存在"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": row}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// CreateTenantUser 创建绑定
|
|
// POST /platform/tenantUser/create
|
|
func (c *PlatformTenantUserController) CreateTenantUser() {
|
|
p, ok := c.parsePayload()
|
|
if !ok {
|
|
return
|
|
}
|
|
if p.Tid == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "tid 不能为空"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
if p.Account == nil || strings.TrimSpace(*p.Account) == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "account 不能为空"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
if p.Password == nil || strings.TrimSpace(*p.Password) == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "password 不能为空"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
if p.Uid == 0 {
|
|
uid, err := generateTenantUID(p.Tid)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "生成租户用户ID失败"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
p.Uid = uid
|
|
}
|
|
|
|
isDefault := int8(0)
|
|
status := int8(1)
|
|
if p.IsDefault != nil {
|
|
isDefault = *p.IsDefault
|
|
}
|
|
if p.Status != nil {
|
|
status = *p.Status
|
|
}
|
|
|
|
id, err := models.BindTenantUser(p.Tid, p.Uid, p.Account, p.Name, p.Phone, p.Email, p.Password, isDefault, status, p.Remark)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "创建失败: " + err.Error()}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
if isDefault == 1 {
|
|
_ = models.SetDefaultTenant(p.Uid, p.Tid)
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": map[string]interface{}{"id": id}}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// EditTenantUser 编辑绑定
|
|
// POST /platform/tenantUser/edit/:id
|
|
func (c *PlatformTenantUserController) EditTenantUser() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "无效ID"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
p, ok := c.parsePayload()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
update := map[string]interface{}{}
|
|
if p.Tid > 0 {
|
|
update["tid"] = p.Tid
|
|
}
|
|
if p.Uid > 0 {
|
|
update["uid"] = p.Uid
|
|
}
|
|
if p.Account != nil {
|
|
update["account"] = p.Account
|
|
}
|
|
if p.Name != nil {
|
|
update["name"] = p.Name
|
|
}
|
|
if p.Phone != nil {
|
|
update["phone"] = p.Phone
|
|
}
|
|
if p.Email != nil {
|
|
update["email"] = p.Email
|
|
}
|
|
if p.Password != nil {
|
|
update["password"] = p.Password
|
|
}
|
|
if p.IsDefault != nil {
|
|
update["is_default"] = *p.IsDefault
|
|
}
|
|
if p.Status != nil {
|
|
update["status"] = *p.Status
|
|
}
|
|
if p.Remark != nil {
|
|
update["remark"] = p.Remark
|
|
}
|
|
|
|
if len(update) == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "无更新字段"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
_, err = models.Orm.QueryTable(new(models.TenantUser)).Filter("id", id).Update(update)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "更新失败: " + err.Error()}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
if p.IsDefault != nil && *p.IsDefault == 1 && p.Uid > 0 && p.Tid > 0 {
|
|
_ = models.SetDefaultTenant(p.Uid, p.Tid)
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success"}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// DeleteTenantUser 删除绑定
|
|
// DELETE /platform/tenantUser/delete/:id
|
|
func (c *PlatformTenantUserController) DeleteTenantUser() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "无效ID"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
if err := models.UnbindTenantUser(id); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "删除失败: " + err.Error()}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success"}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *PlatformTenantUserController) parsePayload() (tenantUserPayload, bool) {
|
|
var p tenantUserPayload
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
|
|
_ = c.ServeJSON()
|
|
return tenantUserPayload{}, false
|
|
}
|
|
return p, true
|
|
}
|
|
|
|
func generateTenantUID(tid uint64) (uint64, error) {
|
|
rand.Seed(time.Now().UnixNano())
|
|
for i := 0; i < 8; i++ {
|
|
uid := uint64(10000000 + rand.Intn(90000000))
|
|
cnt, err := models.Orm.QueryTable(new(models.TenantUser)).
|
|
Filter("tid", tid).
|
|
Filter("uid", uid).
|
|
Count()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if cnt == 0 {
|
|
return uid, nil
|
|
}
|
|
}
|
|
return 0, errors.New("uid collision")
|
|
}
|
|
|