enterprise/server/controller/payment.go

77 lines
2.3 KiB
Go

package controller
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/server/api"
"enterprise/server/service"
"enterprise/server/session"
CommonService "enterprise/service"
"fmt"
"github.com/gin-gonic/gin"
"github.com/smbrave/goutil"
"net/http"
)
type Payment struct {
}
func NewPayment() *Payment {
return &Payment{}
}
func (q *Payment) AlipayAuth(ctx *gin.Context) {
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
authCode := ctx.Query("auth_code")
state := ctx.Query("state")
userid, staffUser := service.NewPayment().AlipayAuth(sess, authCode, state)
username := goutil.If(staffUser.Realname != "", staffUser.Realname, staffUser.Username)
ctx.HTML(http.StatusOK, "alipay.html", gin.H{
"title": "绑定成功",
"message": fmt.Sprintf("支付宝【%s】->【%s】", userid, username),
"style": "payment-success",
})
}
func (q *Payment) Pay(ctx *gin.Context) {
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
corp, err := dao.NewCorpDao().GetByHost(sess.GetHeader().Host)
session.CheckDBError(err)
session.CheckNilError(corp, "域名未绑定")
var req api.PayReq
session.CheckParamError(ctx.ShouldBindJSON(&req))
ctx.Keys[session.ContextRequest] = req
if req.Password != corp.GetConfig().Password {
panic(config.ErrParam.New().Append("密码错误"))
}
user, err := dao.NewStaffUserDao().GetByUsername(corp.Id, req.Username)
session.CheckDBError(err)
session.CheckNilError(user, fmt.Sprintf("用户[%s]不存在", req.Username))
if err = CommonService.NewPay().Pay(corp, user, req.Title, req.PayType, req.Amount); err != nil {
panic(config.ErrInternal.New().Append(err))
}
ctx.JSON(http.StatusOK, session.NewRspOk())
}
func (q *Payment) List(ctx *gin.Context) {
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
var req api.ListPaymentReq
session.CheckParamError(ctx.ShouldBind(&req))
req.Default()
ctx.Keys[session.ContextRequest] = req
total, totalAmount, items := service.NewPayment().List(sess, &req)
ctx.JSON(http.StatusOK, session.NewListAmountRsp(total, totalAmount, items))
}
func (q *Payment) Suggest(ctx *gin.Context) {
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
field := ctx.Query("field")
data := service.NewPayment().Suggest(sess, field)
ctx.JSON(http.StatusOK, session.NewRsp(data))
}