| // +------------------------------------------------+ namespace app\common\service; use app\common\model\Article as ArticleModel; use app\common\model\Category as CategoryModel; use app\common\service\Score as ScoreService; use app\common\validate\Article as ArticleValidate; class Article extends _Init { /** * 添加信息 * @param [type] $data 文章数据 */ public static function create($data) { $validate = new ArticleValidate(); if (!$validate->scene('add')->check($data)) { return $validate->getError(); } $info = ArticleModel::create($data); if ($info) { return true; } else { return '创建文章失败'; } } /** * 编辑文章 * @param [type] $data 更新的数据 * @return [type] 返回 修改的结果 */ public static function edit($data) { $validate = new ArticleValidate(); if (!$validate->scene('add')->check($data)) { return $validate->getError(); } $info = ArticleModel::update($data); if ($info) { return true; } else { return '编辑文章失败'; } } /** * 前台用户添加文章 * @param [type] $data 更新的数据 * @return [type] 返回 修改的结果 */ public static function userAdd($data) { $data = [ 'uid' => $data['uid'] ?? 0, 'title' => $data['title'] ?? '', 'content' => $data['content'] ?? '', 'description' => $data['description'] ?? '', 'category_id' => $data['category_id'] ?? '0', 'storage_id' => $data['storage_id'] ?? '0', 'thumb' => $data['thumb'] ?? '0', 'status' => $data['status'] ?? '1', 'url' => $data['url'] ?? '', 'nickname' => $data['nickname'] ?? '', 'head_img' => $data['head_img'] ?? '', 'click' => 0, ]; $validate = new ArticleValidate(); if (!$validate->scene('add')->check($data)) { return $validate->getError(); } $info = ArticleModel::create($data); if ($info) { ScoreService::addArticle($data['uid'], 'add'); return true; } else { return '文章创建失败'; } } /** * 修改文章状态 * @param [type] $id 文章id * @param [type] $status 状态 * @return [type] 返回结果 */ public static function status($id, $status) { $info = ArticleModel::get($id); if (!$info) { return '文章不存在'; } elseif ($info->save(['status' => $status])) { Logs::write('修改状态', ['status' => $status]); return true; } else { return '修改状态失败'; } } /** * 删除文章 * @param [type] $id 要删除的文章id * @return [type] 返回 结果 */ public static function del($id) { $info = ArticleModel::get($id); if (!$info) { return '文章不存在'; } elseif ($info->save(['status' => -1])) { return true; } else { return '删除失败'; } // $model = new ArticleModel(); // if ($model->destroy($id)) { // return true; // } else { // return '删除失败'; // } } /** * 获取分类列表 * @return [type] [description] */ public static function categoryList() { $map = [ 'status' => '1', 'model' => 'article', ]; return CategoryModel::where($map)->select(); } /** * 采集公众号文章 * @param [type] $url [description] * @param integer $uid [description] * @return [type] [description] */ public static function collect($url, $uid = 1) { $map = [ 'url' => $url, 'uid' => $uid, ]; $userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25'; $info = ArticleModel::where($map)->find(); if ($info) { return $info->id; } else { $info = new ArticleModel; $html = file_get_contents($url); // $html = http($url, 'GET', '', '', $userAgent); //获取文章标题 preg_match_all("/id=\"activity-name\">(.*)<\/h2>/is", $html, $title); //获取文章内容部分 preg_match_all("/id=\"js_content\">(.*)