12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package response
- import (
- "github.com/360EntSecGroup-Skylar/excelize"
- "github.com/gin-gonic/gin"
- "net/http"
- "strconv"
- )
- type Response struct {
- Code int `json:"code"`
- Data interface{} `json:"data"`
- Msg string `json:"msg"`
- }
- const (
- ERROR = 7
- SUCCESS = 0
- )
- func Result(code int, data interface{}, msg string, c *gin.Context) {
- // 开始时间
- c.JSON(http.StatusOK, Response{
- code,
- data,
- msg,
- })
- }
- func Ok(c *gin.Context) {
- Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
- }
- func OkWithMessage(message string, c *gin.Context) {
- Result(SUCCESS, map[string]interface{}{}, message, c)
- }
- func OkWithData(data interface{}, c *gin.Context) {
- Result(SUCCESS, data, "操作成功", c)
- }
- func OkDetailed(data interface{}, message string, c *gin.Context) {
- Result(SUCCESS, data, message, c)
- }
- func OkWithXlsx(list []interface{}, title []string, fileName string, c *gin.Context) {
- file := excelize.NewFile()
- file.SetSheetRow("Sheet1", "A1", &title)
- for i, v := range list {
- // 第一行被title占用
- lint := strconv.Itoa(i + 2)
- file.SetSheetRow("Sheet1", "A"+lint, v)
- }
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename="+fileName)
- c.Header("Content-Transfer-Encoding", "binary")
- _ = file.Write(c.Writer)
- }
- func Fail(c *gin.Context) {
- Result(ERROR, map[string]interface{}{}, "操作失败", c)
- }
- func FailWithMessage(message string, c *gin.Context) {
- Result(ERROR, map[string]interface{}{}, message, c)
- }
- func FailWithDetailed(code int, data interface{}, message string, c *gin.Context) {
- Result(code, data, message, c)
- }
|