This commit is contained in:
Hsy
2025-02-20 17:32:53 +08:00
commit 144c1788f7
57 changed files with 2827 additions and 0 deletions

View File

@ -0,0 +1,145 @@
package cmd
import (
"context"
"database/sql"
"fmt"
"hk/model/config_api"
"hk/model/goods_info"
"hk/model/goods_status"
_73 "hk/pkg/util/172"
"hk/pkg/util/strUtil"
"strconv"
"time"
)
func configApiList() {
configApiInfo, err := svcCtx.ConfigApiModel.FindList(context.Background())
if err != nil {
fmt.Println(err)
}
if configApiInfo == nil {
return
}
for _, item := range configApiInfo {
switch item.Type {
case "0":
getGoodsStatus(item)
updateGoodsStatus(item)
}
}
}
func configApiListv1() {
configApiInfo, err := svcCtx.ConfigApiModel.FindList(context.Background())
if err != nil {
fmt.Println(err)
}
if configApiInfo == nil {
return
}
for _, item := range configApiInfo {
switch item.Type {
case "0":
addGoodsInfo(item)
}
}
}
func addGoodsInfo(configApi config_api.ConfigApi) {
ids, err := svcCtx.GoodsStatusModel.FindNotIdOne(context.Background(), configApi.Id)
if err != nil || ids <= 0 {
return
}
req := _73.GoodsReq{
ProductID: strconv.FormatInt(ids, 10),
BaseParams: _73.BaseParams{
UserID: strUtil.ValidString(configApi.Key),
UserSign: strUtil.ValidString(configApi.Secret),
},
}
result, err := _73.GetProductV2(req)
if err != nil {
return
}
if result.Code != 0 || len(result.Data) <= 0 {
return
}
data := result.Data[0]
status := "0"
if data.Flag {
status = "1"
}
goodsStatus := goods_info.GoodsInfo{
Name: data.ProductName,
Type: "0",
MainPic: data.MainPic,
LittlePicture: data.LittlePicture,
NetAddr: data.NetAddr,
Area: data.Area,
DisableArea: data.DisableArea,
DisableAge: "",
DisableContract: "",
Notes: sql.NullString{},
ApiId: strUtil.Int64ToNullInt64(configApi.Id),
ApiProductId: strUtil.Int64ToNullInt64(data.ProductID),
NumberSel: strconv.Itoa(data.NumberSel),
Status: status,
CreateTime: time.Now(),
UpdateTime: time.Now(),
Remarks: sql.NullString{},
}
svcCtx.GoodsInfoModel.Insert(context.Background(), &goodsStatus)
}
func updateGoodsStatus(item config_api.ConfigApi) {
ids, err := svcCtx.GoodsStatusModel.FindUpList(context.Background(), item.Id)
if err != nil {
return
}
if len(ids) <= 0 {
return
}
svcCtx.GoodsStatusModel.UpdateDownByIds(context.Background(), ids)
}
func getGoodsStatus(configApi config_api.ConfigApi) {
req := _73.GoodsReq{
ProductID: "",
BaseParams: _73.BaseParams{
UserID: strUtil.ValidString(configApi.Key),
UserSign: strUtil.ValidString(configApi.Secret),
},
}
result, err := _73.GetProduct(req)
if err != nil {
return
}
if result.Code == 0 {
var goodsStatusList []goods_status.GoodsStatus
if len(result.Data) > 0 {
err = svcCtx.GoodsStatusModel.DeleteByApiId(context.Background(), configApi.Id)
if err != nil {
return
}
}
for _, item := range result.Data {
var status int64 = 0
if item.Flag == "上架中" {
status = 1
}
goodsStatus := goods_status.GoodsStatus{
ApiId: strUtil.Int64ToNullInt64(configApi.Id),
ApiProductId: strUtil.Int64ToNullInt64(item.ProductID),
Status: strUtil.Int64ToNullInt64(status),
}
svcCtx.GoodsStatusModel.Insert(context.Background(), &goodsStatus)
goodsStatusList = append(goodsStatusList, goodsStatus)
}
}
}

View File

@ -0,0 +1,46 @@
package cronx
// 每5秒
func Every5s() string {
return "*/5 * * * * *"
}
// 每10秒
func Every10s() string {
return "*/10 * * * * *"
}
// 每分钟
func EveryMinute() string {
return "0 */1 * * * *"
}
// 每五分钟
func EveryFiveMinute() string {
return "0 */5 * * * *"
}
// 每十分钟
func EveryTenMinute() string {
return "0 */10 * * * *"
}
// 每半小时
func EveryHalfHour() string {
return "0 0,30 * * * *"
}
// EveryHour 每小时
func EveryHour() string {
return "0 0 * * * *"
}
// 每几分钟执行 1,2,3,26
func Hourly(m string) string {
return "0 " + m + " * * * *"
}
// 每天几点执行
func Daily(h string) string {
return "0 0 " + h + " * * *"
}

View File

@ -0,0 +1,20 @@
package cmd
import (
"fmt"
"github.com/robfig/cron/v3"
"hk/service/api/internal/svc"
)
var svcCtx *svc.ServiceContext
func Execute(ctx *svc.ServiceContext) {
svcCtx = ctx
c := cron.New(cron.WithSeconds())
ScheduleRun(c)
fmt.Println("定时任务启动...")
go c.Start()
defer c.Stop()
//select {}
}

View File

@ -0,0 +1,11 @@
package cmd
import (
"github.com/robfig/cron/v3"
"hk/service/api/internal/cmd/cronx"
)
func ScheduleRun(c *cron.Cron) {
c.AddFunc(cronx.EveryHour(), configApiList)
c.AddFunc(cronx.EveryTenMinute(), configApiListv1)
}

View File

@ -0,0 +1,21 @@
package config
import "github.com/zeromicro/go-zero/rest"
type Config struct {
rest.RestConf
Auth struct {
AccessSecret string
AccessExpire int64
}
Mysql struct {
DataSource string
}
Redis struct {
Host string
Type string
}
}

View File

@ -0,0 +1,28 @@
package api
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"hk/service/api/internal/logic/api"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
)
func GoodsDetailsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GoodsDetailsReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := api.NewGoodsDetailsLogic(r.Context(), svcCtx)
resp, err := l.GoodsDetails(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,28 @@
package api
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"hk/service/api/internal/logic/api"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
)
func GoodsListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GoodsListReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := api.NewGoodsListLogic(r.Context(), svcCtx)
resp, err := l.GoodsList(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,28 @@
package api
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"hk/service/api/internal/logic/api"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
)
func Order172Handler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PushReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := api.NewOrder172Logic(r.Context(), svcCtx)
resp, err := l.Order172(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,28 @@
package api
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"hk/service/api/internal/logic/api"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
)
func TiktokInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.TiktokReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := api.NewTiktokInfoLogic(r.Context(), svcCtx)
resp, err := l.TiktokInfo(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,44 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.6
package handler
import (
"net/http"
api "hk/service/api/internal/handler/api"
"hk/service/api/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.RealIPMiddleware},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/goods/:id",
Handler: api.GoodsDetailsHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/goods/list",
Handler: api.GoodsListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/push/order172",
Handler: api.Order172Handler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/tiktok/getVideoInfo",
Handler: api.TiktokInfoHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api"),
)
}

View File

@ -0,0 +1,48 @@
package api
import (
"context"
"hk/pkg/util/strUtil"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GoodsDetailsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGoodsDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsDetailsLogic {
return &GoodsDetailsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GoodsDetailsLogic) GoodsDetails(req *types.GoodsDetailsReq) (resp *types.GoodsDetailsResp, err error) {
if req.Id <= 0 {
return nil, nil
}
goods, err := l.svcCtx.GoodsInfoModel.FindOne(l.ctx, req.Id)
if err != nil || goods == nil || goods.Status == "0" {
return nil, err
}
resp = &types.GoodsDetailsResp{
Data: types.GoodsItem{
Id: goods.Id,
Name: goods.Name,
MainPic: goods.MainPic,
DisableAge: goods.DisableAge,
UniFlow: goods.UniFlow,
DirFlow: goods.DirFlow,
TalkTime: goods.TalkTime,
Remarks: strUtil.ValidString(goods.Remarks),
},
}
return resp, nil
}

View File

@ -0,0 +1,50 @@
package api
import (
"context"
"hk/pkg/util/strUtil"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GoodsListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
return &GoodsListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GoodsListLogic) GoodsList(req *types.GoodsListReq) (resp *types.GoodsListResp, err error) {
goods, err := l.svcCtx.GoodsInfoModel.GetGoodsIndex(l.ctx, req.Type)
if err != nil || len(goods) == 0 {
return nil, err
}
var goodsList []types.GoodsItem
for _, item := range goods {
good := types.GoodsItem{
Id: item.Id,
Name: item.Name,
MainPic: item.MainPic,
DisableAge: item.DisableAge,
UniFlow: item.UniFlow,
DirFlow: item.DirFlow,
TalkTime: item.TalkTime,
Remarks: strUtil.ValidString(item.Remarks),
}
goodsList = append(goodsList, good)
}
resp = &types.GoodsListResp{
Data: goodsList,
}
return resp, nil
}

View File

@ -0,0 +1,30 @@
package api
import (
"context"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type Order172Logic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewOrder172Logic(ctx context.Context, svcCtx *svc.ServiceContext) *Order172Logic {
return &Order172Logic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *Order172Logic) Order172(req *types.PushReq) (resp *types.Push172Resp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,53 @@
package api
import (
"context"
"hk/pkg/util/tiktok"
"hk/service/api/internal/svc"
"hk/service/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type TiktokInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewTiktokInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TiktokInfoLogic {
return &TiktokInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *TiktokInfoLogic) TiktokInfo(req *types.TiktokReq) (resp *types.TiktokResp, err error) {
if len(req.VideoUrl) <= 0 {
return nil, nil
}
tiktokInfo, err := tiktok.GetTiktokVideoInfo(req.VideoUrl)
if err != nil || tiktokInfo == nil {
return nil, nil
}
resp = &types.TiktokResp{
AuthorID: tiktokInfo.AuthorID,
AuthorAvatar: tiktokInfo.AuthorAvatar,
AuthorUniqueID: tiktokInfo.AuthorUniqueID,
AuthorNickname: tiktokInfo.AuthorNickname,
VideoID: tiktokInfo.VideoID,
VideoTitle: tiktokInfo.VideoTitle,
OriginCover: tiktokInfo.OriginCover,
DynamicCover: tiktokInfo.DynamicCover,
DestinationURL: tiktokInfo.DestinationURL,
WatermarkVideoURL: tiktokInfo.WatermarkVideoURL,
MusicURL: tiktokInfo.MusicURL,
OriginalURL: tiktokInfo.OriginalURL,
DownloaderURL: tiktokInfo.DownloaderURL,
CreateTime: tiktokInfo.CreateTime,
Result: true,
}
return resp, nil
}

View File

@ -0,0 +1,22 @@
package middleware
import (
"context"
"hk/pkg/middleware"
"net/http"
)
type RealIPMiddleware struct {
}
func NewRealIPMiddleware() *RealIPMiddleware {
return &RealIPMiddleware{}
}
func (m *RealIPMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ipaddress := middleware.GetRealIP(r)
ctx := context.WithValue(r.Context(), "ip", ipaddress)
next(w, r.WithContext(ctx))
}
}

View File

@ -0,0 +1,29 @@
package svc
import (
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/rest"
"hk/model/config_api"
"hk/model/goods_info"
"hk/model/goods_status"
"hk/service/api/internal/config"
"hk/service/api/internal/middleware"
)
type ServiceContext struct {
Config config.Config
RealIPMiddleware rest.Middleware
ConfigApiModel config_api.ConfigApiModel
GoodsStatusModel goods_status.GoodsStatusModel
GoodsInfoModel goods_info.GoodsInfoModel
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
RealIPMiddleware: middleware.NewRealIPMiddleware().Handle,
ConfigApiModel: config_api.NewConfigApiModel(sqlx.NewMysql(c.Mysql.DataSource)),
GoodsStatusModel: goods_status.NewGoodsStatusModel(sqlx.NewMysql(c.Mysql.DataSource)),
GoodsInfoModel: goods_info.NewGoodsInfoModel(sqlx.NewMysql(c.Mysql.DataSource)),
}
}

View File

@ -0,0 +1,75 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.6
package types
type GoodsDetailsReq struct {
Id int64 `path:"id"`
}
type GoodsDetailsResp struct {
Data GoodsItem `json:"data"` // Y 商品详情
}
type GoodsItem struct {
Id int64 `json:"id"` // Y 商品ID
Name string `json:"name"` // Y 商品信息
MainPic string `json:"mainPic"` // Y 主图
DisableAge string `json:"disableAge"` // Y 年龄限制
UniFlow string `json:"uniFlow"` // Y 通用流量
DirFlow string `json:"dirFlow"` // Y 定向流量
TalkTime string `json:"talkTime"` // Y 通话时长
Remarks string `json:"remarks"` // Y 备注
}
type GoodsListReq struct {
Type string `json:"type":"type,default=0"` // Y 运营商 0全部 1电信 2联通 3移动 4光电
}
type GoodsListResp struct {
Data []GoodsItem `json:"data"`
}
type Order172 struct {
OrderNo string `json:"OrderNo"` // Y 合作方订单号
OrderNo172 string `json:"OrderNo172"` // Y 172平台订单号请求头sign里加密的是这个号码。
OrderStatus string `json:"OrderStatus"` // Y 订单状态:已发货,已完成,审核不通过,已取消,已撤单
ThirdPhone string `json:"ThirdPhone"` // N 办理号码
ExpressName string `json:"ExpressName"` // N 物流公司
ExpressCode string `json:"ExpressCode"` // N 物流单号
CardStatus string `json:"CardStatus"` // N 激活状态已激活未激活可能为Null
ActiveTime string `json:"ActiveTime"` // N 激活时间格式yyyy-MM-dd HH:mm:ss可能为Null
Remark string `json:"Remark"` // N 备注失败原因
}
type Push172Resp struct {
Code int64 `json:"code",options=-1|0` // Y 响应码 -1错误 0正常
Msg string `json:"message"` // Y 错误信息
}
type PushReq struct {
RequidId string `json:"RequidId"` // Y 推送请求唯一ID
Data Order172 `json:"Data"` // Y 返回订单信息
}
type TiktokReq struct {
VideoUrl string `json:"videoUrl"` // 视频地址
}
type TiktokResp struct {
AuthorID string `json:"authorId"` // 作者ID
AuthorUniqueID string `json:"authorUniqueId"` // 作者账号
AuthorAvatar string `json:"authorAvatar"` // 作者头像
AuthorNickname string `json:"authorNickname"` // 作者昵称
VideoID string `json:"videoId"` // 视频ID
VideoTitle string `json:"videoTitle"` // 作品描述
OriginCover string `json:"originCover"` // 静态封面
DynamicCover string `json:"dynamicCover"` // 动态封面
DestinationURL string `json:"destinationUrl"` // 无水印下载地址
WatermarkVideoURL string `json:"watermarkVideoUrl"` // 有水印下载地址
MusicURL string `json:"musicUrl"` // 背景音乐链接
OriginalURL string `json:"originalUrl"` // 原始下载链接
DownloaderURL string `json:"downloaderUrl"` // 解析后下载地址
CreateTime string `json:"createTime"` // 创建时间
Result bool `json:"result"` // 结果
}