fastadmin标签链接更换成ID查询,或者关键词拼音标签查询

提供几处修改的代码,直接替换即可!如果可用,请给此文章点个赞,鼓励下!

文章基础表archives增加个字段tagids,包含关键词ID和关键词name,字符串(id|name,id|name,*****

application/admin/model/cms/Archives.php找到如下对比修改。

if (isset($row['tags'])) {
    $tags = array_filter(explode(',', $row['tags']));
    if ($tags) {
        $tags_list = $tags;
        $tagslist = Tags::where('name', 'in', $tags)->select();
        foreach ($tagslist as $k => $v) {
            $archives = explode(',', $v['archives']);
            if (!in_array($row['id'], $archives)) {
                $archives[] = $row['id'];
                $v->archives = implode(',', $archives);
                $v->nums++;
                $v->save();
            }
            $tags = array_udiff($tags, [$v['name']], 'strcasecmp');
        }
        $list = [];
        foreach ($tags as $k => $v) {
            $list[] = ['name' => $v, 'archives' => $row['id'], 'nums' => 1];
        }
        if ($list) {
            (new Tags())->saveAll($list);
        }

        //扩展标签tagids
        if ($tags_list) {
            $tags_list_arr = Tags::where('name', 'in', $tags_list)->select();
            $tag_arr = [];
            foreach ($tags_list_arr as $key=>$value_arr){
                $tag_arr[] = $value_arr['id'].'|'.$value_arr['name'];
            }
            $row->where('id', $row['id'])->update(['tagids' => implode(',', $tag_arr)]);
        }

    }
}

文章页面标签链接更新,如下代码:/addons/cms/model/Archives.php

public function getTagslistAttr($value, &$data)
{
    if (isset($data['tagslist'])) {
        return $data['tagslist'];
    }
    $list = [];
    foreach (array_filter(explode(",", $data['tagids'])) as $k => $v) {
        $value_s = explode('|', $v);
        $list[] = ['name' => $value_s[1], 'url' => addon_url('cms/tags/index', [':id' => $value_s[0]])];
    }
    $data['tagslist'] = $list;
    return $list;
}

标签控制器代码修改:/addons/cms/controller/Tags.php

<?php

namespace addons\cms\controller;

use addons\cms\model\Archives;
use addons\cms\model\Tags as TagsModel;

/**
 * 标签控制器
 * Class Tags
 * @package addons\cms\controller
 */
class Tags extends Base
{
    public function index()
    {
        // $tags = null;
        // $name = $this->request->param('name');
        // if ($name) {
        //     $tags = TagsModel::getByName($name);
        // }
        $tags = null;
        $id = $this->request->param('id');
        if ($id) {
            $tags = TagsModel::getById($id);
        }
        if (!$tags) {
            $this->error(__('No specified tags found'));
        }

        $filterlist = [];
        $orderlist = [];

        $orderby = $this->request->get('orderby', '');
        $orderway = $this->request->get('orderway', '', 'strtolower');
        $params = [];
        if ($orderby) {
            $params['orderby'] = $orderby;
        }
        if ($orderway) {
            $params['orderway'] = $orderway;
        }
        $sortrank = [
            ['name' => 'default', 'field' => 'weigh', 'title' => __('Default')],
            ['name' => 'views', 'field' => 'views', 'title' => __('Views')],
            ['name' => 'id', 'field' => 'id', 'title' => __('Post date')],
        ];

        $orderby = $orderby && in_array($orderby, ['default', 'id', 'views']) ? $orderby : 'default';
        $orderway = $orderway ? $orderway : 'desc';
        foreach ($sortrank as $k => $v) {
            $url = '?' . http_build_query(array_merge($params, ['orderby' => $v['name'], 'orderway' => ($orderway == 'desc' ? 'asc' : 'desc')]));
            $v['active'] = $orderby == $v['name'] ? true : false;
            $v['orderby'] = $orderway;
            $v['url'] = $url;
            $orderlist[] = $v;
        }
        $orderby = $orderby == 'default' ? 'weigh DESC,id DESC' : $orderby;
        $pagelist = Archives::with(['channel'])
            ->where('status', 'normal')
            ->where('id', 'in', explode(',', $tags['archives']))
            ->order($orderby, $orderway)
            ->paginate(10, false, ['type' => '\\addons\\cms\\library\\Bootstrap']);
        $pagelist->appends($params);
        $this->view->assign("__FILTERLIST__", $filterlist);
        $this->view->assign("__ORDERLIST__", $orderlist);
        $this->view->assign("__TAGS__", $tags);
        $this->view->assign("__PAGELIST__", $pagelist);
        \think\Config::set('cms.title', $tags['name']);
        return $this->view->fetch('/tags');
    }
}bi/addons/cms/model/tags.php

标签模型链接修改:/addons/cms/model/Tags.php

public function getUrlAttr($value, $data)
{
    // $name = $data['name'] ? $data['name'] : $data['id'];
    // return addon_url('cms/tags/index', [':id' => $data['id'], ':name' => $name], true);
    $name = $data['id'] ? $data['id'] : $data['name'];
    return addon_url('cms/tags/index', [':id' => $data['id'], ':name' => $name], true);
}

最后补充:

插件后台管理,更新关键词链接name修改为:id。

点赞(2) 打赏

Comment list 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部