feat: update
This commit is contained in:
33
backend/app/http/buy.html.tpl
Normal file
33
backend/app/http/buy.html.tpl
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
function onBridgeReady() {
|
||||||
|
WeixinJSBridge.invoke('getBrandWCPayRequest', {{- .signature -}}, function (res) {
|
||||||
|
if (res.err_msg == "get_brand_wcpay_request:ok") {
|
||||||
|
// 使用以上方式判断前端返回,微信团队郑重提示:
|
||||||
|
//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠,商户需进一步调用后端查单确认支付结果。
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (typeof WeixinJSBridge == "undefined") {
|
||||||
|
if (document.addEventListener) {
|
||||||
|
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
|
||||||
|
} else if (document.attachEvent) {
|
||||||
|
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
|
||||||
|
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
onBridgeReady();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/app/models"
|
"quyun/app/models"
|
||||||
@@ -8,7 +12,6 @@ import (
|
|||||||
"quyun/database/schemas/public/model"
|
"quyun/database/schemas/public/model"
|
||||||
"quyun/providers/wepay"
|
"quyun/providers/wepay"
|
||||||
|
|
||||||
"github.com/go-pay/gopay/wechat/v3"
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"github.com/gofiber/fiber/v3/log"
|
"github.com/gofiber/fiber/v3/log"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -24,7 +27,7 @@ type posts struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List posts
|
// List posts
|
||||||
// @Router /posts [get]
|
// @Router / [get]
|
||||||
// @Bind pagination query
|
// @Bind pagination query
|
||||||
// @Bind query query
|
// @Bind query query
|
||||||
// @Bind user local
|
// @Bind user local
|
||||||
@@ -49,26 +52,29 @@ func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
|
|||||||
return models.Users.PostList(ctx.Context(), 1, pagination, query.Keyword)
|
return models.Users.PostList(ctx.Context(), 1, pagination, query.Keyword)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed buy.html.tpl
|
||||||
|
var buyTpl string
|
||||||
|
|
||||||
// Buy
|
// Buy
|
||||||
// @Router /buy/:id [get]
|
// @Router /buy/:id [get]
|
||||||
// @Bind id path
|
// @Bind id path
|
||||||
func (ctl *posts) Buy(ctx fiber.Ctx, id int64) (*wechat.JSAPIPayParams, error) {
|
func (ctl *posts) Buy(ctx fiber.Ctx, id int64) error {
|
||||||
var userId int64 = 1
|
var userId int64 = 1
|
||||||
|
|
||||||
user, err := models.Users.GetByID(ctx.Context(), userId)
|
user, err := models.Users.GetByID(ctx.Context(), userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, " failed to get user: %d", userId)
|
return errors.Wrapf(err, " failed to get user: %d", userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
post, err := models.Posts.GetByID(ctx.Context(), id)
|
post, err := models.Posts.GetByID(ctx.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, " failed to get post: %d", id)
|
return errors.Wrapf(err, " failed to get post: %d", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create order
|
// create order
|
||||||
order, err := models.Orders.Create(ctx.Context(), userId, post.ID)
|
order, err := models.Orders.Create(ctx.Context(), userId, post.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "订单创建失败")
|
return errors.Wrap(err, "订单创建失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
prePayResp, err := ctl.wepay.V3TransactionJsapi(ctx.Context(), func(bm *wepay.BodyMap) {
|
prePayResp, err := ctl.wepay.V3TransactionJsapi(ctx.Context(), func(bm *wepay.BodyMap) {
|
||||||
@@ -80,8 +86,30 @@ func (ctl *posts) Buy(ctx fiber.Ctx, id int64) (*wechat.JSAPIPayParams, error) {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("wepay.V3TransactionJsapi err: %v", err)
|
log.Errorf("wepay.V3TransactionJsapi err: %v", err)
|
||||||
return nil, errors.Wrap(err, "微信支付失败")
|
return errors.Wrap(err, "微信支付失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
return prePayResp.PaySignOfJSAPI()
|
signature, err := prePayResp.PaySignOfJSAPI()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to get pay sign")
|
||||||
|
}
|
||||||
|
|
||||||
|
// render buyTpl with html/template
|
||||||
|
tpl, err := template.New("buy").Parse(buyTpl)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to parse template")
|
||||||
|
}
|
||||||
|
|
||||||
|
signatureBytes, err := json.Marshal(signature)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to marshal signature")
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf strings.Builder
|
||||||
|
if err := tpl.Execute(&buf, map[string]interface{}{"signature": string(signatureBytes)}); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to execute template")
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
return ctx.SendString(buf.String())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func (r *Routes) Register(router fiber.Router) {
|
|||||||
))
|
))
|
||||||
|
|
||||||
// 注册路由组: posts
|
// 注册路由组: posts
|
||||||
router.Get("/posts", DataFunc3(
|
router.Get("/", DataFunc3(
|
||||||
r.posts.List,
|
r.posts.List,
|
||||||
Query[requests.Pagination]("pagination"),
|
Query[requests.Pagination]("pagination"),
|
||||||
Query[ListQuery]("query"),
|
Query[ListQuery]("query"),
|
||||||
@@ -68,7 +68,7 @@ func (r *Routes) Register(router fiber.Router) {
|
|||||||
Query[ListQuery]("query"),
|
Query[ListQuery]("query"),
|
||||||
))
|
))
|
||||||
|
|
||||||
router.Get("/buy/:id", DataFunc1(
|
router.Get("/buy/:id", Func1(
|
||||||
r.posts.Buy,
|
r.posts.Buy,
|
||||||
PathParam[int64]("id"),
|
PathParam[int64]("id"),
|
||||||
))
|
))
|
||||||
|
|||||||
Reference in New Issue
Block a user