From 4d0b5a8961a04f7a36ae69b48081e58861e0b0d2 Mon Sep 17 00:00:00 2001 From: rogee Date: Mon, 11 Aug 2025 10:35:58 +0800 Subject: [PATCH] fix: wechat pay issues --- backend/app/http/posts.go | 24 ++++++-- frontend/wechat/src/views/ArticleDetail.vue | 67 +++++++++++---------- 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/backend/app/http/posts.go b/backend/app/http/posts.go index 921d62a..58b91b3 100644 --- a/backend/app/http/posts.go +++ b/backend/app/http/posts.go @@ -37,7 +37,12 @@ type posts struct { // @Bind pagination query // @Bind query query // @Bind user local -func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) { +func (ctl *posts) List( + ctx fiber.Ctx, + pagination *requests.Pagination, + query *ListQuery, + user *model.Users, +) (*requests.Pager, error) { conds := []model.Cond{ model.PostsModel().CondNotDeleted(), model.PostsModel().CondStatus(fields.PostStatusPublished), @@ -117,7 +122,8 @@ type PostItem struct { func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *model.Users) (*PostItem, error) { log.Infof("Fetching post with ID: %d", id) - post, err := model.PostsModel().GetByID(ctx.Context(), id, model.PostsModel().CondNotDeleted(), model.PostsModel().CondStatus(fields.PostStatusPublished)) + post, err := model.PostsModel(). + GetByID(ctx.Context(), id, model.PostsModel().CondNotDeleted(), model.PostsModel().CondStatus(fields.PostStatusPublished)) if err != nil { log.WithError(err).Errorf("GetByID err: %v", err) return nil, err @@ -196,7 +202,11 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er if asset.Metas.Duration == 0 { duration = 60 * 5 } - url, err := ctl.oss.GetSignedUrl(ctx.Context(), media.Path, ali.WithExpire(time.Second*time.Duration(duration))) + url, err := ctl.oss.GetSignedUrl( + ctx.Context(), + media.Path, + ali.WithExpire(time.Second*time.Duration(duration)), + ) if err != nil { log.WithError(err).Errorf("media GetSignedUrl err: %v", err) return nil, err @@ -213,7 +223,12 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er // @Bind pagination query // @Bind query query // @Bind user local -func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) { +func (ctl *posts) Mine( + ctx fiber.Ctx, + pagination *requests.Pagination, + query *ListQuery, + user *model.Users, +) (*requests.Pager, error) { log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword) conds := []model.Cond{ @@ -307,6 +322,7 @@ func (ctl *posts) Buy(ctx fiber.Ctx, id int64, user *model.Users) (*wechat.JSAPI AppId: "balance", }, nil } + return nil, errors.Errorf("账户余额不足, 当前余额:%0.2f, 请联系管理员购买或充值", float64(user.Balance)/100) payPrice := post.PayPrice() - user.Balance if err := order.SetMeta(ctx.Context(), func(om fields.OrderMeta) fields.OrderMeta { diff --git a/frontend/wechat/src/views/ArticleDetail.vue b/frontend/wechat/src/views/ArticleDetail.vue index 9ba94f9..2dd4940 100644 --- a/frontend/wechat/src/views/ArticleDetail.vue +++ b/frontend/wechat/src/views/ArticleDetail.vue @@ -100,43 +100,45 @@ const handleBuy = async () => { const payData = response.data; - if (payData.appId != "balance") { - // 调用微信支付 - window.WeixinJSBridge.invoke( - "getBrandWCPayRequest", - { - ...payData, - }, - async function (res) { - if (res.err_msg === "get_brand_wcpay_request:ok") { - // 支付成功,刷新文章数据 - fetchArticle(); - await updateMediaSource(); - } else if (res.err_msg === "get_brand_wcpay_request:cancel") { - // 用户取消支付 - console.log("Payment cancelled"); - alert("支付已取消"); - } else { - // 支付失败或取消 - console.error("Payment failed:", res.err_msg); - alert( - "支付失败:" + - (res.err_msg === "get_brand_wcpay_request:cancel" - ? "支付已取消" - : "支付异常") - ); - } - } - ); - return - } + // 暂停使用微支付 + // if (payData.appId != "balance") { + // // 调用微信支付 + // window.WeixinJSBridge.invoke( + // "getBrandWCPayRequest", + // { + // ...payData, + // }, + // async function (res) { + // if (res.err_msg === "get_brand_wcpay_request:ok") { + // // 支付成功,刷新文章数据 + // fetchArticle(); + // await updateMediaSource(); + // } else if (res.err_msg === "get_brand_wcpay_request:cancel") { + // // 用户取消支付 + // console.log("Payment cancelled"); + // alert("支付已取消"); + // } else { + // // 支付失败或取消 + // console.error("Payment failed:", res.err_msg); + // alert( + // "支付失败:" + + // (res.err_msg === "get_brand_wcpay_request:cancel" + // ? "支付已取消" + // : "支付异常") + // ); + // } + // } + // ); + // return + // } alert("余额支付成功"); fetchArticle(); await updateMediaSource(); } catch (error) { console.error("Failed to initiate payment:", error); - alert("发起支付失败,请稍后重试"); + // alert("发起支付失败,请稍后重试"); + alert(error.response?.data?.message || "发起支付失败,请稍后重试"); } finally { buying.value = false; } @@ -229,6 +231,9 @@ onUnmounted(() => {
注意:未购买视频仅可预览 1 分钟,购买后可观看全集。
+
+ 账户充值购买联系微信:13932043996 +
¥