123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617 |
- <?php
- namespace App\Support\qyApi;
- use App\Service\HttpService;
- use App\Log;
- use App\Models\AuthorizeCorp;
- use App\RedisModel;
- use App\Service\TokenService;
- use App\Support\EmailQueue;
- class QyCommon{
- /**
- * path:朋友圈接口路径 如:get_moment_comments(互动数据接口)
- */
- public static function momentCommon($path, $corpid, $params = [])
- {
- try{
- //记录调用日志
- Log::logInfo('企微朋友圈接口调用-'. $path, [
- 'params' => $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 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 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 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;
- }
- }
|