$params, 'corpid' => $corpid ], 'QyCommon/momentCommon'); # 获取AccessToken $accessToken = AuthorizeCorp::getAccessToken($corpid, $path); if(empty($accessToken)) { return false; } $requestUri = config('qyWechat.moment_common'); $url = $requestUri . $path . '?access_token=' . $accessToken; $response = HttpService::httpPost($url, json_encode($params)); if(empty($response)){ Log::logError('企微朋友圈接口调用失败-'.$path, [ 'url' => $url, 'params' => $params, 'corpid' => $corpid, 'response' => $response ], 'QyCommon/momentCommon'); return false; } $response = json_decode($response, true); if( !isset($response['errcode']) || $response['errcode'] !== 0 ){ Log::logError('企微朋友圈接口调用失败-'.$path, [ 'url' => $url, 'params' => $params, 'corpid' => $corpid, 'response' => $response ], 'QyCommon/momentCommon'); return false; } return $response; } catch (\Exception $e){ Log::logError('企微朋友圈接口调用异常-'.$path, [ 'path' => $path, 'params' => $params, 'corpid' => $corpid, 'msg' => $e->getMessage(), 'line' => $e->getLine() ], 'QyCommon/momentCommon'); return false; } } /** * 企业发表朋友圈获取任务创建结果 * @param $corpid string 企微id * @param $jobid string 企微朋友圈发布任务id * @return bool|mixed */ public static function get_moment_task_result($corpid, $jobid) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '企业发表朋友圈获取任务创建结果'); if(empty($accessToken)) { Log::logError('企业发表朋友圈获取任务创建结果', [ 'params' => [ 'corpid' => $corpid, 'jobid' => $jobid ], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $requestUri = config('qyWechat.get_moment_task_result'); $requestUri .= $accessToken.'&jobid='.$jobid; $response = HttpService::httpGet($requestUri ); $responseData = json_decode($response, true); Log::logInfo('企业发表朋友圈获取任务创建结果', [ 'params' => [ 'corpid' => $corpid, 'jobid' => $jobid ], 'response' => $responseData ], 'qyWechat'); return $responseData; } /** * 获取客户朋友圈企业发表的列表 * @param $corpid string 企微id * @param $moment_id string 朋友圈id * @param $retry integer 重试次数 * @return array|bool */ public static function get_moment_task($corpid, $moment_id, $responseDataArr = [], $cursor = '', $retry = 0) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取客户朋友圈企业发表的列表'); if(empty($accessToken)) { Log::logError('获取客户朋友圈企业发表的列表', [ 'params' => [ 'corpid' => $corpid, 'moment_id' => $moment_id ], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $postData = [ 'moment_id' => $moment_id, 'cursor' => $cursor, 'limit' => 1000, ]; $requestUri = config('qyWechat.get_moment_task'); $requestUri .= $accessToken; while(true) { $response = HttpService::httpPost($requestUri, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('获取客户朋友圈企业发表的列表', [ 'params' => [ 'corpid' => $corpid, 'moment_id' => $moment_id, 'cursor' => $postData['cursor'] ], 'response' => $responseData, ], 'qyWechat'); if(isset($responseData['errcode']) && $responseData['errcode'] == 0) { // 获取下一页游标 $cursor = $responseData['next_cursor']; $postData['cursor'] = $cursor; $responseDataArr = array_merge($responseDataArr, $responseData['task_list']); if(empty($cursor)) { // 没有下一页数据了,结束循环 return $responseDataArr; } } else if(isset($responseData['errcode']) && $responseData['errcode'] == 610023) { # 客服的客户达到上限,需业务手动处理 return $responseDataArr; } else { # 检测是否为预期外的错误码需要处理 // 系统繁忙 手机号码已存在 if(isset($responseData['errcode']) && !in_array($responseData['errcode'], [-1])) { $content = '获取客户朋友圈企业发表的列表响应结果异常'; $params = [ 'params' => [ 'corpid' => $corpid, 'moment_id' => $moment_id, 'cursor' => $postData['cursor'] ], 'response' => $responseData ]; self::getMomentTaskErrorMsgSend($content, $params); } if($retry < 3) { sleep(1); $retry++; return self::get_moment_task($corpid, $moment_id, $responseDataArr, $cursor, $retry); } else { // 返回数据异常了,退出循环并报警 $content = '获取客户朋友圈企业发表的列表重试3次后失败'; $params = [ 'params' => [ 'corpid' => $corpid, 'moment_id' => $moment_id, 'cursor' => $postData['cursor'] ], 'response' => $responseData ]; return $responseDataArr; } } } } public static function getMomentTaskErrorMsgSend($content, $params) { Log::logError($content, $params, 'qyWechat'); EmailQueue::rPush($content, json_encode($params, 256), ['song.shen@kuxuan-inc.com'], '猎羽'); } /** * 修改客户备注信息 * @param $corpid string 企微id * @param $userid string 企业成员userid * @param $external_userid string 外部联系人userid * @param $remark string string 此用户对外部联系人的备注,最多20个字符 * @param $description string 此用户对外部联系人的描述,最多150个字符 * @param $remark_company string 此用户对外部联系人备注的所属公司名称,最多20个字符 * @param $remark_mobiles array 此用户对外部联系人备注的手机号 * @param $remark_pic_mediaid string 备注图片的mediaid, * @return array|bool * * remark_company只在此外部联系人为微信用户时有效。 * remark,description,remark_company,remark_mobiles和remark_pic_mediaid不可同时为空。 * 如果填写了remark_mobiles,将会覆盖旧的备注手机号。 * 如果要清除所有备注手机号,请在remark_mobiles填写一个空字符串("")。 * remark_pic_mediaid可以通过素材管理接口获得。 */ public static function update_customer_remark($corpid, $userid, $external_userid, $remark = null, $description = null, $remark_company = null, array $remark_mobiles = null, $remark_pic_mediaid = null) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '修改客户备注信息'); if(empty($accessToken)) { Log::logError('修改客户备注信息', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $postData = [ 'userid' => $userid, 'external_userid' => $external_userid, ]; if(!is_null($remark)) { $postData['remark'] = $remark; } if(!is_null($description)) { $postData['description'] = $description; } if(!is_null($remark_company)) { $postData['remark_company'] = $remark_company; } if(!is_null($remark_mobiles)) { $postData['remark_mobiles'] = $remark_mobiles; } if(!is_null($remark_pic_mediaid)) { $postData['remark_pic_mediaid'] = $remark_pic_mediaid; } $requestUri = config('qyWechat.update_customer_remark'); $requestUri .= $accessToken; $response = HttpService::httpPost($requestUri ,json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('修改客户备注信息', [ 'params' => [ 'corpid' => $corpid, 'userid' => $userid, 'external_userid' => $external_userid, 'remark' => $remark, 'description' => $description, 'remark_company' => $remark_company, 'remark_mobiles' => $remark_mobiles, 'remark_pic_mediaid' => $remark_pic_mediaid, ], 'response' => $responseData ], 'qyWechat'); return $responseData; } /** * 创建企业标签 * @param $corpid string 企微id * @param $group_id string 标签组id * @param $group_name string 标签组名称 * @param $order integer 标签组排序值 * @param $tag array 标签列表 * @return mixed */ public static function add_corp_tag($corpid, $group_id, $group_name, $order, $tag, $retry = 0) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '创建企业标签'); if(empty($accessToken)) { Log::logError('创建企业标签', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $requestUri = config('qyWechat.add_corp_tag'); $requestUri .= $accessToken; $params['tag'] = $tag; // '[{"name": "TAG_NAME_1","order": 1},{"name": "TAG_NAME_2","order": 2}]'; if (!empty($group_name)) { $params['group_name'] = $group_name; } if (!empty($group_id)) { $params['group_id'] = $group_id; } if (!empty($order)) { $params['order'] = $order; } $responseData = HttpService::httpPost($requestUri, json_encode($params), true); if($responseData === false && $retry < 5) { $retry++; return self::add_corp_tag($corpid, $group_id, $group_name, $order, $tag, $retry); } $responseData = json_decode($responseData, 1); Log::logInfo('创建企业标签', [ 'params' => [ 'corpid' => $corpid, 'group_id' => $group_id, 'group_name' => $group_name, 'order' => $order, 'tag' => $tag, ], 'response' => $responseData ], 'qyWechat'); return $responseData; } /** * 编辑客户企业标签 * @param $corpid string 企微id * @param $userid string 企微成员userid * @param $external_userid string 外部联系人userid * @param $add_tag array 要标记的企业标签列表 * @param $remove_tag array 要移除的企业标签列表 * @return bool|mixed */ public static function mark_tag($corpid, $userid, $external_userid, $add_tag, $remove_tag, $retry=0) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '编辑客户企业标签'); if(empty($accessToken)) { Log::logError('编辑客户企业标签', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败', ], 'qyWechat'); return false; } $requestUri = config('qyWechat.mark_tag'); $requestUri .= $accessToken; $postData['userid'] = $userid; $postData['external_userid'] = $external_userid; if($add_tag) { $postData['add_tag'] = $add_tag; } if($remove_tag) { $postData['remove_tag'] = $remove_tag; } $responseData = HttpService::httpPost($requestUri, json_encode($postData), true); if($responseData === false && $retry < 5) { $retry++; return self::mark_tag($corpid, $userid, $external_userid, $add_tag, $remove_tag, $retry); } $responseData = json_decode($responseData, 1); Log::logInfo('编辑客户企业标签', [ 'params' => [ 'corpid' => $corpid, 'userid' => $userid, 'external_userid' => $external_userid, 'add_tag' => $add_tag, 'remove_tag' => $remove_tag, ], 'response' => $responseData, ], 'qyWechat'); return $responseData; } /** * 获取企微所有已授权部门列表 * @param $corpid * @return bool|mixed */ public static function simplelist($corpid) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企微所有已授权部门列表'); if(empty($accessToken)) { Log::logError('获取企微所有已授权部门列表', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $requestUri = config('qyWechat.simplelist'); $requestUri .= $accessToken.'&id='; $response = HttpService::httpGet($requestUri ); $responseData = json_decode($response, true); Log::logInfo('获取企微所有已授权部门列表', [ 'params' => ['corpid' => $corpid], 'response' => $responseData, ], 'qyWechat'); return $responseData; } /** * 获取企业配置了联系我功能的客服列表 * @param $corpid * @return bool|mixed */ public static function get_follow_user_list($corpid) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企业配置了联系我功能的客服列表'); if(empty($accessToken)) { Log::logError('获取企业配置了联系我功能的客服列表', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $requestUri = config('qyWechat.get_follow_user_list'); $requestUri .= $accessToken.'&id='; $response = HttpService::httpGet($requestUri ); $responseData = json_decode($response, true); Log::logInfo('获取企业配置了联系我功能的客服列表', [ 'params' => ['corpid' => $corpid], 'response' => $responseData, ], 'qyWechat'); return $responseData; } /** * 获取部门详细信息 * @param $corpid * @param $department_id * @return bool|mixed */ public static function get_department_info($corpid, $department_id) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取部门详细信息'); if(empty($accessToken)) { Log::logError('获取部门详细信息', [ 'params' => [ 'corpid' => $corpid, 'depatment_id' => $department_id, ], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $requestUri = config('qyWechat.get_department_info'); $requestUri .= $accessToken.'&id='.$department_id; $response = HttpService::httpGet($requestUri ); $responseData = json_decode($response, true); Log::logInfo('获取部门详细信息', [ 'params' => [ 'corpid' => $corpid, 'depatment_id' => $department_id, ], 'response' => $responseData, ], 'qyWechat'); return $responseData; } /** * 根据Oauth2授权code获取用户信息 * */ public static function getCustomerInfoByCode($corpid, $code) { # 获取SuiteAccessToken $accessToken = AuthorizeCorp::getAccessToken($corpid, '根据Oauth2授权code获取用户信息'); if(empty($accessToken)) { Log::logError('根据Oauth2授权code获取用户信息时令牌获取失败', [ 'corpid' => $corpid, 'code' => $code ], 'GetCustomerInfoByCode'); return false; } # 获取用户信息API $getExternalContactUri = config('qyWechat.get_user_info'); $getExternalContactUri .= $accessToken . '&code=' . $code; $response = HttpService::httpGet($getExternalContactUri); $responseData = json_decode($response, true); return $responseData; } /** * 分配在职成员客户 * @param $corpid string 企微id * @param $handoverUserId string 被接替员工id * @param $takeoverUserId string 接替员工id * @param $externalUserIdList array 需要迁移的外部联系人id * @param $transferSuccessMsg string 迁移成功后为客户发送的提醒消息 * @return mixed */ public static function transferCustomerOnJob($corpid, $handoverUserId, $takeoverUserId, $externalUserIdList, $transferSuccessMsg) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '分配在职成员客户'); if(empty($accessToken)) { Log::logError('分配在职成员客户', [ 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId, 'external_userid_list' => $externalUserIdList, 'transfer_success_msg' => $transferSuccessMsg], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.transfer_customer_on_job').$accessToken; $params['handover_userid'] = $handoverUserId; $params['takeover_userid'] = $takeoverUserId; if(!empty($transferSuccessMsg)) { $params['transfer_success_msg'] = $transferSuccessMsg; } $params['external_userid'] = $externalUserIdList; $result = HttpService::httpPost($url, json_encode($params), true); $result = json_decode($result, 1); Log::logInfo('分配在职成员客户', [ 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId, 'external_userid_list' => $externalUserIdList, 'transfer_success_msg' => $transferSuccessMsg], 'responseData' => $result, ], 'qyWechat'); return $result; } /** * 查询待分配的客户列表 * @param $corpid * @param $cursor * @return mixed */ public static function getUnassignedList($corpid, $cursor, $retry = 0) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '查询待分配的离职成员列表'); if(empty($accessToken)) { Log::logError('查询待分配的离职成员列表', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.get_unassigned_list').$accessToken; $params['cursor'] = $cursor; $data = HttpService::httpPost($url, json_encode($params), true); if($data === false && $retry < 3) { $retry++; return self::getUnassignedList($corpid, $cursor, $retry); } $data = json_decode($data, 1); Log::logInfo('查询待分配的离职成员列表', [ 'params' => ['corpid' => $corpid, 'cursor' => $cursor], 'responseData' => $data, ], 'qyWechat'); return $data; } /** * 分配离职成员的客户 * @param $corpid string 企业id * @param $handoverUserId string 原跟进成员的userid * @param $takeoverUserId string 接替成员的userid * @param $externalUserIdList array 客户的external_userid列表,每次最多分配100个客户 * @return mixed * 注意: * 1.external_userid必须是handover_userid的客户(即配置了客户联系功能的成员所添加的联系人)。 * 2.handover_userid必须是已离职用户。 */ public static function transferCustomerQuit($corpid, $handoverUserId, $takeoverUserId, $externalUserIdList) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '分配离职成员的客户'); if(empty($accessToken)) { Log::logError('分配离职成员的客户', [ 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId, 'external_userid_list' => $externalUserIdList], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.transfer_customer_quit').$accessToken; $params['handover_userid'] = $handoverUserId; $params['takeover_userid'] = $takeoverUserId; $params['external_userid'] = $externalUserIdList; $result = HttpService::httpPost($url, json_encode($params), true); $result = json_decode($result, 1); Log::logInfo('分配离职成员的客户', [ 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId, 'external_userid_list' => $externalUserIdList], 'responseData' => $result, ], 'qyWechat'); return $result; } /** * 客户迁移结果查询 * @param $type int 接替类型 1在职 2离职 * @param $corpid * @param $handOverUserId string 原跟进员工user_id * @param $takeOverUserId string 接替成员的user_id * @param string $cursor string 分页查询的游标 * @return mixed */ public static function transferResult($type, $corpid, $handOverUserId, $takeOverUserId, $cursor) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '客户迁移结果查询'); if(empty($accessToken)) { Log::logError('客户迁移结果查询', [ 'params' => [ 'type' => $type, 'corpid' => $corpid, 'handover_userid' => $handOverUserId, 'takeover_userid' => $takeOverUserId, 'cursor' => $cursor, ], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } if(1 == $type) { $url = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/transfer_result?access_token='.$accessToken; } else { $url = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/resigned/transfer_result?access_token='.$accessToken; } $params['handover_userid'] = $handOverUserId; $params['takeover_userid'] = $takeOverUserId; $params['cursor'] = $cursor; $result = HttpService::httpPost($url, json_encode($params), true); $result = json_decode($result, 1); Log::logInfo('客户迁移结果查询', [ 'params' => [ 'type' => $type, 'corpid' => $corpid, 'handover_userid' => $handOverUserId, 'takeover_userid' => $takeOverUserId, 'cursor' => $cursor, ], 'response' => $result, ], 'qyWechat'); return $result; } /** * 客户群基本信息获取 * */ public static function getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter=0, $retry=0) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '客户群基本信息获取'); if(empty($accessToken)) { Log::logError('客户群基本信息获取', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.group_chat_list') . $accessToken; $postData = [ 'status_filter' => $statusFilter, 'owner_filter' => [ 'userid_list' => $ownerFilter ], 'cursor' => $cursor, 'limit' => $limit ]; $response = HttpService::httpPost($url, json_encode($postData)); if($response === false && $retry <=5) { // 发起重试 $retry++; return self::getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter, $retry); } $responseData = json_decode($response, true); Log::logInfo('getGroupChatList', ['params' => [ 'corpid' => $corpid, 'user_list' => $ownerFilter, 'cursor' => $cursor ], 'response' => $responseData], 'qyWechat'); return $responseData; } /** * 获取客户群详情 * */ public static function getGroupChatDetail($corpid, $chatId, $needName=1, $retry=0) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '客户群基本信息获取'); if(empty($accessToken)) { Log::logError('客户群详情获取', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.group_chat_detail') . $accessToken; $postData = [ 'chat_id' => $chatId, 'need_name' => $needName ]; $response = HttpService::httpPost($url, json_encode($postData)); if($response === false && $retry <=5) { // 发起重试 $retry++; return self::getGroupChatDetail($corpid, $chatId, $needName, $retry); } $responseData = json_decode($response, true); Log::logInfo('getGroupChatDetail', ['params' => ['corpid' => $corpid, 'chat_id' => $chatId ], 'response' => $responseData], 'qyWechat'); return $responseData; } /** * 企业成员user_id转换为open_userid * */ public static function useridToOpenUserid($corpid, $userIdList) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '企业成员user_id转换为open_userid'); if(empty($accessToken)) { Log::logError('企业成员user_id转换为open_userid', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.userid_to_open_userid') . $accessToken; $postData = [ 'userid_list' => $userIdList, ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('useridToOpenUserid', ['data' => $responseData, 'param' => $userIdList, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 获取企微客服账号许可信息 * */ public static function getActiveUserList($corpid, $cursor, $limit, $suiteId, $suiteSecret) { # 获取SuiteAccessToken $accessToken = TokenService::getAuthToken($suiteId, $suiteSecret); // $accessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); if(empty($accessToken)) { Log::logError('获取企微客服账号许可信息', [ 'params' => ['corpid' => $corpid], 'errmsg' => '服务商的access_token获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.list_active_account') . $accessToken; $postData = [ 'corpid' => $corpid, 'limit' => $limit, 'cursor' => $cursor ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('getActiveUserList', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 获取指定客服账号许可信息 * */ public static function getUserActiveInfo($corpid, $userId, $suiteId, $suiteSecret) { # 获取SuiteAccessToken $accessToken = TokenService::getAuthToken($suiteId, $suiteSecret); // $accessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); if(empty($accessToken)) { Log::logError('获取企微指定客服账号许可信息', [ 'params' => ['corpid' => $corpid], 'errmsg' => '服务商的access_token获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.active_info_by_user') . $accessToken; $postData = [ 'corpid' => $corpid, 'userid' => $userId, ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('getUserActiveInfo', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 配置客户群进群方式 * @param $corpid string 企微id * @param $scene integer 场景 1:群的小程序插件 2:群的二维码插件 * @param $remark string 联系方式的备注信息,用于助记,超过30个字符将被截断 * @param $autoCreateRoom int 当群满了后,是否自动新建群。0-否;1-是。 默认为1 * @param $roomBaseName string 自动建群的群名前缀 * @param $roomBaseId int 自动建群的起始序号 * @param $chatIdList array 使用该配置的客户群ID列表,支持5个 * @param $state string 企业自定义的state参数,用于区分不同的入群渠道 * */ public static function addGroupJoinWay( $corpid, $scene, $chatIdList, $state, $autoCreateRoom=1, $roomBaseName='', $roomBaseId=1, $remark=null ) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '配置客户群进群方式'); if(empty($accessToken)) { Log::logError('配置客户群进群方式', [ 'params' => [ 'corpid' => $corpid, 'scene' => $scene, 'chat_id_list' => $chatIdList, 'state' => $state, 'auto_create_room' => $autoCreateRoom, 'room_base_name' => $roomBaseName, 'room_base_id' => $roomBaseId, 'remark' => $remark ], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.add_group_join_way') . $accessToken; $postData = [ 'scene' => $scene, 'chat_id_list' => $chatIdList ]; # 联系方式的备注信息 if($remark) $postData['remark'] = $remark; # 当群满了后,是否自动新建群 if(in_array($autoCreateRoom, [0, 1])) $postData['auto_create_room'] = $autoCreateRoom; # 自动建群规则 if($roomBaseName) $postData['room_base_name'] = $roomBaseName; if(is_numeric($roomBaseId)) $postData['room_base_id'] = $roomBaseId; # state if($state) $postData['state'] = $state; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('addGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 获取客户群进群方式配置 * @param $corpid string 企微ID * @param $configId string 联系方式的配置id * */ public static function getGroupJoinWay($corpid, $configId) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取客户群进群方式配置'); if(empty($accessToken)) { Log::logError('获取客户群进群方式配置', [ 'params' => ['corpid' => $corpid, 'config_id' => $configId], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.get_group_join_way') . $accessToken; $postData = [ 'config_id' => $configId, ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('getGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 更新客户群进群方式配置 * @param $corpid string 企微id * @param $configId string 联系方式的配置id * @param $scene integer 场景 1:群的小程序插件 2:群的二维码插件 * @param $remark string 联系方式的备注信息,用于助记,超过30个字符将被截断 * @param $autoCreateRoom int 当群满了后,是否自动新建群。0-否;1-是。 默认为1 * @param $roomBaseName string 自动建群的群名前缀 * @param $roomBaseId int 自动建群的起始序号 * @param $chatIdList array 使用该配置的客户群ID列表,支持5个 * @param $state string 企业自定义的state参数,用于区分不同的入群渠道 * */ public static function updateGroupJoinWay( $corpid, $configId, $scene, $chatIdList, $state, $autoCreateRoom=1, $roomBaseName='', $roomBaseId=1, $remark=null ) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '配置客户群进群方式'); if(empty($accessToken)) { Log::logError('配置客户群进群方式', [ 'params' => [ 'corpid' => $corpid, 'scene' => $scene, 'chat_id_list' => $chatIdList, 'state' => $state, 'auto_create_room' => $autoCreateRoom, 'room_base_name' => $roomBaseName, 'room_base_id' => $roomBaseId, 'remark' => $remark ], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.update_group_join_way') . $accessToken; $postData = [ 'config_id' => $configId, 'scene' => $scene, 'chat_id_list' => $chatIdList ]; # 联系方式的备注信息 if($remark) $postData['remark'] = $remark; # 当群满了后,是否自动新建群 if(in_array($autoCreateRoom, [0, 1])) $postData['auto_create_room'] = $autoCreateRoom; # 自动建群规则 if($roomBaseName) $postData['room_base_name'] = $roomBaseName; if(is_numeric($roomBaseId)) $postData['room_base_id'] = $roomBaseId; # state if($state) $postData['state'] = $state; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('updateGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 删除客户群进群方式配置 * @param $corpid string 企微ID * @param $configId string 联系方式的配置id * */ public static function delGroupJoinWay($corpid, $configId) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '删除客户群进群方式配置'); if(empty($accessToken)) { Log::logError('删除客户群进群方式配置', [ 'params' => ['corpid' => $corpid, 'config_id' => $configId], 'errmsg' => '令牌获取失败' ], 'qyWechat'); return false; } $url = config('qyWechat.del_group_join_way') . $accessToken; $postData = [ 'config_id' => $configId, ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('delGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 获取订单中的帐号列表 * @param $corpid string 企微ID * @param $configId string 联系方式的配置id * */ public static function licenseListOfOrder($orderId, $limit=1000, $cursor=null) { $component = config('qyWechat.suite_info'); $suiteId = $component['suite_id']; $suiteSecret = $component['suite_secret']; # 获取SuiteAccessToken // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); if(!$suiteAccessToken) { Log::logError('获取订单中的帐号列表时获取suiteAccessToken异常', [ 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret], 'errmsg' => '令牌获取失败' ], 'LicenseListOfOrder'); return false; } $url = config('qyWechat.list_order_account') . $suiteAccessToken; $postData = [ 'order_id' => $orderId, 'limit' => $limit, 'cursor' => $cursor ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('订单中的许可信息响应结果为:', ['data' => $responseData, 'param' => $postData], 'LicenseListOfOrder'); return $responseData; } /** * 分配许可激活账号 * */ public static function activeAccountByLicense($activeCode, $corpid, $userid) { $component = config('qyWechat.suite_info'); $suiteId = $component['suite_id']; $suiteSecret = $component['suite_secret']; # 获取SuiteAccessToken // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); if(!$suiteAccessToken) { Log::logError('分配许可激活账号时获取suiteAccessToken异常', [ 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret], 'errmsg' => '令牌获取失败' ], 'ActiveAccountByLicense'); return false; } $url = config('qyWechat.active_by_license') . $suiteAccessToken; $postData = [ 'active_code' => $activeCode, 'corpid' => $corpid, 'userid' => $userid ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('分配许可激活账号响应结果为:', ['data' => $responseData, 'param' => $postData], 'ActiveAccountByLicense'); return $responseData; } /** * 批量分配许可激活账号 * */ public static function batchActiveAccountByLicense($corpid, $activeList) { $component = config('qyWechat.suite_info'); $suiteId = $component['suite_id']; $suiteSecret = $component['suite_secret']; # 获取SuiteAccessToken $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); if(!$suiteAccessToken) { Log::logError('批量分配许可激活账号时获取suiteAccessToken异常', [ 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret], 'errmsg' => '令牌获取失败' ], 'BatchActiveAccountByLicense'); return false; } $url = config('qyWechat.batch_active_by_license') . $suiteAccessToken; $postData = [ 'corpid' => $corpid, 'active_list' => $activeList ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('批量分配许可激活账号响应结果为:', ['data' => $responseData, 'param' => $postData], 'BatchActiveAccountByLicense'); return $responseData; } /** * 批量转移账号许可 * */ public static function batchTransferLicense($corpid, $transferList) { $component = config('qyWechat.suite_info'); $suiteId = $component['suite_id']; $suiteSecret = $component['suite_secret']; # 获取SuiteAccessToken // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); if(!$suiteAccessToken) { Log::logError('批量转移许可激活账号时获取suiteAccessToken异常', [ 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret], 'errmsg' => '令牌获取失败' ], 'BatchTransferLicense'); return false; } $url = config('qyWechat.batch_transfer_license') . $suiteAccessToken; $postData = [ 'corpid' => $corpid, 'transfer_list' => $transferList ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('批量分配许可激活账号响应结果为:', ['data' => $responseData, 'param' => $postData], 'BatchTransferLicense'); return $responseData; } /** * 停止企业群发 * */ public static function cancelMsg($corpid, $msgId) { $accessToken = AuthorizeCorp::getAccessToken($corpid); if(empty($accessToken)) { Log::logError('停止企业群发令牌获取失败', [ 'params' => ['corpid' => $corpid, 'msg_id' => $msgId], ], 'qyWechat'); return false; } $url = config('qyWechat.cancel_mass_msg') . $accessToken; $postData = [ 'msgid' => $msgId, ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('cancelMsg', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } # 生成客服许可续期任务 public static function createRenewOrderJob($corpid, $accountList, $retry = 0) { $component = config('qyWechat.suite_info'); $suiteId = $component['suite_id']; $suiteSecret = $component['suite_secret']; # 获取SuiteAccessToken // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); if(!$suiteAccessToken) { Log::logError('创建客服许可续期任务时获取suiteAccessToken异常', [ 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret], 'errmsg' => '令牌获取失败' ], 'CreateRenewOrderJob'); return false; } $url = config('qyWechat.create_renew_order_job') . $suiteAccessToken; $postData = [ 'corpid' => $corpid, 'account_list' => $accountList ]; $response = HttpService::httpPost($url, json_encode($postData)); if(empty($response) && $retry<5) { $retry++; sleep(1); return self::createRenewOrderJob($corpid, $accountList, $retry); } $responseData = json_decode($response, true); Log::logInfo('创建客服许可续期任务响应结果为:', ['data' => $responseData, 'param' => $postData, 'url' => $url], 'CreateRenewOrderJob'); return $responseData; } # 提交客服许可续期订单 public static function submitOrderJob($jobId, $buyerUserId, $accountDuration, $retry = 0) { $component = config('qyWechat.suite_info'); $suiteId = $component['suite_id']; $suiteSecret = $component['suite_secret']; # 获取SuiteAccessToken // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); if(!$suiteAccessToken) { Log::logError('提交客服许可续期任务订单时获取suiteAccessToken异常', [ 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret], 'errmsg' => '令牌获取失败' ], 'submitOrderJob'); return false; } $url = config('qyWechat.submit_order_job') . $suiteAccessToken; $postData = [ 'jobid' => $jobId, 'buyer_userid' => $buyerUserId, 'account_duration' => $accountDuration ]; $response = HttpService::httpPost($url, json_encode($postData)); if(empty($response) && $retry<5) { $retry++; sleep(1); return self::submitOrderJob($jobId, $buyerUserId, $accountDuration, $retry); } $responseData = json_decode($response, true); Log::logInfo('可续期任务订单响应结果为:', ['data' => $responseData, 'param' => $postData, 'url' => $url], 'submitOrderJob'); return $responseData; } # 取消订单 public static function cancelOrder($corpid, $orderId, $retry = 0) { $component = config('qyWechat.suite_info'); $suiteId = $component['suite_id']; $suiteSecret = $component['suite_secret']; # 获取SuiteAccessToken // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret); $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); if(!$suiteAccessToken) { Log::logError('取消订单时获取suiteAccessToken异常', [ 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret], 'errmsg' => '令牌获取失败' ], 'cancelOrder'); return false; } $url = config('qyWechat.cancel_order') . $suiteAccessToken; $postData = [ 'corpid' => $corpid, 'order_id' => $orderId ]; $response = HttpService::httpPost($url, json_encode($postData)); if(empty($response) && $retry<5) { $retry++; sleep(1); return self::cancelOrder($corpid, $orderId, $retry); } $responseData = json_decode($response, true); Log::logInfo('取消订单响应结果为:', ['data' => $responseData, 'param' => $postData, 'url' => $url], 'cancelOrder'); return $responseData; } // 根据内部员工获取外部联系客户具体方法 public static function getExternalContactList($userId, $corpid, $retry = 0) { $accessToken = AuthorizeCorp::getAccessToken($corpid, '根据员工获取外部客户'); if(!$accessToken) { Log::logError('获取客户列表时accessToken异常', [ 'params' => ['corpid' => $corpid], 'errmsg' => '令牌获取失败' ], 'getCustomerList'); return '令牌获取失败'; } $url = config('qyWechat.external_contact_list').$accessToken.'&userid='.$userId; $response = HttpService::HttpGet($url); if(empty($response) && $retry<5) { $retry++; sleep(1); return self::getExternalContactList($userId, $corpid, $retry); } $responseData = json_decode($response, true); if(in_array($responseData['errcode'],[-1])) { $retry++; sleep(1); return self::getExternalContactList($userId, $corpid, $retry); } Log::logInfo('获取客户列表响应结果为:', ['data' => $responseData, 'param' => ['corpid' => $corpid, 'user_id' => $userId] , 'url' => $url], 'getExternalContactList'); return $responseData; } /** * 客户群在职迁移 * */ public static function onJobTransfer($corpid, $chatIdList, $newOwner) { $accessToken = AuthorizeCorp::getAccessToken($corpid); if(empty($accessToken)) { Log::logError('【onJobTransfer】客户群在职迁移令牌获取失败', [ 'params' => ['corpid' => $corpid, 'chat_id_list' => $chatIdList, 'new_owner' => $newOwner], ], 'qyWechat'); return false; } $url = config('qyWechat.chat_group_on_job_transfer') . $accessToken; $postData = [ 'chat_id_list' => $chatIdList, 'new_owner' => $newOwner ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('onJobTransfer', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 离职迁移 * */ public static function transfer($corpid, $chatIdList, $newOwner) { $accessToken = AuthorizeCorp::getAccessToken($corpid); if(empty($accessToken)) { Log::logError('【OffJobTransfer】客户群离职迁移令牌获取失败', [ 'params' => ['corpid' => $corpid, 'chat_id_list' => $chatIdList, 'new_owner' => $newOwner], ], 'qyWechat'); return false; } $url = config('qyWechat.chat_group_transfer') . $accessToken; $postData = [ 'chat_id_list' => $chatIdList, 'new_owner' => $newOwner ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('OffJobTransfer', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 停止发表企业朋友圈 * */ public static function cancelMomentTask($corpid, $momentId) { $accessToken = AuthorizeCorp::getAccessToken($corpid); if(empty($accessToken)) { Log::logError('停止发表企业朋友圈令牌获取失败', [ 'params' => ['corpid' => $corpid, 'moment_id' => $momentId], ], 'qyWechat'); return false; } $url = config('qyWechat.cancel_moment_task') . $accessToken; $postData = [ 'moment_id' => $momentId, ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('cancelMomentTask', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 知识库分组管理-添加分组 * */ public static function addGroup($corpid, $name) { $accessToken = 'O2npAJry4n5WKw_a8-toxrRACiRDl7tEDrfY12PDNHXLALb6UfK8t_bmtQwPnmtIelZRJQLLrF0tQt2vD_6D0pxt9ZeJTr9hMMQkkd5goeTK2YqgLul3F7lqgIYNJJK7tYvTjGObPS7bY7hyUgnwGRsYpWCwzBMU3IUO59MrNMSaU9pcnwNxw7sC-_N7b2GEQcoIr7g1voYRF2m-uZRWLw'; $url = config('qyWechat.knowledge_add_group') . $accessToken; $postData = [ 'name' => $name, ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('knowledgeAddGroup', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 知识库问答管理-添加问答 * */ public static function addIntent($corpid, $groupId, $question, $similarQuestions, $answers) { $accessToken = 'O2npAJry4n5WKw_a8-toxrRACiRDl7tEDrfY12PDNHXLALb6UfK8t_bmtQwPnmtIelZRJQLLrF0tQt2vD_6D0pxt9ZeJTr9hMMQkkd5goeTK2YqgLul3F7lqgIYNJJK7tYvTjGObPS7bY7hyUgnwGRsYpWCwzBMU3IUO59MrNMSaU9pcnwNxw7sC-_N7b2GEQcoIr7g1voYRF2m-uZRWLw'; $url = config('qyWechat.knowledge_add_intent') . $accessToken; $postData = [ 'group_id' => $groupId, 'question' => [ 'text' => [ 'content' => $question ] ], 'similar_questions' => $similarQuestions, 'answers' => json_decode($answers, true) ]; $response = HttpService::httpPost($url, json_encode($postData)); $responseData = json_decode($response, true); Log::logInfo('knowledgeAddGroup', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } # 添加客户入群欢迎语素材 public static function addChatGroupWelcomeTemplate($corpid, $content, $attachments, $retry = 0) { $accessToken = AuthorizeCorp::getAccessToken($corpid); if(empty($accessToken)) { Log::logError('添加客户入群欢迎语素材令牌获取失败', [ 'params' => ['corpid' => $corpid, 'content' => $content, 'attachments' => $attachments], ], 'qyWechat'); return false; } $url = config('qyWechat.add_chat_group_welcome_template') . $accessToken; $text = [ "notify" => 0, 'text' => [ 'content' => $content ], ]; $postData = array_merge($text, $attachments); $response = HttpService::httpPost($url, json_encode($postData)); if($response === false && $retry < 3) { $retry++; return self::addChatGroupWelcomeTemplate($corpid, $content, $attachments, $retry); } $responseData = json_decode($response, true); Log::logInfo('addChatGroupWelcomeTemplate', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } public static function editChatGroupWelcomeTemplate($corpid, $templateId, $content, $attachments, $retry = 0) { $accessToken = AuthorizeCorp::getAccessToken($corpid); if(empty($accessToken)) { Log::logError('编辑客户入群欢迎语素材令牌获取失败', [ 'params' => ['corpid' => $corpid, 'content' => $content, 'attachments' => $attachments], ], 'qyWechat'); return false; } $url = config('qyWechat.edit_chat_group_welcome_template') . $accessToken; $text = [ "template_id" => $templateId, "notify" => 0, 'text' => [ 'content' => $content ], ]; $postData = array_merge($text, $attachments); $response = HttpService::httpPost($url, json_encode($postData)); if($response === false && $retry < 3) { $retry++; return self::editChatGroupWelcomeTemplate($corpid, $templateId, $content, $attachments, $retry); } $responseData = json_decode($response, true); Log::logInfo('editChatGroupWelcomeTemplate', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } public static function delChatGroupWelcomeTemplate($corpid, $templateId, $retry = 0) { $accessToken = AuthorizeCorp::getAccessToken($corpid); if(empty($accessToken)) { Log::logError('删除客户入群欢迎语素材令牌获取失败', [ 'params' => ['corpid' => $corpid, 'template_id' => $templateId], ], 'qyWechat'); return false; } $url = config('qyWechat.del_chat_group_welcome_template') . $accessToken; $postData = [ "template_id" => $templateId ]; $response = HttpService::httpPost($url, json_encode($postData)); if($response === false && $retry < 3) { $retry++; return self::delChatGroupWelcomeTemplate($corpid, $templateId, $retry); } $responseData = json_decode($response, true); Log::logInfo('delChatGroupWelcomeTemplate', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 获取应用的接口许可状态 * */ public static function getAppLicenseInfo($corpid, $suiteId, $suiteCorpid, $suiteSecret, $retry=0) { $accessToken = TokenService::getProviderToken($suiteCorpid, $suiteSecret); $url = config('qyWechat.get_app_license_info') . $accessToken; $postData = [ 'corpid' => $corpid, 'suite_id' => $suiteId ]; $response = HttpService::httpPost($url, json_encode($postData)); if($response === false && $retry < 3) { $retry++; return self::getAppLicenseInfo($corpid, $suiteCorpid, $suiteSecret, $retry); } $responseData = json_decode($response, true); Log::logInfo('getAppLicenseInfo', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat'); return $responseData; } /** * 同步企微信息 * */ public static function syncCorpData($corpid, $permanentCode, $retry=0) { $component = config('qyWechat.suite_info'); $suiteId = $component['app_template_id']; $suiteSecret = $component['app_template_secret']; $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret); $url = config('qyWechat.get_corp_info') . $suiteAccessToken; $params = [ 'auth_corpid' => $corpid, 'permanent_code' => $permanentCode ]; $response = HttpService::httpPost($url, json_encode($params), true); if($response === false && $retry < 3) { $retry++; return self::syncCorpData($corpid, $permanentCode, $retry); } $responseData = json_decode($response, true); Log::logInfo('syncCorpData', ['data' => $responseData, 'param' => $params, 'corpid' => $corpid], 'qyWechat'); return $responseData; } }