101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"math/rand"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/pkg/passwordutil"
|
|
"server/services"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
type PlatformUserController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
type addUserPayload struct {
|
|
Tid uint64 `json:"tid"`
|
|
Account string `json:"account"`
|
|
Password string `json:"password"`
|
|
Name string `json:"name"`
|
|
Phone string `json:"phone"`
|
|
Email string `json:"email"`
|
|
Status *int8 `json:"status"`
|
|
Remark *string `json:"remark"`
|
|
}
|
|
|
|
func (c *PlatformUserController) AddUser() {
|
|
var p addUserPayload
|
|
|
|
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
|
|
}
|
|
|
|
p.Account = strings.TrimSpace(p.Account)
|
|
p.Password = strings.TrimSpace(p.Password)
|
|
p.Name = strings.TrimSpace(p.Name)
|
|
p.Phone = strings.TrimSpace(p.Phone)
|
|
p.Email = strings.TrimSpace(p.Email)
|
|
|
|
if p.Tid == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "tid 不能为空"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
if p.Account == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "account 不能为空"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
if p.Password == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "password 不能为空"}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
hashed, err := passwordutil.Hash(p.Password)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 400, "msg": err.Error()}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
status := int8(1)
|
|
if p.Status != nil {
|
|
status = *p.Status
|
|
}
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
var uid uint64
|
|
for i := 0; i < 5; i++ {
|
|
uid = uint64(10000000 + rand.Intn(90000000))
|
|
account := &p.Account
|
|
name := &p.Name
|
|
phone := &p.Phone
|
|
email := &p.Email
|
|
hashedPwd := hashed
|
|
password := &hashedPwd
|
|
|
|
_, err := services.BindTenantUser(p.Tid, uid, account, name, phone, email, nil, nil, password, 0, status, p.Remark)
|
|
if err == nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": map[string]interface{}{"tid": p.Tid, "uid": uid},
|
|
}
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "添加失败,请重试"}
|
|
_ = c.ServeJSON()
|
|
}
|