package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"bwallet/pkg/errno"
)

//返回数据的结构体
type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func SendResponse(c *gin.Context, err error, data interface{}) {
	if errno.ErrBind == err {
		code, _ := errno.DecodeErr(err)
		msg := data.(string)
		c.JSON(http.StatusOK, Response{
			Code: code,
			Msg:  msg,
			Data: nil,
		})
	} else {
		code, msg := errno.DecodeErr(err)
		c.JSON(http.StatusOK, Response{
			Code: code,
			Msg:  msg,
			Data: data,
		})
	}
}
