<?php namespace Cate\Lib;
use Dever;
class Icon
{
    private $token = 'Y9rt0NCy1H4LZKhxDklRks26';
    private $cookie = 'cna=/pxEH/gQiVwCAWUeNvDgjxzu; EGG_SESS_ICONFONT=dzIFRF6UFHpWicZx2_bPclFOIt1cQpz_PZTqMLSyvEnnoZwVqWvK7ht2RRKNNSygMgEcp7bWa98XYDNW-LoTE9nMjNLdOSTa9Y6-Rn5H_k6bjWOLDvZKzgsypKSm2zK0qd29cv9I1s8YqVK5LF4J6g==; ctoken={token}; u=14130793; u.sig=PEpkMN-VcF3I7NkWH0aAoIBpIeCaAQCeG6H89fjtkgk; xlly_s=1; isg=BKioBV44WoUIjnZyvDfQlqLHeZa60QzbxRlelGLZ9CMWvUgnCuHcaz77tVVNjcSz; tfstk=fLXxt1waVz4cpbNGhiNkIfmz3cZuttI22ZSIIFYm1aQRAMuD1s4VW1QdzCDGfr5OyG_L0h4VIaeOyw1mSOIZBhs17o8g07jV0dJ_-yvTKiSq24rxizv_fg_gSTzLKJjVcmxbxgw3b6lglQ86CIObFUTycF96cCZJPhKE1q_6CuIWzH3jlhT_FUtqTcmvSmTtB1Gqp5AuiUDsCtwwMeIqsAM1endACiTRqgXJDILHZ5_u_t_lftX2aShXQgfdW1_QrjYf9HQJtsUKHwQ2fGK1kJopPtIOeI5oljtpHEdChInndiR1wTpchPlFqgLX9LfuarAMHZCe8INzzNIJoN6vNqUkS6jlhQQQrYQw616DFta7pgJAK9h_t2YpjjZ82flwG3AHndkb-EaYy3L3qbcZ_Qxy2eq82flwG3-J-u2o_fRk4';
    private $origin = 'https://www.iconfont.cn';

    # 获取图标
    public function get($name, $method = 'find')
    {
        $where['search_name'] = $name;
        $info = Dever::db('icon', 'cate')->$method($where);
        if (!$info) {
            $url = 'https://www.iconfont.cn/api/icon/search.json';
            if (strstr($name, '（')) {
                $temp = explode('（', $name);
                $name = $temp[0];
            }
            $param['q'] = $name;
            $param['sortType'] = 'updated_at';
            $param['page'] = '1';
            $param['pageSize'] = '20';
            $param['fromCollection'] = '-1';
            $param['ctoken'] = $this->token;
            $param['t'] = time();
            $header['cookie'] = str_replace('{token}', $this->token, $this->cookie);
            $header['origin'] = $this->origin;
            $data = Dever::curl($url, $param, 'post', false, $header)->result();
            $data = Dever::json_decode($data);
            if (isset($data['data']['icons'])) {
                $id = false;
                foreach ($data['data']['icons'] as $k => $v) {
                    if (stristr($v['name'], 'logo')) {
                        continue;
                    }
                    if (stristr($v['path_attributes'], 'FFFFFF')) {
                        continue;
                    }

                    $up = $where;
                    $up['name'] = $v['name'];
                    $up['svg'] = $v['show_svg'];
                    $state = Dever::db('icon', 'cate')->insert($up);
                    if (!$id) {
                        $id = $state;
                    }
                }
                if ($method == 'find') {
                    $info = Dever::db('icon', 'cate')->$method($id);
                } else {
                    $info = Dever::db('icon', 'cate')->$method($where);
                }
            }
        }
        return $info;
    }

    # 展示图标
    public function show($id)
    {
        $info = Dever::db('icon', 'cate')->find($id);
        if ($info) {
            $pattern = '/(width|height): [^;]+;/';
            $replacement = 'width: 30px; height: 30px;';
            $info['svg'] = preg_replace($pattern, $replacement, $info['svg']);
            return $info['svg'];
        } else {
            return '';
        }
    }
}