package app

import (
	"dsbqj-admin/pkg/e"
	"dsbqj-admin/pkg/util"
	"os"

	"github.com/goccy/go-json"
)

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func encode(data interface{}) interface{} {
	if os.Getenv("NAME") == "admin" {
		buf, _ := json.Marshal(data)
		return util.EncryptDES_ECB(buf, os.Getenv("CRYPRO_SECRET"))
	} else {
		return data
	}
}

// Response setting gin.JSON
func (g *Gin) Response(httpCode, errCode int, data interface{}) {

	g.C.JSON(httpCode, Response{
		Code: errCode,
		Msg:  e.GetMsg(errCode),
		Data: encode(data),
		// Data: data,
	})
	return
}
