diff --git a/app/constants/errorcode/error_code.go b/app/constants/errorcode/error_code.go index ab6a619..8e52b96 100644 --- a/app/constants/errorcode/error_code.go +++ b/app/constants/errorcode/error_code.go @@ -52,7 +52,10 @@ const ( AppAesEncryptFail = 1234 AppDeEncryptFail = 1250 // 加密方式不存在 - EncryptTypeNotFound = 1241 + EncryptTypeNotFound = 1241 + PayChannelConfigNotFound = 1242 + // 加密参数异常 + PayEncryptParamFail = 1243 //渠道 PayChannelNotFound = 1300 @@ -138,7 +141,9 @@ var MsgZH = map[int]string{ AppAesEncryptFail: "aes 加密失败", - EncryptTypeNotFound: "加密方式不存在", + EncryptTypeNotFound: "加密方式不存在", + PayChannelConfigNotFound: "secret支付方式配置不存在", + PayEncryptParamFail: "加密参数错误,解析失败", AppDeEncryptFail: "未知原因导致解密失败,请检查加密数据是和app加密配置", diff --git a/app/http/controllers/front/payment_controller.go b/app/http/controllers/front/payment_controller.go index 1efcf63..b8459df 100644 --- a/app/http/controllers/front/payment_controller.go +++ b/app/http/controllers/front/payment_controller.go @@ -3,6 +3,8 @@ package front import ( "PaymentCenter/app/constants/common" "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/http/controllers" + "PaymentCenter/app/http/entities/front" "PaymentCenter/app/models/paychannelmodel" "PaymentCenter/app/services" "PaymentCenter/app/third/paymentService" @@ -190,22 +192,22 @@ func BrokerWechatUrl(c *gin.Context) { } } -//// 首页 -//func Ind/*ex(c *gin.Context) { -// c.HTML(200, "index.html", gin.H{}) -//} +// 首页 +func Index(c *gin.Context) { + c.HTML(200, "index.html", gin.H{}) +} -//// 获取微信授权链接 -//func GetWxAuthUrl(c *gin.Context) { -// req, _ := controllers.GetRequest(c).(*front.GetWxAuthUrlRequest) -// -// url, code := services.GetWxAuthUrl(*req) -// controllers.HandCodeRes(c, url, code) -//} -// -//// 通过code获取授权,openid -//func GetWxAuth(c *gin.Context) { -// //req, _ := controllers.GetRequest(c).(*front.GetWxAuthRequest) -// //openId, code := services.GetWxAuth(*req) -// //controllers.HandCodeRes(c, openId, code) -//}*/ +// 获取微信授权链接 +func GetWxAuthUrl(c *gin.Context) { + req, _ := controllers.GetRequest(c).(*front.GetWxAuthUrlRequest) + + url, code := services.GetWxAuthUrl(*req) + controllers.HandCodeRes(c, url, code) +} + +// 通过code获取授权,openid +func GetWxAuth(c *gin.Context) { + req, _ := controllers.GetRequest(c).(*front.GetWxAuthRequest) + code := services.GetWxAuth(*req) + controllers.HandCodeRes(c, code, code) +} diff --git a/app/http/routes/route.go b/app/http/routes/route.go index a2d5b66..4ef94fa 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -69,12 +69,12 @@ func RegisterRoute(router *gin.Engine) { // 微信获取授权相关 router.LoadHTMLGlob("./front/templates/*") - //wx := v1.Group("/wx", middlewares.ValidateRequest()) - //{ - // wx.GET("/index", front.Index) // 获取页面 - // wx.POST("/getWxAuthUrl", front.GetWxAuthUrl) // 获取授权code - // wx.GET("/getWxAuth", front.GetWxAuth) // 获取openId - //} + wx := v1.Group("/wx", middlewares.ValidateRequest()) + { + wx.GET("/index", front.Index) // 获取页面 + wx.POST("/getWxAuthUrl", front.GetWxAuthUrl) // 获取授权code + wx.GET("/getWxAuth", front.GetWxAuth) // 获取openId + } router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) diff --git a/app/services/api_request_valid.go b/app/services/api_request_valid.go index 6f528a3..ea74ee4 100644 --- a/app/services/api_request_valid.go +++ b/app/services/api_request_valid.go @@ -64,7 +64,7 @@ func (a *AppCheck) Crypt() (cryptFunc func(app *appmodel.App) apicrypt.ApiCrypt) func (a *AppCheck) ReCheckAfterDecrypt(data []byte, requestData *front.RequestBody) bool { var requestCommonData front.ApiCommonBody if err := sonic.Unmarshal(data, &requestCommonData); err != nil { - a.Code = errorcode.ParamError + a.Code = errorcode.PayEncryptParamFail return false } if requestCommonData.AppId != requestData.AppId || requestCommonData.Timestamp != requestData.Timestamp { diff --git a/app/services/wecaht.go b/app/services/wecaht.go index 9f1ffe6..b37246f 100644 --- a/app/services/wecaht.go +++ b/app/services/wecaht.go @@ -2,11 +2,14 @@ package services import ( "PaymentCenter/app/constants/common" + "PaymentCenter/app/constants/errorcode" "PaymentCenter/app/http/entities/front" "PaymentCenter/app/models/paychannelmodel" - "PaymentCenter/app/third/paymentService" + "PaymentCenter/app/utils" + "PaymentCenter/app/utils/httpclient" "PaymentCenter/config" "encoding/json" + "fmt" "net/url" "strconv" ) @@ -47,7 +50,7 @@ func GetWxAuthUrl(param front.GetWxAuthUrlRequest) (targetUrl string, code int) return } -// 通过code换取网页授权access_token +// // 通过code换取网页授权access_token func GetWxAuth(param front.GetWxAuthRequest) (code int) { // 获取支付渠道的配置 @@ -62,19 +65,35 @@ func GetWxAuth(param front.GetWxAuthRequest) (code int) { return } - // 配置解析 - wxConfig := paymentService.WxPay{} + //// 配置解析 + wxConfig := make(map[string]interface{}) err = json.Unmarshal([]byte(payChannel.ExtJson), &wxConfig) if err != nil { code = handErr(err) return } + sk := wxConfig["secret"].(string) + if sk == "" { + code = errorcode.PayChannelConfigNotFound + return + } - //targetUrl := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", - // payChannel.AppId, - // wxConfig.SerialNo, - // param.Code, - //) + targetUrl := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", + payChannel.AppId, + sk, + param.Code, + ) + + header := map[string]string{ + "Content-Type": "application/json", + } + body := map[string]string{} + response, err := httpclient.FastHttpGet(targetUrl, header, body, 0) + if err != nil { + code = handErr(err) + return + } + utils.Log(nil, "获取微信授权信息", string(response), targetUrl) return } diff --git a/app/utils/httpclient/fasthttp.go b/app/utils/httpclient/fasthttp.go index f79b3f9..3ffd264 100644 --- a/app/utils/httpclient/fasthttp.go +++ b/app/utils/httpclient/fasthttp.go @@ -88,7 +88,7 @@ func FastHttpGet(url string, header map[string]string, body map[string]string, t resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) // 用完需要释放资源 var err error - if timeout == 0 { + if timeout <= 0 { if err = fasthttp.Do(req, resp); err != nil { utils.Log(nil, "http请求失败", err, url) return nil, err