123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AndroidToolRequestLog;
- use App\Models\AuthorizeCorp;
- use App\Models\ChatGroupMassMsgRecord;
- use App\Models\DjUser;
- use App\Models\MassMsgRecord;
- use App\Models\MassMsgSendNotice;
- use App\Models\MomentRecord;
- use App\Models\PeriodMassMsg\PeriodMassMsg;
- use App\Models\PeriodMassMsg\PeriodMassMsgRecord;
- use App\Models\System\AdminManageCorp;
- class MassMsgService
- {
- /**
- * 获取群发成员发送任务列表
- * */
- public static function getMassMsgTaskStatus($corpid, $msgId, $limit, $cursor)
- {
- # 获取accessToken
- $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取群发成员发送任务状态');
- if(empty($accessToken)) {
- return false;
- }
- $postData = [
- 'msgid' => $msgId,
- 'limit' => $limit,
- 'cursor' => $cursor
- ];
- $requestUri = config('qyWechat.mass_msg_task_status');
- $requestUri .= $accessToken;
- $response = HttpService::httpPost($requestUri, json_encode($postData));
- $responseData = json_decode($response, true);
- return $responseData;
- }
- /**
- * 处理群发信息 / 欢迎语中的推广链接类型附件
- * @param string $corpid 企业id
- * @param string $jumpLink 目标链接
- * */
- public static function getPromoteLink($corpid, $jumpLink)
- {
- $htmlLink = env('DOMAIN', 'http://dj.wenxingshuju.com/') . 'playlet/qwh5/dist/index.html#/extend';
- $param = [
- 'corpid' => $corpid,
- 'jumpLink' => urlencode($jumpLink)
- ];
- return $htmlLink . '?' . http_build_query($param);
- }
- public static function drawPid($attachments, &$pid, $logName)
- {
- Log::logInfo('提取开始', $attachments, $logName);
- $drawPids = [];
- foreach ($attachments as $datum) {
- if (isset($datum['msgtype'])) {
- $url = null;
- // 小程序
- if (
- ($datum['msgtype'] == 'miniprogram')
- &&
- isset($datum['miniprogram']['page'])
- &&
- !empty($datum['miniprogram']['page'])
- ) {
- $url = $datum['miniprogram']['page'];
- }
- // 链接
- if (
- ($datum['msgtype'] == 'link')
- &&
- isset($datum['link']['url'])
- &&
- !empty($datum['link']['url'])
- ) {
- $url = $datum['link']['url'];
- }
- // H5推广
- if (
- ($datum['msgtype'] == 'promote')
- &&
- isset($datum['promote']['jump_url'])
- &&
- !empty($datum['promote']['jump_url'])
- ) {
- $url = $datum['promote']['jump_url'];
- }
- // 欢迎语中的H5推广会被转换成普通链接方式
- if (
- ($datum['msgtype'] == 'link')
- &&
- isset($datum['link']['jump_url'])
- &&
- !empty($datum['link']['jump_url'])
- ) {
- $url = $datum['link']['jump_url'];
- }
- // 属于其他类型
- if (is_null($url)) continue;
- Log::logInfo('提取URL', [
- 'url' => $url
- ], $logName);
- // 处理url
- $filedExistNum = 0;
- self::dealUrl($url, $filedExistNum, $drawPids, $logName);
- if ($filedExistNum > 1) {
- Log::logError('提取推广链接PID存在多个字段', $datum, $logName);
- return false;
- }
- }
- // if (count($drawPids) > 1) {
- // Log::logError('提取推广链接PID存在多个值', $datum, $logName);
- // return false;
- // }
- if (count($drawPids) > 1) {
- Log::logError('提取推广链接PID存在多个值', $datum, $logName);
- $pid = $drawPids;
- } else {
- if (!empty($drawPids)) $pid = $drawPids;
- }
- // if (!empty($drawPids)) $pid = current($drawPids);
- Log::logInfo('提取结束', $attachments, $logName);
- }
- return true;
- }
- public static function dealUrl($url, &$filedExistNum, &$drawPids, $logName)
- {
- $url = str_replace("%25", "%", $url);
- $url = str_replace("&", "&", $url);
- $url = urldecode($url);
- $urlArr = parse_url($url);
- if (!isset($urlArr['query']) && empty($urlArr['query'])) return false;
- Log::logInfo('提取url', [
- 'url' => $url,
- 'filedExistNum' => $filedExistNum,
- 'drawPids' => $drawPids
- ], $logName);
- parse_str($urlArr['query'], $query);
- // 柚子 pathId
- if (isset($query['pathId']) && !empty($query['pathId'])) {
- $filedExistNum ++;
- $drawPids[] = $query['pathId'];
- }
- // 嘉书 article
- if (isset($query['article']) && !empty($query['article'])) {
- $filedExistNum ++;
- $drawPids[] = $query['article'];
- }
- // 迈步 pid
- if (isset($query['pid']) && !empty($query['pid'])) {
- $filedExistNum ++;
- $drawPids[] = $query['pid'];
- }
- // 点众阳光 referral_id
- // if (isset($query['referral_id']) && !empty($query['referral_id'])) {
- // $filedExistNum ++;
- // $drawPids[] = $query['referral_id'];
- // }
- foreach ($query as $urlVal) {
- self::dealUrl($urlVal, $filedExistNum, $drawPids, $logName);
- }
- Log::logInfo('提取url结果', [
- 'url' => $url,
- 'filedExistNum' => $filedExistNum,
- 'drawPids' => $drawPids
- ], $logName);
- }
- public static function drawChannel($attachments, &$channel, $logName)
- {
- Log::logInfo('提取开始', $attachments, $logName);
- $drawChannels = [];
- foreach ($attachments as $datum) {
- if (isset($datum['msgtype'])) {
- $url = null;
- // 小程序
- if (
- ($datum['msgtype'] == 'miniprogram')
- &&
- isset($datum['miniprogram']['page'])
- &&
- !empty($datum['miniprogram']['page'])
- ) {
- $url = $datum['miniprogram']['page'];
- }
- // 链接
- if (
- ($datum['msgtype'] == 'link')
- &&
- isset($datum['link']['url'])
- &&
- !empty($datum['link']['url'])
- ) {
- $url = $datum['link']['url'];
- }
- // H5推广
- if (
- ($datum['msgtype'] == 'promote')
- &&
- isset($datum['promote']['jump_url'])
- &&
- !empty($datum['promote']['jump_url'])
- ) {
- $url = $datum['promote']['jump_url'];
- }
- // 欢迎语中的H5推广会被转换成普通链接方式
- if (
- ($datum['msgtype'] == 'link')
- &&
- isset($datum['link']['jump_url'])
- &&
- !empty($datum['link']['jump_url'])
- ) {
- $url = $datum['link']['jump_url'];
- }
- // 属于其他类型
- if (is_null($url)) continue;
- Log::logInfo('提取URL', [
- 'url' => $url
- ], $logName);
- // 处理url
- $filedExistNum = 0;
- self::dealUrlNew($url, $filedExistNum, $drawChannels, $logName);
- if ($filedExistNum > 1) {
- Log::logError('提取渠道channel存在多个字段', $datum, $logName);
- return false;
- }
- }
- if (count($drawChannels) > 1) {
- Log::logError('提取渠道channel存在多个值', $datum, $logName);
- return false;
- }
- if (!empty($drawChannels)) $channel = current($drawChannels);
- Log::logInfo('提取结束', $attachments, $logName);
- }
- return true;
- }
- /**
- * 获取待确认发送的消息列表
- * */
- public static function senderConfirmList($deviceId)
- {
- # 查询支持无障碍模式的企微列表
- $corpIds = AdminManageCorp::select('corpid')->where('is_delete', 0)->whereIn('sys_user_id', [2, 3, 38, 51])->distinct()->pluck('corpid');
- $corpidList = AuthorizeCorp::whereIn('id', $corpIds)->where('enable', 1)->pluck('corpid');
- # 查询待发送群发消息记录
- $msgList = MassMsgRecord::select('corpid')->where('status', 1)
- ->whereIn('corpid', $corpidList)
- ->where('send_time', '>=', date('Y-m-d H:i:s', strtotime('-60 minutes')))
- ->groupBy(['corpid'])
- ->get();
- # 查询待发送的智能推送消息记录
- $periodMsgList = PeriodMassMsgRecord::select('corpid')->where('status', 1)
- ->whereIn('corpid', $corpidList)
- ->where('create_time', '>=', date('Y-m-d H:i:s', strtotime('-60 minutes')))
- ->groupBy(['corpid'])
- ->get();
- # 查询待确认发送的客户群消息
- $chatGroupMsgList = ChatGroupMassMsgRecord::select('corpid')->where('status', 1)
- ->whereIn('corpid', $corpidList)
- ->where('create_time', '>=', date('Y-m-d H:i:s', strtotime('-60 minutes')))
- ->groupBy(['corpid'])
- ->get();
- $corpList = AuthorizeCorp::select('corpid', 'corp_name')->whereIn('corpid', $corpidList)->where('enable', 1)->get();
- $msgList = array_unique(array_merge(
- $msgList->pluck('corpid')->toArray(), $periodMsgList->pluck('corpid')->toArray(),
- $chatGroupMsgList->pluck('corpid')->toArray()
- ));
- $data = [];
- foreach ($msgList as $item) {
- # 获取企微名称
- $corpInfo = $corpList->where('corpid', $item)->first();
- $companyName = $corpInfo->corp_name;
- $data[] = [
- 'companyName' => $companyName,
- 'corpid' => $item
- ];
- # 获取客服名
- // $userInfo = $userList->where('user_id', $item->sender)->where('corpid', $item->corpid)->first();
- // $item->userName = $userInfo->name;
- }
- Log::logInfo('设备请求日志', [
- 'type' => 1,
- 'device_id' => $deviceId,
- 'msg_list' => $data
- ], 'SenderConfirmList');
- return $data;
- }
- public static function dealUrlNew($url, &$filedExistNum, &$drawChannels, $logName)
- {
- $url = str_replace("%25", "%", $url);
- $url = str_replace("&", "&", $url);
- $url = urldecode($url);
- $urlArr = parse_url($url);
- if (!isset($urlArr['query']) && empty($urlArr['query'])) return false;
- Log::logInfo('提取url', [
- 'url' => $url,
- 'filedExistNum' => $filedExistNum,
- 'drawChannels' => $drawChannels
- ], $logName);
- parse_str($urlArr['query'], $query);
- if (isset($query['channel']) && !empty($query['channel'])) {
- $filedExistNum ++;
- $drawChannels[] = $query['channel'];
- }
- if (isset($query['wxAppId']) && !empty($query['wxAppId'])) {
- $filedExistNum ++;
- $drawChannels[] = $query['wxAppId'];
- }
- foreach ($query as $urlVal) {
- self::dealUrl($urlVal, $filedExistNum, $drawChannels, $logName);
- }
- Log::logInfo('提取url结果', [
- 'url' => $url,
- 'filedExistNum' => $filedExistNum,
- 'drawChannels' => $drawChannels
- ], $logName);
- }
- public static function sendNoticeList($params, $page, $pageSize)
- {
- $corpStat = AuthorizeCorp::getAllCorpList();
- $sys_corp_id_list = AdminManageCorp::getSysGroupCorpList($params['sys_group_id']);
- if(empty($sys_corp_id_list)) {
- return [[], 0];
- }
- $corp_list = $corpStat->whereIn('id', $sys_corp_id_list)->all();
- $params['corp_id_list'] = array_column($corp_list, 'corpid');
- # 如果筛选了客服
- $params['user_sql'] = [];
- if(!empty($userName)) {
- $userRes = DjUser::getUserBySearch([
- 'corpid' => $params['corp_id'],
- 'user_name' => $params['user_name'],
- 'corp_list' => $params['corp_id_list'] ?? null,
- ]);
- if($userRes->isEmpty()) {
- return [[], 0];
- }
- foreach($userRes as $userInfo) {
- $params['user_sql'][] = '("' . $userInfo->corpid . '","' . $userInfo->user_id . '")';
- }
- }
- list($data, $count) = MassMsgSendNotice::getNoticeList($params, $page, $pageSize);
- # 查询公司账号下得所有客服信息
- $userDataList = DjUser::getUserBySearch(['corp_list' => $params['corp_id_list']]);
- # 查询智能群发组id
- $ruleIdList = array_column($data->toArray(), 'rule_id');
- $periodMassMsgList = PeriodMassMsg::getInfoById($ruleIdList);
- # 遍历数据,将企微名称以及客服名称写入
- foreach($data as $item) {
- # 企微名称
- $corpInfo = $corpStat->where('corpid', $item->corpid)->first();
- $item->corp_name = $corpInfo->corp_name ?? '';
- # 客服名称
- $userInfo = $userDataList->where('corpid', $item->corpid)->where('user_id', $item->user_id)->first();
- $item->user_name = $userInfo->name ?? '';
- # 提醒信息
- $item->notice = '【'.$item->corp_name.'】'.'客服【'.$item->user_name.'】'.'群发消息超2小时未点击发送';
- # 发送状态
- $item->send_status_text = $item->send_status == 1 ? '待发送' : '已发送';
- # 消息类型
- $item->msg_type_text = $item->msg_type == 1 ? '群发消息' : ($item->msg_type == 2 ? '客户群群发消息' : '智能群发消息');
- # 智能群发组id
- $item->group_id = null;
- if(3 == $item->msg_type) {
- $periodMassMsgInfo = $periodMassMsgList->where('id', $item->rule_id)->first();
- $item->group_id = $periodMassMsgInfo->group_id ?? null;
- }
- }
- return [$data, $count];
- }
- public static function sendNoticeListNew($params, $page, $pageSize) {
- $corpStat = AuthorizeCorp::getAllCorpList();
- $sys_corp_id_list = AdminManageCorp::getSysGroupCorpList($params['sys_group_id']);
- if(empty($sys_corp_id_list)) {
- return [[], 0];
- }
- $corp_list = $corpStat->whereIn('id', $sys_corp_id_list)->all();
- $params['corp_id_list'] = array_column($corp_list, 'corpid');
- # 如果筛选了客服
- $params['user_sql'] = [];
- // if(!empty($params['user_name'])) {
- $userRes = DjUser::getUserBySearch([
- 'corpid' => $params['corp_id'],
- 'user_name' => $params['user_name'],
- 'corp_list' => $params['corp_id_list'] ?? null,
- ]);
- if($userRes->isEmpty()) {
- return [[], 0];
- }
- foreach($userRes as $userInfo) {
- $params['user_sql'][] = '("' . $userInfo->corpid . '","' . $userInfo->user_id . '")';
- }
- // }
- switch($params['msg_type']) {
- case 1: // 客户群发
- list($data, $count) = MassMsgRecord::getNoticeList($params, $page, $pageSize);
- $msgTypeText = '客户群发消息';
- break;
- case 2: // 客户群群发
- list($data, $count) = ChatGroupMassMsgRecord::getNoticeList($params, $page, $pageSize);
- $msgTypeText = '客户群群发消息';
- break;
- case 3: // 智能群发
- list($data, $count) = PeriodMassMsgRecord::getNoticeList($params, $page, $pageSize);
- $msgTypeText = '智能群发消息';
- break;
- case 4: // 客户朋友圈
- list($data, $count) = MomentRecord::getNoticeList($params, $page, $pageSize);
- $msgTypeText = '客户朋友圈消息';
- break;
- }
- # 查询公司账号下得所有客服信息
- $userDataList = DjUser::getUserBySearch(['corp_list' => $params['corp_id_list']]);
- # 查询智能群发组id
- $ruleIdList = array_column($data->toArray(), 'rule_id');
- $periodMassMsgList = PeriodMassMsg::getInfoById($ruleIdList);
- # 遍历数据,将企微名称以及客服名称写入
- foreach($data as $item) {
- # 企微名称
- $corpInfo = $corpStat->where('corpid', $item->corpid)->first();
- $item->corp_name = $corpInfo->corp_name ?? '';
- # 客服名称
- $userInfo = $userDataList->where('corpid', $item->corpid)->where('user_id', $item->sender)->first();
- $item->user_name = $userInfo->name ?? '';
- # 提醒信息
- if(in_array($params['msg_type'], [1, 3, 4])) {
- $item->notice = '【'.$item->corp_name.'】'.'客服【'.$item->user_name.'】'.'超30分钟未点击发送'.$msgTypeText;
- } else if($params['msg_type'] == 2) {
- $item->notice = '【'.$item->corp_name.'】'.'客服【'.$item->user_name.'】'.'超20分钟未点击发送'.$msgTypeText;
- }
- $item->msg_type = $params['msg_type'];
- $item->msg_type_text = $msgTypeText;
- # 智能群发组id
- $item->group_id = null;
- if(3 == $item->msg_type) {
- $periodMassMsgInfo = $periodMassMsgList->where('id', $item->rule_id)->first();
- $item->group_id = $periodMassMsgInfo->group_id ?? null;
- }
- }
- return [$data, $count];
- }
- }
|