main
elf 8 months ago
parent 2e651ca330
commit 0a66dfa6f3

@ -103,7 +103,7 @@ func Order(c *gin.Context) (utils.Data, error) {
} }
func GetWxAuthUrl(c *gin.Context) (utils.Data, error) { func GetWxAuthUrl(c *gin.Context) (utils.Data, error) {
authUrl := weixin.WxApi.GetAuthUrl("") authUrl := weixin.WxApi.GetAuthUrl("https://www.baidu.com")
data := utils.Data{"url": authUrl} data := utils.Data{"url": authUrl}
return data, nil return data, nil
} }

@ -1,7 +0,0 @@
package config
type Alipay struct {
AppId string `yaml:"app-id"`
PrivateKey string `yaml:"private-key"`
AliPublicKey string `yaml:"ali-public-key"`
}

@ -3,7 +3,6 @@ package config
type Config struct { type Config struct {
Server Server Server Server
Database Database Database Database
Alipay Alipay
Jwt Jwt Jwt Jwt
Redis Redis Redis Redis
Payment Payment Payment Payment

@ -1,13 +1,13 @@
package config package config
type Payment struct { type Payment struct {
BaseUrl string BaseUrl string `yaml:"base-url"`
AppKey string ApiKey string `yaml:"api-key"`
SignKey string SignKey string `yaml:"sign-key"`
WxPay WxPay WxPay WxPay `yaml:"wx-pay"`
} }
type WxPay struct { type WxPay struct {
SubAppId string SubAppId string `yaml:"sub-app-id"`
SubAppSecret string SubAppSecret string `yaml:"sub-app-secret"`
} }

@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger"
"os" "os"
) )
@ -28,7 +29,11 @@ func InitConfig() *config.Config {
} }
func InitDB(conf *config.Config) *gorm.DB { func InitDB(conf *config.Config) *gorm.DB {
db, err := gorm.Open(mysql.Open(conf.Database.GetDsn()), &gorm.Config{}) var mysqlLogger logger.Interface
mysqlLogger = logger.Default.LogMode(logger.Info)
db, err := gorm.Open(mysql.Open(conf.Database.GetDsn()), &gorm.Config{
Logger: mysqlLogger,
})
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -16,7 +16,7 @@ import (
) )
func main() { func main() {
payOrderCommon() RunServer()
} }
func RunServer() { func RunServer() {

@ -22,9 +22,9 @@ func h5RouteInit(r *gin.Engine) {
authGroup.GET("/ping", utils.JsonHandler(api.Index)) authGroup.GET("/ping", utils.JsonHandler(api.Index))
authGroup.GET("/error", utils.JsonHandler(api.Error)) authGroup.GET("/error", utils.JsonHandler(api.Error))
authGroup.GET("/get-user-info", utils.JsonHandler(api.GetUserInfo)) authGroup.GET("/get-user-info", utils.JsonHandler(api.GetUserInfo))
authGroup.GET("/order", utils.JsonHandler(api.Order)) authGroup.POST("/order", utils.JsonHandler(api.Order))
authGroup.GET("/modify-password", utils.JsonHandler(api.ModifyPassword)) authGroup.POST("/modify-password", utils.JsonHandler(api.ModifyPassword))
authGroup.GET("/payment", utils.JsonHandler(api.Payment)) authGroup.POST("/payment", utils.JsonHandler(api.Payment))
authGroup.GET("/get-wx-open-id", utils.JsonHandler(api.GetWxOpenId)) authGroup.GET("/get-wx-open-id", utils.JsonHandler(api.GetWxOpenId))
authGroup.GET("/get-wx-auth-url", utils.JsonHandler(api.GetWxAuthUrl)) authGroup.GET("/get-wx-auth-url", utils.JsonHandler(api.GetWxAuthUrl))
} }

@ -17,7 +17,7 @@ type orderService struct {
func (orderService) Order(orderRequest request.OrderRequest, user *model.User) (*model.Order, error) { func (orderService) Order(orderRequest request.OrderRequest, user *model.User) (*model.Order, error) {
product := &model.Product{} product := &model.Product{}
err := global.DB.Where("product_id", orderRequest.ProductID).First(product).Error err := global.DB.Where("id", orderRequest.ProductID).First(product).Error
if e.Is(err, gorm.ErrRecordNotFound) { if e.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.NewBusinessError("商品不存在") return nil, errors.NewBusinessError("商品不存在")
} }

@ -21,7 +21,7 @@ func (api *payApi) WxPay(param WxPayParam) (*WxPayResult, error) {
payment := global.Config.Payment payment := global.Config.Payment
data := url.Values{} data := url.Values{}
data.Set("service", "comm.js.pay") data.Set("service", "comm.js.pay")
data.Set("apikey", payment.AppKey) data.Set("apikey", payment.ApiKey)
data.Set("money", fmt.Sprintf("%.2f", param.Amount)) data.Set("money", fmt.Sprintf("%.2f", param.Amount))
data.Set("sub_appid", payment.WxPay.SubAppId) data.Set("sub_appid", payment.WxPay.SubAppId)
data.Set("sub_openid", param.OpenId) data.Set("sub_openid", param.OpenId)

@ -1,6 +1,8 @@
package weixin package weixin
type AccessTokenResult struct { type AccessTokenResult struct {
ErrorCode int `json:"errcode"`
ErrorMessage string `json:"errmsg"`
AccessToken string `json:"access_token"` AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"` ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"` RefreshToken string `json:"refresh_token"`

@ -32,10 +32,14 @@ func (api wxApi) GetAccessToken(code string) (*AccessTokenResult, error) {
if err != nil { if err != nil {
return nil, errors.NewBusinessError("数据解析错误") return nil, errors.NewBusinessError("数据解析错误")
} }
if result.ErrorCode != 200 {
return nil, errors.NewBusinessError(result.ErrorMessage)
}
return &result, nil return &result, nil
} }
func (api wxApi) GetAuthUrl(redirectUrl string) string { func (api wxApi) GetAuthUrl(redirectUrl string) string {
fmt.Println(global.Config)
params := url.Values{} params := url.Values{}
params.Set("appid", global.Config.Payment.WxPay.SubAppId) params.Set("appid", global.Config.Payment.WxPay.SubAppId)
params.Set("redirect_uri", redirectUrl) params.Set("redirect_uri", redirectUrl)

Loading…
Cancel
Save