support data export

This commit is contained in:
MaysWind
2021-01-02 02:04:38 +08:00
parent 102c945aa0
commit 7d3e05c548
24 changed files with 515 additions and 42 deletions
+34 -4
View File
@@ -11,16 +11,25 @@ import (
"github.com/mayswind/lab/pkg/errs"
)
// PrintSuccessResult writes success response to current http context
func PrintSuccessResult(c *core.Context, result interface{}) {
// PrintJsonSuccessResult writes success response in json format to current http context
func PrintJsonSuccessResult(c *core.Context, result interface{}) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"result": result,
})
}
// PrintErrorResult writes error response to current http context
func PrintErrorResult(c *core.Context, err *errs.Error) {
// PrintDataSuccessResult writes success response in custom content type to current http context
func PrintDataSuccessResult(c *core.Context, contentType string, fileName string, result []byte) {
if fileName != "" {
c.Header("Content-Disposition", "attachment;filename=" + fileName)
}
c.Data(http.StatusOK, contentType, result)
}
// PrintJsonErrorResult writes error response in json format to current http context
func PrintJsonErrorResult(c *core.Context, err *errs.Error) {
c.SetResponseError(err)
errorMessage := err.Error()
@@ -44,6 +53,27 @@ func PrintErrorResult(c *core.Context, err *errs.Error) {
})
}
// PrintDataErrorResult writes error response in custom content type to current http context
func PrintDataErrorResult(c *core.Context, contentType string, err *errs.Error) {
c.SetResponseError(err)
errorMessage := err.Error()
if err.Code() == errs.ErrIncompleteOrIncorrectSubmission.Code() && len(err.BaseError) > 0 {
validationErrors, ok := err.BaseError[0].(validator.ValidationErrors)
if ok {
for _, err := range validationErrors {
errorMessage = getValidationErrorText(err)
break
}
}
}
c.Data(err.HttpStatusCode, contentType, []byte(errorMessage))
c.Abort()
}
func getValidationErrorText(err validator.FieldError) string {
fieldName := GetFirstLowerCharString(err.Field())
+12
View File
@@ -3,7 +3,9 @@ package utils
import "time"
const (
unixTimeFormat = "1136239445"
longDateTimeFormat = "2006-01-02 15:04:05"
longDateTimeWithoutSecondFormat = "2006-01-02 15:04"
)
// FormatToLongDateTime returns a textual representation of the time value formatted by long date time format
@@ -11,6 +13,16 @@ func FormatToLongDateTime(t time.Time) string {
return t.Format(longDateTimeFormat)
}
// FormatToLongDateTimeWithoutSecond returns a textual representation of the time value formatted by long date time format (no second)
func FormatToLongDateTimeWithoutSecond(t time.Time) string {
return t.Format(longDateTimeWithoutSecondFormat)
}
// ParseFromUnixTime parses a unix time and returns a golang time struct
func ParseFromUnixTime(unixTime int64) time.Time {
return time.Unix(unixTime, 0)
}
// ParseFromLongDateTime parses a formatted string in long date time format
func ParseFromLongDateTime(t string) (time.Time, error) {
return time.Parse(longDateTimeFormat, t)
+2 -1
View File
@@ -29,8 +29,9 @@ func SubString(str string, start int, length int) string {
end := 0
if start < 0 {
start = realLength - 1 + start
start = realLength + start
}
end = start + length
if start > end {