Commit ebb9b15d authored by pengfei's avatar pengfei

add wx group chat

parent fbfc397f
<?php
/**
* Created by PhpStorm.
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 1:44 下午
*/
namespace App\Models\wx\mysql;
use Api\PhpUtils\Mysql\MysqlBase;
class WxGroupChat extends MysqlBase
{
const TABLE_NAME = 'wx_group_chat';
const CONFIG_INDEX = 'goods';
const PRIMARY_KEY = 'id';
public static function getRecords($where, $columns = []): array
{
if (empty($columns)) {
$columns = '*';
}
return (array)self::select($columns, $where);
}
public static function updateRecord($colums, $where)
{
return self::update($colums, $where);
}
public static function save($data, $where = [])
{
if (empty($where)) {
return self::insert($data);
}
return self::update($data, $where);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 1:45 下午
*/
namespace App\Models\wx\mysql;
use Api\PhpUtils\Mysql\MysqlBase;
class WxGroupChatUser extends MysqlBase
{
const TABLE_NAME = 'wx_group_chat_user';
const CONFIG_INDEX = 'goods';
const PRIMARY_KEY = 'id';
const STATUS_LEAVE = 0; // 状态: 退出群聊
const STATUS_NORMAL = 1; // 状态:正常
public static function getRecords($where, $columns = []): array
{
if (empty($columns)) {
$columns = '*';
}
return (array)self::select($columns, $where);
}
public static function updateRecord($columns, $where)
{
return self::update($columns, $where);
}
public static function save($data, $where = [])
{
if (empty($where)) {
return self::insert($data);
}
return self::update($data, $where);
}
}
<?php
/**
* Created by PhpStorm.
* User: pengfei@yidian-inc.com
* Date: 2021/9/27 8:18 下午
*/
use App\Base\Job;
use App\Services\wx\GroupChatService;
class WxgroupchatController extends Job
{
public function indexAction()
{
(new GroupChatService)->process();
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 10:57 上午
*/
namespace App\Services\wx;
use Api\PhpUtils\EasyWechat\AppConfig;
use Exception;
use EasyWeChat\Factory;
use Api\PhpUtils\Log\JobLog;
use App\Models\wx\mysql\WxGroupChat;
use App\Models\wx\mysql\WxGroupChatUser;
class GroupChatService
{
public $wxApp;
public $pageSize = 100;
public $nextCursor = '';
public $ownerUserIds = [];
public $departmentId; // 部门id
/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function __construct()
{
$this->wxApp = Factory::work(AppConfig::getCorpConfig());
$this->loadConfig();
}
/**
* Notes: 处理微信群
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 7:36 下午
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function process()
{
while (true) {
$result = $this->getGroupChatList($this->nextCursor);
if (empty($result['group_chat_list'])) {
break;
}
$existChatIds = $this->getWxGroupChatIds(array_column($result['group_chat_list'], 'chat_id'));
foreach ($result['group_chat_list'] as $group) {
try {
$groupChat = $this->getGroupChatDetail($group['chat_id']);
$chatInfo = $this->getFormatGroupChat($group, $groupChat);
$this->processGroupChat($existChatIds, $chatInfo);
if (!empty($groupChat['group_chat']['member_list'])) {
$this->processGroupChatUser($group['chat_id'], $groupChat['group_chat']['member_list']);
}
$this->loggerInfo("处理成功:chat_id={$group['chat_id']},name={$chatInfo['name']},memberCount=" . count($groupChat['group_chat']['member_list']));
} catch (Exception $e) {
$this->loggerError("处理失败:chat_id={$group['chat_id']},msg=" . $e->getMessage() . ',code=' . $e->getCode() . ',line=' . $e->getLine());
}
}
if (empty($result['next_cursor'])) {
break;
}
$this->nextCursor = $result['next_cursor'];
}
}
/**
* Notes: 处理群
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 3:04 下午
* @param $existChatIds
* @param $groupChat
*/
private function processGroupChat($existChatIds, $groupChat)
{
$where = [];
if (isset($existChatIds[$groupChat['chat_id']])) {
$where = ['chat_id' => $groupChat['chat_id']];
}
WxGroupChat::save($groupChat, $where);
}
/**
* Notes: 处理群成员
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 3:19 下午
* @param $chatId
* @param $memberList
*/
private function processGroupChatUser($chatId, $memberList)
{
$insertMember = [];
$existUserIds = $this->getWxGroupChatUserIds(array_column($memberList, 'userid'));
WxGroupChatUser::updateRecord(['status' => WxGroupChatUser::STATUS_LEAVE], []);
foreach ($memberList as $member) {
$member = $this->getFormatGroupChatUser($chatId, $member);
if (isset($existUserIds[$member['user_id']])) {
WxGroupChatUser::save($member, ['user_id' => $member['user_id']]);
} else {
$insertMember[] = $member;
}
}
if (!empty($insertMember)) {
WxGroupChatUser::save($insertMember);
}
}
/**
* Notes: 获取组装群组成员数据
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 3:20 下午
* @param $chatId
* @param $member
* @return array
*/
private function getFormatGroupChatUser($chatId, $member): array
{
return [
'chat_id' => $chatId,
'user_id' => $member['userid'],
'name' => $member['name'] ?? '',
'status' => WxGroupChatUser::STATUS_NORMAL,
'type' => $member['type'],
'join_time' => $member['join_time'],
'join_scene' => $member['join_scene'],
'invitor_user_id' => !empty($member['invitor']['user_id']) ? $member['invitor']['user_id'] : '',
'group_nickname' => $member['group_nickname'] ?? ''
];
}
/**
* Notes: 获取组装群组数据
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 3:10 下午
* @param $group
* @param $groupDetail
* @return array
*/
private function getFormatGroupChat($group, $groupDetail): array
{
$groupChatInfo = $groupDetail['group_chat'];
return [
'chat_id' => $groupChatInfo['chat_id'],
'name' => $groupChatInfo['name'],
'notice' => $groupChatInfo['notice'] ?? '',
'owner' => $groupChatInfo['owner'] ?? '',
'status' => $group['status'],
'chat_create_time' => $groupChatInfo['create_time'],
];
}
/**
* Notes: 获取群id
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 3:03 下午
* @param $chatIds
* @return array
*/
private function getWxGroupChatIds($chatIds): array
{
$chatList = WxGroupChat::getRecords(['chat_id' => $chatIds], ['chat_id']);
return !empty($chatList) ? array_column($chatList, null, 'chat_id') : [];
}
/**
* Notes: 获取群成员id
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 3:03 下午
* @param $userIds
* @return array
*/
private function getWxGroupChatUserIds($userIds): array
{
$userList = WxGroupChatUser::getRecords(['user_id' => $userIds], ['user_id']);
return !empty($userList) ? array_column($userList, null, 'user_id') : [];
}
/**
* Notes: 获取部门下成员
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 7:35 下午
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
private function getGroupChatOwnerList(): array
{
if (empty($this->departmentId)) {
return [];
}
return (array)$this->wxApp->user->getDepartmentUsers($this->departmentId, true);
}
/**
* Notes: 获取群详情
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 7:35 下午
* @param $chatId
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
private function getGroupChatDetail($chatId): array
{
return (array)$this->wxApp->external_contact->getGroupChat($chatId);
}
/**
* Notes: 获取群列表
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 7:36 下午
* @param string $nextCursor
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
private function getGroupChatList(string $nextCursor = ''): array
{
$params = [
'limit' => $this->pageSize
];
// 群主过滤
if (!empty($this->ownerUserIds)) {
$params['owner_filter']['userid_list'] = $this->ownerUserIds;
}
if (!empty($nextCursor)) {
$params['cursor'] = $nextCursor;
}
return (array)$this->wxApp->external_contact->getGroupChats($params);
}
/**
* Notes: 加载配置
* User: pengfei@yidian-inc.com
* Date: 2021/9/28 7:36 下午
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
private function loadConfig()
{
$this->departmentId = config('wechat', 'corp.secondary_market_department_id');
$result = $this->getGroupChatOwnerList();
$this->ownerUserIds = array_column($result['userlist'], 'userid');
}
/**
* Notes: 记录 info 日志
* User: pengfei@yidian-inc.com
* Date: 2021/9/3 4:10 下午
* @param $log
*/
private function loggerInfo($log)
{
JobLog::getInstance()->info('colonel_distributor_order', $log)->output();
}
/**
* Notes: 记录 error 日志
* User: pengfei@yidian-inc.com
* Date: 2021/9/3 4:10 下午
* @param $log
*/
private function loggerError($log)
{
JobLog::getInstance()->error('colonel_distributor_order', $log, null, 'bp-server@yidian-inc.com')->output();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment