黄梓健 5 years ago
parent
commit
2890eb8c92
  1. 9
      controllers/client/auction.go
  2. 5
      controllers/client/login.go
  3. 5
      controllers/client/reward.go
  4. 1
      controllers/client/sign.go
  5. 29
      controllers/common/wechat_oauth.go
  6. 8
      controllers/pc/sign.go
  7. 1
      go.mod
  8. 48
      log/hdzj.log
  9. 42
      logger/logger.go
  10. 3
      models/reward_history.go
  11. 4
      models/sign_history.go
  12. 2
      services/pay/handle.go
  13. 16
      services/pay/order.go
  14. 2
      test/config_test.go
  15. 14
      test/order_test.go

9
controllers/client/auction.go

@ -19,6 +19,7 @@ func (t *AuctionCtl) Auction() {
auctionId := t.MustGetInt64("auction_activity_id")
totalMoney := t.MustGetDouble("total_money")
uid := t.MustGetUID()
areaId := t.MustGetInt64("area_id")
if totalMoney < 0 {
t.ERROR("金额不能为0", code.MSG_ERR)
@ -42,7 +43,7 @@ func (t *AuctionCtl) Auction() {
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
area := new(models.AreaStore)
exist, err = models.Get(area, user.AreaId)
exist, err = models.Get(area, areaId)
t.CheckErr(err)
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
@ -57,7 +58,7 @@ func (t *AuctionCtl) Auction() {
history.ActivityId = activity.Id
history.RehearsalId = activity.RehearsalId
history.UserId = uid
history.AreaId = user.AreaId
history.AreaId = area.Id
history.UserName = user.Nickname
history.AreaName = area.Name
history.UserPhone = user.Phone
@ -112,8 +113,8 @@ func (t *AuctionCtl) Auction() {
record.UserId = uid
record.UserPhone = user.Phone
record.UserName = user.Nickname
record.AreaId = user.AreaId
record.AreaName = user.AreaName
record.AreaId = area.Id
record.AreaName = area.Name
record.DealPrice = totalMoney
record.RehearsalId = activity.RehearsalId
record.PlayerCode = player.Code

5
controllers/client/login.go

@ -132,9 +132,6 @@ func (t *UserCtl) WxLogin() {
signIn = "已签到"
}
//realSignExist, err := new(models.RealSignHistory).Check(user.Id, activity.Id, activity.RehearsalId)
//t.CheckErr(err)
jwtToken, err := jwt.GenJwtToken(define.TYPE_H5USER, user.Id, customer.Id, customer.Pid, area.Id, activityId)
t.CheckErr(err)
t.SetSession(define.TOKEN, jwtToken)
@ -154,8 +151,6 @@ func (t *UserCtl) WxLogin() {
func (t *UserCtl) Login() {
wxcode := t.MustGet("code")
// _type, _ := t.Get("type")
logger.Error("错误code:", wxcode)
user := new(models.User)
token, err := wechat.GetToken(wxcode)
t.CheckErr(err)

5
controllers/client/reward.go

@ -54,10 +54,11 @@ func (t *RewardCtl) Reward() {
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
var res = make(map[string]interface{}, 0)
var expireAt = time.Now().Add(24 * time.Hour).Unix()
res["out_trade_no"] = ""
if activity.RehearsalId == 0 || _type == 1 { // 直播不用彩排
res, err = pay_service.UnifiedOrder("欧轩互动-打赏支付", user.Openid, int64(amount*100), 2,
user.Id, activityId, time.Now().Add(1*time.Hour).Unix())
user.Id, activityId, expireAt)
t.CheckErr(err)
}
@ -72,7 +73,7 @@ func (t *RewardCtl) Reward() {
RehearsalId: activity.RehearsalId,
Status: -1,
ReviewTime: 0,
ExpireTime: time.Now().Add(24 * time.Hour).Unix(),
ExpireTime: expireAt,
Type: _type,
}

1
controllers/client/sign.go

@ -144,6 +144,7 @@ func (t *SignCtl) Sign() {
signHistory.ActivityId = activityId
signHistory.SignRuleId = signUp.Id
signHistory.AreaId = areaId
signHistory.Status = 2
signHistory.IsDelete = false
signHistory.UpdatedAt = time.Now()
signHistory.CreatedAt = time.Now()

29
controllers/common/wechat_oauth.go

@ -69,30 +69,36 @@ type CallbackParam struct {
ReturnMsg CDATA `xml:"return_msg"`
}
var counter = new(atomic.Int64)
var orderSuccessCounter = new(atomic.Int64)
var orderFailedCounter = new(atomic.Int64)
var refundSuccessCounter = new(atomic.Int64)
var refundFailedCounter = new(atomic.Int64)
func (t *WeChatOauthCtl) CallbackOrder() {
counter.Add(1)
//搜索支付的order表, 查找到某条记录
_, err := pay_service.NotifyOrder(t.Request.BODY)
param := new(CallbackParam)
if err != nil {
logger.Error(err)
orderFailedCounter.Add(1)
logger.Error(fmt.Sprintf("call order失败微信响应次数: %v", orderFailedCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_FAIL}
param.ReturnMsg = CDATA{Text: err.Error()}
} else {
orderSuccessCounter.Add(1)
logger.Error(fmt.Sprintf("call order成功微信响应次数: %v", orderSuccessCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_SUCCESS}
param.ReturnMsg = CDATA{Text: "OK"}
}
xmlRes, _ := xml.Marshal(param)
if t.Request.OriginRequest.Method == "GET" {
t.JSON(map[string]interface{}{
"counter": counter.Load(),
"time": time.Now(),
"xml": param,
"time": time.Now(),
"xml": param,
})
return
}
logger.Error(fmt.Sprintf("微信响应次数: %v", counter.Load()))
t.XML(xmlRes)
}
@ -100,21 +106,22 @@ func (t *WeChatOauthCtl) CallbackRefund() {
_, err := pay_service.NotifyRefund(t.Request.BODY)
param := new(CallbackParam)
if err != nil {
logger.Error("", err)
refundFailedCounter.Add(1)
logger.Error(fmt.Sprintf("call refund失败微信响应次数: %v", refundFailedCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_FAIL}
param.ReturnMsg = CDATA{Text: err.Error()}
} else {
refundSuccessCounter.Add(1)
logger.Error(fmt.Sprintf("call refund成功微信响应次数: %v", refundSuccessCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_SUCCESS}
param.ReturnMsg = CDATA{Text: "OK"}
}
xmlRes, _ := xml.Marshal(param)
if t.Request.OriginRequest.Method == "GET" {
t.JSON(map[string]interface{}{
"counter": counter.Load(),
"time": time.Now(),
"xml": param,
"time": time.Now(),
"xml": param,
})
}
logger.Error(fmt.Sprintf("微信响应次数: %v", counter.Load()))
t.XML(xmlRes)
}

8
controllers/pc/sign.go

@ -99,7 +99,7 @@ func (t *SignCtl) SignInfo() {
func (t *SignCtl) RealSignInfo() {
aid := t.MustGetInt64("activity_id")
rid := t.MustGetInt64("rehearsal_id")
results, err := models.GetSignHistories(aid, rid)
results, err := models.GetApplyRealSigns(aid, rid)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"result": results,
@ -109,13 +109,9 @@ func (t *SignCtl) RealSignInfo() {
func (t *SignCtl) RealSignVerify() {
ids := strings.Split(t.MustGet("real_sign_history_ids"), ",")
idList := make([]interface{}, len(ids))
for _, id := range ids {
idList = append(idList, id)
}
history := new(models.SignHistory)
history.Status = 2
err := history.UpdateById(idList, "status")
err := history.UpdateById(ids, "status")
t.CheckErr(err)
t.SUCCESS("审核成功")
}

1
go.mod

@ -14,6 +14,7 @@ require (
github.com/go-chi/chi v4.1.0+incompatible
github.com/go-errors/errors v1.0.2 // indirect
github.com/go-redis/redis v6.15.7+incompatible // indirect
github.com/go-stack/stack v1.8.0
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/websocket v1.4.2
github.com/iGoogle-ink/gopay/v2 v2.0.5

48
log/hdzj.log

@ -392,3 +392,51 @@
2020-06-02 09:29:38.051 ERROR logger/logger.go:87 定时任务错误原因dial tcp: lookup ouxuanhudongapi.t.3pr.com.cn: no such host
2020-06-02 10:25:13.946 ERROR logger/logger.go:87 获取过期打赏错误原因Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
2020-06-02 10:25:13.946 ERROR logger/logger.go:87 定时任务错误原因Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
2020-06-02 11:17:39.778 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.778 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.832 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.835 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.835 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.836 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.836 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.837 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.837 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.838 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.838 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.839 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.841 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.841 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.846 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.846 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.846 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.846 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.846 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.846 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.937 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.946 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.985 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:39.989 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.193 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.193 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.235 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.236 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.238 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.238 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.242 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.242 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.242 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.242 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.243 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.243 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.243 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.243 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.243 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.243 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.246 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.246 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.246 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.246 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.362 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.365 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.410 ERROR logger/logger.go:87 客户订单异常
2020-06-02 11:17:47.413 ERROR logger/logger.go:87 客户订单异常

42
logger/logger.go

@ -1,10 +1,12 @@
package logger
import (
"github.com/go-stack/stack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"path/filepath"
"time"
)
@ -12,21 +14,37 @@ import (
var logger *zap.SugaredLogger
func init() {
InitZap()
SetLevel("debug")
}
func InitZap() {
// @func SetLevel 设置日志等级
// @param level 日志等级从高大底: fatal panic dpanic error warn info debug
func SetLevel(level string) {
wd, _ := os.Getwd()
SetLogger(filepath.Join(wd, "log", "hdzj.log"), level, 1024, 10, 7)
}
// @func SetLogger 日志定制化, 包括日志切割的定制化
// @param filename 存放日志的文件, 最好带上路径
// @param level 日志写入的等级
// @param size 单个日志文件的大小
// @param backups 日志备份的数量
// @param age 日志存放的时间
func SetLogger(filename, level string, size, backups, age int) {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "log/hdzj.log",
MaxSize: 50, // megabytes
MaxBackups: 3,
MaxAge: 7, // days
Filename: filename,
MaxSize: size, // megabytes
MaxBackups: backups,
MaxAge: age, // days
})
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(NewEncoderConfig()),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), w),
zap.DebugLevel)
Level(level))
logger = zap.New(core, zap.AddCaller()).Sugar()
}
func NewEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
// Keys can be anything except the empty string.
@ -40,15 +58,19 @@ func NewEncoderConfig() zapcore.EncoderConfig {
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeCaller: func(caller zapcore.EntryCaller, encoder zapcore.PrimitiveArrayEncoder) {
frame := stack.Caller(7).Frame()
caller.Line = frame.Line
caller.File = frame.File
encoder.AppendString(caller.TrimmedPath())
},
}
}
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
func level(level string) zapcore.Level {
func Level(level string) zapcore.Level {
switch level {
case "info":
return zap.InfoLevel

3
models/reward_history.go

@ -64,7 +64,8 @@ func GetRewardHistoryByIds(ids interface{}) ([]*RewardHistory, error) {
func GetExpireRewardHistory() ([]*RewardHistory, error) {
result := make([]*RewardHistory, 0)
err := core.GetXormAuto().Where("is_delete=0 and status=0 and expire_time<=?", time.Now().Unix()).Find(&result)
err := core.GetXormAuto().Where("rehearsal_id = 0 or type <> 0").
Where("is_delete=0 and status=0 and expire_time<=?", time.Now().Unix()).Find(&result)
return result, err
}

4
models/sign_history.go

@ -30,7 +30,7 @@ func (t *SignHistory) TableName() string {
}
func (t *SignHistory) GetByUserId(aid, uid, rid, arid int64) (bool, error) {
return core.GetXormAuto().Where("is_delete=0 and activity_id=? and user_id=? and "+
return core.GetXormAuto().Where("is_delete=0 and status=2 and activity_id=? and user_id=? and "+
"rehearsal_id=? and area_id=?", aid, uid, rid, arid).Get(t)
}
@ -53,7 +53,7 @@ func (t *SignHistory) Insert() error {
return err
}
func GetSignHistories(aid interface{}, rid interface{}) ([]*SignHistory, error) {
func GetApplyRealSigns(aid interface{}, rid interface{}) ([]*SignHistory, error) {
results := make([]*SignHistory, 0)
err := core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=? and status=1",
aid, rid).Asc("updated_at").Find(&results)

2
services/pay/handle.go

@ -14,7 +14,7 @@ import (
)
func init() {
go loopOrder()
//go loopOrder()
go utils.HandleTicker(1*time.Hour, HandleReward) // 打赏24小时退款
}

16
services/pay/order.go

@ -32,7 +32,7 @@ func UnifiedOrder(content, openId string, fee, goodType, userId, activityId, exp
body["nonce_str"] = nonceStr
body["sign_type"] = pay_core.SignType_MD5
body["openid"] = openId
// body["time_expire"] = pay_core.FormatTime(time.Unix(expireAt, 0))
//body["time_expire"] = pay_core.FormatTime(time.Unix(expireAt, 0))
resp, err := pay.UnifiedOrder(client, body)
if err != nil {
@ -106,6 +106,8 @@ func ReOrder(outTradeNo string) (map[string]interface{}, error) {
// Notify
type NotifyOrderParam struct {
XMLName struct{} `xml:"xml" json:"-"`
ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"`
ReturnMsg string `xml:"return_msg,omitempty" json:"return_msg,omitempty"`
ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"`
@ -136,26 +138,23 @@ func NotifyOrder(body string) (*models.UserOrder, error) {
err := xml.Unmarshal([]byte(body), &res)
if err != nil {
err = fmt.Errorf("xml unmarsal error:%+v", err)
logger.Error(err)
return nil, err
}
if res.ReturnCode != define.CODE_SUCCESS {
err = fmt.Errorf("network error, retrun_code: %+v and return_msg: %+v", res.ReturnCode, res.ReturnMsg)
logger.Error(err)
return nil, err
}
if res.ResultCode != define.CODE_SUCCESS {
err = fmt.Errorf("trade error, result_code: %+v and err_code: %+v and err_desc: %+v",
res.ResultCode, res.ErrCode, res.ErrCodeDes)
logger.Error(err)
return nil, err
}
order := new(models.UserOrder)
exist, err := order.GetByOutTradeNo(res.OutTradeNo)
if err == nil || !exist {
if err != nil || !exist {
err = fmt.Errorf("user order get by out_trade_no: %+v, error: %+v, exist: %+v", res.OutTradeNo, err, exist)
return nil, err
}
@ -176,11 +175,6 @@ func NotifyOrder(body string) (*models.UserOrder, error) {
}
type OrderQueryResult struct {
Order *models.UserOrder
Query *pay.OrderQueryResponse
}
func OrderQuery(outTradeNo string) (map[string]string, error) {
client, err := Client()
if err != nil {
@ -278,6 +272,8 @@ func QueryRefund(outTradeNo string) (*pay.RefundQueryResponse, error) {
}
type NotifyRefundParam struct {
XMLName struct{} `xml:"xml" json:"-"`
ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"`
ReturnMsg string `xml:"return_msg,omitempty" json:"return_msg,omitempty"`
ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"`

2
test/config_test.go

@ -196,7 +196,7 @@ func TestDuration(t *testing.T) {
//}
func TestRefundRedPack(t *testing.T) {
res, err := pay_service.Refund("退款", "f8nYoKC5Gk0O5t68AzU3boo2eJfETKRf")
res, err := pay_service.Refund("退款", "9dAV4ROBNLZgAHzrRmF55uCKsGvNGkeE")
fmt.Printf("%+v, %v", res, err)
//res, err := pay_service.QueryRedPack("tDYW8edlzegSlVNaJMXsteZEeuVL")
//fmt.Printf("%+v, %+v", res, err)

14
test/order_test.go

@ -2,6 +2,7 @@ package test
import (
"fmt"
"hudongzhuanjia/logger"
pay_service "hudongzhuanjia/services/pay"
"testing"
"time"
@ -17,7 +18,8 @@ func TestUnifiedOrder(t *testing.T) {
}
func TestRefund(t *testing.T) {
res, err := pay_service.Refund("测试退款", "ZxzwZv6B0A0JmiDsKMUvFCDTupu4v3kZ")
//res, err := pay_service.Refund("测试退款", "ZxzwZv6B0A0JmiDsKMUvFCDTupu4v3kZ")
res, err := pay_service.Refund("测试退款", "9dAV4ROBNLZgAHzrRmF55uCKsGvNGkeE")
if err != nil {
t.Error(err)
}
@ -25,7 +27,15 @@ func TestRefund(t *testing.T) {
}
func TestApi(t *testing.T) {
res, err := pay_service.SendRedPack("欧轩测试", "o9XM41l_c27wRuc33hutRVwsQO4U",
res, err := pay_service.SendRedPack("欧轩测试", "9dAV4ROBNLZgAHzrRmF55uCKsGvNGkeE",
"恭喜发财", "抢红包活动", "越抢越多", 1, 1, 1)
fmt.Println(res, err)
}
func TestHandleReward(t *testing.T) {
pay_service.HandleReward()
}
func TestLoggerLine(t *testing.T) {
logger.Error("test")
}
Loading…
Cancel
Save