初始化

This commit is contained in:
SunYF
2026-08-28 14:07:22 +08:00
parent a185247ab1
commit b34325a16f
971 changed files with 291 additions and 220360 deletions
-43
View File
@@ -1,43 +0,0 @@
/*
* Copyright (c) 2025 Beijing Hardman Automation Equipment Co., LTD. All rights reserved.
*
* Project: adapter
* File: ctx_data.go
* Last: 2025-04-15 16:40:17
* Author: wangcheng@bj-hardman.com
*/
package xhttp
import (
"context"
"encoding/json"
"github.com/zeromicro/go-zero/core/logx"
)
// CtxKeyJwtUserId get uid from ctx
var CtxKeyJwtUserId = "jwtUserId"
// CtxKeyJwtOpenId get openid from ctx
var CtxKeyJwtOpenId = "jwtOpenId"
func GetUidFromCtx(ctx context.Context) int64 {
var uid int64
if jsonUid, ok := ctx.Value(CtxKeyJwtUserId).(json.Number); ok {
if int64Uid, err := jsonUid.Int64(); err == nil {
uid = int64Uid
} else {
logx.WithContext(ctx).Errorf("GetUidFromCtx err : %+v", err)
}
}
return uid
}
func GetOidFromCtx(ctx context.Context) string {
if jsonUid, ok := ctx.Value(CtxKeyJwtOpenId).(string); ok {
return jsonUid
}
logx.WithContext(ctx).Error("GetOidFromCtx failed")
return ""
}
-48
View File
@@ -1,48 +0,0 @@
package xhttp
import (
"net/http"
"os"
"path"
"strings"
)
const basename = "/"
type NotFoundHandler struct {
fs http.FileSystem
fileServer http.Handler
}
func NewNotFoundHandler(fs http.FileSystem) NotFoundHandler {
return NotFoundHandler{
fs: fs,
fileServer: http.FileServer(fs),
}
}
func (n NotFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/") {
http.Error(w, "not found", http.StatusNotFound)
return
}
filePath := strings.TrimPrefix(path.Clean(r.URL.Path), basename)
if len(filePath) == 0 {
filePath = basename
}
file, err := n.fs.Open(filePath)
switch {
case err == nil:
n.fileServer.ServeHTTP(w, r)
_ = file.Close()
return
case os.IsNotExist(err):
r.URL.Path = "/" // all virtual routes in react app means visit index.html
n.fileServer.ServeHTTP(w, r)
return
default:
http.Error(w, "not found", http.StatusNotFound)
return
}
}
-77
View File
@@ -1,77 +0,0 @@
package xhttp
import (
"context"
"errors"
"bj_power_mes/common/errorx"
"github.com/go-playground/validator/v10"
"github.com/iancoleman/strcase"
"github.com/zeromicro/go-zero/core/logx"
)
type Body struct {
Code uint32 `json:"code"`
Msg string `json:"message"`
Fields []FieldError `json:"fields,omitempty"`
Detail string `json:"detail,omitempty"`
Data interface{} `json:"data,omitempty"`
}
type FieldError struct {
Field string `json:"field"`
Error string `json:"error"`
}
func OkJsonCtx(_ context.Context, data any) any {
return Body{Msg: "OK", Data: data}
}
func ErrorCtx(ctx context.Context, err error) (int, any) {
logx.WithContext(ctx).Errorf("[API-ERR] %v", err)
var errs validator.ValidationErrors
if errors.As(err, &errs) { // 参数验证错误
var fields []FieldError
for _, er := range errs {
fields = append(fields, FieldError{
Field: strcase.ToLowerCamel(er.StructField()),
Error: er.Translate(translator),
})
}
body := Body{Code: errorx.RequestParamInvalidWithField, Msg: errorx.MapErrMsg(errorx.RequestParamInvalid), Fields: fields}
return 200, body
}
var fes errorx.ServiceFieldErrors
if errors.As(err, &fes) { // 业务验证错误
es := fes.Errors()
if len(es) > 0 {
fieldErrors := make([]FieldError, len(es))
for i, err := range es {
var e errorx.ServiceFieldError
errors.As(err, &e)
fieldErrors[i].Field = e.Field()
fieldErrors[i].Error = e.Error()
}
body := Body{Code: errorx.RequestParamInvalidWithField, Msg: errorx.MapErrMsg(errorx.RequestParamInvalid), Fields: fieldErrors}
return 200, body
}
return 200, Body{Code: errorx.RequestParamInvalid, Msg: errorx.MapErrMsg(errorx.RequestParamInvalid)}
}
var fe errorx.ServiceFieldError
if errors.As(err, &fe) { // 业务验证错误
fieldErrors := make([]FieldError, 1)
fieldErrors[0].Field = fe.Field()
fieldErrors[0].Error = fe.Error()
body := Body{Code: fe.Code(), Msg: errorx.MapErrMsg(fe.Code()), Fields: fieldErrors}
return 200, body
}
var ce errorx.ServiceError
if errors.As(err, &ce) {
body := Body{Code: ce.Code(), Msg: errorx.MapErrMsg(ce.Code(), ce.Error())}
return 200, body
}
body := Body{Code: errorx.CommonErrCode, Msg: errorx.MapErrMsg(errorx.CommonErrCode), Detail: err.Error()}
return 200, body
}
-51
View File
@@ -1,51 +0,0 @@
package xhttp
import (
"net/http"
"reflect"
"regexp"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
)
var mobileMatcher = regexp.MustCompile(`^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$`)
var translator ut.Translator
type Validator struct {
v *validator.Validate
}
func NewValidator() *Validator {
validate := validator.New()
uni := ut.New(en.New(), zh.New(), en.New())
translator, _ = uni.GetTranslator("zh")
_ = zhTranslations.RegisterDefaultTranslations(validate, translator)
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
return fld.Tag.Get("comment")
})
_ = validate.RegisterValidation("mobile", validateMobile)
_ = validate.RegisterTranslation("mobile", translator, func(ut ut.Translator) error {
return ut.Add("mobile", "{0}必须是一个有效的手机号", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("mobile", fe.Field())
return t
})
return &Validator{
v: validate,
}
}
func (v *Validator) Validate(r *http.Request, data any) error {
return v.v.Struct(data)
}
func validateMobile(fl validator.FieldLevel) bool {
return mobileMatcher.MatchString(fl.Field().String())
}