Explorar el Código

refactor(merchant): 优化搜索关键词查询逻辑

- 将数组去重逻辑改为 SQL 层去重,提升查询效率
- 移除 PHP 层的 array_unique 操作
- 保留原有序排列和数据过滤功能
shichen hace 2 meses
padre
commit
af64e20174
Se han modificado 1 ficheros con 3 adiciones y 2 borrados
  1. 3 2
      app/controller/api/store/merchant/Merchant.php

+ 3 - 2
app/controller/api/store/merchant/Merchant.php

@@ -174,15 +174,16 @@ class Merchant extends BaseController
174 174
         $hasKeyword = !empty($params['keyword'] ?? '') || !empty($this->request->param('keyword', ''));
175 175
         if (!is_null($this->userInfo) && !empty($result['list']) && !$hasKeyword) {
176 176
             try {
177
-                // 1. 获取用户最近的搜索关键词(去重)
177
+                // 1. 获取用户最近的搜索关键词(SQL 层去重)
178 178
                 $searchKeywords = Db::name('user_visit')
179 179
                     ->where('uid', $this->userInfo['uid'])
180 180
                     ->where('type', 'searchProduct')
181 181
                     ->where('content', '<>', '')
182
+                    ->group('content')
182 183
                     ->order('create_time', 'DESC')
183 184
                     ->limit(5)
184 185
                     ->column('content');
185
-                $searchKeywords = array_unique(array_filter($searchKeywords));
186
+                $searchKeywords = array_filter($searchKeywords);
186 187
 
187 188
                 if (!empty($searchKeywords)) {
188 189
                     // 2. 查询所有匹配的商品ID(不 limit,形成推荐池),排除已在当前列表中的