You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
8.4 KiB
Go

7 months ago
package h5
import (
6 months ago
"encoding/json"
6 months ago
"fmt"
7 months ago
"github.com/gin-gonic/gin"
"gold-shop/errors"
6 months ago
"gold-shop/global"
7 months ago
"gold-shop/request"
"gold-shop/service"
5 months ago
"gold-shop/utils/alipay"
7 months ago
"gold-shop/utils/result"
6 months ago
"gold-shop/utils/tlpay/notify"
7 months ago
"gold-shop/utils/weixin"
6 months ago
"gorm.io/gorm"
7 months ago
"net/http"
7 months ago
"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("参数错误")
}
7 months ago
payments, total := service.PaymentService.GetUserPayments(user(c).ID, paymentQueryRequest.Page, paymentQueryRequest.PageSize)
7 months ago
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)
6 months ago
fmt.Println(paymentRequest)
7 months ago
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("参数错误")
}
7 months ago
orders, total := service.OrderService.GetUserOrders(user(c).ID, orderQueryRequest.Page, orderQueryRequest.PageSize)
7 months ago
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) {
6 months ago
amount := c.DefaultQuery("amount", "")
redirectUri := global.Config.Payment.WxPay.AuthRedirectUri
if amount != "" {
redirectUri += "?amount=" + amount
}
authUrl := weixin.WxApi.GetAuthUrl(redirectUri)
fmt.Println(authUrl)
7 months ago
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
}
6 months ago
data := result.Data{"openId": res.Openid}
7 months ago
return data, nil
}
7 months ago
5 months ago
func GetAliOpenId(c *gin.Context) (result.Data, error) {
code := c.Query("code")
res, err := alipay.AlipayApi.GetAuthToken(code)
if err != nil {
return nil, err
}
data := result.Data{"openId": res.OpenID}
return data, nil
}
7 months ago
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
}
6 months ago
6 months ago
func PaymentNotify(c *gin.Context) {
var req request.PaymentNotifyRequest
6 months ago
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)
6 months ago
service.PaymentService.AfterPaymentNotify(req)
c.String(http.StatusOK, "success")
return
}
func BindingPayNotify(c *gin.Context) {
var req notify.PayAgreeNotify
err := c.ShouldBind(&req)
6 months ago
if err != nil {
6 months ago
fmt.Println("error: " + err.Error())
6 months ago
c.String(http.StatusOK, "fail")
return
}
6 months ago
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)
6 months ago
c.String(http.StatusOK, "success")
return
}
6 months ago
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
}
6 months ago
func TlBindingApply(c *gin.Context) (result.Data, error) {
6 months ago
var req request.TlBindingApplyRequest
6 months ago
err := c.ShouldBind(&req)
if err != nil {
return nil, err
}
6 months ago
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
}
6 months ago
bankCard, err := service.UserService.GetBankCardByUserID(user(c).ID)
6 months ago
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
6 months ago
}
6 months ago
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)
}
6 months ago
6 months ago
func GatewayPay(c *gin.Context) {
6 months ago
c.Writer.Header().Set("content-type", "text/html")
6 months ago
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)
6 months ago
return
}
6 months ago
5 months ago
func UnionPay(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.UnionPay(token)
if err != nil {
c.Writer.WriteString(err.Error())
return
}
c.Writer.WriteString(body)
return
}
6 months ago
func BankCardUnbind(c *gin.Context) (result.Data, error) {
err := service.UserService.BankCardUnbind(user(c))
if err != nil {
return nil, err
}
return nil, nil
}