<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Usercenter extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //我的搜索历史
    public function search_history(){
        $list = Db::name('user_search')->where('user_id',$this->auth->id)->order('id desc')->select();

        //只留最近10条
        if(!empty($list) && count($list) > 10){
            $list = array_chunk($list,10);
            $list = $list[0];

            $rs_ids = array_column($list,'id');

            Db::name('user_search')->where('user_id',$this->auth->id)->where('id','NOT IN',$rs_ids)->delete();
        }

        $this->success(1,$list);
    }

    //清空搜索历史
    public function search_clear(){
        Db::name('user_search')->where('user_id',$this->auth->id)->delete();
        $this->success(1);
    }

}
