package h5 import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" "gold-shop/errors" "gold-shop/global" "gold-shop/request" "gold-shop/service" "gold-shop/utils/result" "gold-shop/utils/tlpay/notify" "gold-shop/utils/weixin" "gorm.io/gorm" "net/http" "strconv" ) func Index(c *gin.Context) (result.Data, error) { return result.Data{"hello": "elf"}, nil } func Error(c *gin.Context) (result.Data, error) { return nil, errors.NewBusinessError("测试", "1122") } func Register(c *gin.Context) (result.Data, error) { mobile := c.PostForm("mobile") password := c.PostForm("password") err := service.UserService.Register(mobile, password) return nil, err } func Login(c *gin.Context) (result.Data, error) { mobile := c.PostForm("mobile") password := c.PostForm("password") token, err := service.UserService.Login(mobile, password) return result.Data{"token": token}, err } func GetUserInfo(c *gin.Context) (result.Data, error) { user, _ := c.Get("user") data := result.Data{"user": user} return data, nil } func GetUserData(c *gin.Context) (result.Data, error) { data := service.UserService.GetUserData(user(c)) return data, nil } func ModifyPassword(c *gin.Context) (result.Data, error) { oldPassword := c.PostForm("oldPassword") password := c.PostForm("password") err := service.UserService.ModifyPassword(password, oldPassword, user(c)) return nil, err } func GetProductInfo(c *gin.Context) (result.Data, error) { productId := c.Query("productId") id, _ := strconv.Atoi(productId) product, err := service.ProductService.GetProductInfo(id) if err != nil { return nil, err } data := result.Data{"product": product} return data, nil } func GetPayments(c *gin.Context) (result.Data, error) { var paymentQueryRequest request.PaymentQueryRequest err := c.ShouldBindQuery(&paymentQueryRequest) if err != nil { return nil, errors.NewBusinessError("参数错误") } payments, total := service.PaymentService.GetUserPayments(user(c).ID, paymentQueryRequest.Page, paymentQueryRequest.PageSize) data := result.Data{ "payments": payments, "total": total, } return data, nil } func Payment(c *gin.Context) (result.Data, error) { var paymentRequest request.PaymentRequest err := c.ShouldBind(&paymentRequest) fmt.Println(paymentRequest) if err != nil { return nil, errors.NewBusinessError("参数错误") } return service.PaymentService.Payment(user(c).ID, paymentRequest) } func GetOrders(c *gin.Context) (result.Data, error) { var orderQueryRequest request.OrderQueryRequest err := c.ShouldBindQuery(&orderQueryRequest) if err != nil { return nil, errors.NewBusinessError("参数错误") } orders, total := service.OrderService.GetUserOrders(user(c).ID, orderQueryRequest.Page, orderQueryRequest.PageSize) data := result.Data{ "orders": orders, "total": total, } return data, nil } func Order(c *gin.Context) (result.Data, error) { var orderRequest request.OrderRequest err := c.ShouldBind(&orderRequest) if err != nil { return nil, errors.NewBusinessError("参数错误") } order, err := service.OrderService.Order(orderRequest, user(c)) if err != nil { return nil, err } data := result.Data{"orderNo": order.OrderNo} return data, nil } func GetWxAuthUrl(c *gin.Context) (result.Data, error) { amount := c.DefaultQuery("amount", "") redirectUri := global.Config.Payment.WxPay.AuthRedirectUri if amount != "" { redirectUri += "?amount=" + amount } authUrl := weixin.WxApi.GetAuthUrl(redirectUri) fmt.Println(authUrl) data := result.Data{"url": authUrl} return data, nil } func GetWxOpenId(c *gin.Context) (result.Data, error) { code := c.Query("code") res, err := weixin.WxApi.GetAccessToken(code) if err != nil { return nil, err } data := result.Data{"openId": res.Openid} return data, nil } func SaveAlipayAccount(c *gin.Context) (result.Data, error) { alipayAccount := c.PostForm("alipayAccount") err := service.UserService.SaveAlipayAccount(alipayAccount, user(c)) return nil, err } func TransferPayNotify(c *gin.Context) { var req request.TransferPayNotifyRequest err := c.ShouldBindQuery(&req) if err != nil { c.String(http.StatusOK, "fail") return } service.PaymentService.AfterTransferPayNotify(req) c.String(http.StatusOK, "success") return } func PaymentNotify(c *gin.Context) { var req request.PaymentNotifyRequest err := c.ShouldBind(&req) if err != nil { fmt.Println("error: " + err.Error()) c.String(http.StatusOK, "fail") return } fmt.Println("has-data") fmt.Println(req) service.PaymentService.AfterPaymentNotify(req) c.String(http.StatusOK, "success") return } func BindingPayNotify(c *gin.Context) { var req notify.PayAgreeNotify err := c.ShouldBind(&req) if err != nil { fmt.Println("error: " + err.Error()) c.String(http.StatusOK, "fail") return } fmt.Println("has-data") fmt.Println(req) reqJson, err := json.Marshal(req) fmt.Println(string(reqJson)) if err != nil { c.String(http.StatusOK, "fail") return } service.PaymentService.AfterBindingPayNotify(req) c.String(http.StatusOK, "success") return } func GatewayPayRet(c *gin.Context) { var req notify.GatewayPayRet err := c.ShouldBind(&req) if err != nil { fmt.Println("error: " + err.Error()) c.String(http.StatusOK, "fail") return } fmt.Println("has-data") fmt.Println(req) reqJson, err := json.Marshal(req) fmt.Println(string(reqJson)) if err != nil { c.String(http.StatusOK, "fail") return } c.Redirect(http.StatusMovedPermanently, "http://mall.wrtcjt.com/pages/knq-detail/index") return } func TlBindingApply(c *gin.Context) (result.Data, error) { var req request.TlBindingApplyRequest err := c.ShouldBind(&req) if err != nil { return nil, err } thpInfo, err := service.UserService.TlBindingApply(req, user(c)) data := result.Data{"thpInfo": thpInfo} return data, err } func TlBindingConfirm(c *gin.Context) (result.Data, error) { var req request.TlBindingConfirmRequest err := c.ShouldBind(&req) if err != nil { return nil, err } data, err := service.UserService.TlBindingConfirm(req, user(c)) return data, err } func GetUserBankCard(c *gin.Context) (result.Data, error) { var req request.TlBindingConfirmRequest err := c.ShouldBind(&req) if err != nil { return nil, err } bankCard, err := service.UserService.GetBankCardByUserID(user(c).ID) if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { return result.Data{"bankCard": bankCard}, nil } if err != nil { return nil, err } card := result.Data{"agreeId": bankCard.AgreeID, "bankCode": bankCard.BankCode, "bankName": bankCard.BankName} data := result.Data{"bankCard": card} return data, err } func BindingPayConfirm(c *gin.Context) (result.Data, error) { var req request.BindingPayConfirmRequest err := c.ShouldBind(&req) if err != nil { return nil, err } data, err := service.PaymentService.ConfirmBindingPayment(req, user(c)) return data, err } func BindingPaySms(c *gin.Context) (result.Data, error) { var req request.BindingPaySmsRequest err := c.ShouldBind(&req) if err != nil { return nil, err } data, err := service.PaymentService.SmsBindingPayment(req, user(c)) return data, err } func GatewayPayApply(c *gin.Context) (result.Data, error) { var req request.PaymentRequest err := c.ShouldBind(&req) if err != nil { return nil, err } req.PayType = "gateway" return service.PaymentService.GatewayPayApply(user(c).ID, req) } func GatewayPay(c *gin.Context) { c.Writer.Header().Set("content-type", "text/html") token := c.DefaultQuery("token", "") if token == "" { c.Writer.WriteString("token empty") return } body, err := service.PaymentService.GatewayPay(token) if err != nil { c.Writer.WriteString(err.Error()) return } c.Writer.WriteString(body) return } func BankCardUnbind(c *gin.Context) (result.Data, error) { err := service.UserService.BankCardUnbind(user(c)) if err != nil { return nil, err } return nil, nil }