Browse Source

feat:新增搜索前白名单校验、以及增加关键词校验规则

zhangsiya 1 year ago
parent
commit
a35c92002e

+ 17 - 0
apps/bigmember_pc/src/api/modules/search.js

@@ -42,3 +42,20 @@ export function getCMCustomInfo () {
     method: 'post'
   })
 }
+
+/**
+ * 检测当前账号是否在反爬虫白名单中
+ * 如果在反爬白名单,则空搜索刷新搜索结果(即允许空搜索)
+ * 不在,则不允许空搜索(此处空搜索指的是主搜索框是否为空)
+ *
+ * 该接口也返回一些校验关键词输入规范的正则,
+ * 需要在搜索前进行校验是否可以进行搜索
+ * @returns {*}
+ */
+
+export function getInAntiSpiderWhiteList () {
+  return request({
+    url: '/publicapply/userbase/whitelist',
+    method: 'post'
+  })
+}

+ 1 - 1
apps/bigmember_pc/src/components/search-input/SearchInput.vue

@@ -79,7 +79,7 @@ export default {
       this.$emit('input', e)
     },
     onSearch() {
-      this.$emit('onSearch')
+      this.$emit('onSearch', this.value)
     }
   }
 }

+ 3 - 2
apps/bigmember_pc/src/views/search/bidding/components/search-bid-header.vue

@@ -23,8 +23,9 @@ const { searchTabs } = SearchTabsModel
 
 const that = getCurrentInstance().proxy
 
-function doSearch() {
-  return doQuery()
+function doSearch($event) {
+  const firstSearch = !$event ? 'firstSearch' : undefined
+  return doQuery({}, firstSearch)
 }
 // 跳转信息发布
 function goToPublish () {

+ 56 - 9
apps/bigmember_pc/src/views/search/bidding/model/base.js

@@ -1,4 +1,4 @@
-import { computed, reactive, ref, toRefs, onMounted, getCurrentInstance } from 'vue'
+import { computed, reactive, ref, toRefs, onMounted, onBeforeUnmount, getCurrentInstance } from 'vue'
 import { without, throttle } from 'lodash'
 import { useStore } from '@/store'
 import { useRoute, useRouter } from 'vue-router/composables'
@@ -29,6 +29,8 @@ import { joinBidActionsModel } from './modules/join-bid-actions'
 import { dataEmployActionsModel } from './modules/data-employ-actions'
 // 添加业务
 import { dataAddActionsModel } from './modules/data-add-actions'
+// 进行搜索前业务判断
+import { beforeSearchModel } from './modules/before-search'
 
 
 export default function () {
@@ -71,8 +73,6 @@ export default function () {
   // 本地调试,可改为工作台内isInApp = ref(true),  isInWeb = ref(false) 提交记得改回!
   const isInApp = ref(InContainer.inApp)
   const isInWeb = ref(InContainer.inWeb)
-  console.log(8888888)
-  console.log(isInWeb)
 
   // 是否是渠道商
   const cooperateCode = ref(false)
@@ -128,13 +128,19 @@ export default function () {
   const {
     inputKeywordsState,
     searchModelOptions,
-    getFormatAPIParams: getFormatOfInputKeywords
+    getFormatAPIParams: getFormatOfInputKeywords,
+    updateInputKeywordsState,
   } = useSearchInputKeywordsModel()
   const {
     filterState,
     getFormatAPIParams: getFormatOfFilter,
     updateFilterBase
   } = useSearchFilterModel({ inBIPropertyIframe })
+
+  const {
+    onTheWhiteList,
+    checkAndClearTextIncludesCommonWords
+  } = beforeSearchModel({ inputKeywordsState })
   const {
     limitActions,
     headerActions,
@@ -144,8 +150,11 @@ export default function () {
     disabledHeaderActions
   } = useSearchListHeaderActionsModel()
 
-  // 页面tab切换Model
+  $bus.$on('bidding:updateInputKeywords', function (obj) {
+    updateInputKeywordsState(obj)
+  })
 
+  // 页面tab切换Model
   const SearchTabsModel = useSearchTabsModel({ showTabs2: !isInApp.value })
   const { activeTab, doChangeTab } = SearchTabsModel
   // tab切换处理
@@ -155,7 +164,7 @@ export default function () {
     })
     doChangeTab(item)
     if(isInApp.value) {
-      doQuery()
+      doQuery({}, 'tab')
     } else {
       if(item.link) {
         location.replace(item.link)
@@ -493,12 +502,50 @@ export default function () {
     )
     return result
   }
+
+  function beforeSearch () {
+    console.log('beforesearch----------')
+    // 如果在反爬白名单,则空搜索刷新搜索结果(即允许空搜索)
+    // 不在,则不允许空搜索(此处空搜索指的是主搜索框是否为空)
+    if (!onTheWhiteList.value) {
+      const searchKeywords = inputKeywordsState.value.input
+      const hasOneKey = (filterState.value.buyer?.length || filterState.value.winner?.length ||  filterState.value.agency?.length) > 0
+      // 切换三种筛选类型时候判断(切换tab时不弹窗)
+      // if (!searchKeywords && !hasOneKey) {
+      //   if (from.indexOf('tab-') !== -1) {
+      //     return false
+      //   }
+      // }
+      // 关键词去两边空格后为空
+      if (!searchKeywords && inputKeywordsState.value.additionalWords.length === 0) {
+        if (!hasOneKey) {
+          that.$toast('请先输入关键词')
+          return false
+        }
+      } else {
+        // 判断关键词中是否有通用词,并清空对应通用词的项
+        const hasCommonWords = checkAndClearTextIncludesCommonWords(searchKeywords)
+        if (hasCommonWords) {
+          that.$toast('请输入项目名称等关键词')
+          return false
+        }
+      }
+    }
+    return true
+  }
   /**
    * 统一查询入口
    * - 拦截 doQuery 进行一些返回值处理
    * @param [params] - 可选值,默认会和 getParams(params) 返回值进行合并
    */
-  function doQuery(params = {}) {
+  function doQuery(params = {}, searchType) {
+
+    if(!searchType) {
+      const bSearch = beforeSearch()
+      if(!bSearch) {
+        return
+      }
+    }
     return doRunQuery(getParams(params)).then((res) => {
 
       let matchKeys = []
@@ -559,7 +606,7 @@ export default function () {
     showFilter.value = !showFilter.value
   }
 
-  // 组合好的筛选条件
+  // 组合好的组件格式的筛选条件
   function packageFilter () {
     const originParams = Object.assign(
       {
@@ -620,7 +667,7 @@ export default function () {
     sessionStorage.setItem('search:bidding:filter', JSON.stringify(originParams))
     // window.location.replace('/page_workDesktop/work-bench/swordfish/page_big_pc/search/bidding?goback=true')
 
-    window.location.replace(`/page_workDesktop/work-bench/page?link=${encodeURIComponent(goHref_ + 'goback=true')}`)
+    window.location.replace(`/page_workDesktop/work-bench/page?link=${encodeURIComponent(goHref_ + '?goback=true')}`)
   }
   // 监听路由事件
   onMounted(() => {

+ 102 - 0
apps/bigmember_pc/src/views/search/bidding/model/modules/before-search.js

@@ -0,0 +1,102 @@
+// 获取是否允许空搜索白名单
+import { ref } from 'vue'
+import { trim } from 'lodash'
+// 获取反爬虫白名单,用于搜索前处理
+import { getInAntiSpiderWhiteList } from '@/api/modules/'
+import { showToast } from '@/components/toast'
+import $bus from '@/utils/bus'
+
+
+export function beforeSearchModel (paramsObj = { }) {
+
+  const { inputKeywordsState } = paramsObj
+  // 是否是白名单用户
+  let onTheWhiteList = ref(false)
+  // 关键词搜索的校验规则
+  const commonSearchWordsRegExp = ref([])
+
+  async function getWhiteList () {
+    const keywords = inputKeywordsState.value.input
+
+    const { error_code: code, data } = await getInAntiSpiderWhiteList()
+    if(code === 0 && data) {
+      onTheWhiteList.value = data.onTheWhiteList
+
+      if (Array.isArray(data.filterReg)) {
+        const newExp = []
+        data.filterReg.forEach(function(item) {
+          newExp.push(new RegExp(item))
+        })
+        commonSearchWordsRegExp.value = newExp
+      }
+
+
+      if (keywords && !onTheWhiteList) {
+        const timer = setTimeout(function() {
+          clearTimeout(timer)
+          const hasCommonWords = checkAndClearTextIncludesCommonWords(keywords)
+          if (hasCommonWords) {
+            showToast('请输入项目名称等关键词')
+          }
+        }, 500)
+      }
+    }
+  }
+  getWhiteList()
+
+  // 判断文字中是否有通用词(返回true表示text是通用词)
+  // (关键词不为空时,判断是否包含通用词)
+  function checkTextIncludesCommonWords (text) {
+    console.log(commonSearchWordsRegExp)
+    if (!text) {
+      return false
+    }
+    const passArr = []
+    text = trim(text)
+    if (Array.isArray(commonSearchWordsRegExp.value)) {
+      commonSearchWordsRegExp.value.forEach((reg) => {
+        const newText = text.replace(reg, '')
+        passArr.push(!!newText)
+      })
+    }
+
+    const hasNoPass = passArr.indexOf(false) !== -1
+    return hasNoPass
+  }
+   // text: 去空格后的主关键词
+  function checkAndClearTextIncludesCommonWords (text) {
+    let mainKeysHasCommonWords = false
+    let additionalWordsHasCommonWords = false
+    // 检查主关键词
+    if (checkTextIncludesCommonWords(text)) {
+      mainKeysHasCommonWords = true
+      $bus.$emit('bidding:updateInputKeywords', { input: ''})
+    }
+    // 检查附加词
+    const additionalWords = inputKeywordsState.value.additionalWords
+    const replacedAdditionalWords = []
+    if (Array.isArray(additionalWords)) {
+      for(let i = 0; i < additionalWords.length; i++) {
+        const item = additionalWords[i]
+        if (checkTextIncludesCommonWords(item)) {
+          // ...
+          additionalWordsHasCommonWords = true
+        } else {
+          replacedAdditionalWords.push(item)
+        }
+      }
+      if (additionalWordsHasCommonWords) {
+        $bus.$emit('bidding:updateInputKeywords', { additionalWords: replacedAdditionalWords})
+      }
+    }
+    return mainKeysHasCommonWords || additionalWordsHasCommonWords
+  }
+
+  return {
+    onTheWhiteList,
+    checkAndClearTextIncludesCommonWords
+  }
+
+}
+
+

+ 8 - 1
apps/bigmember_pc/src/views/search/bidding/model/modules/filter-keywords.js

@@ -38,9 +38,16 @@ export function useSearchInputKeywordsModel() {
     return params
   }
 
+  // 动态更新筛选条件
+  function updateInputKeywordsState (keyObj = {}) {
+    inputKeywordsState.value = Object.assign(inputKeywordsState.value, keyObj)
+    console.log(inputKeywordsState.value)
+  }
+
   return {
     inputKeywordsState,
     searchModelOptions,
-    getFormatAPIParams
+    getFormatAPIParams,
+    updateInputKeywordsState
   }
 }

+ 8 - 3
apps/bigmember_pc/src/views/search/bidding/model/modules/filter.js

@@ -140,9 +140,14 @@ export function useSearchFilterModel(conf) {
   // 动态更新筛选条件
   function updateFilterBase (keyObj = {}) {
     const {key, value } = keyObj
-    filterBase.value = Object.assign(filterBase.value, {
-      [key]: value
-    })
+
+    if(keyObj && Object.keys(keyObj).length > 0) {
+      filterBase.value = Object.assign(filterBase.value, {
+        [key]: value
+      })
+    } else {
+      filterBase.value = Object.assign(filterBase.value)
+    }
     if(!inBIPropertyIframe) {
       filterState.value = filterBase.value
     }

+ 4 - 0
apps/bigmember_pc/src/views/search/bidding/model/modules/save-filter-actions.js

@@ -115,6 +115,10 @@ export function saveFilterActionsModel () {
    * @returns {Promise<void>}
    */
   async function onHasFilter () {
+    if(!historyFilterCount.value) {
+      showToast('请先保存筛选条件')
+      return
+    }
     historyFilterDialogVisible.value = true
   }
   // 已存筛选条件弹窗,单条展开收起

+ 2 - 2
apps/bigmember_pc/src/views/search/components/SearchHeader.vue

@@ -94,8 +94,8 @@ export default {
     onInput(e) {
       this.$emit('input', e)
     },
-    onSearch() {
-      this.$emit('search')
+    onSearch(e) {
+      this.$emit('search', e)
       // this.setUrlQuery()
     },
     setUrlQuery: function () {

+ 1 - 1
apps/bigmember_pc/src/views/search/components/search-header-card.vue

@@ -59,7 +59,7 @@ function goWorkSpaceCustom() {
       :searchContent="value"
       :show-wx-qr="showWxQr"
       @input="$emit('input', $event)"
-      @search="$emit('search')"
+      @search="$emit('search', $event)"
     >
       <div class="tab-switch" slot="tab-switch">
         <slot name="tab-switch-content"></slot>