enterprise/server/controller/qyweixin.go

91 lines
2.6 KiB
Go

package controller
import (
"enterprise/common/config"
"enterprise/common/global"
"enterprise/server/service"
"enterprise/server/session"
"fmt"
"git.u8t.cn/open/gosdk/qyweixin"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"net/http"
"strings"
"time"
)
type QyWeixin struct {
}
func (q *QyWeixin) Approve(ctx *gin.Context) {
cfg := config.GetConfig()
reply := new(service.Approve).Reply
qyApp := qyweixin.NewApp(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
Secret: cfg.QyWeixin.ApproveSecret,
Agent: cfg.QyWeixin.ApproveAgent,
Token: config.QyWeixinAgentToken,
AesKey: config.QyWeixinAgentAesKey,
Replay: reply,
})
qyApp.Callback(ctx)
}
func (q *QyWeixin) Pay(ctx *gin.Context) {
userid := ctx.Query("userid")
amount := cast.ToInt64(ctx.Query("amount"))
title := ctx.Query("title")
pass := ctx.Query("pass")
if pass != "1c9dea1fb85cf06ab0b4d946d49aa92f" {
panic("password error")
}
// 获取openid
approve := qyweixin.NewAppApprove(&qyweixin.AppConfig{
Corpid: config.GetConfig().QyWeixin.Corpid,
Secret: config.GetConfig().QyWeixin.EnterpriseSecret,
Agent: config.GetConfig().QyWeixin.EnterpriseAgent,
})
openid, err := approve.GetOpenid(userid)
if err != nil {
panic(err)
}
// 支付费用
var req qyweixin.PayReq
req.BillNo = fmt.Sprintf("API%s%s", time.Now().Format("20060102150405"), userid)
req.Title = title
req.Openid = openid
req.TotalAmount = amount
qyPay := qyweixin.NewAppPay(&qyweixin.PayConfig{
Corpid: config.GetConfig().QyWeixin.Corpid,
Secret: config.GetConfig().QyWeixin.PaySecret,
Agent: config.GetConfig().QyWeixin.PayAgent,
SerialNumber: config.GetConfig().WxPay.PaySerialNumber,
ApiKey: config.GetConfig().WxPay.PayApiKeyV2,
MchId: config.GetConfig().WxPay.PayMchId,
CertPem: config.GetConfig().WxPay.PayCertPem,
KeyPem: config.GetConfig().WxPay.PayKeyPem,
})
if err = qyPay.PayMoney(&req); err != nil {
log.Errorf("pay req[%s] error :%s", goutil.EncodeJSON(req), err.Error())
panic(err)
}
message := make([]string, 0)
message = append(message, fmt.Sprintf("【红包发放】[%s]", userid))
message = append(message, fmt.Sprintf("发放金额:%s", goutil.FormatMoney(amount)))
message = append(message, fmt.Sprintf("员工名称:%s", userid))
message = append(message, fmt.Sprintf("费用说明:%s", title))
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
ctx.JSON(http.StatusOK, session.NewRspOk())
}