139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/pkg/identity"
|
|
"server/pkg/jwtutil"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
type AppErpBaseController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpClaims() (*jwtutil.Claims, error) {
|
|
return identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndApp, identity.EndBackend)
|
|
}
|
|
|
|
func (c *AppErpBaseController) actor(cl *jwtutil.Claims) erpActor {
|
|
return erpNewActor(cl)
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpOk(data interface{}) {
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpListOK(list interface{}, total int64, page, pageSize int, extra map[string]interface{}) {
|
|
data := map[string]interface{}{"list": list, "total": total, "page": page, "pageSize": pageSize}
|
|
for k, v := range extra {
|
|
data[k] = v
|
|
}
|
|
c.erpOk(data)
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpFail(httpStatus, bizCode int, msg string) {
|
|
c.Ctx.Output.SetStatus(httpStatus)
|
|
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpDenied(err error) bool {
|
|
if err != nil {
|
|
code := identity.Status(err)
|
|
c.erpFail(code, code, err.Error())
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpPathID() uint64 {
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
return id
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpPage() (int, int) {
|
|
page, _ := c.GetInt("page", 1)
|
|
pageSize, _ := c.GetInt("pageSize", 20)
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 || pageSize > 100 {
|
|
pageSize = 20
|
|
}
|
|
return page, pageSize
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpBody(v interface{}, allowEmpty bool) bool {
|
|
raw, err := io.ReadAll(c.Ctx.Request.Body)
|
|
if err != nil {
|
|
c.erpFail(400, 400, "参数错误")
|
|
return false
|
|
}
|
|
if len(strings.TrimSpace(string(raw))) == 0 {
|
|
if allowEmpty {
|
|
return true
|
|
}
|
|
c.erpFail(400, 400, "请求体为空")
|
|
return false
|
|
}
|
|
if err := json.Unmarshal(raw, v); err != nil {
|
|
c.erpFail(400, 400, "参数错误: "+err.Error())
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpQuery(name string) string {
|
|
return strings.TrimSpace(c.GetString(name))
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpUint(name string) uint64 {
|
|
return erpUintParam(c.GetString(name))
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpFlag(name string) bool {
|
|
return c.erpQuery(name) == "1"
|
|
}
|
|
|
|
func (c *AppErpBaseController) erpDateRange(defMonths int) (time.Time, time.Time) {
|
|
from, to := erpMonthRange(defMonths)
|
|
if d := c.erpQuery("date_from"); d != "" {
|
|
if t, e := time.ParseInLocation("2006-01-02", d, time.Local); e == nil {
|
|
from = t
|
|
}
|
|
}
|
|
if d := c.erpQuery("date_to"); d != "" {
|
|
if t, e := time.ParseInLocation("2006-01-02", d, time.Local); e == nil {
|
|
to = t.AddDate(0, 0, 1)
|
|
}
|
|
}
|
|
return from, to
|
|
}
|
|
|
|
type AppErpController struct {
|
|
AppErpBaseController
|
|
}
|
|
|
|
func (c *AppErpController) Dashboard() {
|
|
cl, err := c.erpClaims()
|
|
if c.erpDenied(err) {
|
|
return
|
|
}
|
|
c.erpOk(erpDashboardCore(c.actor(cl), c.erpQuery("period")))
|
|
}
|
|
|
|
func (c *AppErpController) Meta() {
|
|
cl, err := c.erpClaims()
|
|
if c.erpDenied(err) {
|
|
return
|
|
}
|
|
c.erpOk(erpDocMetaDict(c.actor(cl)))
|
|
}
|