Merge branch 'main' of https://git.1024tool.vip/xl/mini-chat
This commit is contained in:
commit
78a2b442ec
@ -2,14 +2,13 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mini-chat/internal/code"
|
||||
"mini-chat/internal/pkg/core"
|
||||
"mini-chat/internal/pkg/httpclient"
|
||||
"mini-chat/internal/pkg/timeutil"
|
||||
"mini-chat/internal/pkg/validation"
|
||||
"net/http"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"mini-chat/internal/code"
|
||||
"mini-chat/internal/pkg/core"
|
||||
"mini-chat/internal/pkg/timeutil"
|
||||
"mini-chat/internal/pkg/validation"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -137,37 +136,15 @@ func (h *handler) PageList() core.HandlerFunc {
|
||||
res.Total = count
|
||||
res.List = make([]listData, len(resultData))
|
||||
|
||||
type checkAppResponse struct {
|
||||
Code int `json:"code"`
|
||||
Appid string `json:"appid"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
checkAppRes := new(checkAppResponse)
|
||||
for k, v := range resultData {
|
||||
userTotalCount, _ := h.readDB.AppUser.WithContext(ctx.RequestContext()).
|
||||
Where(h.readDB.AppUser.AppID.Eq(v.AppID)).
|
||||
Count()
|
||||
|
||||
checkStatusText := "未知"
|
||||
response, err := httpclient.GetHttpClientWithContext(ctx.RequestContext()).R().
|
||||
SetQueryParams(map[string]string{
|
||||
"appid": v.AppID,
|
||||
}).
|
||||
SetResult(checkAppRes).
|
||||
Get("https://api.wxapi.work/xcx/checkxcx.php")
|
||||
|
||||
if err != nil {
|
||||
h.logger.Error("请求失败", zap.Error(err))
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
if response.IsError() {
|
||||
h.logger.Error(fmt.Sprintf("请求APP验证服务异常(%d)", response.StatusCode()))
|
||||
}
|
||||
|
||||
if checkAppRes.Code == 1 {
|
||||
if v.Status == 1 {
|
||||
checkStatusText = "正常"
|
||||
} else if checkAppRes.Code == 0 {
|
||||
} else if v.Status == -1 {
|
||||
checkStatusText = "封禁"
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,80 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mini-chat/internal/pkg/httpclient"
|
||||
"mini-chat/internal/repository/mysql/dao"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (s *server) Start() {
|
||||
s.cron.Start()
|
||||
go s.taskCount.Wait()
|
||||
|
||||
// 每 5 分钟监控小程序状态
|
||||
s.cron.AddFunc("0 */10 * * * ?", func() {
|
||||
s.taskCount.Add()
|
||||
defer s.taskCount.Done()
|
||||
|
||||
readDB := dao.Use(s.db.GetDbR())
|
||||
writeDB := dao.Use(s.db.GetDbW())
|
||||
|
||||
appList, err := readDB.MiniProgram.Find()
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
s.logger.Error("获取小程序列表失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
if len(appList) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, app := range appList {
|
||||
type checkAppResponse struct {
|
||||
Code int `json:"code"`
|
||||
Appid string `json:"appid"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
checkAppRes := new(checkAppResponse)
|
||||
response, err := httpclient.GetHttpClient().R().
|
||||
SetQueryParams(map[string]string{
|
||||
"appid": app.AppID,
|
||||
}).
|
||||
SetResult(checkAppRes).
|
||||
Get("https://api.wxapi.work/xcx/checkxcx.php")
|
||||
if err != nil {
|
||||
s.logger.Error(fmt.Sprintf("[更新小程序状态失败]%s 请求APP验证服务失败", app.AppID), zap.Error(err))
|
||||
continue
|
||||
}
|
||||
if response.IsError() {
|
||||
s.logger.Error(fmt.Sprintf("[更新小程序状态失败]%s 请求APP验证服务异常(%d)", app.AppID, response.StatusCode()))
|
||||
}
|
||||
|
||||
status := 0
|
||||
if checkAppRes.Code == 1 {
|
||||
status = 1
|
||||
} else if checkAppRes.Code == 0 {
|
||||
status = -1
|
||||
}
|
||||
|
||||
if _, err := writeDB.MiniProgram.
|
||||
Where(writeDB.MiniProgram.AppID.Eq(app.AppID)).
|
||||
Updates(map[string]interface{}{
|
||||
"status": status,
|
||||
"updated_user": "cron",
|
||||
"updated_at": time.Now(),
|
||||
}); err != nil {
|
||||
s.logger.Error("更新小程序状态失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
}, "监控小程序状态")
|
||||
|
||||
// 初始化任务
|
||||
s.initTask()
|
||||
}
|
||||
|
||||
@ -35,6 +35,7 @@ func newMiniProgram(db *gorm.DB, opts ...gen.DOOption) miniProgram {
|
||||
_miniProgram.Name = field.NewString(tableName, "name")
|
||||
_miniProgram.Description = field.NewString(tableName, "description")
|
||||
_miniProgram.Avatar = field.NewString(tableName, "avatar")
|
||||
_miniProgram.Status = field.NewInt32(tableName, "status")
|
||||
_miniProgram.CreatedUser = field.NewString(tableName, "created_user")
|
||||
_miniProgram.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_miniProgram.UpdatedUser = field.NewString(tableName, "updated_user")
|
||||
@ -58,6 +59,7 @@ type miniProgram struct {
|
||||
Name field.String // 名称
|
||||
Description field.String // 描述
|
||||
Avatar field.String // 头像
|
||||
Status field.Int32 // 状态(1:正常 -1:封禁)
|
||||
CreatedUser field.String // 创建人
|
||||
CreatedAt field.Time // 创建时间
|
||||
UpdatedUser field.String // 更新人
|
||||
@ -86,6 +88,7 @@ func (m *miniProgram) updateTableName(table string) *miniProgram {
|
||||
m.Name = field.NewString(table, "name")
|
||||
m.Description = field.NewString(table, "description")
|
||||
m.Avatar = field.NewString(table, "avatar")
|
||||
m.Status = field.NewInt32(table, "status")
|
||||
m.CreatedUser = field.NewString(table, "created_user")
|
||||
m.CreatedAt = field.NewTime(table, "created_at")
|
||||
m.UpdatedUser = field.NewString(table, "updated_user")
|
||||
@ -106,7 +109,7 @@ func (m *miniProgram) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (m *miniProgram) fillFieldMap() {
|
||||
m.fieldMap = make(map[string]field.Expr, 12)
|
||||
m.fieldMap = make(map[string]field.Expr, 13)
|
||||
m.fieldMap["id"] = m.ID
|
||||
m.fieldMap["app_id"] = m.AppID
|
||||
m.fieldMap["app_secret"] = m.AppSecret
|
||||
@ -115,6 +118,7 @@ func (m *miniProgram) fillFieldMap() {
|
||||
m.fieldMap["name"] = m.Name
|
||||
m.fieldMap["description"] = m.Description
|
||||
m.fieldMap["avatar"] = m.Avatar
|
||||
m.fieldMap["status"] = m.Status
|
||||
m.fieldMap["created_user"] = m.CreatedUser
|
||||
m.fieldMap["created_at"] = m.CreatedAt
|
||||
m.fieldMap["updated_user"] = m.UpdatedUser
|
||||
|
||||
@ -20,6 +20,7 @@ type MiniProgram struct {
|
||||
Name string `gorm:"column:name;not null;comment:名称" json:"name"` // 名称
|
||||
Description string `gorm:"column:description;not null;comment:描述" json:"description"` // 描述
|
||||
Avatar string `gorm:"column:avatar;not null;comment:头像" json:"avatar"` // 头像
|
||||
Status int32 `gorm:"column:status;not null;comment:状态(1:正常 -1:封禁)" json:"status"` // 状态(1:正常 -1:封禁)
|
||||
CreatedUser string `gorm:"column:created_user;not null;comment:创建人" json:"created_user"` // 创建人
|
||||
CreatedAt time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
|
||||
UpdatedUser string `gorm:"column:updated_user;not null;comment:更新人" json:"updated_user"` // 更新人
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user