You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.1 KiB

2 years ago
package common
import (
"encoding/json"
"fmt"
"net/http"
)
2 years ago
// 通过json tag 进行结构体赋值
func SwapToStruct(req, target interface{}) (err error) {
dataByte, err := json.Marshal(req)
if err != nil {
return
}
err = json.Unmarshal(dataByte, target)
return
}
2 years ago
type H struct {
Code string
Message string
TraceId string
Data interface{}
Rows interface{}
Total interface{}
SkyWalkingDynamicField string
}
func Resp(w http.ResponseWriter, code string, data interface{}, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
h := H{
Code: code,
Data: data,
Message: message,
}
ret, err := json.Marshal(h)
if err != nil {
fmt.Println(err)
}
w.Write(ret)
}
func RespList(w http.ResponseWriter, code string, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
h := H{
Code: code,
Data: data,
Message: message,
Rows: rows,
Total: total,
SkyWalkingDynamicField: skyWalkingDynamicField,
}
ret, err := json.Marshal(h)
if err != nil {
fmt.Println(err)
}
w.Write(ret)
}
2 years ago
/*
*
2 years ago
200 OKLoginSuccessVO
201 Created
401 Unauthorized
403 Forbidden
404 Not Found
2 years ago
*
*/
2 years ago
func RespOK(w http.ResponseWriter, data interface{}, message string) {
Resp(w, "SUCCESS", data, message)
}
func RespFail(w http.ResponseWriter, data interface{}, message string) {
Resp(w, "TOKEN_FAIL", data, message)
}
func RespListOK(w http.ResponseWriter, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
RespList(w, "SUCCESS", data, message, rows, total, skyWalkingDynamicField)
}
func RespListFail(w http.ResponseWriter, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
RespList(w, "TOKEN_FAIL", data, message, rows, total, skyWalkingDynamicField)
}