421 lines
15 KiB
PHP
421 lines
15 KiB
PHP
<?php
|
|
|
|
|
|
namespace Yunshop\Wechat\service;
|
|
|
|
use app\common\models\UniAccount;
|
|
use app\common\facades\Setting;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yunshop\Wechat\admin\material\model\WechatAttachmentImage;
|
|
use Yunshop\Wechat\common\helper\Helper;
|
|
use app\common\helpers\Url;
|
|
use Exception;
|
|
use Yunshop\Wechat\common\model\WechatNews;
|
|
use Yunshop\Wechat\common\model\WechatNewsExamine;
|
|
use Yunshop\Wechat\common\model\WechatNewsExamineArticle;
|
|
|
|
// 主要用于更新用户信息
|
|
class DraftService
|
|
{
|
|
|
|
/**
|
|
* @param $id
|
|
* @return array
|
|
* 刷新审核结果
|
|
*/
|
|
public function refreshPublish($id = null)
|
|
{
|
|
$query = WechatNewsExamine::uniacid()->where('status', 0);
|
|
if ($id) {
|
|
$query->where('id', $id);
|
|
}
|
|
$list = $query->get()->toArray();
|
|
|
|
$material = self::getRequestClass();
|
|
foreach ($list as $v) {
|
|
|
|
$res = $material->checkArticleExamine($v['publish_id']);
|
|
if (!$res['publish_id']) {
|
|
\Log::debug('查询微信文章发布结果异常' . $v['publish_id'], $res);
|
|
continue;
|
|
}
|
|
if ($res['publish_status'] === 1) {
|
|
continue;
|
|
} elseif ($res['publish_status'] === 0) {
|
|
$res = self::refreshExamine($v['id']);
|
|
\Log::debug('刷新微信文章异常' . $v['publish_id'], $res);
|
|
} elseif ($res['publish_status'] === 2) {
|
|
WechatNewsExamine::where('id', $v['id'])->update([
|
|
'examine_time' => time(),
|
|
'update_time' => time(),
|
|
'examine_status' => $v['publish_status'],
|
|
'status' => -1,
|
|
]);
|
|
}
|
|
|
|
|
|
}
|
|
return self::returnArr(1, '成功');
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return array
|
|
* 删除已发布文章
|
|
*/
|
|
public static function deleteExamine($id, $article_id = null)
|
|
{
|
|
try {
|
|
$index = null;
|
|
$material = self::getRequestClass();
|
|
if (!$examine = WechatNewsExamine::uniacid()->find($id)) {
|
|
throw new Exception('文章不存在');
|
|
}
|
|
if ($examine->status == 0) {
|
|
throw new Exception('审核中无法删除');
|
|
}
|
|
if ($article_id) {
|
|
if ($article = WechatNewsExamineArticle::uniacid()->find($article_id)) {
|
|
$index = $article->displayorder;
|
|
if ($examine->status == 1) {
|
|
$res = $material->unsetArticle($examine->article_id, $index);
|
|
if ($res['errcode'] !== 0) {
|
|
throw new Exception('删除失败:' . ($res['errmsg'] ?: '未知原因'));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (!$index) {
|
|
WechatNewsExamineArticle::uniacid()->where('examine_id', $examine->id)->delete();
|
|
$examine->delete();
|
|
} elseif ($examine->status == 1) {
|
|
$res = self::refreshExamine($examine->id);
|
|
if (!$res['result']) {
|
|
throw new Exception($res['msg']);
|
|
}
|
|
} else {
|
|
$article->delete();
|
|
if (!WechatNewsExamineArticle::uniacid()->where('examine_id', $id)->value('id')) {
|
|
$examine->delete();
|
|
}
|
|
}
|
|
return self::returnArr(1, '成功');
|
|
} catch (Exception $e) {
|
|
return self::returnArr(0, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $examine_id
|
|
* @param $offset
|
|
* @param $count
|
|
* @param $no_content
|
|
* @return array
|
|
* 获取已发布文章
|
|
*/
|
|
public static function getExamine($examine_id = null, $offset = 0, $count = 20, $no_content = 0)
|
|
{
|
|
try {
|
|
$material = self::getRequestClass();
|
|
if ($examine_id) {
|
|
if (!$examine = WechatNewsExamine::uniacid()->find($examine_id)) {
|
|
throw new Exception('文章不存在');
|
|
}
|
|
$res = $material->getArticleOne($examine->article_id);
|
|
if (!$res['news_item']) {
|
|
$msg = '';
|
|
if ($res['errcode']) $msg .= "获取微信草稿失败,{$res['errcode']}:";
|
|
if ($res['errmsg']) $msg .= $res['errmsg'];
|
|
throw new Exception('获取已发布文章失败:' . $msg);
|
|
}
|
|
$res = [
|
|
'article_id' => $examine->article_id,
|
|
'content' => $res,
|
|
];
|
|
} else {
|
|
$res = $material->getArticleList($offset, $count, $no_content);
|
|
if (!isset($res['total_count'])) {
|
|
$msg = '';
|
|
if ($res['errcode']) $msg .= "获取微信草稿失败,{$res['errcode']}:";
|
|
if ($res['errmsg']) $msg .= $res['errmsg'];
|
|
throw new Exception('获取已发布文章列表失败:' . $msg);
|
|
}
|
|
}
|
|
return self::returnArr(1, '成功', $res);
|
|
} catch (Exception $e) {
|
|
return self::returnArr(0, $e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $examine_id
|
|
* @return array
|
|
* 刷新已发布文章列表
|
|
*/
|
|
public static function refreshExamine($examine_id = null)
|
|
{
|
|
try {
|
|
$res = self::getExamine($examine_id, 0, 1, 1);
|
|
if (!$res['result']) {
|
|
throw new Exception($res['msg']);
|
|
}
|
|
if ($examine_id) {
|
|
$data = [$res['data']];
|
|
self::changeExamine($data);
|
|
} else {
|
|
$article_ids = [];
|
|
$count = 20;
|
|
$max = $res['data']['total_count'];
|
|
$offset = 0;
|
|
while ($offset < $max) {
|
|
$res = self::getExamine(null, $offset, $count);
|
|
if (!$res['result']) {
|
|
throw new Exception($res['msg']);
|
|
}
|
|
$article_ids = array_merge($article_ids, array_column($res['data']['item'], 'article_id'));
|
|
self::changeExamine($res['data']['item']);
|
|
unset($res);
|
|
$offset += $count;
|
|
};
|
|
|
|
if ($article_ids && $delete_ids = WechatNewsExamine::uniacid()->status(1)->whereNotIn('article_id', $article_ids)->select('id')->pluck('id')->toArray()) {
|
|
WechatNewsExamineArticle::whereIn('examine_id', $delete_ids)->delete();
|
|
WechatNewsExamine::whereIn('id', $delete_ids)->delete();
|
|
}
|
|
|
|
}
|
|
return self::returnArr(1, '成功');
|
|
} catch (Exception $e) {
|
|
return self::returnArr(0, $e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static function changeExamine(&$examine_data = [])
|
|
{
|
|
foreach ($examine_data as $k => &$v) {
|
|
$is_deleted = !in_array(0, array_column($v['content']['news_item'], 'is_deleted'));
|
|
if (!$examine = WechatNewsExamine::uniacid()->where('article_id', $v['article_id'])->first()) {
|
|
if ($is_deleted) {
|
|
continue;
|
|
}
|
|
$examine = new WechatNewsExamine([
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'status' => 1,
|
|
'examine_status' => 0,
|
|
'article_id' => $v['article_id'],
|
|
'examine_time' => time(),
|
|
]);
|
|
} elseif ($is_deleted) {
|
|
WechatNewsExamineArticle::where('examine_id', $examine->id)->delete();
|
|
$examine->delete();
|
|
continue;
|
|
}
|
|
$examine->update_time = $v['update_time'];
|
|
$examine->save();
|
|
|
|
$insert_data = [];
|
|
$articles = WechatNewsExamineArticle::uniacid()->where('examine_id', $examine->id)->get();
|
|
foreach ($v['content']['news_item'] as $kk => &$vv) {
|
|
$this_article = $articles->where('displayorder', $kk + 1)->first();
|
|
if ($vv['is_deleted'] && $this_article) {
|
|
WechatNewsExamineArticle::where('id', $this_article->id)->delete();
|
|
continue;
|
|
}
|
|
$update_data = [
|
|
'title' => $vv['title'] ?: '',
|
|
'author' => $vv['author'] ?: '',
|
|
'digest' => $vv['digest'] ?: '',
|
|
'content' => $vv['content'] ?: '',
|
|
'content_source_url' => $vv['content_source_url'] ?: '',
|
|
'thumb_media_id' => $vv['thumb_media_id'] ?: '',
|
|
'show_cover_pic' => $vv['show_cover_pic'] ? 1 : 0,
|
|
'need_open_comment' => $vv['need_open_comment'] ? 1 : 0,
|
|
'only_fans_can_comment' => $vv['only_fans_can_comment'] ? 1 : 0,
|
|
'url' => $vv['url'] ?: '',
|
|
'displayorder' => $kk + 1,
|
|
];
|
|
|
|
if (($thumb = WechatAttachmentImage::uniacid()->where('media_id', $vv['thumb_media_id'])->first()) && $thumb->attachment) {
|
|
$update_data['thumb_url'] = $thumb->attachment;
|
|
}
|
|
if ($this_article) {
|
|
$this_article->fill($update_data);
|
|
$this_article->save();
|
|
} else {
|
|
$insert_data[] = array_merge([
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'examine_id' => $examine->id,
|
|
'thumb_url' => '',
|
|
'created_at' => time(),
|
|
'updated_at' => time(),
|
|
], $update_data);
|
|
}
|
|
}
|
|
unset($vv);
|
|
if ($insert_data) {
|
|
WechatNewsExamineArticle::insert($insert_data);
|
|
}
|
|
}
|
|
unset($v);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $attachment
|
|
* @return array
|
|
* 发布文章
|
|
*/
|
|
public static function createExamine($attachment)
|
|
{
|
|
$material = self::getRequestClass();
|
|
$news = $attachment->hasManyWechatNews;
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$examine = WechatNewsExamine::create([
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
]);
|
|
$insert_data = [];
|
|
$time = time();
|
|
foreach ($news->toArray() as $k => $v) {
|
|
$insert_data[] = [
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'examine_id' => $examine->id,
|
|
'thumb_url' => $v['thumb_url'],
|
|
'title' => $v['title'],
|
|
'author' => $v['author'],
|
|
'digest' => $v['digest'],
|
|
'content' => $v['content'],
|
|
'content_source_url' => $v['content_source_url'],
|
|
'thumb_media_id' => $v['thumb_media_id'],
|
|
'show_cover_pic' => $v['show_cover_pic'],
|
|
'need_open_comment' => $v['need_open_comment'],
|
|
'only_fans_can_comment' => $v['only_fans_can_comment'],
|
|
'url' => $v['url'],
|
|
'displayorder' => $k + 1,
|
|
'created_at' => $time,
|
|
'updated_at' => $time,
|
|
];
|
|
}
|
|
if (!$insert_data) {
|
|
throw new Exception('请编辑文章详情');
|
|
}
|
|
WechatNewsExamineArticle::insert($insert_data);
|
|
|
|
$res = $material->pushArticle($attachment->media_id);
|
|
if ($res['errcode'] !== 0) {
|
|
throw new Exception($res['errmsg']);
|
|
}
|
|
$examine->publish_id = $res['publish_id'];
|
|
$examine->save();
|
|
DB::commit();
|
|
|
|
return self::returnArr(1, '成功');
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
return self::returnArr(0, $e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $has_many_wechat_news
|
|
* @return array
|
|
* 创建草稿
|
|
*/
|
|
public static function createArticle($has_many_wechat_news)
|
|
{
|
|
// 重新查询图文列表hasManyWechatNews
|
|
$material = self::getRequestClass();
|
|
$newsList = [];
|
|
foreach ($has_many_wechat_news as $news) {
|
|
$news['show_cover'] = $news['show_cover_pic'];
|
|
unset($news['show_cover_pic']);//升级easywechat4.0 参数改为show_cover
|
|
$newsList[] = new \EasyWeChat\Kernel\Messages\Article($news);
|
|
}
|
|
if (empty($newsList)) {
|
|
return self::returnArr(0, '缺少图文数据!');
|
|
}
|
|
try {
|
|
$media = $material->uploadArticle($newsList);
|
|
} catch (\Exception $exception) {
|
|
return self::returnArr(0, '上传微信数据失败!' . Helper::getErrorMessage($exception->getCode(), $exception->getMessage()));
|
|
}
|
|
if (empty($media['media_id'])) {
|
|
\Log::debug('--------wechat_message--------', $media);
|
|
return self::returnArr(0, '上传微信失败,无法获取media_id');
|
|
} else {
|
|
return self::returnArr(1, '成功', $media['media_id']);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $media_id
|
|
* @param $has_many_wechat_news
|
|
* @return array
|
|
* 编辑草稿
|
|
*/
|
|
public static function updateArticle($media_id, $has_many_wechat_news)
|
|
{
|
|
$material = self::getRequestClass();
|
|
for ($i = 0; $i < count($has_many_wechat_news); $i++) {
|
|
try {
|
|
$res = $material->updateArticle($media_id, $has_many_wechat_news[$i], $i);
|
|
if ($res['errcode'] !== 0){
|
|
throw new Exception($res['errmsg']);
|
|
}
|
|
} catch (\Exception $exception) {
|
|
return self::returnArr(0, '更新微信数据失败:' .$exception->getMessage());
|
|
}
|
|
}
|
|
return self::returnArr(1, '成功');
|
|
}
|
|
|
|
/**
|
|
* @param $media_id
|
|
* @return array
|
|
*获取草稿
|
|
*/
|
|
public static function getArticle($media_id)
|
|
{
|
|
$material = self::getRequestClass();
|
|
$res = $material->getArticle($media_id);
|
|
if (!$res['news_item']) {
|
|
$msg = '';
|
|
if ($res['errcode']) $msg .= "获取微信草稿失败,{$res['errcode']}:";
|
|
if ($res['errmsg']) $msg .= $res['errmsg'];
|
|
return self::returnArr(0, $msg, []);
|
|
} else {
|
|
return self::returnArr(1, '成功', $res['news_item']);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $class_name
|
|
* @return \EasyWeChat\OfficialAccount\Application|mixed
|
|
* 获取esayWechat请求类
|
|
*/
|
|
public static function getRequestClass($class_name = 'material')
|
|
{
|
|
$wechatApp = \app\common\facades\EasyWeChat::officialAccount();
|
|
if ($class_name) {
|
|
$wechatApp = $wechatApp->$class_name;
|
|
}
|
|
return $wechatApp;
|
|
}
|
|
|
|
public static function returnArr($result, $msg = '', $data = [])
|
|
{
|
|
return ['result' => $result, 'msg' => $msg, 'data' => $data];
|
|
}
|
|
} |