gorm wire

This commit is contained in:
2025-05-07 14:12:53 +08:00
parent 461531c308
commit 68606c76f9
111 changed files with 1726 additions and 5809 deletions

View File

@@ -3,22 +3,20 @@ package common
import (
"net/http"
"management/internal/erpserver/handler"
v1 "management/internal/erpserver/service/v1"
"management/internal/pkg/config"
"management/internal/pkg/tpl"
"management/internal/pkg/render"
)
type captchaHandler struct {
conf *config.Captcha
render tpl.Renderer
svc v1.CaptchaService
type CaptchaHandler struct {
*handler.Handler
captchaService v1.CaptchaService
}
func NewCaptchaHandler(conf *config.Captcha, render tpl.Renderer, svc v1.CaptchaService) *captchaHandler {
return &captchaHandler{
conf: conf,
render: render,
svc: svc,
func NewCaptchaHandler(handler *handler.Handler, captchaService v1.CaptchaService) *CaptchaHandler {
return &CaptchaHandler{
Handler: handler,
captchaService: captchaService,
}
}
@@ -29,20 +27,22 @@ type CaptchaResponse struct {
OpenCaptcha int `json:"open_captcha"`
}
func (h *captchaHandler) Captcha(w http.ResponseWriter, r *http.Request) {
keyLong := h.conf.KeyLong
oc := h.conf.OpenCaptcha
id, b64s, _, err := h.svc.Generate(h.conf.ImgHeight, h.conf.ImgWidth, keyLong, 0.7, 80)
func (h *CaptchaHandler) Captcha(w http.ResponseWriter, _ *http.Request) {
id, b64s, _, err := h.captchaService.Generate(
h.Config.Captcha.ImgHeight,
h.Config.Captcha.ImgWidth,
h.Config.Captcha.KeyLong,
0.7, 80)
if err != nil {
h.render.JSON(w, tpl.Response{Success: false, Message: "获取验证码失败"})
h.JSONErr(w, "获取验证码失败")
return
}
rsp := CaptchaResponse{
CaptchaID: id,
PicPath: b64s,
CaptchaLength: keyLong,
OpenCaptcha: oc,
CaptchaLength: h.Config.Captcha.KeyLong,
OpenCaptcha: h.Config.Captcha.OpenCaptcha,
}
h.render.JSON(w, tpl.Response{Success: true, Message: "ok", Data: rsp})
h.JSON(w, render.Response{Success: true, Message: "ok", Data: rsp})
}

View File

@@ -1,59 +1,70 @@
package common
import (
"io"
"mime/multipart"
"net/http"
"management/internal/erpserver/handler"
fileutil "management/internal/pkg/file"
"management/internal/pkg/tpl"
)
type uploadHandler struct {
render tpl.Renderer
type UploadHandler struct {
*handler.Handler
}
func NewUploadHandler(render tpl.Renderer) *uploadHandler {
return &uploadHandler{
render: render,
func NewUploadHandler(handler *handler.Handler) *UploadHandler {
return &UploadHandler{
Handler: handler,
}
}
const maxImageSize = 100 << 20 // 100 MB
func (h *uploadHandler) Img(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
func (h *UploadHandler) Img(w http.ResponseWriter, r *http.Request) {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
h.Log.Error(err.Error(), err)
}
}(r.Body)
_, fh, err := r.FormFile("files")
if err != nil {
h.render.JSONERR(w, err.Error())
h.JSONErr(w, err.Error())
return
}
path, err := fileutil.UploadFile(fh, fileutil.IMG)
if err != nil {
h.render.JSONERR(w, err.Error())
h.JSONErr(w, err.Error())
return
}
h.render.JSON(w, tpl.Response{Success: true, Message: "ok", Data: path})
h.JSONObj(w, "ok", path)
}
func (h *uploadHandler) File(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
func (h *UploadHandler) File(w http.ResponseWriter, r *http.Request) {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
h.Log.Error(err.Error(), err)
}
}(r.Body)
_, fh, err := r.FormFile("files")
if err != nil {
h.render.JSONERR(w, err.Error())
h.JSONErr(w, err.Error())
return
}
path, err := fileutil.UploadFile(fh, fileutil.ALL)
if err != nil {
h.render.JSONERR(w, err.Error())
h.JSONErr(w, err.Error())
return
}
h.render.JSON(w, tpl.Response{Success: true, Message: "ok", Data: path})
h.JSONObj(w, "ok", path)
}
type UploadFileRes struct {
@@ -61,8 +72,13 @@ type UploadFileRes struct {
Path string `json:"path"`
}
func (h *uploadHandler) MutilFiles(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
func (h *UploadHandler) MultiFiles(w http.ResponseWriter, r *http.Request) {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
h.Log.Error(err.Error(), err)
}
}(r.Body)
err := r.ParseMultipartForm(int64(maxImageSize))
if err != nil {
@@ -80,7 +96,7 @@ func (h *uploadHandler) MutilFiles(w http.ResponseWriter, r *http.Request) {
filePath, err := fileutil.UploadFile(item, fileutil.ALL)
if err != nil {
h.render.JSONERR(w, err.Error())
h.JSONErr(w, err.Error())
return
}
@@ -99,5 +115,5 @@ func (h *uploadHandler) MutilFiles(w http.ResponseWriter, r *http.Request) {
}
}
h.render.JSON(w, tpl.Response{Success: true, Message: "ok", Data: res})
h.JSONObj(w, "ok", res)
}