diff --git a/api/core/app_server.go b/api/core/app_server.go index a8fa466e..4e4f3a1f 100644 --- a/api/core/app_server.go +++ b/api/core/app_server.go @@ -151,6 +151,7 @@ func authorizeMiddleware(s *AppServer, client *redis.Client) gin.HandlerFunc { c.Request.URL.Path == "/api/sd/jobs" || strings.HasPrefix(c.Request.URL.Path, "/api/sms/") || strings.HasPrefix(c.Request.URL.Path, "/api/captcha/") || + strings.HasPrefix(c.Request.URL.Path, "/api/payment/") || strings.HasPrefix(c.Request.URL.Path, "/static/") || c.Request.URL.Path == "/api/admin/config/get" { c.Next() diff --git a/api/core/config.go b/api/core/config.go index 77722515..90e5abfb 100644 --- a/api/core/config.go +++ b/api/core/config.go @@ -36,7 +36,7 @@ func NewDefaultConfig() *types.AppConfig { MjConfig: types.MidJourneyConfig{Enabled: false}, SdConfig: types.StableDiffusionConfig{Enabled: false, Txt2ImgJsonPath: "res/text2img.json"}, WeChatBot: false, - AlipayConfig: types.AlipayConfig{SandBox: true}, + AlipayConfig: types.AlipayConfig{Enabled: false}, } } diff --git a/api/core/types/config.go b/api/core/types/config.go index 221399e3..c4b1100d 100644 --- a/api/core/types/config.go +++ b/api/core/types/config.go @@ -62,7 +62,6 @@ type AliYunSmsConfig struct { type AlipayConfig struct { Enabled bool // 是否启用该服务 - SandBox bool // 是否沙箱环境 Company string // 公司名称 UserId string // 支付宝用户 ID AppId string // 支付宝 AppID diff --git a/api/handler/payment_handler.go b/api/handler/payment_handler.go index e329f926..39ce615d 100644 --- a/api/handler/payment_handler.go +++ b/api/handler/payment_handler.go @@ -19,6 +19,11 @@ import ( "time" ) +const ( + PayWayAlipay = "支付宝" + PayWayWechat = "微信支付" +) + // PaymentHandler 支付服务回调 handler type PaymentHandler struct { BaseHandler @@ -110,6 +115,7 @@ func (h *PaymentHandler) OrderQuery(c *gin.Context) { func (h *PaymentHandler) AlipayQrcode(c *gin.Context) { var data struct { ProductId uint `json:"product_id"` + UserId int `json:"user_id"` } if err := c.ShouldBindJSON(&data); err != nil { resp.ERROR(c, types.InvalidArgs) @@ -128,9 +134,10 @@ func (h *PaymentHandler) AlipayQrcode(c *gin.Context) { resp.ERROR(c, "error with generate trade no: "+err.Error()) return } - user, err := utils.GetLoginUser(c, h.db) - if err != nil { - resp.NotAuth(c) + var user model.User + res = h.db.First(&user, data.UserId) + if res.Error != nil { + resp.ERROR(c, "Invalid user ID") return } @@ -150,6 +157,7 @@ func (h *PaymentHandler) AlipayQrcode(c *gin.Context) { Subject: product.Name, Amount: product.Price - product.Discount, Status: types.OrderNotPaid, + PayWay: PayWayAlipay, Remark: utils.JsonEncode(remark), } res = h.db.Create(&order) @@ -229,6 +237,7 @@ func (h *PaymentHandler) AlipayNotify(c *gin.Context) { user.ExpiredTime = time.Now().AddDate(0, 0, remark.Days).Unix() } user.Vip = true + } else if !user.Vip { // 充值点卡的非 VIP 用户 user.ExpiredTime = time.Now().AddDate(0, 0, 30).Unix() } diff --git a/api/main.go b/api/main.go index 953793eb..7c61d806 100644 --- a/api/main.go +++ b/api/main.go @@ -33,7 +33,7 @@ import ( var logger = logger2.GetLogger() -//go:embed res/ip2region.xdb +//go:embed res var xdbFS embed.FS // AppLifecycle 应用程序生命周期 diff --git a/api/service/payment/alipay_service.go b/api/service/payment/alipay_service.go index d33d5032..9dd1a9c5 100644 --- a/api/service/payment/alipay_service.go +++ b/api/service/payment/alipay_service.go @@ -28,7 +28,7 @@ func NewAlipayService(appConfig *types.AppConfig) (*AlipayService, error) { return nil, fmt.Errorf("error with read App Private key: %v", err) } - xClient, err := alipay.New(config.AppId, priKey, !config.SandBox) + xClient, err := alipay.New(config.AppId, priKey, false) if err != nil { return nil, fmt.Errorf("error with initialize alipay service: %v", err) } diff --git a/api/store/model/order.go b/api/store/model/order.go index 65d5580f..16d01b0c 100644 --- a/api/store/model/order.go +++ b/api/store/model/order.go @@ -17,5 +17,6 @@ type Order struct { Status types.OrderStatus Remark string PayTime int64 + PayWay string // 支付方式 DeletedAt gorm.DeletedAt } diff --git a/api/store/vo/order.go b/api/store/vo/order.go index f7d6bc3c..99878dc1 100644 --- a/api/store/vo/order.go +++ b/api/store/vo/order.go @@ -14,5 +14,6 @@ type Order struct { Amount float64 `json:"amount"` Status types.OrderStatus `json:"status"` PayTime int64 `json:"pay_time"` + PayWay string `json:"pay_way"` Remark types.OrderRemark `json:"remark"` } diff --git a/web/src/views/Member.vue b/web/src/views/Member.vue index 7b283bae..eada7e7f 100644 --- a/web/src/views/Member.vue +++ b/web/src/views/Member.vue @@ -14,7 +14,7 @@ @@ -50,7 +76,7 @@ import {nextTick, onMounted, ref} from "vue" import {ElMessage} from "element-plus"; import {httpGet, httpPost} from "@/utils/http"; import ItemList from "@/components/ItemList.vue"; -import {Delete, Plus} from "@element-plus/icons-vue"; +import {Delete, InfoFilled, Plus, SuccessFilled} from "@element-plus/icons-vue"; import LoginDialog from "@/components/LoginDialog.vue"; import {checkSession} from "@/action/session"; import {arrayContains, removeArrayItem, substr} from "@/utils/libs"; @@ -59,8 +85,13 @@ import router from "@/router"; const listBoxHeight = window.innerHeight - 97 const list = ref([]) const showLoginDialog = ref(false) +const showPayDialog = ref(false) const elements = ref(null) const vipImg = ref("/images/vip.png") +const qrcode = ref("") +const amount = ref(0) +const discount = ref(0) +const text = ref("") onMounted(() => { httpGet("/api/product/list").then((res) => { list.value = res.data @@ -69,14 +100,39 @@ onMounted(() => { }) }) -const pay = (row) => { +const orderPay = (row) => { checkSession().then(user => { console.log(row) - }).catch(() => { + httpPost("/api/payment/alipay/qrcode", {product_id: row.id, user_id: user.id}).then(res => { + console.log(res) + showPayDialog.value = true + qrcode.value = res.data['image'] + queryOrder(res.data['order_no']) + }).catch(e => { + ElMessage.error("生成支付订单失败:" + e.message) + }) + }).catch(e => { + console.log(e) showLoginDialog.value = true }) } +const queryOrder = (orderNo) => { + httpPost("/api/payment/query", {order_no: orderNo}).then(res => { + if (res.data.status === 1) { + text.value = "扫码成功,请在手机上进行支付!" + queryOrder(orderNo) + } else if (res.data.status === 2) { + text.value = "支付成功,正在刷新页面" + setTimeout(() => location.reload(), 500) + } else { + queryOrder(orderNo) + } + }).catch(e => { + ElMessage.error("查询支付状态失败:" + e.message) + }) +} +