package handlers import ( "awesomeProject/internal/common" "awesomeProject/internal/models" "net/http" "github.com/gin-gonic/gin" ) // AdminPaymentHandler 管理员付款方式处理器 type AdminPaymentHandler struct{} // GetPaymentInfo 获取管理员付款方式信息 func (h *AdminPaymentHandler) GetPaymentInfo(c *gin.Context) { db := common.GetDB() // 查询系统角色为管理员的用户的付款信息 var admin models.User err := db.Where("system_role = ?", 0).Select("main_payment_qr_image, sub_payment_qr_image").First(&admin).Error if err != nil { c.JSON(http.StatusOK, common.Error(500, "查询管理员付款信息失败")) return } // 返回管理员付款方式信息 paymentInfo := map[string]interface{}{ "main_payment_qr_image": admin.MainPaymentQrImage, "sub_payment_qr_image": admin.SubPaymentQrImage, } c.JSON(http.StatusOK, common.Success(paymentInfo)) } // UpdatePaymentInfo 更新管理员付款方式信息 func (h *AdminPaymentHandler) UpdatePaymentInfo(c *gin.Context) { currentUser, _ := c.Get("current_user") user := currentUser.(models.User) // 验证是否为管理员 if user.SystemRole != 0 { c.JSON(http.StatusOK, common.Error(403, "权限不足")) return } // 解析请求数据 var req struct { MainPaymentQrImage string `json:"main_payment_qr_image"` SubPaymentQrImage string `json:"sub_payment_qr_image"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusOK, common.Error(400, "请求参数错误")) return } // 更新管理员付款方式信息 updates := map[string]interface{}{} if req.MainPaymentQrImage != "" { updates["main_payment_qr_image"] = req.MainPaymentQrImage } if req.SubPaymentQrImage != "" { updates["sub_payment_qr_image"] = req.SubPaymentQrImage } if len(updates) == 0 { c.JSON(http.StatusOK, common.Error(400, "没有要更新的信息")) return } if err := common.GetDB().Model(&user).Updates(updates).Error; err != nil { c.JSON(http.StatusOK, common.Error(500, "更新付款方式失败")) return } // 返回更新后的信息 var updatedUser models.User common.GetDB().First(&updatedUser, user.ID) paymentInfo := map[string]interface{}{ "main_payment_qr_image": updatedUser.MainPaymentQrImage, "sub_payment_qr_image": updatedUser.SubPaymentQrImage, } c.JSON(http.StatusOK, common.Success(paymentInfo)) }