作者 何书鹏

搜索历史-清空

@@ -5,6 +5,7 @@ namespace app\api\controller; @@ -5,6 +5,7 @@ namespace app\api\controller;
5 use app\api\model\Category; 5 use app\api\model\Category;
6 use app\api\model\Goods; 6 use app\api\model\Goods;
7 use app\api\model\News; 7 use app\api\model\News;
  8 +use app\api\model\UserSearch;
8 use app\common\controller\Api; 9 use app\common\controller\Api;
9 use think\Config; 10 use think\Config;
10 use think\Db; 11 use think\Db;
@@ -246,7 +247,16 @@ class Index extends Api @@ -246,7 +247,16 @@ class Index extends Api
246 $item['line_price'] = $goods_spec['line_price']; 247 $item['line_price'] = $goods_spec['line_price'];
247 }); 248 });
248 } 249 }
249 - Db::name('user_search')->insert(['user_id'=>$this->auth->id,'text'=>$text,'createtime'=>time()]); 250 + // 搜索记录
  251 + $search_text = UserSearch::where(['user_id'=>$this->auth->id,'text'=>$text])->find();
  252 + if($search_text){
  253 + $search_text->save(['updatetime' => time()]);
  254 + }else{
  255 + UserSearch::create([
  256 + 'user_id' => $this->auth->id,
  257 + 'text' => $text
  258 + ]);
  259 + }
250 $this->success('搜索',$list); 260 $this->success('搜索',$list);
251 } 261 }
252 262
@@ -258,17 +268,15 @@ class Index extends Api @@ -258,17 +268,15 @@ class Index extends Api
258 * @ApiReturn ({ 268 * @ApiReturn ({
259 'code':'1', 269 'code':'1',
260 'msg':'搜索历史' 270 'msg':'搜索历史'
261 - 'data':{  
262 - 271 + 'data':null
263 }) 272 })
264 */ 273 */
265 public function searchHistory() 274 public function searchHistory()
266 { 275 {
267 if ($this->auth->isLogin()){ 276 if ($this->auth->isLogin()){
268 - $list = Db::name('user_search')  
269 - ->where('user_id',$this->auth->id) 277 + $list = UserSearch::where('user_id',$this->auth->id)
270 ->limit(10) 278 ->limit(10)
271 - ->order('id','desc') 279 + ->order('updatetime','desc')
272 ->column('text'); 280 ->column('text');
273 }else{ 281 }else{
274 $list = []; 282 $list = [];
@@ -276,4 +284,19 @@ class Index extends Api @@ -276,4 +284,19 @@ class Index extends Api
276 $this->success('搜索历史',$list); 284 $this->success('搜索历史',$list);
277 } 285 }
278 286
  287 + /**
  288 + * @ApiTitle (搜索历史-清空)
  289 + * @ApiMethod (POST)
  290 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  291 + * @ApiReturn ({
  292 + 'code':'1',
  293 + 'msg':'成功'
  294 + 'data':null
  295 + })
  296 + */
  297 + public function searchHistoryClear()
  298 + {
  299 + UserSearch::where('user_id',$this->auth->id)->delete();
  300 + $this->success('成功');
  301 + }
279 } 302 }