275 lines
6.6 KiB
Go
275 lines
6.6 KiB
Go
package mainservice
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"f5"
|
|
"io"
|
|
"main/constant"
|
|
"main/model"
|
|
"main/mt"
|
|
"main/service"
|
|
"q5"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type MainServiceApi struct {
|
|
}
|
|
|
|
func (this *MainServiceApi) RefreshToken(c *gin.Context) {
|
|
reqtime := q5.SafeToInt64(c.Query("time"))
|
|
reqsign := c.Query("sign")
|
|
nowTime := f5.GetApp().GetRealSeconds()
|
|
if reqtime > nowTime+30 || reqtime+30 < nowTime {
|
|
f5.RspErr(c, 400, "request error")
|
|
return
|
|
}
|
|
|
|
sign := service.Wxpay.GenSHA256Signature(c.Query("time")+constant.GLOBAL_SALT, constant.GLOBAL_SALT)
|
|
if reqsign != sign {
|
|
f5.RspErr(c, 400, "request error 2")
|
|
return
|
|
}
|
|
|
|
rspObj := struct {
|
|
Data []service.TokenInfo `json:"data"`
|
|
ErrCode int64 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
}{}
|
|
|
|
service.Wxpay.GetAccessTokenList(&rspObj.Data)
|
|
c.JSON(200, rspObj)
|
|
}
|
|
|
|
func (this *MainServiceApi) WxTNotify(c *gin.Context) {
|
|
f5.GetSysLog().Debug("wx test notify:%s", c.Request.URL.RawQuery)
|
|
|
|
signature := c.Query("signature")
|
|
timestamp := c.Query("timestamp")
|
|
nonce := c.Query("nonce")
|
|
echostr := c.Query("echostr")
|
|
strs := []string{mt.Table.Config.GetWxNotifyToken(), timestamp, nonce}
|
|
sort.Strings(strs)
|
|
sb := strings.Builder{}
|
|
sb.WriteString(strs[0])
|
|
sb.WriteString(strs[1])
|
|
sb.WriteString(strs[2])
|
|
m := sha1.New()
|
|
io.WriteString(m, sb.String())
|
|
sign := string(hex.EncodeToString(m.Sum(nil)))
|
|
|
|
f5.GetSysLog().Debug("wx sign:%s, %s", sign, signature)
|
|
|
|
if sign != signature {
|
|
c.String(200, "wrong")
|
|
return
|
|
}
|
|
c.String(200, echostr)
|
|
}
|
|
|
|
func (this *MainServiceApi) WxNotifyPurchase(c *gin.Context) {
|
|
f5.GetSysLog().Debug("wx notify purchase:%s", c.Request.URL.RawQuery)
|
|
|
|
signature := c.Query("signature")
|
|
timestamp := c.Query("timestamp")
|
|
nonce := c.Query("nonce")
|
|
|
|
if len(signature) > 0 || len(timestamp) > 0 || len(nonce) > 0 {
|
|
strs := []string{mt.Table.Config.GetWxNotifyToken(), timestamp, nonce}
|
|
sort.Strings(strs)
|
|
sb := strings.Builder{}
|
|
sb.WriteString(strs[0])
|
|
sb.WriteString(strs[1])
|
|
sb.WriteString(strs[2])
|
|
m := sha1.New()
|
|
io.WriteString(m, sb.String())
|
|
sign := string(hex.EncodeToString(m.Sum(nil)))
|
|
|
|
f5.GetSysLog().Debug("wx sign:%s, %s", sign, signature)
|
|
if sign != signature {
|
|
c.String(200, "wrong")
|
|
return
|
|
}
|
|
c.String(200, c.Query("echostr"))
|
|
return
|
|
}
|
|
|
|
rspObj := struct {
|
|
ErrorCode int32 `json:"ErrCode"`
|
|
ErrMsg string `json:"ErrMsg"`
|
|
}{
|
|
ErrorCode: 99999,
|
|
ErrMsg: "internal error",
|
|
}
|
|
|
|
msg_signature := c.Query("msg_signature")
|
|
if msg_signature != "" {
|
|
postObj := struct {
|
|
Encrypt string `json:"Encrypt"`
|
|
ToUserName string `json:"ToUserName"`
|
|
}{}
|
|
|
|
if err := c.ShouldBindJSON(&postObj); err != nil {
|
|
rspObj.ErrorCode = 401
|
|
rspObj.ErrMsg = "post data error"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
smsg, appid := service.Wxpay.DecryptMsg(msg_signature, timestamp, nonce, postObj.Encrypt)
|
|
if smsg == nil || appid == nil || len(smsg) == 0 || len(appid) == 0 {
|
|
rspObj.ErrorCode = 402
|
|
rspObj.ErrMsg = "decrypt data error"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
f5.GetSysLog().Debug("wx decrypt msg:%s", smsg)
|
|
|
|
wxnotifyobj := service.WxPurchaseNotify{}
|
|
if json.Unmarshal(smsg, &wxnotifyobj) != nil {
|
|
rspObj.ErrorCode = 403
|
|
rspObj.ErrMsg = "unmarshal data error"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
gameid := int64(0)
|
|
appkey := ""
|
|
notifyurl := ""
|
|
mt.Table.Wxconfig.Traverse(func(w *mt.Wxconfig) bool {
|
|
if w.GetAppid() == string(appid) {
|
|
gameid = w.GetGameid()
|
|
appkey = w.GetAppkey()
|
|
notifyurl = w.GetNotifyurl()
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
|
|
if appkey == "" {
|
|
f5.GetSysLog().Error("wx app config error:%s", appid)
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
oristr := wxnotifyobj.Event + "&" + wxnotifyobj.MiniGame.Payload
|
|
sig := service.Wxpay.GenSHA256Signature(oristr, appkey)
|
|
if sig != wxnotifyobj.MiniGame.PayEventSig {
|
|
f5.GetSysLog().Error("pay event sig error:%s, %s, %s", appid, sig, wxnotifyobj.MiniGame.PayEventSig)
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
payloadobj := new(service.WxPayload)
|
|
if json.Unmarshal([]byte(wxnotifyobj.MiniGame.Payload), &payloadobj) != nil {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
envpass := true
|
|
if f5.IsOnlineEnv() {
|
|
if payloadobj.Env != 0 {
|
|
f5.GetSysLog().Error("notify test info to prod url")
|
|
envpass = false
|
|
}
|
|
} else {
|
|
if payloadobj.Env != 1 {
|
|
f5.GetSysLog().Error("notify prod info to test url")
|
|
envpass = false
|
|
}
|
|
}
|
|
|
|
if !envpass {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
orderModel := new(model.InAppOrder)
|
|
if err, found := orderModel.FindByOrderId(payloadobj.OutTradeNo); err != nil {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
} else if !found {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
if orderModel.Status > 1 {
|
|
rspObj.ErrorCode = 0
|
|
rspObj.ErrMsg = "Success"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
rediskey := "ls:accountid:" + orderModel.AccountId
|
|
str, err := service.Redis.Get(constant.LOGIN_REDIS, rediskey)
|
|
if err != nil {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
data := map[string]interface{}{}
|
|
if json.Unmarshal([]byte(str), &data) != nil {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
openid := q5.SafeToString(data["openid"])
|
|
if openid != payloadobj.OpenId {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
|
|
orderModel.GameId = int32(gameid)
|
|
f5.GetSysLog().Debug("notify url:%s, %s", appid, notifyurl)
|
|
|
|
nowtimestr := q5.SafeToString(f5.GetApp().GetRealSeconds())
|
|
originstr := "account_id=" + orderModel.AccountId
|
|
originstr += "&goodsid=" + string(orderModel.ItemId)
|
|
originstr += "&orderid=" + orderModel.OrderId
|
|
originstr += "&amount=" + q5.SafeToString(payloadobj.GoodsInfo.ActualPrice)
|
|
originstr += ":" + nowtimestr + constant.NOFITY_GAMESERVER_SALT
|
|
params := map[string]string{
|
|
"c": "Recharge",
|
|
"a": "purchaseNotify",
|
|
"account_id": orderModel.AccountId,
|
|
"orderid": orderModel.OrderId,
|
|
"timestamp": nowtimestr,
|
|
"goodsid": string(orderModel.ItemId),
|
|
"amount": q5.SafeToString(payloadobj.GoodsInfo.ActualPrice),
|
|
"sign": q5.Md5Str(originstr),
|
|
}
|
|
f5.GetHttpCliMgr().SendGoStyleRequest(
|
|
notifyurl,
|
|
params,
|
|
func(hcr f5.HttpCliResponse) {
|
|
if hcr.GetErr() != nil {
|
|
return
|
|
}
|
|
|
|
rspObj := struct {
|
|
ErrCode int64 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
}{}
|
|
|
|
f5.GetSysLog().Debug("get game rsp:%s", hcr.GetRawData())
|
|
if json.Unmarshal([]byte(hcr.GetRawData()), &rspObj) != nil {
|
|
return
|
|
}
|
|
|
|
if rspObj.ErrCode == 0 {
|
|
orderModel.Status = 2
|
|
orderModel.UpdateFields([]string{"status"})
|
|
rspObj.ErrCode = 0
|
|
rspObj.ErrMsg = "Success"
|
|
}
|
|
})
|
|
}
|
|
|
|
c.JSON(200, rspObj)
|
|
}
|