Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
goods
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
goods
Commits
ebb9b15d
Commit
ebb9b15d
authored
Sep 28, 2021
by
pengfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add wx group chat
parent
fbfc397f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
362 additions
and
0 deletions
+362
-0
WxGroupChat.php
application/models/wx/mysql/WxGroupChat.php
+38
-0
WxGroupChatUser.php
application/models/wx/mysql/WxGroupChatUser.php
+41
-0
Wxgroupchat.php
application/modules/Job/controllers/Wxgroupchat.php
+18
-0
GroupChatService.php
application/services/wx/GroupChatService.php
+265
-0
No files found.
application/models/wx/mysql/WxGroupChat.php
0 → 100644
View file @
ebb9b15d
<?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
application/models/wx/mysql/WxGroupChatUser.php
0 → 100644
View file @
ebb9b15d
<?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
);
}
}
application/modules/Job/controllers/Wxgroupchat.php
0 → 100644
View file @
ebb9b15d
<?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
application/services/wx/GroupChatService.php
0 → 100644
View file @
ebb9b15d
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment