朋友圈表格导出
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace app\cunkebao\controller\wechat;
|
||||
|
||||
use app\common\controller\ExportController;
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
|
||||
@@ -145,6 +146,164 @@ class GetWechatMomentsV1Controller extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出朋友圈数据到Excel
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
try {
|
||||
$wechatId = $this->request->param('wechatId/s', '');
|
||||
if (empty($wechatId)) {
|
||||
return ResponseHelper::error('wechatId不能为空');
|
||||
}
|
||||
|
||||
// 权限校验:只能查看当前账号可访问的微信
|
||||
$accessibleWechatIds = $this->getAccessibleWechatIds();
|
||||
if (!in_array($wechatId, $accessibleWechatIds, true)) {
|
||||
return ResponseHelper::error('无权查看该微信的朋友圈', 403);
|
||||
}
|
||||
|
||||
// 获取对应的微信账号ID
|
||||
$accountId = Db::table('s2_wechat_account')
|
||||
->where('wechatId', $wechatId)
|
||||
->value('id');
|
||||
|
||||
if (empty($accountId)) {
|
||||
return ResponseHelper::error('微信账号不存在或尚未同步', 404);
|
||||
}
|
||||
|
||||
$query = Db::table('s2_wechat_moments')
|
||||
->where('wechatAccountId', $accountId);
|
||||
|
||||
// 关键词搜索
|
||||
if ($keyword = trim((string)$this->request->param('keyword', ''))) {
|
||||
$query->whereLike('content', '%' . $keyword . '%');
|
||||
}
|
||||
|
||||
// 类型筛选
|
||||
$type = $this->request->param('type', '');
|
||||
if ($type !== '' && $type !== null) {
|
||||
$query->where('type', (int)$type);
|
||||
}
|
||||
|
||||
// 时间筛选
|
||||
$startTime = $this->request->param('startTime', '');
|
||||
$endTime = $this->request->param('endTime', '');
|
||||
if ($startTime || $endTime) {
|
||||
$start = $startTime ? strtotime($startTime) : 0;
|
||||
$end = $endTime ? strtotime($endTime) : time();
|
||||
if ($start && $end && $end < $start) {
|
||||
return ResponseHelper::error('结束时间不能早于开始时间');
|
||||
}
|
||||
$query->whereBetween('createTime', [$start ?: 0, $end ?: time()]);
|
||||
}
|
||||
|
||||
// 获取所有数据(不分页)
|
||||
$moments = $query->order('createTime', 'desc')->select();
|
||||
|
||||
if (empty($moments)) {
|
||||
return ResponseHelper::error('暂无数据可导出');
|
||||
}
|
||||
|
||||
// 定义表头
|
||||
$headers = [
|
||||
'date' => '日期',
|
||||
'postTime' => '投放时间',
|
||||
'functionCategory' => '作用分类',
|
||||
'content' => '朋友圈文案',
|
||||
'selfReply' => '自回评内容',
|
||||
'displayForm' => '朋友圈展示形式',
|
||||
'image1' => '配图1',
|
||||
'image2' => '配图2',
|
||||
'image3' => '配图3',
|
||||
'image4' => '配图4',
|
||||
'image5' => '配图5',
|
||||
'image6' => '配图6',
|
||||
'image7' => '配图7',
|
||||
'image8' => '配图8',
|
||||
'image9' => '配图9',
|
||||
];
|
||||
|
||||
// 格式化数据
|
||||
$rows = [];
|
||||
foreach ($moments as $moment) {
|
||||
$resUrls = $this->decodeJson($moment['resUrls'] ?? null);
|
||||
$imageUrls = is_array($resUrls) ? $resUrls : [];
|
||||
|
||||
// 格式化日期和时间
|
||||
$createTime = !empty($moment['createTime'])
|
||||
? (is_numeric($moment['createTime']) ? $moment['createTime'] : strtotime($moment['createTime']))
|
||||
: 0;
|
||||
$date = $createTime ? date('Y年m月d日', $createTime) : '';
|
||||
$postTime = $createTime ? date('H:i', $createTime) : '';
|
||||
|
||||
// 判断展示形式
|
||||
$displayForm = '';
|
||||
if (!empty($moment['content']) && !empty($imageUrls)) {
|
||||
$displayForm = '文字+图片';
|
||||
} elseif (!empty($moment['content'])) {
|
||||
$displayForm = '文字';
|
||||
} elseif (!empty($imageUrls)) {
|
||||
$displayForm = '图片';
|
||||
}
|
||||
|
||||
$row = [
|
||||
'date' => $date,
|
||||
'postTime' => $postTime,
|
||||
'functionCategory' => '', // 暂时放空
|
||||
'content' => $moment['content'] ?? '',
|
||||
'selfReply' => '', // 暂时放空
|
||||
'displayForm' => $displayForm,
|
||||
];
|
||||
|
||||
// 分配图片到配图1-9列
|
||||
for ($i = 1; $i <= 9; $i++) {
|
||||
$imageKey = 'image' . $i;
|
||||
$row[$imageKey] = isset($imageUrls[$i - 1]) ? $imageUrls[$i - 1] : '';
|
||||
}
|
||||
|
||||
$rows[] = $row;
|
||||
}
|
||||
|
||||
// 定义图片列(配图1-9)
|
||||
$imageColumns = ['image1', 'image2', 'image3', 'image4', 'image5', 'image6', 'image7', 'image8', 'image9'];
|
||||
|
||||
// 生成文件名
|
||||
$fileName = '朋友圈投放_' . date('Ymd_His');
|
||||
|
||||
// 调用导出方法,优化图片显示效果
|
||||
ExportController::exportExcelWithImages(
|
||||
$fileName,
|
||||
$headers,
|
||||
$rows,
|
||||
$imageColumns,
|
||||
'朋友圈投放',
|
||||
[
|
||||
'imageWidth' => 120, // 图片宽度(像素)
|
||||
'imageHeight' => 120, // 图片高度(像素)
|
||||
'imageColumnWidth' => 18, // 图片列宽(Excel单位)
|
||||
'rowHeight' => 130, // 行高(像素)
|
||||
'columnWidths' => [ // 特定列的固定宽度
|
||||
'date' => 15, // 日期列宽
|
||||
'postTime' => 12, // 投放时间列宽
|
||||
'functionCategory' => 15, // 作用分类列宽
|
||||
'content' => 40, // 朋友圈文案列宽(自动调整可能不够)
|
||||
'selfReply' => 30, // 自回评内容列宽
|
||||
'displayForm' => 18, // 朋友圈展示形式列宽
|
||||
],
|
||||
'titleRow' => [ // 标题行内容(第一行)
|
||||
'朋友圈投放',
|
||||
'我能提供什么价值? (40%) 有谁正在和我合作 (20%) 如何和我合作? (20%) 你找我合作需要付多少钱? (20%)'
|
||||
]
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('导出失败:' . $e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化朋友圈数据
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user