| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 | <?phpdeclare(strict_types=1);/** * +---------------------------------------------------------------------- * 后台文章控制制器 * @author huwhis@163.com * @version   0.0.1 * +---------------------------------------------------------------------- */namespace app\controller\sys;// 引入框架内置类use think\Exception;use think\facade\Db;use think\facade\View;use app\model\Category as CategoryModel;use app\model\Article as ArticleModel;use app\model\TagArticle as TagArticleModel;use app\utils\FileUtils;use app\utils\ReUtils;class Article extends Base{    protected $modelName = "Article";    public function index()    {        $categories = CategoryModel::select();        $category_tree = list_tree($categories);        $cid = $this->app->request->param('cid');        $params = $this->app->request->param();        $list = ArticleModel::queryPage($params);        View::assign([            'category_tree' => $category_tree,            'list' => $list,            'cid' => $cid        ]);        return View::fetch();    }    public function save($content_type = 0, $id = 0)    {        if ($this->app->request->isPost()) {            $params = $this->app->request->param();            if (!$params['cid']) {                $this->error('请选择栏目');            }            if ($params['title'] == '') {                $this->error("标题不能为空");            }            if ($params['titlepic'] && preg_match('/(http[s]:\/\/.*)/isU', $params['titlepic'])) {                $params['titlepic'] = FileUtils::downloadUrlImg($params['titlepic']);            }            $params['content'] =  isset($params['content']) ? $params['content'] : '';            $params['content'] = $this->saveRomteImage($params['content']);            $params['keywords'] = trim($params['keywords']);            try {                if ($params['id'] != 0) {                    $oldKeywords = ArticleModel::getKeywordsById((int) $params['id']);                    $article = ArticleModel::update($params);                    $oldTags = explode(",", $oldKeywords);                    $newTags = explode(",", $article->keywords);                    $addTas = array_diff($newTags, $oldTags);                    $subTas = array_diff($oldTags, $newTags);                    TagArticleModel::saveArticleTag($addTas, (int) $article->id, (int)$article->cid);                    TagArticleModel::delArticleTag($subTas, (int) $article->id);                } else {                    $params['userid'] = $this->getSysUser()->userid;                    $params['username'] = $this->getSysUser()->nickname != "" ? $this->getSysUser()->nickname : $this->getSysUser()->username;                    unset($params['id']);                    $article = ArticleModel::create($params);                    $tags = explode(",", $article->keywords);                    TagArticleModel::saveArticleTag($tags, (int) $article->id, (int) $article->cid);                }            } catch (\Exception $e) {                $this->error("错误提示:" . $e->getMessage());            }            $this->success('操作成功', (string) url('/sys/article/index'));        } else {            if ($id) {                $data = ArticleModel::find($id);            } else {                $data = new ArticleModel();                $data->content_type = $content_type;            }            $categories = CategoryModel::field('id, parent_id, name')->select();            View::assign('category_tree', list_tree($categories));            View::assign('data', $data);            $template = $content_type ? 'savemd' : 'save';            return View::fetch($template);        }    }    protected function saveRomteImage($content)    {        $content = stripslashes($content);        $img_array = [];        // 匹配所有远程图片        $pattern = '/src="(http[s]:\/\/.*)"/isU';        preg_match_all($pattern, $content, $img_array);        // 删除重复 url        $img_arrays = array_unique($img_array[1]);        foreach ($img_arrays as $value) {            $filename = FileUtils::downloadUrlImg($value);            $content = str_replace($value, $filename, $content);        }        return $content;    }    public function move()    {        $ids = $this->request->param('ids');        $cid = $this->request->param('cid');        $tablename = ArticleModel::getTable();        $idss = implode(',', $ids);        $sql = "update " . $tablename . " set cid=" . $cid . " where id IN (" . $idss . ");";        try {            Db::execute($sql);        } catch (Exception $e) {            return ReUtils::error($e->getMessage());        }        return ReUtils::success();    }    /**     * 删除     * @param int|array $id  info id     * @return array     */    public function delete($id)    {        if ($this->request->isPost()) {            if (ArticleModel::destroy($id)) {                if (is_array($id)) {                    TagArticleModel::where('aid',"IN",$id)->delete();                } else {                    TagArticleModel::where('aid',"=",$id)->delete();                }                return ReUtils::success();            } else {                return ReUtils::error();            }        }    }}
 |