修改敏感词过滤代码结构

This commit is contained in:
Rosy-zhudan
2019-08-19 10:27:04 +08:00
parent 9425f3c07a
commit dee9eea64a
9 changed files with 39 additions and 38 deletions

View File

@@ -509,13 +509,28 @@ func GetSkuNames(ctx *jxcontext.Context, keyword string, isBySku bool, params ma
}
func CheckHasSensitiveWord(word string) (bool, error) {
if hasSensitiveWord, sensitiveWord := dao.CheckHasSensitiveWord(word); hasSensitiveWord {
if hasSensitiveWord, sensitiveWord := IsSensitiveWordInList(word); hasSensitiveWord {
return true, errors.New(fmt.Sprintf("不能包含敏感词:[%s]", sensitiveWord))
}
return false, nil
}
func IsSensitiveWordInList(str string) (bool, string) {
wordList, err := dao.GetSensitiveWordList()
if err == nil {
for _, value := range wordList {
keyWord := value.Word
checkHas := strings.Contains(str, keyWord)
if checkHas {
return true, keyWord
}
}
}
return false, ""
}
func AddSkuName(ctx *jxcontext.Context, skuNameExt *model.SkuNameExt, userName string) (outSkuNameExt *model.SkuNameExt, err error) {
if skuNameExt.CategoryID == 0 {
return nil, errors.New("CategoryID不能为空")
@@ -1027,4 +1042,4 @@ func UploadImg2Platforms(ctx *jxcontext.Context, parentTask tasksch.ITask, imgUR
}
}
return imgHintMap, err
}
}

View File

@@ -18,7 +18,7 @@ import (
)
const (
canWriteTolocal = !false
canWriteTolocal = false
needStatistic = true
isFilterToBeCreateAndNotSale = true
parallelCount = 5

View File

@@ -416,8 +416,8 @@ func syncStoreSkuNew(ctx *jxcontext.Context, parentTask tasksch.ITask, isFull bo
successList = batchedStoreSkuList
} else {
//handle error for sensitive words, if find, then insert to table sensitive_words
if sensitiveWords := GetSensitiveWords(singleStoreHandler, err.Error()); sensitiveWords != "" {
dao.InsertSensitiveWord(sensitiveWords)
if sensitiveWord := GetSensitiveWord(singleStoreHandler, err.Error()); sensitiveWord != "" {
dao.InsertSensitiveWord(sensitiveWord, vendorID, ctx.GetUserName())
}
}
if len(successList) > 0 {
@@ -586,12 +586,12 @@ func ClearRemoteStoreStuffAndSetNew(ctx *jxcontext.Context, parentTask tasksch.I
return rootTask.ID, err
}
func GetSensitiveWords(singleStoreHandler partner.ISingleStoreStoreSkuHandler, str string) string {
var regFindKeyWords = singleStoreHandler.GetRegexp()
var subRegFindKeyWords = regexp.MustCompile(`[^\[\]\"\}]`)
findResult := regFindKeyWords.FindStringSubmatch(str)
func GetSensitiveWord(singleStoreHandler partner.ISingleStoreStoreSkuHandler, str string) string {
var regFindKeyWord = singleStoreHandler.GetSensitiveWordRegexp()
var subRegFindKeyWord = regexp.MustCompile(`[^\[\]\"\}]`)
findResult := regFindKeyWord.FindStringSubmatch(str)
if findResult != nil && len(findResult) > 1 {
findSubResult := subRegFindKeyWords.FindAllString(findResult[1], -1)
findSubResult := subRegFindKeyWord.FindAllString(findResult[1], -1)
return strings.Join(findSubResult, "")
}

View File

@@ -1,32 +1,17 @@
package dao
import (
"strings"
"git.rosy.net.cn/jx-callback/business/model"
)
func GetSensitiveWordList() (wordList []*model.SensitiveWords, err error) {
sql := `SELECT * FROM sensitive_words`
func GetSensitiveWordList() (wordList []*model.SensitiveWord, err error) {
sql := `SELECT * FROM sensitive_word`
err = GetRows(nil, &wordList, sql)
return wordList, err
}
func InsertSensitiveWord(word string) error {
return CreateEntity(nil, &model.SensitiveWords{Words: word})
}
func CheckHasSensitiveWord(str string) (bool, string) {
wordList, err := GetSensitiveWordList()
if err == nil {
for _, value := range wordList {
keyWord := value.Words
checkHas := strings.Contains(str, keyWord)
if checkHas {
return true, keyWord
}
}
}
return false, ""
func InsertSensitiveWord(word string, vendorID int, userName string) error {
sensitiveWord := &model.SensitiveWord{Word: word, VendorID: vendorID}
WrapAddIDCULDEntity(sensitiveWord, userName)
return CreateEntity(nil, sensitiveWord)
}

View File

@@ -1,6 +1,7 @@
package model
type SensitiveWords struct {
ID int `orm:"column(id)" json:"id"`
Words string `orm:"size(30);column(words);unique" json:"words"`
type SensitiveWord struct {
ModelIDCULD
VendorID int `orm:"column(vendor_id)" json:"vendorID"`
Word string `orm:"size(30);unique" json:"word"`
}

View File

@@ -138,7 +138,7 @@ type ISingleStoreStoreSkuHandler interface {
IsErrCategoryExist(err error) (isExist bool)
IsErrCategoryNotExist(err error) (isNotExist bool)
GetRegexp() *regexp.Regexp
GetSensitiveWordRegexp() *regexp.Regexp
}
func BuildSkuName(skuID int, vendorSkuID string) (skuName *SkuNameInfo) {

View File

@@ -388,6 +388,6 @@ func vendorSkuList2Jx(vendorSkuList []*ebaiapi.SkuInfo) (skuNameList []*partner.
return skuNameList
}
func (p *PurchaseHandler) GetRegexp() *regexp.Regexp {
func (p *PurchaseHandler) GetSensitiveWordRegexp() *regexp.Regexp {
return regexp.MustCompile(`商品名称中含有敏感词(\[.*\])`)
}

View File

@@ -367,6 +367,6 @@ func vendorSkuList2Jx(appFoodList []*mtwmapi.AppFood) (skuNameList []*partner.Sk
return skuNameList
}
func (p *PurchaseHandler) GetRegexp() *regexp.Regexp {
func (p *PurchaseHandler) GetSensitiveWordRegexp() *regexp.Regexp {
return regexp.MustCompile(`包含敏感词:(\[.*\])`)
}

View File

@@ -53,7 +53,7 @@ func Init() {
orm.RegisterModel(&model.NewConfig{})
orm.RegisterModel(&model.CasbinRule{})
orm.RegisterModel(&model.SensitiveWords{})
orm.RegisterModel(&model.SensitiveWord{})
// create table
orm.RunSyncdb("default", false, true)
}