|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"gitea.drugeyes.vip/pharnexbase/utils/enum"
|
|
|
|
transPbV1 "gitea.drugeyes.vip/pharnexbase/utils/transport/v1"
|
|
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
|
|
"github.com/go-kratos/kratos/v2/transport/http"
|
|
|
|
"github.com/tjfoc/gmsm/sm4"
|
|
|
|
stdhttp "net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
baseContentType = "application"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrInternalServer = errors.InternalServer("InternalServerError", "服务错误")
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Code int `json:"code" form:"code"`
|
|
|
|
Data interface{} `json:"data" form:"data"`
|
|
|
|
Reason string `json:"reason" form:"reason"`
|
|
|
|
Message string `json:"message" form:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) ResponseEncoder() http.EncodeResponseFunc {
|
|
|
|
return func(w stdhttp.ResponseWriter, r *stdhttp.Request, v interface{}) error {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 重定向
|
|
|
|
if rd, ok := v.(http.Redirector); ok {
|
|
|
|
url, code := rd.Redirect()
|
|
|
|
stdhttp.Redirect(w, r, url, code)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 文件下载
|
|
|
|
if data, ok := v.(*transPbV1.FileResponse); ok {
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", data.Filename))
|
|
|
|
if data.Filetype != "" {
|
|
|
|
w.Header().Set("Content-Type", data.Filetype)
|
|
|
|
}
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", "*")
|
|
|
|
w.WriteHeader(stdhttp.StatusOK)
|
|
|
|
w.Write(data.Data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
codec, _ := http.CodecForRequest(r, "Accept")
|
|
|
|
data, err := codec.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", ContentType(codec.Name()))
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", "Traceid, En ,Content-Disposition, Authorization")
|
|
|
|
|
|
|
|
d := fmt.Sprintf(`{"code": 200,"data": %s,"reason": "","message": ""}`, string(data))
|
|
|
|
|
|
|
|
_, err = w.Write([]byte(d))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) ResponseEncoderWithEncrypt() http.EncodeResponseFunc {
|
|
|
|
return func(w stdhttp.ResponseWriter, r *stdhttp.Request, v interface{}) error {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 重定向
|
|
|
|
if rd, ok := v.(http.Redirector); ok {
|
|
|
|
url, code := rd.Redirect()
|
|
|
|
stdhttp.Redirect(w, r, url, code)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 文件下载
|
|
|
|
if data, ok := v.(*transPbV1.FileResponse); ok {
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", data.Filename))
|
|
|
|
if data.Filetype != "" {
|
|
|
|
w.Header().Set("Content-Type", data.Filetype)
|
|
|
|
}
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", "*")
|
|
|
|
w.WriteHeader(stdhttp.StatusOK)
|
|
|
|
w.Write(data.Data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
codec, _ := http.CodecForRequest(r, "Accept")
|
|
|
|
data, err := codec.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dataRes := string(data)
|
|
|
|
// 正式服加密
|
|
|
|
if t.env == enum.Env_Production {
|
|
|
|
ecbMsg, err := sm4.Sm4Ecb([]byte(t.sm4Key), data, true)
|
|
|
|
if err == nil {
|
|
|
|
dataRes = hex.EncodeToString(ecbMsg)
|
|
|
|
w.Header().Set("en", "4")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", ContentType(codec.Name()))
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", "Traceid, En ,Content-Disposition, Authorization")
|
|
|
|
|
|
|
|
d := fmt.Sprintf(`{"code": 200,"data": %s,"reason": "","message": ""}`, dataRes)
|
|
|
|
|
|
|
|
_, err = w.Write([]byte(d))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) ErrorEncoder() http.EncodeErrorFunc {
|
|
|
|
return func(w stdhttp.ResponseWriter, r *stdhttp.Request, err error) {
|
|
|
|
se := errors.FromError(err)
|
|
|
|
if t.env == enum.Env_Production {
|
|
|
|
if _, ok := err.(*errors.Error); !ok {
|
|
|
|
se = ErrInternalServer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
codec, _ := http.CodecForRequest(r, "Accept")
|
|
|
|
body, err := codec.Marshal(se)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(stdhttp.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", ContentType(codec.Name()))
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", "TraceID, En ,Content-Disposition, Authorization")
|
|
|
|
w.WriteHeader(int(se.Code))
|
|
|
|
_, _ = w.Write(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentType returns the content-type with base prefix.
|
|
|
|
func ContentType(subtype string) string {
|
|
|
|
return strings.Join([]string{baseContentType, subtype}, "/")
|
|
|
|
}
|