朋友圈自动同步

This commit is contained in:
wong
2025-04-11 16:15:48 +08:00
parent 2411bb6d05
commit 0cd689e6ae
20 changed files with 1862 additions and 207 deletions

View File

@@ -115,7 +115,7 @@ class WechatFriendController extends BaseController
'additionalPicture' => $item['additionalPicture'],
'desc' => $item['desc'],
'country' => $item['country'],
'province' => isset($item['province']) ? $item['province'] : '',
'privince' => isset($item['privince']) ? $item['privince'] : '',
'city' => isset($item['city']) ? $item['city'] : '',
'createTime' =>isset($item['createTime']) ? $item['createTime'] : '',
'updateTime' => time()

View File

@@ -8,6 +8,7 @@ use think\console\Output;
use think\facade\Log;
use think\Queue;
use app\job\WechatFriendJob;
use think\facade\Cache;
class WechatFriendCommand extends Command
{
@@ -22,12 +23,16 @@ class WechatFriendCommand extends Command
$output->writeln('开始处理微信列表任务...');
try {
// 初始页码
$pageIndex = 0;
// 从缓存获取初始页码和上次处理的好友ID缓存10分钟有效
$pageIndex = Cache::get('friendsPage', 0);
$preFriendId = Cache::get('preFriendId', '');
$output->writeln('从缓存获取页码:' . $pageIndex . '上次处理的好友ID' . ($preFriendId ?: '无'));
$pageSize = 1000; // 每页获取1000条记录
// 将第一页任务添加到队列
$this->addToQueue($pageIndex, $pageSize);
// 将任务添加到队列
$this->addToQueue($pageIndex, $pageSize, $preFriendId);
$output->writeln('微信列表任务已添加到队列');
} catch (\Exception $e) {
@@ -43,12 +48,14 @@ class WechatFriendCommand extends Command
* 添加任务到队列
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
* @param string $preFriendId 上一个好友ID
*/
protected function addToQueue($pageIndex, $pageSize)
protected function addToQueue($pageIndex, $pageSize, $preFriendId = '')
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
'pageSize' => $pageSize,
'preFriendId' => $preFriendId
];
// 添加到队列,设置任务名为 wechat_friends

View File

@@ -54,5 +54,7 @@ Route::group('v1/', function () {
Route::post('update-status', 'app\\cunkebao\\controller\\WorkbenchController@updateStatus'); // 更新工作台状态
Route::delete('delete', 'app\\cunkebao\\controller\\WorkbenchController@delete'); // 删除工作台
Route::post('copy', 'app\\cunkebao\\controller\\WorkbenchController@copy'); // 拷贝工作台
Route::get('detail', 'app\\cunkebao\\controller\\WorkbenchController@detail'); // 获取工作台详情
Route::post('update', 'app\\cunkebao\\controller\\WorkbenchController@update'); // 更新工作台
});
})->middleware(['jwt']);

View File

@@ -0,0 +1,298 @@
<?php
namespace app\cunkebao\controller;
use app\cunkebao\model\ContentLibrary;
use app\cunkebao\model\ContentItem;
use think\Controller;
use think\Db;
/**
* 内容库控制器
*/
class ContentLibraryController extends Controller
{
/**
* 获取内容库列表
* @return \think\response\Json
*/
public function getList()
{
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$keyword = $this->request->param('keyword', '');
$where = [
['userId', '=', $this->request->userInfo['id']]
];
// 添加名称模糊搜索
if ($keyword !== '') {
$where[] = ['name', 'like', '%' . $keyword . '%'];
}
$list = ContentLibrary::where($where)
->field('id,name,description,createTime,updateTime')
->order('id', 'desc')
->page($page, $limit)
->select();
$total = ContentLibrary::where($where)->count();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $list,
'total' => $total,
'page' => $page,
'limit' => $limit
]
]);
}
/**
* 获取内容库详情
* @param int $id 内容库ID
* @return \think\response\Json
*/
public function detail($id)
{
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
$library = ContentLibrary::where([
['id', '=', $id],
['userId', '=', $this->request->userInfo['id']]
])
->field('id,name,description,createTime,updateTime')
->find();
if (empty($library)) {
return json(['code' => 404, 'msg' => '内容库不存在']);
}
// 获取内容项目
$items = ContentItem::where('libraryId', $id)->select();
$library['items'] = $items;
return json(['code' => 200, 'msg' => '获取成功', 'data' => $library]);
}
/**
* 创建内容库
* @return \think\response\Json
*/
public function create()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
// 获取请求参数
$param = $this->request->post();
// 简单验证
if (empty($param['name'])) {
return json(['code' => 400, 'msg' => '内容库名称不能为空']);
}
Db::startTrans();
try {
// 创建内容库
$library = new ContentLibrary;
$library->name = $param['name'];
$library->description = isset($param['description']) ? $param['description'] : '';
$library->userId = $this->request->userInfo['id'];
$library->companyId = $this->request->userInfo['companyId'];
$library->save();
// 如果有内容项目,也一并创建
if (!empty($param['items']) && is_array($param['items'])) {
foreach ($param['items'] as $item) {
$contentItem = new ContentItem;
$contentItem->libraryId = $library->id;
$contentItem->type = $item['type'];
$contentItem->title = $item['title'] ?? '';
$contentItem->contentData = $item['contentData'];
$contentItem->save();
}
}
Db::commit();
return json(['code' => 200, 'msg' => '创建成功', 'data' => ['id' => $library->id]]);
} catch (\Exception $e) {
Db::rollback();
return json(['code' => 500, 'msg' => '创建失败:' . $e->getMessage()]);
}
}
/**
* 更新内容库
* @return \think\response\Json
*/
public function update()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
// 获取请求参数
$param = $this->request->post();
// 简单验证
if (empty($param['id'])) {
return json(['code' => 400, 'msg' => '参数错误']);
}
if (empty($param['name'])) {
return json(['code' => 400, 'msg' => '内容库名称不能为空']);
}
// 查询内容库是否存在
$library = ContentLibrary::where([
['id', '=', $param['id']],
['userId', '=', $this->request->userInfo['id']]
])->find();
if (!$library) {
return json(['code' => 404, 'msg' => '内容库不存在']);
}
Db::startTrans();
try {
// 更新内容库基本信息
$library->name = $param['name'];
$library->description = isset($param['description']) ? $param['description'] : '';
$library->save();
Db::commit();
return json(['code' => 200, 'msg' => '更新成功']);
} catch (\Exception $e) {
Db::rollback();
return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]);
}
}
/**
* 删除内容库
* @param int $id 内容库ID
* @return \think\response\Json
*/
public function delete($id)
{
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
$library = ContentLibrary::where([
['id', '=', $id],
['userId', '=', $this->request->userInfo['id']]
])->find();
if (!$library) {
return json(['code' => 404, 'msg' => '内容库不存在']);
}
Db::startTrans();
try {
// 删除相关内容项目
ContentItem::where('libraryId', $id)->delete();
// 删除内容库
$library->delete();
Db::commit();
return json(['code' => 200, 'msg' => '删除成功']);
} catch (\Exception $e) {
Db::rollback();
return json(['code' => 500, 'msg' => '删除失败:' . $e->getMessage()]);
}
}
/**
* 添加内容项目
* @return \think\response\Json
*/
public function addItem()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
// 获取请求参数
$param = $this->request->post();
// A简单验证
if (empty($param['libraryId'])) {
return json(['code' => 400, 'msg' => '内容库ID不能为空']);
}
if (empty($param['type'])) {
return json(['code' => 400, 'msg' => '内容类型不能为空']);
}
if (empty($param['contentData'])) {
return json(['code' => 400, 'msg' => '内容数据不能为空']);
}
// 查询内容库是否存在
$library = ContentLibrary::where([
['id', '=', $param['libraryId']],
['userId', '=', $this->request->userInfo['id']]
])->find();
if (!$library) {
return json(['code' => 404, 'msg' => '内容库不存在']);
}
try {
// 创建内容项目
$item = new ContentItem;
$item->libraryId = $param['libraryId'];
$item->type = $param['type'];
$item->title = $param['title'] ?? '';
$item->contentData = $param['contentData'];
$item->save();
return json(['code' => 200, 'msg' => '添加成功', 'data' => ['id' => $item->id]]);
} catch (\Exception $e) {
return json(['code' => 500, 'msg' => '添加失败:' . $e->getMessage()]);
}
}
/**
* 删除内容项目
* @param int $id 内容项目ID
* @return \think\response\Json
*/
public function deleteItem($id)
{
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
// 查询内容项目是否存在并检查权限
$item = ContentItem::alias('i')
->join('content_library l', 'i.libraryId = l.id')
->where([
['i.id', '=', $id],
['l.userId', '=', $this->request->userInfo['id']]
])
->find();
if (!$item) {
return json(['code' => 404, 'msg' => '内容项目不存在或无权限操作']);
}
try {
// 删除内容项目
ContentItem::destroy($id);
return json(['code' => 200, 'msg' => '删除成功']);
} catch (\Exception $e) {
return json(['code' => 500, 'msg' => '删除失败:' . $e->getMessage()]);
}
}
}

View File

@@ -84,8 +84,11 @@ class WorkbenchController extends Controller
$config->syncInterval = $param['syncInterval'];
$config->syncCount = $param['syncCount'];
$config->syncType = $param['syncType'];
$config->startTime = $param['startTime'];
$config->endTime = $param['endTime'];
$config->accountType = $param['accountType'];
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->contentLibraries = json_encode($param['contentLibraries'] ?? []);
$config->createTime = time();
$config->updateTime = time();
$config->save();
@@ -155,9 +158,9 @@ class WorkbenchController extends Controller
'autoLike' => function($query) {
$query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,targetGroups');
},
// 'momentsSync' => function($query) {
// $query->field('workbenchId,syncInterval,syncCount,syncType,devices,targetGroups');
// },
'momentsSync' => function($query) {
$query->field('workbenchId,syncInterval,syncCount,syncType,startTime,endTime,accountType,devices,contentLibraries');
},
// 'groupPush' => function($query) {
// $query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups');
// },
@@ -188,8 +191,9 @@ class WorkbenchController extends Controller
if (!empty($item->momentsSync)) {
$item->config = $item->momentsSync;
$item->config->devices = json_decode($item->config->devices, true);
$item->config->targetGroups = json_decode($item->config->targetGroups, true);
$item->config->contentLibraries = json_decode($item->config->contentLibraries, true);
}
unset($item->momentsSync,$item->moments_sync);
break;
case self::TYPE_GROUP_PUSH:
if (!empty($item->groupPush)) {
@@ -199,6 +203,7 @@ class WorkbenchController extends Controller
$item->config->pushContent = json_decode($item->config->pushContent, true);
$item->config->pushTime = json_decode($item->config->pushTime, true);
}
unset($item->groupPush,$item->group_push);
break;
case self::TYPE_GROUP_CREATE:
if (!empty($item->groupCreate)) {
@@ -206,9 +211,9 @@ class WorkbenchController extends Controller
$item->config->devices = json_decode($item->config->devices, true);
$item->config->targetGroups = json_decode($item->config->targetGroups, true);
}
unset($item->groupCreate,$item->group_create);
break;
}
unset( $item->momentsSync, $item->groupPush, $item->groupCreate);
return $item;
});
@@ -231,8 +236,10 @@ class WorkbenchController extends Controller
* @param int $id 工作台ID
* @return \think\response\Json
*/
public function detail($id)
public function detail()
{
$id = $this->request->param('id', '');
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
@@ -243,14 +250,14 @@ class WorkbenchController extends Controller
$query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,targetGroups');
},
'momentsSync' => function($query) {
$query->field('workbenchId,syncInterval,syncCount,syncType,devices,targetGroups');
$query->field('workbenchId,syncInterval,syncCount,syncType,startTime,endTime,accountType,devices,contentLibraries');
},
'groupPush' => function($query) {
$query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups');
},
'groupCreate' => function($query) {
$query->field('workbenchId,groupNamePrefix,maxGroups,membersPerGroup,devices,targetGroups');
}
// 'groupPush' => function($query) {
// $query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups');
// },
// 'groupCreate' => function($query) {
// $query->field('workbenchId,groupNamePrefix,maxGroups,membersPerGroup,devices,targetGroups');
// }
];
$workbench = Workbench::where([
@@ -273,14 +280,15 @@ class WorkbenchController extends Controller
$workbench->config = $workbench->autoLike;
$workbench->config->devices = json_decode($workbench->config->devices, true);
$workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true);
$workbench->config->contentTypes = explode(',', $workbench->config->contentTypes);
$workbench->config->contentTypes = json_decode($workbench->config->contentTypes, true);
unset($workbench->autoLike,$workbench->auto_like);
}
break;
case self::TYPE_MOMENTS_SYNC:
if (!empty($workbench->momentsSync)) {
$workbench->config = $workbench->momentsSync;
$workbench->config->devices = json_decode($workbench->config->devices, true);
$workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true);
$workbench->config->contentLibraries = json_decode($workbench->config->contentLibraries, true);
}
break;
case self::TYPE_GROUP_PUSH:
@@ -305,6 +313,112 @@ class WorkbenchController extends Controller
return json(['code' => 200, 'msg' => '获取成功', 'data' => $workbench]);
}
/**
* 更新工作台
* @return \think\response\Json
*/
public function update()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
// 获取请求参数
$param = $this->request->post();
// 验证数据
$validate = new WorkbenchValidate;
if (!$validate->scene('update')->check($param)) {
return json(['code' => 400, 'msg' => $validate->getError()]);
}
// 查询工作台是否存在
$workbench = Workbench::where([
['id', '=', $param['id']],
['userId', '=', $this->request->userInfo['id']],
['isDel', '=', 0]
])->find();
if (!$workbench) {
return json(['code' => 404, 'msg' => '工作台不存在']);
}
Db::startTrans();
try {
// 更新工作台基本信息
$workbench->name = $param['name'];
$workbench->autoStart = !empty($param['autoStart']) ? 1 : 0;
$workbench->updateTime = time();
$workbench->save();
// 根据类型更新对应的配置
switch ($workbench->type) {
case self::TYPE_AUTO_LIKE:
$config = WorkbenchAutoLike::where('workbenchId', $param['id'])->find();
if ($config) {
$config->interval = $param['interval'];
$config->maxLikes = $param['maxLikes'];
$config->startTime = $param['startTime'];
$config->endTime = $param['endTime'];
$config->contentTypes = json_encode($param['contentTypes']);
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->tagOperator = $param['tagOperator'];
$config->updateTime = time();
$config->save();
}
break;
case self::TYPE_MOMENTS_SYNC:
$config = WorkbenchMomentsSync::where('workbenchId', $param['id'])->find();
if ($config) {
$config->syncInterval = $param['syncInterval'];
$config->syncCount = $param['syncCount'];
$config->syncType = $param['syncType'];
$config->startTime = $param['startTime'];
$config->endTime = $param['endTime'];
$config->accountType = $param['accountType'];
$config->devices = json_encode($param['devices']);
$config->contentLibraries = json_encode($param['contentLibraries'] ?? []);
$config->updateTime = time();
$config->save();
}
break;
case self::TYPE_GROUP_PUSH:
$config = WorkbenchGroupPush::where('workbenchId', $param['id'])->find();
if ($config) {
$config->pushInterval = $param['pushInterval'];
$config->pushContent = json_encode($param['pushContent']);
$config->pushTime = json_encode($param['pushTime']);
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->save();
}
break;
case self::TYPE_GROUP_CREATE:
$config = WorkbenchGroupCreate::where('workbenchId', $param['id'])->find();
if ($config) {
$config->groupNamePrefix = $param['groupNamePrefix'];
$config->maxGroups = $param['maxGroups'];
$config->membersPerGroup = $param['membersPerGroup'];
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->updateTime = time();
$config->save();
}
break;
}
Db::commit();
return json(['code' => 200, 'msg' => '更新成功']);
} catch (\Exception $e) {
Db::rollback();
return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]);
}
}
/**
* 更新工作台状态
* @return \think\response\Json
@@ -341,8 +455,9 @@ class WorkbenchController extends Controller
/**
* 删除工作台(软删除)
*/
public function delete($id)
public function delete()
{
$id = $this->request->param('id');
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
@@ -399,6 +514,7 @@ class WorkbenchController extends Controller
$newWorkbench->status = 1; // 新拷贝的默认启用
$newWorkbench->autoStart = $workbench->autoStart;
$newWorkbench->userId = $this->request->userInfo['id'];
$newWorkbench->companyId = $this->request->userInfo['companyId'];
$newWorkbench->save();
// 根据类型拷贝对应的配置
@@ -426,8 +542,11 @@ class WorkbenchController extends Controller
$newConfig->syncInterval = $config->syncInterval;
$newConfig->syncCount = $config->syncCount;
$newConfig->syncType = $config->syncType;
$newConfig->startTime = $config->startTime;
$newConfig->endTime = $config->endTime;
$newConfig->accountType = $config->accountType;
$newConfig->devices = $config->devices;
$newConfig->targetGroups = $config->targetGroups;
$newConfig->contentLibraries = $config->contentLibraries;
$newConfig->save();
}
break;

View File

@@ -0,0 +1,52 @@
<?php
namespace app\cunkebao\model;
use think\Model;
class ContentItem extends Model
{
protected $pk = 'id';
protected $name = 'content_items';
// 内容类型
const TYPE_TEXT = 1; // 文本
const TYPE_IMAGE = 2; // 图片
const TYPE_VIDEO = 3; // 视频
const TYPE_LINK = 4; // 链接
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 定义关联的内容库
public function library()
{
return $this->belongsTo('ContentLibrary', 'libraryId', 'id');
}
// 内容类型获取器
public function getTypeTextAttr($value, $data)
{
$types = [
self::TYPE_TEXT => '文本',
self::TYPE_IMAGE => '图片',
self::TYPE_VIDEO => '视频',
self::TYPE_LINK => '链接'
];
return isset($types[$data['type']]) ? $types[$data['type']] : '未知';
}
// 内容数据获取器
public function getContentDataAttr($value)
{
return json_decode($value, true);
}
// 内容数据修改器
public function setContentDataAttr($value)
{
return is_array($value) ? json_encode($value) : $value;
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace app\cunkebao\model;
use think\Model;
class ContentLibrary extends Model
{
protected $pk = 'id';
protected $name = 'content_library';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 定义关联的用户
public function user()
{
return $this->belongsTo('User', 'userId', 'id');
}
// 定义关联的内容项目
public function items()
{
return $this->hasMany('ContentItem', 'libraryId', 'id');
}
// 根据ID数组获取内容库列表
public static function getByIds($ids)
{
if (empty($ids)) {
return [];
}
return self::where('id', 'in', $ids)->select();
}
}

View File

@@ -9,6 +9,12 @@ class WorkbenchMomentsSync extends Model
protected $pk = 'id';
protected $name = 'workbench_moments_sync';
// 同步类型
const SYNC_TYPE_TEXT = 1; // 文本
const SYNC_TYPE_IMAGE = 2; // 图片
const SYNC_TYPE_VIDEO = 3; // 视频
const SYNC_TYPE_LINK = 4; // 链接
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
@@ -19,4 +25,34 @@ class WorkbenchMomentsSync extends Model
{
return $this->belongsTo('Workbench', 'workbenchId', 'id');
}
// 定义关联的内容库
public function contentLibraries()
{
return $this->belongsToMany('ContentLibrary', 'workbench_content_relation', 'contentLibraryId', 'workbenchId');
}
// 开始时间获取器
public function getStartTimeAttr($value)
{
return $value ? date('H:i', strtotime($value)) : '';
}
// 结束时间获取器
public function getEndTimeAttr($value)
{
return $value ? date('H:i', strtotime($value)) : '';
}
// 同步类型获取器
public function getSyncTypeTextAttr($value, $data)
{
$types = [
self::SYNC_TYPE_TEXT => '文本',
self::SYNC_TYPE_IMAGE => '图片',
self::SYNC_TYPE_VIDEO => '视频',
self::SYNC_TYPE_LINK => '链接'
];
return isset($types[$data['syncType']]) ? $types[$data['syncType']] : '未知';
}
}

View File

@@ -25,10 +25,15 @@ class Workbench extends Validate
'startTime' => 'requireIf:type,1|dateFormat:H:i',
'endTime' => 'requireIf:type,1|dateFormat:H:i',
'contentTypes' => 'requireIf:type,1|array|contentTypeEnum:text,image,video',
'targetGroups' => 'requireIf:type,1|array',
// 朋友圈同步特有参数
'syncInterval' => 'requireIf:type,2|number|min:1',
'syncCount' => 'requireIf:type,2|number|min:1',
'syncType' => 'requireIf:type,2|in:1,2,3,4',
'startTime' => 'requireIf:type,2|dateFormat:H:i',
'endTime' => 'requireIf:type,2|dateFormat:H:i',
'accountType' => 'requireIf:type,2|in:1,2',
'contentLibraries' => 'requireIf:type,2|array',
// 群消息推送特有参数
'pushInterval' => 'requireIf:type,3|number|min:1',
'pushContent' => 'requireIf:type,3|array',
@@ -39,7 +44,6 @@ class Workbench extends Validate
'membersPerGroup' => 'requireIf:type,4|number|min:1',
// 通用参数
'devices' => 'require|array',
'targetGroups' => 'require|array'
];
/**
@@ -75,6 +79,14 @@ class Workbench extends Validate
'syncCount.min' => '同步数量必须大于0',
'syncType.requireIf' => '请选择同步类型',
'syncType.in' => '同步类型错误',
'startTime.requireIf' => '请设置发布开始时间',
'startTime.dateFormat' => '发布开始时间格式错误',
'endTime.requireIf' => '请设置发布结束时间',
'endTime.dateFormat' => '发布结束时间格式错误',
'accountType.requireIf' => '请选择账号类型',
'accountType.in' => '账号类型错误',
'contentLibraries.requireIf' => '请选择内容库',
'contentLibraries.array' => '内容库格式错误',
// 群消息推送相关提示
'pushInterval.requireIf' => '请设置推送间隔',
'pushInterval.number' => '推送间隔必须为数字',

View File

@@ -6,6 +6,7 @@ use think\queue\Job;
use think\facade\Log;
use think\Queue;
use think\facade\Config;
use think\facade\Cache;
use app\api\controller\WechatFriendController;
class WechatFriendJob
@@ -58,7 +59,7 @@ class WechatFriendJob
$pageSize = isset($data['pageSize']) ? $data['pageSize'] : 1000;
$preFriendId = isset($data['preFriendId']) ? $data['preFriendId'] : '';
Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize);
Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize . '上一好友ID' . $preFriendId);
// 实例化控制器
$wechatFriendController = new WechatFriendController();
@@ -86,10 +87,24 @@ class WechatFriendJob
// 判断是否有下一页
if (!empty($data) && count($data) > 0) {
// 获取最后一条记录的ID
$lastFriendId = $data[count($data)-1]['id'];
// 更新缓存中的页码和最后一个好友ID设置10分钟过期
Cache::set('friendsPage', $pageIndex + 1, 600);
Cache::set('preFriendId', $lastFriendId, 600);
Log::info('更新缓存,下一页页码:' . ($pageIndex + 1) . '最后好友ID' . $lastFriendId . '缓存时间10分钟');
// 有下一页,将下一页任务添加到队列
$nextPageIndex = $pageIndex + 1;
$this->addNextPageToQueue($nextPageIndex, $pageSize,$data[count($data)-1]['id']);
$this->addNextPageToQueue($nextPageIndex, $pageSize, $lastFriendId);
Log::info('添加下一页任务到队列,页码:' . $nextPageIndex);
} else {
// 没有下一页重置缓存设置10分钟过期
Cache::set('friendsPage', 0, 600);
Cache::set('preFriendId', '', 600);
Log::info('获取完成重置缓存缓存时间10分钟');
}
return true;
@@ -104,6 +119,7 @@ class WechatFriendJob
* 添加下一页任务到队列
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
* @param string $preFriendId 上一个好友ID
*/
protected function addNextPageToQueue($pageIndex, $pageSize,$preFriendId)
{