1
0
Files
helper/application/common/service/Article.php
2020-08-06 14:58:51 +08:00

266 lines
9.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
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\">(.*)<script/iUs", $html, $content, PREG_PATTERN_ORDER);
//格式化标题
$title = str_replace("\r\n", "", str_replace(" ", "", $title[1][0]));
//拼接正确的内容标签
$content = "<div id='js_content' class='ce-padding-sm'>" . $content[1][0];
//将所有图片的data-src更改为src
$content = str_replace("data-src", "src", $content);
$content = str_replace('0?"', '0"', $content);
//获取所有图片地址
$pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
preg_match_all('/<img.*?src="(.*?)".*?>/is', $content, $img);
//遍历所有图片,采集到服务器
$i = 0;
$thumb = '';
$img_pre = uniqid(); // 确保每篇文章的图片前缀一致,路径按照日期存储
$dir = './uploads/collect/' . date('Y-m/d/');
foreach ($img[1] as $key => $value) {
//判断图片是否存在http头
if (preg_match('/(http:\/\/)|(https:\/\/)/i', $value)) {
//分割微信图片地址
$str = explode('/', $value);
//获取图片地址名称,不包括拓展名
$allname = $str[4];
//识别拓展名
if (strrpos($value, 'wx_fmt=') > 0) {
$ext = substr($value, strrpos($value, 'wx_fmt=') + 7);
$allname = $img_pre . '_' . $i . '.' . $ext;
}
//拓展名拼接图片名称
//获取图片数据
// $image = file_get_contents($value);
$image = http($value, 'GET', '', '', $userAgent);
//保存服务器目录文件
try {
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
file_put_contents($dir . $allname, $image);
//更替图片文件地址为服务器图片地址
$content = str_replace($value, ltrim($dir, '.') . $allname, $content);
if ($i == 0) {
$thumb = ltrim($dir, '.') . $allname;
}
$i++;
} catch (Exception $e) {
continue;
}
}
}
//修正视频内容
$content = str_replace("preview.html", "player.html", $content);
//重新设置 iframe
preg_match('/<iframe[^>]*\s+src="([^"]*)"[^>]*>/is', $content, $matched);
if (!empty($matched[0])) {
$src = explode('&', $matched['1']);
$str = '<p style="max-width:100%; margin:14px"><iframe frameborder="0" src="' . $src['0'] . '&auto=0" style="z-index: 1; width: 100% ! important; height: 231.75px ! important; overflow: hidden;" class="video_iframe" scrolling="no"></iframe></p>';
$content = str_replace($matched['0'], $str, $content);
}
//获取公众号名称
preg_match_all('/var nickname = \"(.*?)\";/si', $html, $m);
$nickname = $m[1][0];
//获取公众号头像
preg_match_all('/var round_head_img = \"(.*?)\";/si', $html, $m);
$head_img = $m[1][0];
$data = [
'title' => $title,
'content' => $content,
'description' => '',
'category_id' => 0,
'storage_id' => 0,
'thumb' => $thumb,
'status' => 1,
'create_time' => time(),
'update_time' => 0,
'url' => $url,
'nickname' => $nickname,
'head_img' => $head_img,
'uid' => $uid,
'click' => 0,
];
$info->save($data);
if ($info) {
ScoreService::addArticle($data['uid'], 'collect');
return $info->id;
} else {
return false;
}
}
}
}