From 46ea96b2de6186efa77c6eb333b3a3292891f131 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Tue, 11 Jul 2023 10:21:51 +0800 Subject: [PATCH] test --- app/db/db_master.go | 33 ++ app/db/db_master_amount.go | 62 +++ app/db/db_master_amount_list.go | 36 ++ app/db/db_master_withdrawal_flow.go | 108 +++++ app/db/db_playlet_sale_order.go | 122 +++++- app/db/model/master.go | 20 + app/db/model/master_amount.go | 16 + app/db/model/master_amount_flow.go | 18 + app/db/model/master_list_cfg.go | 8 +- app/db/model/master_withdrawal_flow.go | 24 ++ app/db/model/playlet_sale_order.go | 12 +- app/db/model/sys_cfg.go | 6 +- app/e/code.go | 2 + app/hdl/hdl_token.go | 86 ++++ .../hdl_playlet_order.go | 0 app/hdl/zhimeng_platform/hdl_order_list.go | 13 + app/hdl/zhimeng_platform/hdl_withdrawal.go | 25 ++ app/lib/auth/base.go | 10 +- app/md/md_app_redis_key.go | 7 + app/md/md_playlet.go | 4 + app/mw/mw_auth_jwt.go | 51 +++ app/mw/mw_request_cache.go | 61 +++ app/router/router.go | 26 +- app/svc/platform/svc_order_list.go | 139 +++++++ app/svc/platform/svc_withdrawal.go | 372 ++++++++++++++++++ app/task/svc/svc_playlet_adv_order.go | 24 +- app/task/svc/svc_playlet_video_order.go | 8 +- app/utils/auth.go | 48 ++- app/utils/xls.go | 36 ++ cmd_db.bat | 2 +- etc/cfg.yml | 19 +- go.mod | 1 + 32 files changed, 1356 insertions(+), 43 deletions(-) create mode 100644 app/db/db_master.go create mode 100644 app/db/db_master_amount.go create mode 100644 app/db/db_master_amount_list.go create mode 100644 app/db/db_master_withdrawal_flow.go create mode 100644 app/db/model/master.go create mode 100644 app/db/model/master_amount.go create mode 100644 app/db/model/master_amount_flow.go create mode 100644 app/db/model/master_withdrawal_flow.go create mode 100644 app/hdl/hdl_token.go rename app/hdl/{ => zhimeng_internal}/hdl_playlet_order.go (100%) create mode 100644 app/hdl/zhimeng_platform/hdl_order_list.go create mode 100644 app/hdl/zhimeng_platform/hdl_withdrawal.go create mode 100644 app/md/md_playlet.go create mode 100644 app/mw/mw_auth_jwt.go create mode 100644 app/mw/mw_request_cache.go create mode 100644 app/svc/platform/svc_order_list.go create mode 100644 app/svc/platform/svc_withdrawal.go create mode 100644 app/utils/xls.go diff --git a/app/db/db_master.go b/app/db/db_master.go new file mode 100644 index 0000000..9cf3a45 --- /dev/null +++ b/app/db/db_master.go @@ -0,0 +1,33 @@ +package db + +import ( + "applet/app/db/model" + "time" + "xorm.io/xorm" +) + +type MasterDb struct { + Db *xorm.Engine `json:"db"` +} + +func (masterDb *MasterDb) Set() { // set方法 + masterDb.Db = ZhimengDb +} + +func (masterDb *MasterDb) GetMaster(mid string) *model.Master { + var data model.Master + get, err := masterDb.Db.Where("master_id=?", mid).Get(&data) + if get == false || err != nil { + data = model.Master{MasterId: mid, CreateTime: time.Now(), UpdateTime: time.Now(), FormWay: "api"} + masterDb.Db.InsertOne(&data) + } + return &data +} + +func (masterDb *MasterDb) MasterUpdate(data *model.Master) bool { + get, err := masterDb.Db.Where("id=?", data.Id).Update(data) + if get == 0 || err != nil { + return false + } + return true +} diff --git a/app/db/db_master_amount.go b/app/db/db_master_amount.go new file mode 100644 index 0000000..ed2882e --- /dev/null +++ b/app/db/db_master_amount.go @@ -0,0 +1,62 @@ +package db + +import ( + "applet/app/db/model" + "time" + "xorm.io/xorm" +) + +type MasterAmountDb struct { + Db *xorm.Engine `json:"db"` +} + +func (masterAmountDb *MasterAmountDb) Set() { // set方法 + masterAmountDb.Db = ZhimengDb +} + +func (masterAmountDb *MasterAmountDb) GetMasterAmount(id, types string) *model.MasterAmount { + var data model.MasterAmount + get, err := masterAmountDb.Db.Where("uid=? and type=?", id, types).Get(&data) + if get == false || err != nil { + data = model.MasterAmount{Uid: id, Type: types, UpdateTime: time.Now()} + masterAmountDb.Db.InsertOne(&data) + } + return &data +} +func (masterAmountDb *MasterAmountDb) GetMasterAmountByListId(id string) *model.MasterAmount { + var data model.MasterAmount + get, err := masterAmountDb.Db.Where("id=?", id).Get(&data) + if get == false || err != nil { + return nil + } + return &data +} +func (masterAmountDb *MasterAmountDb) MasterAmountUpdate(id int, data *model.MasterAmount) bool { + get, err := masterAmountDb.Db.Where("id=?", id).Update(data) + if get == 0 || err != nil { + return false + } + return true +} +func (masterAmountDb *MasterAmountDb) MasterAmountInsert(data *model.MasterAmount) bool { + get, err := masterAmountDb.Db.InsertOne(data) + if get == 0 || err != nil { + return false + } + return true +} +func GetMasterAmountByListIdWithSess(sess *xorm.Session, id string) *model.MasterAmount { + var data model.MasterAmount + get, err := sess.Where("id=?", id).Get(&data) + if get == false || err != nil { + return nil + } + return &data +} +func MasterAmountUpdateWithSess(sess *xorm.Session, id int, data *model.MasterAmount) bool { + get, err := sess.Where("id=?", id).Update(data) + if get == 0 || err != nil { + return false + } + return true +} diff --git a/app/db/db_master_amount_list.go b/app/db/db_master_amount_list.go new file mode 100644 index 0000000..7a807b7 --- /dev/null +++ b/app/db/db_master_amount_list.go @@ -0,0 +1,36 @@ +package db + +import ( + "applet/app/db/model" + "xorm.io/xorm" +) + +type MasterAmountFlowDb struct { + Db *xorm.Engine `json:"db"` +} + +func (masterAmountFlowDb *MasterAmountFlowDb) Set() { // set方法 + masterAmountFlowDb.Db = ZhimengDb +} + +func (masterAmountFlowDb *MasterAmountFlowDb) MasterAmountListUpdate(id int, data *model.MasterAmountFlow) bool { + get, err := masterAmountFlowDb.Db.Where("id=?", id).Update(data) + if get == 0 || err != nil { + return false + } + return true +} +func (masterAmountFlowDb *MasterAmountFlowDb) MasterAmountFlowInsert(data *model.MasterAmountFlow) bool { + get, err := masterAmountFlowDb.Db.InsertOne(data) + if get == 0 || err != nil { + return false + } + return true +} +func MasterAmountFlowInsertWithSess(sess *xorm.Session, data *model.MasterAmountFlow) bool { + get, err := sess.InsertOne(data) + if get == 0 || err != nil { + return false + } + return true +} diff --git a/app/db/db_master_withdrawal_flow.go b/app/db/db_master_withdrawal_flow.go new file mode 100644 index 0000000..252aed5 --- /dev/null +++ b/app/db/db_master_withdrawal_flow.go @@ -0,0 +1,108 @@ +package db + +import ( + "applet/app/db/model" + "applet/app/utils" + "xorm.io/xorm" +) + +type MasterWithdrawalFlowDb struct { + Db *xorm.Engine `json:"db"` +} + +func (masterWithdrawalFlowDb *MasterWithdrawalFlowDb) Set() { // set方法 + masterWithdrawalFlowDb.Db = ZhimengDb +} + +func (masterWithdrawalFlowDb *MasterWithdrawalFlowDb) GetWithdrawalFlowListWithTotal(id string, args map[string]string) (*[]model.MasterWithdrawalFlow, int64) { + var data []model.MasterWithdrawalFlow + sess := masterWithdrawalFlowDb.Db.Where("uid=?", id) + if args["start_time"] != "" { + sess = sess.And("time>=?", args["start_time"]) + } + if args["end_time"] != "" { + sess = sess.And("time<=?", args["end_time"]) + } + if args["check_start_time"] != "" { + sess = sess.And("check_time>=?", args["check_start_time"]) + } + if args["check_end_time"] != "" { + sess = sess.And("check_time<=?", args["check_end_time"]) + } + if args["status"] != "" { + sess = sess.And("status=?", args["status"]) + } + if args["alipay"] != "" { + sess = sess.And("alipay LIKE ?", "%"+args["alipay"]+"%") + } + p := utils.StrToInt(args["p"]) + size := utils.StrToInt(args["size"]) + offet := (p - 1) * size + total, err := sess.Limit(size, offet).OrderBy("time desc,id desc").FindAndCount(&data) + if err != nil { + return nil, total + } + return &data, total +} +func (masterWithdrawalFlowDb *MasterWithdrawalFlowDb) GetWithdrawalFlowList(id string, args map[string]string) *[]model.MasterWithdrawalFlow { + var data []model.MasterWithdrawalFlow + sess := masterWithdrawalFlowDb.Db.Where("uid=?", id) + if args["start_time"] != "" { + sess = sess.And("time>=?", args["start_time"]) + } + if args["end_time"] != "" { + sess = sess.And("time<=?", args["end_time"]) + } + if args["check_start_time"] != "" { + sess = sess.And("check_time>=?", args["check_start_time"]) + } + if args["check_end_time"] != "" { + sess = sess.And("check_time<=?", args["check_end_time"]) + } + if args["status"] != "" { + sess = sess.And("status=?", args["status"]) + } + if args["alipay"] != "" { + sess = sess.And("alipay LIKE ?", "%"+args["alipay"]+"%") + } + p := utils.StrToInt(args["p"]) + size := utils.StrToInt(args["size"]) + offet := (p - 1) * size + err := sess.Limit(size, offet).OrderBy("time desc,id desc").Find(&data) + if err != nil { + return nil + } + return &data +} + +func (masterWithdrawalFlowDb *MasterWithdrawalFlowDb) MasterWithdrawalFlowInsert(data *model.MasterWithdrawalFlow) bool { + get, err := masterWithdrawalFlowDb.Db.InsertOne(data) + if get == 0 || err != nil { + return false + } + return true +} + +func (masterWithdrawalFlowDb *MasterWithdrawalFlowDb) MasterWithdrawalFlowInsertUpdate(data *model.MasterWithdrawalFlow) bool { + get, err := masterWithdrawalFlowDb.Db.Where("id=?", data.Id).Update(data) + if get == 0 || err != nil { + return false + } + return true +} +func MasterWithdrawalFlowInsertWithSess(sess *xorm.Session, data *model.MasterWithdrawalFlow) bool { + get, err := sess.InsertOne(data) + if get == 0 || err != nil { + return false + } + return true +} +func (masterWithdrawalFlowDb *MasterWithdrawalFlowDb) MasterWithdrawalFlowById(id string) *model.MasterWithdrawalFlow { + var data model.MasterWithdrawalFlow + get, err := masterWithdrawalFlowDb.Db.Where("id=?", id).Get(&data) + if get == false || err != nil { + return nil + } + return &data + +} diff --git a/app/db/db_playlet_sale_order.go b/app/db/db_playlet_sale_order.go index f999eec..f369fcb 100644 --- a/app/db/db_playlet_sale_order.go +++ b/app/db/db_playlet_sale_order.go @@ -3,6 +3,7 @@ package db import ( "applet/app/db/model" "applet/app/utils" + "time" "xorm.io/xorm" ) @@ -38,7 +39,19 @@ func (playletSaleOrderDb *PlayletSaleOrderDb) PlayletVideoOrderInsert(data *mode } return true } - +func (playletSaleOrderDb *PlayletSaleOrderDb) PlayletVideoOrderSum(mid, status, timeType string) float64 { + sess := playletSaleOrderDb.Db.Where("uid=?", mid) + if status != "" { + sess = sess.And("status=?", status) + } + if timeType != "" { + timeRange := utils.GetTimeRange(timeType) + sess = sess.And("create_time>=?", time.Unix(timeRange["start"], 0).Format("2006-01-02 15:04:05")) + sess = sess.And("create_time=?", args["start_time"]) } if args["end_time"] != "" { sess = sess.And("update_time>=?", args["end_time"]) } + sess = commWhere(sess, args) + sort := "update_time desc,id desc" + if args["sort"] != "" { + sort = args["sort"] + } + sess.Limit(size, offet).OrderBy(sort).Find(&data) + return data +} +func (playletSaleOrderDb *PlayletSaleOrderDb) GetPlayletVideoOrderListAndTotal(args map[string]string) (*[]model.PlayletSaleOrder, int64) { + /*** + p 页数 + size 个数 + start_time 开始时间 + end_time 结束时间 + ord_type 订单类型 + video_type 视频类型 + status 订单状态 + settle_status 结算状态 + oid 订单号 + sort 排序 + */ + var data = make([]model.PlayletSaleOrder, 0) + size := utils.StrToInt(args["size"]) + offet := (utils.StrToInt(args["p"]) - 1) * size + sess := playletSaleOrderDb.Db.Where("1=1") + if args["start_time"] != "" { + sess = sess.And("create_time>=?", args["start_time"]) + } + if args["end_time"] != "" { + sess = sess.And("create_time>=?", args["end_time"]) + } + sess = commWhere(sess, args) + sort := "update_time desc,id desc" + if args["sort"] != "" { + sort = args["sort"] + } + count, err := sess.Limit(size, offet).OrderBy(sort).FindAndCount(&data) + if count == 0 || err != nil { + return nil, count + } + return &data, count +} +func (playletSaleOrderDb *PlayletSaleOrderDb) GetPlayletVideoOrderListWithCreateTime(args map[string]string) *[]model.PlayletSaleOrder { + /*** + p 页数 + size 个数 + start_time 开始时间 + end_time 结束时间 + ord_type 订单类型 + video_type 视频类型 + status 订单状态 + settle_status 结算状态 + oid 订单号 + sort 排序 + */ + var data = make([]model.PlayletSaleOrder, 0) + size := utils.StrToInt(args["size"]) + offet := (utils.StrToInt(args["p"]) - 1) * size + sess := playletSaleOrderDb.Db.Where("1=1") + if args["start_time"] != "" { + sess = sess.And("create_time>=?", args["start_time"]) + } + if args["end_time"] != "" { + sess = sess.And("create_time>=?", args["end_time"]) + } + sess = commWhere(sess, args) + sort := "update_time desc,id desc" + if args["sort"] != "" { + sort = args["sort"] + } + err := sess.Limit(size, offet).OrderBy(sort).Find(&data) + if err != nil { + return nil + } + return &data +} +func commWhere(sess *xorm.Session, args map[string]string) *xorm.Session { + if args["mid"] != "" { + sess = sess.And("uid=?", args["mid"]) + } + + if args["oid"] != "" { + sess = sess.And("oid LIKE ? or custom_oid LIKE ?", "%"+args["oid"]+"%", "%"+args["oid"]+"%") + } + if args["sub_uid"] != "" { + sess = sess.And("sub_uid=?", args["sub_uid"]) + } if args["ord_type"] != "" { sess = sess.And("ord_type=?", args["ord_type"]) } if args["video_type"] != "" { sess = sess.And("video_type=?", args["video_type"]) } - sess.Limit(size, offet).OrderBy("update_time desc,id desc").Find(&data) - return data + if args["status"] != "" { + sess = sess.And("status=?", args["status"]) + } + if args["settle_status"] != "" { + if args["settle_status"] == "未结算" { + sess = sess.And("settle_at=?", "0") + } else { + sess = sess.And("settle_at>?", "0") + } + } + return sess } diff --git a/app/db/model/master.go b/app/db/model/master.go new file mode 100644 index 0000000..be56c3c --- /dev/null +++ b/app/db/model/master.go @@ -0,0 +1,20 @@ +package model + +import ( + "time" +) + +type Master struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + MasterId string `json:"master_id" xorm:"VARCHAR(255)"` + Phone string `json:"phone" xorm:"VARCHAR(255)"` + Password string `json:"password" xorm:"VARCHAR(255)"` + CreateTime time.Time `json:"create_time" xorm:"DATETIME"` + IsAgent int `json:"is_agent" xorm:"comment('是否代理') INT(1)"` + FormWay string `json:"form_way" xorm:"VARCHAR(255)"` + ZyId string `json:"zy_id" xorm:"VARCHAR(255)"` + MasterName string `json:"master_name" xorm:"VARCHAR(255)"` + UpdateTime time.Time `json:"update_time" xorm:"DATETIME"` + Alipay string `json:"alipay" xorm:"VARCHAR(255)"` + AlipayName string `json:"alipay_name" xorm:"VARCHAR(255)"` +} diff --git a/app/db/model/master_amount.go b/app/db/model/master_amount.go new file mode 100644 index 0000000..9a276c8 --- /dev/null +++ b/app/db/model/master_amount.go @@ -0,0 +1,16 @@ +package model + +import ( + "time" +) + +type MasterAmount struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Uid string `json:"uid" xorm:"comment('master表的id') unique VARCHAR(255)"` + Amount string `json:"amount" xorm:"default 0.00 DECIMAL(20,2)"` + Type string `json:"type" xorm:"VARCHAR(255)"` + UpdateTime time.Time `json:"update_time" xorm:"DATETIME"` + LastMonthAmount string `json:"last_month_amount" xorm:"DECIMAL(20,2)"` + MonthAmount string `json:"month_amount" xorm:"DECIMAL(20,2)"` + ChangeTime time.Time `json:"change_time" xorm:"comment('标记本月是否把预估结算佣金转换') DATETIME"` +} diff --git a/app/db/model/master_amount_flow.go b/app/db/model/master_amount_flow.go new file mode 100644 index 0000000..e45486a --- /dev/null +++ b/app/db/model/master_amount_flow.go @@ -0,0 +1,18 @@ +package model + +import ( + "time" +) + +type MasterAmountFlow struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Uid string `json:"uid" xorm:"VARCHAR(255)"` + Time time.Time `json:"time" xorm:"DATETIME"` + BeforeAmount string `json:"before_amount" xorm:"default 0.00 DECIMAL(20,2)"` + Amount string `json:"amount" xorm:"default 0.00 DECIMAL(20,2)"` + AfterAmount string `json:"after_amount" xorm:"default 0.00 DECIMAL(20,2)"` + Platform string `json:"platform" xorm:"VARCHAR(255)"` + Oid string `json:"oid" xorm:"VARCHAR(255)"` + Title string `json:"title" xorm:"VARCHAR(255)"` + FlowType string `json:"flow_type" xorm:"VARCHAR(255)"` +} diff --git a/app/db/model/master_list_cfg.go b/app/db/model/master_list_cfg.go index 8511817..34d4f7e 100644 --- a/app/db/model/master_list_cfg.go +++ b/app/db/model/master_list_cfg.go @@ -1,8 +1,8 @@ package model type MasterListCfg struct { - K string `json:"k" xorm:"not null pk comment('键') VARCHAR(127)"` - V string `json:"v" xorm:"comment('值') TEXT"` - Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"` - Uid string `json:"uid" xorm:"not null default '' comment('') VARCHAR(255)"` + K string `json:"k" xorm:"not null pk VARCHAR(255)"` + V string `json:"v" xorm:"TEXT"` + Memo string `json:"memo" xorm:"VARCHAR(255)"` + Uid string `json:"uid" xorm:"comment('0是官方') VARCHAR(255)"` } diff --git a/app/db/model/master_withdrawal_flow.go b/app/db/model/master_withdrawal_flow.go new file mode 100644 index 0000000..1ec5aab --- /dev/null +++ b/app/db/model/master_withdrawal_flow.go @@ -0,0 +1,24 @@ +package model + +import ( + "time" +) + +type MasterWithdrawalFlow struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Uid string `json:"uid" xorm:"VARCHAR(255)"` + Time time.Time `json:"time" xorm:"DATETIME"` + UpdateTime time.Time `json:"update_time" xorm:"DATETIME"` + Remark string `json:"remark" xorm:"VARCHAR(255)"` + Alipay string `json:"alipay" xorm:"VARCHAR(255)"` + AlipayName string `json:"alipay_name" xorm:"VARCHAR(255)"` + Amount string `json:"amount" xorm:"default 0.00 DECIMAL(20,2)"` + Fee string `json:"fee" xorm:"default 0.00 DECIMAL(20,2)"` + Reason string `json:"reason" xorm:"VARCHAR(255)"` + Status string `json:"status" xorm:"VARCHAR(255)"` + CheckTime time.Time `json:"check_time" xorm:"DATETIME"` + Img string `json:"img" xorm:"VARCHAR(500)"` + RealAmount string `json:"real_amount" xorm:"default 0.00 DECIMAL(20,2)"` + HasInvoice int `json:"has_invoice" xorm:"default 0 comment('是否有发票') INT(1)"` + InvoiceBili string `json:"invoice_bili" xorm:"comment('发票比例') VARCHAR(255)"` +} diff --git a/app/db/model/playlet_sale_order.go b/app/db/model/playlet_sale_order.go index 8ecd581..8a950dc 100644 --- a/app/db/model/playlet_sale_order.go +++ b/app/db/model/playlet_sale_order.go @@ -6,19 +6,15 @@ import ( type PlayletSaleOrder struct { Id int `json:"id" xorm:"not null pk autoincr INT(11)"` - Uid string `json:"uid" xorm:"default '' comment('用户id') VARCHAR(255)"` - SubUid int `json:"sub_uid" xorm:"default 0 comment('') INT(11)"` + Uid string `json:"uid" xorm:"default '0' comment('用户id') VARCHAR(255)"` Data string `json:"data" xorm:"comment('第三方返回数据') VARCHAR(5000)"` - Oid string `json:"oid" xorm:"comment('订单号') VARCHAR(255)"` + Oid string `json:"oid" xorm:"comment('订单号') unique VARCHAR(255)"` Amount string `json:"amount" xorm:"default 0.00 comment('金额') DECIMAL(20,2)"` Commission string `json:"commission" xorm:"default 0.00 comment('佣金') DECIMAL(20,2)"` - PlatformFee string `json:"platform_fee" xorm:"default 0.00 comment('佣金') DECIMAL(20,2)"` - Fee string `json:"fee" xorm:"default 0.00 comment('佣金') DECIMAL(20,2)"` Status string `json:"status" xorm:"comment('状态') VARCHAR(255)"` CreateTime time.Time `json:"create_time" xorm:"comment('下单时间') DATETIME"` RefundTime time.Time `json:"refund_time" xorm:"comment('退款时间') DATETIME"` UpdateTime time.Time `json:"update_time" xorm:"comment('广告统计更新时间') DATETIME"` - PlatformSettleTime time.Time `json:"platform_settle_time" xorm:"comment('广告统计更新时间') DATETIME"` Title string `json:"title" xorm:"comment('标题') VARCHAR(255)"` VideoType string `json:"video_type" xorm:"comment('视频类型') VARCHAR(255)"` PlatformType string `json:"platform_type" xorm:"comment('平台类型') VARCHAR(255)"` @@ -27,6 +23,10 @@ type PlayletSaleOrder struct { OrdType string `json:"ord_type" xorm:"VARCHAR(255)"` TimesNum string `json:"times_num" xorm:"VARCHAR(255)"` PeopleNum string `json:"people_num" xorm:"VARCHAR(255)"` + SubUid int `json:"sub_uid" xorm:"default 0 INT(11)"` + PlatformSettleTime time.Time `json:"platform_settle_time" xorm:"DATETIME"` CustomOid string `json:"custom_oid" xorm:"VARCHAR(255)"` ExtendUid string `json:"extend_uid" xorm:"VARCHAR(255)"` + Fee string `json:"fee" xorm:"default 0.00 DECIMAL(20,2)"` + PlatformFee string `json:"platform_fee" xorm:"default 0.00 DECIMAL(20,2)"` } diff --git a/app/db/model/sys_cfg.go b/app/db/model/sys_cfg.go index 5508c9a..5159ef3 100644 --- a/app/db/model/sys_cfg.go +++ b/app/db/model/sys_cfg.go @@ -1,7 +1,7 @@ package model type SysCfg struct { - K string `json:"k" xorm:"not null pk comment('键') VARCHAR(127)"` - V string `json:"v" xorm:"comment('值') TEXT"` - Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"` + K string `json:"k" xorm:"VARCHAR(255)"` + V string `json:"v" xorm:"TEXT"` + Memo string `json:"memo" xorm:"VARCHAR(255)"` } diff --git a/app/e/code.go b/app/e/code.go index cc8be46..3c29156 100644 --- a/app/e/code.go +++ b/app/e/code.go @@ -19,6 +19,7 @@ const ( ERR_ADMIN_API = 400012 ERR_QINIUAPI_RESPONSE = 400013 ERR_URL_TURNCHAIN = 400014 + ERR_TOKEN_EXPIRE = 401010 // 401 未授权 ERR_UNAUTHORIZED = 401000 @@ -124,6 +125,7 @@ var MsgFlags = map[int]string{ // 200 ERR_FILE_SAVE: "文件保存失败", // 400 + ERR_TOKEN_EXPIRE: "Token过期", ERR_BAD_REQUEST: "请求失败", ERR_INVALID_ARGS: "请求参数错误", ERR_API_RESPONSE: "API错误", diff --git a/app/hdl/hdl_token.go b/app/hdl/hdl_token.go new file mode 100644 index 0000000..145ac98 --- /dev/null +++ b/app/hdl/hdl_token.go @@ -0,0 +1,86 @@ +package hdl + +import ( + "applet/app/e" + "applet/app/md" + "applet/app/utils" + "applet/app/utils/cache" + "errors" + "time" + + "github.com/gin-gonic/gin" +) + +func GetAuthorizationCode(c *gin.Context) { + masterId := c.Query("master_id") + cacheKey := md.ZhimengAuthorizationCodeCacheKey + authorizationCode := utils.RandString(32) + _, err := cache.HSet(cacheKey, authorizationCode, masterId) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + //设置过期日期 + _, err = cache.Expire(cacheKey, md.ZhimengAuthorizationCodeCacheTime*30) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, map[string]interface{}{ + "authorization_code": authorizationCode, + }, nil) +} + +func GetToken(c *gin.Context) { + authorizationCode := c.Query("authorization_code") + cacheKey := md.ZhimengAuthorizationCodeCacheKey + get, err := cache.HGetString(cacheKey, authorizationCode) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + if get == "" { + e.OutErr(c, e.ERR, errors.New("授权码有误")) + return + } + token, refreshToken, err := utils.GenToken(get) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, map[string]interface{}{ + "token": token, + "refresh_token": refreshToken, + "master_id": get, + }, nil) +} + +func RefreshToken(c *gin.Context) { + var args struct { + RefreshToken string `json:"refresh_token"` + } + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + //1、验证refresh_token正确性 + //验证token字符串 + claim, err := utils.ParseRToken(args.RefreshToken) + if err != nil { + e.OutErr(c, e.ERR_UNAUTHORIZED, errors.New("refresh_token 验证失败")) + return + } + //过期判断 + if time.Now().Unix() > claim.ExpiresAt { + e.OutErr(c, e.ERR_TOKEN_EXPIRE, errors.New("refresh_token 过期已失效")) + return + } + + //2、生成新的token && refresh_token + token, refreshToken, err := utils.GenToken(claim.MasterId) + e.OutSuc(c, map[string]interface{}{ + "token": token, + "refresh_token": refreshToken, + "master_id": claim.MasterId, + }, nil) +} diff --git a/app/hdl/hdl_playlet_order.go b/app/hdl/zhimeng_internal/hdl_playlet_order.go similarity index 100% rename from app/hdl/hdl_playlet_order.go rename to app/hdl/zhimeng_internal/hdl_playlet_order.go diff --git a/app/hdl/zhimeng_platform/hdl_order_list.go b/app/hdl/zhimeng_platform/hdl_order_list.go new file mode 100644 index 0000000..e3a4413 --- /dev/null +++ b/app/hdl/zhimeng_platform/hdl_order_list.go @@ -0,0 +1,13 @@ +package zhimeng_platform + +import ( + "applet/app/svc/platform" + "github.com/gin-gonic/gin" +) + +func OrderList(c *gin.Context) { + platform.OrderList(c) +} +func OrderOutput(c *gin.Context) { + platform.OrderOutput(c) +} diff --git a/app/hdl/zhimeng_platform/hdl_withdrawal.go b/app/hdl/zhimeng_platform/hdl_withdrawal.go new file mode 100644 index 0000000..fdca9b1 --- /dev/null +++ b/app/hdl/zhimeng_platform/hdl_withdrawal.go @@ -0,0 +1,25 @@ +package zhimeng_platform + +import ( + "applet/app/svc/platform" + "github.com/gin-gonic/gin" +) + +func WithdrawalIncome(c *gin.Context) { + platform.WithdrawalIncome(c) +} +func WithdrawalList(c *gin.Context) { + platform.WithdrawalList(c) +} +func WithdrawalDoing(c *gin.Context) { + platform.WithdrawalDoing(c) +} +func WithdrawalOutput(c *gin.Context) { + platform.WithdrawalOutput(c) +} +func WithdrawalInvoiceImg(c *gin.Context) { + platform.WithdrawalInvoiceImg(c) +} +func WithdrawalBindAlipay(c *gin.Context) { + platform.WithdrawalBindAlipay(c) +} diff --git a/app/lib/auth/base.go b/app/lib/auth/base.go index d771802..b622fc1 100644 --- a/app/lib/auth/base.go +++ b/app/lib/auth/base.go @@ -8,8 +8,10 @@ import ( // TokenExpireDuration is jwt 过期时间 const TokenExpireDuration = time.Hour * 4380 +const RefreshTokenExpireDuration = time.Hour * 6 -var Secret = []byte("zyos") +var Secret = []byte("zyos_zhimeng") +var RSecret = []byte("zyos_zhimeng_refresh_token") // JWTUser 如果想要保存更多信息,都可以添加到这个结构体中 type JWTUser struct { @@ -17,3 +19,9 @@ type JWTUser struct { Username string `json:"username"` jwt.StandardClaims } + +// JWTMaster 如果想要保存更多信息,都可以添加到这个结构体中 +type JWTMaster struct { + MasterId string `json:"master_id"` + jwt.StandardClaims +} diff --git a/app/md/md_app_redis_key.go b/app/md/md_app_redis_key.go index 4f93079..592dca4 100644 --- a/app/md/md_app_redis_key.go +++ b/app/md/md_app_redis_key.go @@ -7,4 +7,11 @@ const ( CfgCacheTime = 86400 AppCfgCacheKey = "one_item_one_code:%s" // 占位符: key的第一个字母 WxOfficialAccountCacheKey = "wx_official_account" // 占位符: key的第一个字母 + + ZhimengIsUseCacheKey = "zhimeng_is_use_cache" //授权码 + ZhimengAuthorizationCodeCacheKey = "zhimeng_authorization_code_cache" //授权码 + ZhimengAuthorizationCodeCacheTime = 60 + + ZhimengDataRequestCacheKey = "%s:zhimeng_data_request_cache:%s:%s" //请求缓存(masterId:big_data_screen_data_request_cache:uri:md5(body参数)) + ) diff --git a/app/md/md_playlet.go b/app/md/md_playlet.go new file mode 100644 index 0000000..c5af739 --- /dev/null +++ b/app/md/md_playlet.go @@ -0,0 +1,4 @@ +package md + +var OrdTypeMap = map[string]string{"video": "短剧", "adv": "广告"} +var VideoTypeMap = map[string]string{"channel": "视频号", "douyin": "抖音", "kuaishou": "快手"} diff --git a/app/mw/mw_auth_jwt.go b/app/mw/mw_auth_jwt.go new file mode 100644 index 0000000..a6b2f3c --- /dev/null +++ b/app/mw/mw_auth_jwt.go @@ -0,0 +1,51 @@ +package mw + +import ( + "applet/app/e" + "applet/app/utils" + "errors" + "github.com/dgrijalva/jwt-go" + "strings" + "time" + + "github.com/gin-gonic/gin" +) + +// AuthJWT is jwt middleware +func AuthJWT(c *gin.Context) { + + //获取请求头中的Authorization + authHeader := c.Request.Header.Get("Authorization") + if authHeader == "" { + e.OutErr(c, e.ERR_UNAUTHORIZED, errors.New("token 不能为空")) + return + } + + //拆分Authorization字段获取token字符串 + parts := strings.SplitN(authHeader, " ", 2) + if !(len(parts) == 2 && parts[0] == "Bearer") { + e.OutErr(c, e.ERR_TOKEN_FORMAT, errors.New("token 格式不对")) + return + } + + //验证token字符串 + claim, err := utils.ParseToken(parts[1]) + if err != nil { + v, _ := err.(*jwt.ValidationError) + if v.Errors == jwt.ValidationErrorExpired { + e.OutErr(c, e.ERR_TOKEN_EXPIRE, errors.New("token 过期已失效")) + return + } + e.OutErr(c, e.ERR_UNAUTHORIZED, errors.New("token 验证失败")) + return + } + + //过期判断 + if time.Now().Unix() > claim.ExpiresAt { + e.OutErr(c, e.ERR_TOKEN_EXPIRE, errors.New("token 过期已失效")) + return + } + //设置上下文信息 + c.Set("master_id", claim.MasterId) + c.Next() +} diff --git a/app/mw/mw_request_cache.go b/app/mw/mw_request_cache.go new file mode 100644 index 0000000..bb7261c --- /dev/null +++ b/app/mw/mw_request_cache.go @@ -0,0 +1,61 @@ +package mw + +import ( + "applet/app/e" + "applet/app/md" + "applet/app/utils" + "applet/app/utils/cache" + "bytes" + "fmt" + "github.com/gin-gonic/gin" + "io/ioutil" +) + +//自己实现一个type gin.ResponseWriter interface +type responseWriter struct { + gin.ResponseWriter + b *bytes.Buffer +} + +//重写Write([]byte) (int, error) +func (w responseWriter) Write(b []byte) (int, error) { + //向一个bytes.buffer中再写一份数据 + w.b.Write(b) + //完成gin.Context.Writer.Write()原有功能 + return w.ResponseWriter.Write(b) +} + +// RequestCache is cache middleware +func RequestCache(c *gin.Context) { + tempMasterId, _ := c.Get("master_id") + masterId := tempMasterId.(string) + uri := c.Request.RequestURI + md5Params := dealBodyParams(c) + cacheKey := fmt.Sprintf(md.ZhimengDataRequestCacheKey, masterId, uri, md5Params) + //自己实现一个type gin.ResponseWriter + writer := responseWriter{ + c.Writer, + bytes.NewBuffer([]byte{}), + } + isUse, _ := cache.GetInt(md.ZhimengIsUseCacheKey) + var res = map[string]interface{}{} + cache.GetJson(cacheKey, &res) + if res["data"] != nil && isUse == 1 { + e.OutSuc(c, res["data"], nil) + return + } + c.Writer = writer + c.Next() +} + +func dealBodyParams(c *gin.Context) (md5Params string) { + body, err := c.GetRawData() + if err != nil { + panic(err) + } + fmt.Println(">>>>>>>>>>string<<<<<<<<<<<", string(body)) + md5Params = utils.Md5(string(body)) + //TODO::把读过的字节流重新放到body + c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + return +} diff --git a/app/router/router.go b/app/router/router.go index 66e1a51..94a9fcd 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -3,6 +3,9 @@ package router import ( "applet/app/cfg" "applet/app/hdl" + internalHdl "applet/app/hdl/zhimeng_internal" + platformHdl "applet/app/hdl/zhimeng_platform" + "applet/app/mw" "github.com/gin-gonic/gin" ) @@ -37,14 +40,15 @@ func Init() *gin.Engine { c.JSON(405, gin.H{"code": 405, "msg": "method not allowed", "data": []struct{}{}}) }) r.Use(mw.Cors) - routeInternal(r.Group("/api/v1/internal")) + routeInternal(r.Group("/api/internal")) + routeZhimeng(r.Group("/api/admin")) return r } func routeInternal(r *gin.RouterGroup) { r.Use(mw.DB) // 以下接口需要用到数据库 { - r.POST("/playlet_order", hdl.GetPlayletOrder) + r.POST("/playlet_order", internalHdl.GetPlayletOrder) } r.Use(mw.Checker) // 以下接口需要检查Header: platform @@ -56,3 +60,21 @@ func routeInternal(r *gin.RouterGroup) { } } +func routeZhimeng(r *gin.RouterGroup) { + r.GET("/authorizationCode", hdl.GetAuthorizationCode) + r.GET("/token", hdl.GetToken) + r.POST("/rToken", hdl.RefreshToken) + + r.Use(mw.AuthJWT, mw.RequestCache) // 以下接口需要JWT验证 + { + r.POST("/order_list", platformHdl.OrderList) + r.POST("/order_output", platformHdl.OrderOutput) + r.POST("/withdrawal_income", platformHdl.WithdrawalIncome) + r.POST("/withdrawal_bind_alipay", platformHdl.WithdrawalBindAlipay) + r.POST("/withdrawal_list", platformHdl.WithdrawalList) + r.POST("/withdrawal_doing", platformHdl.WithdrawalDoing) + r.POST("/withdrawal_output", platformHdl.WithdrawalOutput) + r.POST("/withdrawal_invoice_img", platformHdl.WithdrawalInvoiceImg) + + } +} diff --git a/app/svc/platform/svc_order_list.go b/app/svc/platform/svc_order_list.go new file mode 100644 index 0000000..0cb21a9 --- /dev/null +++ b/app/svc/platform/svc_order_list.go @@ -0,0 +1,139 @@ +package platform + +import ( + "applet/app/db" + "applet/app/e" + "applet/app/md" + "applet/app/utils" + "github.com/gin-gonic/gin" + "time" +) + +func OrderList(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + masterId, _ := c.Get("master_id") + playletSaleOrderDb := db.PlayletSaleOrderDb{} + playletSaleOrderDb.Set() + args["mid"] = utils.AnyToString(masterId) + args["sort"] = "create_time desc,id desc" + list, total := playletSaleOrderDb.GetPlayletVideoOrderListAndTotal(args) + var data = make([]map[string]string, 0) + + if list != nil { + for _, v := range *list { + source := md.OrdTypeMap[v.OrdType] + md.VideoTypeMap[v.VideoType] + settleStatus := "未结算" + settleTime := "-" + if v.SettleTime > 0 { + settleStatus = "已结算" + settleTime = time.Unix(int64(v.SettleTime), 0).Format("2006-01-02 15:04:05") + } + var tmp = map[string]string{ + "oid": v.Oid, + "mid": v.Uid, + "custom_oid": v.CustomOid, + "title": v.Title, + "source": source, + "amount": v.Amount, + "platform_fee": v.PlatformFee, + "commission": v.Commission, + "status": v.Status, + "settle_status": settleStatus, + "create_time": v.CreateTime.Format("2006-01-02 15:04:05"), + "settle_time": settleTime, + "commission_bili": "-", + } + if utils.StrToFloat64(v.Commission) > 0 { + tmp["commission_bili"] = utils.Float64ToStr(utils.StrToFloat64(v.Commission)/utils.StrToFloat64(v.Amount)*100) + "%" + } + data = append(data, tmp) + } + } + sourceList := []map[string]string{ + {"name": "短剧", "value": "video"}, + {"name": "广告", "value": "adv"}, + } + videoList := []map[string]string{ + {"name": "抖音", "value": "douyin"}, + {"name": "快手", "value": "kuaishou"}, + {"name": "视频号", "value": "channel"}, + } + statusList := []map[string]string{ + {"name": "订单付款", "value": "订单付款"}, + {"name": "订单结算", "value": "订单结算"}, + } + settleStatusList := []map[string]string{ + {"name": "未结算", "value": "未结算"}, + {"name": "已结算", "value": "已结算"}, + } + var res = map[string]interface{}{ + "list": data, + "total": total, + "source_list": sourceList, + "video_ist": videoList, + "status_list": statusList, + "settle_status_list": settleStatusList, + } + e.OutSuc(c, res, nil) + return +} + +func OrderOutput(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + masterId, _ := c.Get("master_id") + playletSaleOrderDb := db.PlayletSaleOrderDb{} + playletSaleOrderDb.Set() + args["mid"] = utils.AnyToString(masterId) + args["size"] = "3000" + if args["ids"] != "" { + args["size"] = "0" + } + name := "订单_" + args["p"] + //写入数据 + data := map[string]string{ + "A1": "订单号", + "B1": "会员ID", + "C1": "标题", + "D1": "来源", + "E1": "结算时间", + "F1": "订单状态", + "G1": "金额", + "H1": "佣金", + } + list := playletSaleOrderDb.GetPlayletVideoOrderListWithCreateTime(args) + if list != nil { + for k, v := range *list { + source := md.OrdTypeMap[v.OrdType] + md.VideoTypeMap[v.PlatformType] + settleTime := "-" + if v.SettleTime > 0 { + settleTime = time.Unix(int64(v.SettleTime), 0).Format("2006-01-02 15:04:05") + } + i := utils.IntToStr(k + 2) + data["A"+i] = v.Oid + data["B"+i] = utils.IntToStr(v.SubUid) + data["C"+i] = v.Title + data["D"+i] = source + data["E"+i] = settleTime + data["F"+i] = v.Status + data["G"+i] = v.Amount + data["H"+i] = v.Commission + } + } + + file := utils.Output(c, name, data) + filename := name + ".xlsx" + r := map[string]string{ + "file": file, + "filename": filename, + } + e.OutSuc(c, r, nil) + return +} diff --git a/app/svc/platform/svc_withdrawal.go b/app/svc/platform/svc_withdrawal.go new file mode 100644 index 0000000..a3b015c --- /dev/null +++ b/app/svc/platform/svc_withdrawal.go @@ -0,0 +1,372 @@ +package platform + +import ( + "applet/app/db" + "applet/app/db/model" + "applet/app/e" + "applet/app/utils" + "applet/app/utils/cache" + "encoding/json" + "fmt" + "github.com/gin-gonic/gin" + "time" +) + +func WithdrawalIncome(c *gin.Context) { + args, mid, err := commArg(c) + if err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + fmt.Println(args) + fmt.Println(mid) + if args["type"] == "" { + args["type"] = "playlet" + } + amountMap := masterAmount(mid, args["type"]) + monthAmountMap := masterMonthAmount(mid) + isNeedBingAlipay := "1" + if amountMap["alipay"] != "" { + isNeedBingAlipay = "0" + } + var res = []map[string]string{ + {"name": "账户余额", "value": amountMap["amount"], "type": "amount", "tip": "", "alipay": amountMap["alipay"], "alipay_name": amountMap["alipay_name"], "is_need_bing_alipay": isNeedBingAlipay, "is_show_withdrawal": "1"}, + {"name": "上月预估收益", "value": monthAmountMap["last_month_amount"], "type": "last_month_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"}, + {"name": "上月预估结算收益", "value": amountMap["last_month_settle_amount"], "type": "last_month_settle_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"}, + {"name": "本月预估收益", "value": monthAmountMap["month_amount"], "type": "month_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"}, + {"name": "本月预估结算收益", "value": monthAmountMap["month_settle_amount"], "type": "month_settle_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"}, + } + e.OutSuc(c, res, nil) + return +} +func WithdrawalList(c *gin.Context) { + args, mid, err := commArg(c) + if err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + amountMap := masterAmount(mid, args["type"]) + masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{} + masterWithdrawalFlowDb.Set() + list, total := masterWithdrawalFlowDb.GetWithdrawalFlowListWithTotal(amountMap["id"], args) + data := make([]map[string]string, 0) + if list != nil { + for _, v := range *list { + var tmp = map[string]string{ + "id": utils.IntToStr(v.Id), + "alipay": v.Alipay, + "alipay_name": v.AlipayName, + "amount": v.Amount, + "real_amount": v.RealAmount, + "fee": v.Fee, + "time": v.Time.Format("2006-01-02 15:04:05"), + "check_time": "", + "status": v.Status, + "remark": v.Remark, + "reason": v.Reason, + "img": v.Img, + "is_need_upload_invoice": utils.IntToStr(v.HasInvoice), + } + if v.Img != "" { + tmp["is_need_upload_invoice"] = "0" + } + if v.CheckTime.IsZero() == false { + tmp["check_time"] = v.CheckTime.Format("2006-01-02 15:04:05") + } + data = append(data, tmp) + } + } + statusList := []map[string]string{ + {"name": "提现审核", "value": "提现审核"}, + {"name": "提现通过", "value": "提现通过"}, + {"name": "提现失败", "value": "提现失败"}, + } + var res = map[string]interface{}{ + "list": data, "total": total, "status_list": statusList, + } + e.OutSuc(c, res, nil) + return +} +func WithdrawalDoing(c *gin.Context) { + args, mid, err := commArg(c) + if err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + if utils.StrToFloat64(args["amount"]) <= 0 { + e.OutErr(c, 400, e.NewErr(400, "金额不正确")) + return + } + amountMap := masterAmount(mid, args["type"]) + leaveAmount := utils.StrToFloat64(amountMap["amount"]) - utils.StrToFloat64(args["amount"]) + if leaveAmount < 0 { + e.OutErr(c, 400, e.NewErr(400, "余额不足")) + return + } + masterListCfgDb := db.MasterListCfgDb{} + masterListCfgDb.Set() + withdrawalBili := masterListCfgDb.MasterListCfgGetOneData("0", "withdrawal_bili") + invoiceBili := masterListCfgDb.MasterListCfgGetOneData("0", "invoice_bili") + var fee float64 = 0 + if utils.StrToFloat64(withdrawalBili) > 0 { + bili := utils.StrToFloat64(withdrawalBili) / 100 + var invoiceBiliMap = make([]string, 0) + json.Unmarshal([]byte(invoiceBili), &invoiceBiliMap) + if utils.InArr(args["invoice_bili"], invoiceBiliMap) == false && utils.StrToInt(args["has_invoice"]) == 1 { + e.OutErr(c, 400, e.NewErr(400, "发票税率不正确")) + return + } + //开了发票的话再扣掉对应的发票比例 + if utils.InArr(args["invoice_bili"], invoiceBiliMap) && utils.StrToInt(args["has_invoice"]) == 1 { + bili -= utils.StrToFloat64(args["invoice_bili"]) / 100 + } + fee = utils.StrToFloat64(args["amount"]) * bili + } + realAmount := utils.StrToFloat64(args["amount"]) - fee + if amountMap["alipay"] == "" { + e.OutErr(c, 400, e.NewErr(400, "未绑定支付宝")) + return + } + + mutexKey := fmt.Sprintf("withdrawal:%s", amountMap["id"]) + withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 30, "NX") + if err != nil { + e.OutErr(c, e.ERR, err) + return + } + if withdrawAvailable != "OK" { + e.OutErr(c, e.ERR, e.NewErr(400000, "操作过于频繁,请稍后再试")) + return + } + sess := db.ZhimengDb.NewSession() + err = sess.Begin() + if err != nil { + sess.Rollback() + e.OutErr(c, 400, e.NewErr(400000, "请重试")) + return + } + defer sess.Close() + //先扣钱 + amountData := db.GetMasterAmountByListIdWithSess(sess, amountMap["list_id"]) + if amountData == nil { + sess.Rollback() + e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败")) + return + } + oldAmount := amountData.Amount + leaveAmount = utils.StrToFloat64(amountData.Amount) - utils.StrToFloat64(args["amount"]) + if leaveAmount < 0 { + e.OutErr(c, 400, e.NewErr(400, "余额不足")) + return + } + amountData.Amount = utils.Float64ToStr(leaveAmount) + update := db.MasterAmountUpdateWithSess(sess, amountData.Id, amountData) + if update == false { + e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败")) + return + } + //再写入明细 + var tmpFlow = model.MasterAmountFlow{ + Uid: amountMap["id"], + Time: time.Now(), + BeforeAmount: oldAmount, + Amount: args["amount"], + AfterAmount: amountData.Amount, + Platform: args["type"], + Oid: "", + Title: "提现", + FlowType: "withdrawal", + } + flowInsert := db.MasterAmountFlowInsertWithSess(sess, &tmpFlow) + if flowInsert == false { + e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败")) + return + } + var tmp = model.MasterWithdrawalFlow{ + Uid: amountMap["id"], + Time: time.Now(), + UpdateTime: time.Now(), + Remark: args["remark"], + Alipay: amountMap["alipay"], + AlipayName: amountMap["alipay_name"], + Amount: args["amount"], + RealAmount: utils.Float64ToStr(realAmount), + Fee: utils.Float64ToStr(fee), + Reason: "", + Status: "提现审核", + HasInvoice: utils.StrToInt(args["has_invoice"]), + InvoiceBili: args["invoice_bili"], + } + insert := db.MasterWithdrawalFlowInsertWithSess(sess, &tmp) + if insert == false { + e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败")) + return + } + sess.Commit() + e.OutSuc(c, "success", nil) + return +} +func WithdrawalOutput(c *gin.Context) { + args, mid, err := commArg(c) + if err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + args["size"] = "3000" + amountMap := masterAmount(mid, args["type"]) + masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{} + masterWithdrawalFlowDb.Set() + list := masterWithdrawalFlowDb.GetWithdrawalFlowList(amountMap["id"], args) + name := "订单_" + args["p"] + //写入数据 + data := map[string]string{ + "A1": "提现支付宝账号", + "B1": "提现支付宝姓名", + "C1": "提现金额", + "D1": "实际金额", + "E1": "手续费", + "F1": "提现状态", + "G1": "申请时间", + "H1": "审核时间", + "I1": "备注", + "J1": "失败原因", + } + if list != nil { + for k, v := range *list { + checkTime := "" + if v.CheckTime.IsZero() == false { + checkTime = v.CheckTime.Format("2006-01-02 15:04:05") + } + i := utils.IntToStr(k + 2) + data["A"+i] = v.Alipay + data["B"+i] = v.AlipayName + data["C"+i] = v.Amount + data["D"+i] = v.RealAmount + data["E"+i] = v.Fee + data["F"+i] = v.Status + data["G"+i] = v.Time.Format("2006-01-02 15:04:05") + data["H"+i] = checkTime + data["I"+i] = v.Remark + data["J"+i] = v.Reason + } + } + file := utils.Output(c, name, data) + filename := name + ".xlsx" + r := map[string]string{ + "file": file, + "filename": filename, + } + e.OutSuc(c, r, nil) + return +} +func WithdrawalInvoiceImg(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{} + masterWithdrawalFlowDb.Set() + flow := masterWithdrawalFlowDb.MasterWithdrawalFlowById(args["id"]) + flow.Img = args["img"] + update := masterWithdrawalFlowDb.MasterWithdrawalFlowInsertUpdate(flow) + if update == false { + e.OutErr(c, 400, e.NewErr(400, "上传失败")) + return + } + e.OutSuc(c, "success", nil) + return +} + +func WithdrawalBindAlipay(c *gin.Context) { + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + if args["alipay"] == "" || args["alipay_name"] == "" { + e.OutErr(c, 400, e.NewErr(400, "支付宝信息不能为空")) + return + } + masterId, _ := c.Get("master_id") + mid := utils.AnyToString(masterId) + masterDb := db.MasterDb{} + masterDb.Set() + master := masterDb.GetMaster(mid) + master.AlipayName = args["alipay_name"] + master.Alipay = args["alipay"] + update := masterDb.MasterUpdate(master) + if update == false { + e.OutErr(c, 400, e.NewErr(400, "修改失败")) + return + } + e.OutSuc(c, "success", nil) + return +} + +func commArg(c *gin.Context) (map[string]string, string, error) { + masterId, _ := c.Get("master_id") + mid := utils.AnyToString(masterId) + fmt.Println(mid) + var args map[string]string + if err := c.ShouldBindJSON(&args); err != nil { + return args, mid, err + } + return args, mid, nil + +} + +func masterInfo(mid string) map[string]string { + masterDb := db.MasterDb{} + masterDb.Set() + master := masterDb.GetMaster(mid) + res := make(map[string]string) + if master != nil { + res["id"] = utils.IntToStr(master.Id) + res["alipay"] = master.Alipay + res["alipay_name"] = master.AlipayName + } + return res +} +func masterAmount(mid, types string) map[string]string { + masterInfos := masterInfo(mid) + res := map[string]string{ + "amount": "0.00", + "last_month_settle_amount": "0.00", + } + if masterInfos["id"] == "" { + return res + } + masterAmountDb := db.MasterAmountDb{} + masterAmountDb.Set() + masterAmounts := masterAmountDb.GetMasterAmount(masterInfos["id"], types) + if masterAmounts == nil { + return res + } + res["amount"] = masterAmounts.Amount + if res["amount"] == "" { + res["amount"] = "0" + } + res["id"] = masterAmounts.Uid + res["list_id"] = utils.IntToStr(masterAmounts.Id) + res["alipay"] = masterInfos["alipay"] + res["alipay_name"] = masterInfos["alipay_name"] + res["last_month_settle_amount"] = masterAmounts.LastMonthAmount + if res["last_month_settle_amount"] == "" { + res["last_month_settle_amount"] = "0" + } + return res +} +func masterMonthAmount(mid string) map[string]string { + playletSaleOrder := db.PlayletSaleOrderDb{} + playletSaleOrder.Set() + lastMonthSum := playletSaleOrder.PlayletVideoOrderSum(mid, "", "last_month") + monthSum := playletSaleOrder.PlayletVideoOrderSum(mid, "", "current_month") + monthSettleSum := playletSaleOrder.PlayletVideoOrderSum(mid, "订单结算", "current_month") + res := map[string]string{ + "last_month_amount": utils.Float64ToStr(lastMonthSum), + "month_amount": utils.Float64ToStr(monthSum), + "month_settle_amount": utils.Float64ToStr(monthSettleSum), + } + return res +} diff --git a/app/task/svc/svc_playlet_adv_order.go b/app/task/svc/svc_playlet_adv_order.go index 7c4699a..179aae2 100644 --- a/app/task/svc/svc_playlet_adv_order.go +++ b/app/task/svc/svc_playlet_adv_order.go @@ -52,8 +52,8 @@ func PlayletAdvOrder() { time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]interface{}{ - "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"), - "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"), + "start": time.Unix(beginTime, 0).Format("2006-01-02"), + "end": time.Unix(endTime, 0).Format("2006-01-02"), "page_size": pageSize, "page_index": pageNo, } @@ -115,8 +115,8 @@ func PlayletAdvOrderYesterday(hours int, runtimeStr string) { time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]interface{}{ - "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"), - "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"), + "start": time.Unix(beginTime, 0).Format("2006-01-02"), + "end": time.Unix(endTime, 0).Format("2006-01-02"), "page_size": pageSize, "page_index": pageNo, } @@ -156,9 +156,9 @@ func PlayletAdvOrderMonth() { runtime := syscfgDb.MasterListCfgGetOneData(uid, key) keyIsEnd := "playlet_adv_order_month_is_end" if utils.TimeStdParseUnix(starttime) < stime { - syscfgDb.MasterListCfgUpdate(uid, key, time.Now().Format("2006-01-02 15:04:05")) + syscfgDb.MasterListCfgUpdate(uid, key, time.Now().Format("2006-01-02")) syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "") - runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02 15:04:05") + runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02") } //当前是否结束了 isEnd := syscfgDb.MasterListCfgGetOneData(uid, keyIsEnd) @@ -177,8 +177,8 @@ func PlayletAdvOrderMonth() { time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]interface{}{ - "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"), - "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"), + "start": time.Unix(beginTime, 0).Format("2006-01-02"), + "end": time.Unix(endTime, 0).Format("2006-01-02"), "page_size": pageSize, "page_index": pageNo, } @@ -257,7 +257,13 @@ func getAdvOrder(uids string, arg map[string]interface{}) int { zyBili := zyBiliMap[v.Channel] platformFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(bili) / 100) zyFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(zyBili) / 100) - commission := utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee)) + commission := "0" + if utils.StrToFloat64(money) > 0 { + commission = utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee)) + } + if utils.StrToFloat64(commission) < 0 { + commission = "0" + } var tmp = model.PlayletSaleOrder{ Uid: mid, SubUid: utils.StrToInt(uid), diff --git a/app/task/svc/svc_playlet_video_order.go b/app/task/svc/svc_playlet_video_order.go index fd39952..5250174 100644 --- a/app/task/svc/svc_playlet_video_order.go +++ b/app/task/svc/svc_playlet_video_order.go @@ -246,7 +246,13 @@ func getVideoOrder(uids string, arg map[string]interface{}) int { zyBili := zyBiliMap[v.Channel] platformFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(bili) / 100) zyFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(zyBili) / 100) - commission := utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee)) + commission := "0" + if utils.StrToFloat64(money) > 0 { + commission = utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee)) + } + if utils.StrToFloat64(commission) < 0 { + commission = "0" + } var tmp = model.PlayletSaleOrder{ Uid: mid, SubUid: utils.StrToInt(uid), diff --git a/app/utils/auth.go b/app/utils/auth.go index 7b95682..556e32a 100644 --- a/app/utils/auth.go +++ b/app/utils/auth.go @@ -9,32 +9,60 @@ import ( ) // GenToken 生成JWT -func GenToken(admId int, username string) (string, error) { +func GenToken(masterId string) (aToken, rToken string, err error) { // 创建一个我们自己的声明 - c := auth.JWTUser{ - AdmId: admId, - Username: username, - StandardClaims: jwt.StandardClaims{ + c := auth.JWTMaster{ + masterId, + jwt.StandardClaims{ ExpiresAt: time.Now().Add(auth.TokenExpireDuration).Unix(), // 过期时间 - Issuer: "zyos", // 签发人 + Issuer: "zyos_zhimeng", // 签发人 }, } // 使用指定的签名方法创建签名对象 token := jwt.NewWithClaims(jwt.SigningMethodHS256, c) // 使用指定的secret签名并获得完整的编码后的字符串token - return token.SignedString(auth.Secret) + aToken, err = token.SignedString(auth.Secret) + if err != nil { + return "", "", err + } + + // rToken 生成 + rc := auth.JWTMaster{ + masterId, + jwt.StandardClaims{ + ExpiresAt: time.Now().Add(auth.RefreshTokenExpireDuration).Unix(), // 过期时间 + Issuer: "zyos_zhimeng", // 签发人 + }, + } + rToken, err = jwt.NewWithClaims(jwt.SigningMethodHS256, rc).SignedString(auth.RSecret) + return } // ParseToken 解析JWT -func ParseToken(tokenString string) (*auth.JWTUser, error) { +func ParseToken(tokenString string) (*auth.JWTMaster, error) { // 解析token - token, err := jwt.ParseWithClaims(tokenString, &auth.JWTUser{}, func(token *jwt.Token) (i interface{}, err error) { + token, err := jwt.ParseWithClaims(tokenString, &auth.JWTMaster{}, func(token *jwt.Token) (i interface{}, err error) { return auth.Secret, nil }) if err != nil { return nil, err } - if claims, ok := token.Claims.(*auth.JWTUser); ok && token.Valid { // 校验token + if claims, ok := token.Claims.(*auth.JWTMaster); ok && token.Valid { // 校验token + return claims, nil + } + return nil, errors.New("invalid token") +} + +// ParseRToken 解析JWT +func ParseRToken(tokenString string) (*auth.JWTMaster, error) { + // 解析token + token, err := jwt.ParseWithClaims(tokenString, &auth.JWTMaster{}, func(token *jwt.Token) (i interface{}, err error) { + return auth.RSecret, nil + }) + if err != nil { + return nil, err + } + if claims, ok := token.Claims.(*auth.JWTMaster); ok && token.Valid { // 校验token return claims, nil } return nil, errors.New("invalid token") diff --git a/app/utils/xls.go b/app/utils/xls.go new file mode 100644 index 0000000..4b0318a --- /dev/null +++ b/app/utils/xls.go @@ -0,0 +1,36 @@ +package utils + +import ( + "bytes" + "fmt" + "github.com/360EntSecGroup-Skylar/excelize" + "github.com/gin-gonic/gin" + "io/ioutil" +) + +func Output(c *gin.Context, name string, data map[string]string) string { + //创建excel文件 + xlsx := excelize.NewFile() + //创建新表单 + index := xlsx.NewSheet(name) + for k, v := range data { + //设置单元格的值 + xlsx.SetCellValue(name, k, v) + } + //设置默认打开的表单 + xlsx.SetActiveSheet(index) + ////保存文件到指定路径 + //err := xlsx.SaveAs("./" + name + ".xlsx") + //if err != nil { + // log.Fatal(err) + //} + //_ = file.Save(fileName) + c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".xlsx")) + c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + var buffer bytes.Buffer + _ = xlsx.Write(&buffer) + r := bytes.NewReader(buffer.Bytes()) + fSrc, _ := ioutil.ReadAll(r) + str := "data:application/vnd.ms-excel;base64," + Base64RawStdEncode(string(fSrc)) + return str +} diff --git a/cmd_db.bat b/cmd_db.bat index 7a9e8d5..324f997 100644 --- a/cmd_db.bat +++ b/cmd_db.bat @@ -12,7 +12,7 @@ if "%one%" NEQ "" ( set BasePath="./" set DBUSER="root" set DBPSW="Fnuo123com@" -set DBNAME="fnuoos_test1" +set DBNAME="zhi_meng" set DBHOST="119.23.182.117" set DBPORT="3306" diff --git a/etc/cfg.yml b/etc/cfg.yml index 5dadd9f..44c8133 100644 --- a/etc/cfg.yml +++ b/etc/cfg.yml @@ -6,7 +6,7 @@ local: true # 服务器参数 srv_addr: ':5600' # 缓存 -redis_addr: '47.108.198.174:6379' +redis_addr: '120.24.28.6:32572' app_comm: # 本模块外网地址 @@ -24,6 +24,17 @@ db: max_idle_conns: 100 path: 'tmp/%s.log' +zhimeng_db: + host: '119.23.182.117:3306' + name: 'zhi_meng' + user: 'root' + psw: 'Fnuo123com@' + show_log: true + max_lifetime: 30 + max_open_conns: 100 + max_idle_conns: 100 + path: 'tmp/%s.log' + # 日志 log: app_name: 'applet' @@ -36,3 +47,9 @@ log: file_max_size: 256 file_max_age: 1 file_name: 'debug.log' + +# 连接ElasticSearch +es: + url: 'http://120.55.48.175:9200' + user: 'elastic' + pwd: 'fnuo123' \ No newline at end of file diff --git a/go.mod b/go.mod index c2724a3..1f943a3 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.15 require ( code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20230707081910-52e70aa52998 code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git v1.1.21-0.20230703061209-fc6ac71cc155 + github.com/360EntSecGroup-Skylar/excelize v1.4.1 github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 github.com/boombuler/barcode v1.0.1 github.com/dchest/uniuri v1.2.0