PaymentCenter/app/http/controllers/backend/app.go

86 lines
2.3 KiB
Go

package backend
import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/constants/pojo"
"PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/appmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/utils/encrypt/rsa"
"PaymentCenter/app/utils/encrypt/sm2"
"PaymentCenter/app/utils/encrypt/sm4"
"github.com/ahmetb/go-linq/v3"
"github.com/gin-gonic/gin"
)
func AppList(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.AppListRequest)
req.SetDefault()
payChannelList, total, code := services.AppList(*req)
result := make([]backend.AppResponse, 0, len(payChannelList))
linq.From(payChannelList).SelectT(func(in appmodel.App) (out backend.AppResponse) {
out.ResponseFromDb(in)
return
}).ToSlice(&result)
data := entities.PageRsp{
Total: total,
Data: result,
}
controllers.HandCodeRes(c, data, code)
}
func AppCreate(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.AppCreateRequest)
payChannel := req.RequestToDb()
code := services.AppCreate(&payChannel)
data := backend.AppResponse{}
data.ResponseFromDb(payChannel)
controllers.HandCodeRes(c, data, code)
}
func AppUpdate(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.AppUpdateRequest)
payChannel := req.RequestToDb()
code := services.AppUpdate(&payChannel)
data := backend.AppResponse{}
data.ResponseFromDb(payChannel)
controllers.HandCodeRes(c, data, code)
}
func AppDelete(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*entities.IdRequest)
code := services.AppDelete(*req)
controllers.HandCodeRes(c, nil, code)
}
func GenerateDecrypt(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.GenerateDecryptKeyRequest)
var publicKey, privateKey string
var err error
switch req.KeyType {
case pojo.SM2:
publicKey, privateKey, err = sm2.GenerateSM2Key()
case pojo.RSA:
publicKey, privateKey, err = rsa.GenerateKey()
case pojo.SM4:
privateKey, publicKey = sm4.GenerateKey()
default:
controllers.HandCodeRes(c, "", errorcode.EncryptTypeNotFound)
return
}
if err != nil {
controllers.Error(c, errorcode.SystemError, err.Error())
return
}
controllers.HandCodeRes(c, map[string]string{
"publicKey": publicKey,
"privateKey": privateKey,
}, errorcode.Success)
}