企微短剧业务系统

QyCommon.php 59KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. <?php
  2. namespace App\Support\qyApi;
  3. use App\Service\HttpService;
  4. use App\Log;
  5. use App\Models\AuthorizeCorp;
  6. use App\RedisModel;
  7. use App\Service\TokenService;
  8. use App\Support\EmailQueue;
  9. class QyCommon{
  10. /**
  11. * path:朋友圈接口路径 如:get_moment_comments(互动数据接口)
  12. */
  13. public static function momentCommon($path, $corpid, $params = [])
  14. {
  15. try{
  16. //记录调用日志
  17. Log::logInfo('企微朋友圈接口调用-'. $path, [
  18. 'params' => $params,
  19. 'corpid' => $corpid
  20. ], 'QyCommon/momentCommon');
  21. # 获取AccessToken
  22. $accessToken = AuthorizeCorp::getAccessToken($corpid, $path);
  23. if(empty($accessToken)) {
  24. return false;
  25. }
  26. $requestUri = config('qyWechat.moment_common');
  27. $url = $requestUri . $path . '?access_token=' . $accessToken;
  28. $response = HttpService::httpPost($url, json_encode($params));
  29. if(empty($response)){
  30. Log::logError('企微朋友圈接口调用失败-'.$path, [
  31. 'url' => $url,
  32. 'params' => $params,
  33. 'corpid' => $corpid,
  34. 'response' => $response
  35. ], 'QyCommon/momentCommon');
  36. return false;
  37. }
  38. $response = json_decode($response, true);
  39. if( !isset($response['errcode']) || $response['errcode'] !== 0 ){
  40. Log::logError('企微朋友圈接口调用失败-'.$path, [
  41. 'url' => $url,
  42. 'params' => $params,
  43. 'corpid' => $corpid,
  44. 'response' => $response
  45. ], 'QyCommon/momentCommon');
  46. return false;
  47. }
  48. return $response;
  49. } catch (\Exception $e){
  50. Log::logError('企微朋友圈接口调用异常-'.$path, [
  51. 'path' => $path,
  52. 'params' => $params,
  53. 'corpid' => $corpid,
  54. 'msg' => $e->getMessage(),
  55. 'line' => $e->getLine()
  56. ], 'QyCommon/momentCommon');
  57. return false;
  58. }
  59. }
  60. /**
  61. * 企业发表朋友圈获取任务创建结果
  62. * @param $corpid string 企微id
  63. * @param $jobid string 企微朋友圈发布任务id
  64. * @return bool|mixed
  65. */
  66. public static function get_moment_task_result($corpid, $jobid)
  67. {
  68. $accessToken = AuthorizeCorp::getAccessToken($corpid, '企业发表朋友圈获取任务创建结果');
  69. if(empty($accessToken)) {
  70. Log::logError('企业发表朋友圈获取任务创建结果', [
  71. 'params' => [
  72. 'corpid' => $corpid,
  73. 'jobid' => $jobid
  74. ],
  75. 'errmsg' => '令牌获取失败'
  76. ], 'qyWechat');
  77. return false;
  78. }
  79. $requestUri = config('qyWechat.get_moment_task_result');
  80. $requestUri .= $accessToken.'&jobid='.$jobid;
  81. $response = HttpService::httpGet($requestUri );
  82. $responseData = json_decode($response, true);
  83. Log::logInfo('企业发表朋友圈获取任务创建结果', [
  84. 'params' => [
  85. 'corpid' => $corpid,
  86. 'jobid' => $jobid
  87. ],
  88. 'response' => $responseData
  89. ], 'qyWechat');
  90. return $responseData;
  91. }
  92. /**
  93. * 获取客户朋友圈企业发表的列表
  94. * @param $corpid string 企微id
  95. * @param $moment_id string 朋友圈id
  96. * @param $retry integer 重试次数
  97. * @return array|bool
  98. */
  99. public static function get_moment_task($corpid, $moment_id, $responseDataArr = [], $cursor = '', $retry = 0)
  100. {
  101. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取客户朋友圈企业发表的列表');
  102. if(empty($accessToken)) {
  103. Log::logError('获取客户朋友圈企业发表的列表', [
  104. 'params' => [
  105. 'corpid' => $corpid,
  106. 'moment_id' => $moment_id
  107. ],
  108. 'errmsg' => '令牌获取失败'
  109. ], 'qyWechat');
  110. return false;
  111. }
  112. $postData = [
  113. 'moment_id' => $moment_id,
  114. 'cursor' => $cursor,
  115. 'limit' => 1000,
  116. ];
  117. $requestUri = config('qyWechat.get_moment_task');
  118. $requestUri .= $accessToken;
  119. while(true) {
  120. $response = HttpService::httpPost($requestUri, json_encode($postData));
  121. $responseData = json_decode($response, true);
  122. Log::logInfo('获取客户朋友圈企业发表的列表', [
  123. 'params' => [
  124. 'corpid' => $corpid,
  125. 'moment_id' => $moment_id,
  126. 'cursor' => $postData['cursor']
  127. ],
  128. 'response' => $responseData,
  129. ], 'qyWechat');
  130. if(isset($responseData['errcode']) && $responseData['errcode'] == 0) {
  131. // 获取下一页游标
  132. $cursor = $responseData['next_cursor'];
  133. $postData['cursor'] = $cursor;
  134. $responseDataArr = array_merge($responseDataArr, $responseData['task_list']);
  135. if(empty($cursor)) {
  136. // 没有下一页数据了,结束循环
  137. return $responseDataArr;
  138. }
  139. } else if(isset($responseData['errcode']) && $responseData['errcode'] == 610023) {
  140. # 客服的客户达到上限,需业务手动处理
  141. return $responseDataArr;
  142. } else {
  143. # 检测是否为预期外的错误码需要处理
  144. // 系统繁忙 手机号码已存在
  145. if(isset($responseData['errcode']) && !in_array($responseData['errcode'], [-1])) {
  146. $content = '获取客户朋友圈企业发表的列表响应结果异常';
  147. $params = [
  148. 'params' => [
  149. 'corpid' => $corpid,
  150. 'moment_id' => $moment_id,
  151. 'cursor' => $postData['cursor']
  152. ],
  153. 'response' => $responseData
  154. ];
  155. self::getMomentTaskErrorMsgSend($content, $params);
  156. }
  157. if($retry < 3) {
  158. sleep(1);
  159. $retry++;
  160. return self::get_moment_task($corpid, $moment_id, $responseDataArr, $cursor, $retry);
  161. } else {
  162. // 返回数据异常了,退出循环并报警
  163. $content = '获取客户朋友圈企业发表的列表重试3次后失败';
  164. $params = [
  165. 'params' => [
  166. 'corpid' => $corpid,
  167. 'moment_id' => $moment_id,
  168. 'cursor' => $postData['cursor']
  169. ],
  170. 'response' => $responseData
  171. ];
  172. return $responseDataArr;
  173. }
  174. }
  175. }
  176. }
  177. public static function getMomentTaskErrorMsgSend($content, $params) {
  178. Log::logError($content, $params, 'qyWechat');
  179. EmailQueue::rPush($content, json_encode($params, 256), ['song.shen@kuxuan-inc.com'], '猎羽');
  180. }
  181. /**
  182. * 修改客户备注信息
  183. * @param $corpid string 企微id
  184. * @param $userid string 企业成员userid
  185. * @param $external_userid string 外部联系人userid
  186. * @param $remark string string 此用户对外部联系人的备注,最多20个字符
  187. * @param $description string 此用户对外部联系人的描述,最多150个字符
  188. * @param $remark_company string 此用户对外部联系人备注的所属公司名称,最多20个字符
  189. * @param $remark_mobiles array 此用户对外部联系人备注的手机号
  190. * @param $remark_pic_mediaid string 备注图片的mediaid,
  191. * @return array|bool
  192. *
  193. * remark_company只在此外部联系人为微信用户时有效。
  194. * remark,description,remark_company,remark_mobiles和remark_pic_mediaid不可同时为空。
  195. * 如果填写了remark_mobiles,将会覆盖旧的备注手机号。
  196. * 如果要清除所有备注手机号,请在remark_mobiles填写一个空字符串("")。
  197. * remark_pic_mediaid可以通过素材管理接口获得。
  198. */
  199. public static function update_customer_remark($corpid, $userid, $external_userid, $remark = null, $description = null,
  200. $remark_company = null, array $remark_mobiles = null, $remark_pic_mediaid = null)
  201. {
  202. $accessToken = AuthorizeCorp::getAccessToken($corpid, '修改客户备注信息');
  203. if(empty($accessToken)) {
  204. Log::logError('修改客户备注信息', [
  205. 'params' => ['corpid' => $corpid],
  206. 'errmsg' => '令牌获取失败'
  207. ], 'qyWechat');
  208. return false;
  209. }
  210. $postData = [
  211. 'userid' => $userid,
  212. 'external_userid' => $external_userid,
  213. ];
  214. if(!is_null($remark)) {
  215. $postData['remark'] = $remark;
  216. }
  217. if(!is_null($description)) {
  218. $postData['description'] = $description;
  219. }
  220. if(!is_null($remark_company)) {
  221. $postData['remark_company'] = $remark_company;
  222. }
  223. if(!is_null($remark_mobiles)) {
  224. $postData['remark_mobiles'] = $remark_mobiles;
  225. }
  226. if(!is_null($remark_pic_mediaid)) {
  227. $postData['remark_pic_mediaid'] = $remark_pic_mediaid;
  228. }
  229. $requestUri = config('qyWechat.update_customer_remark');
  230. $requestUri .= $accessToken;
  231. $response = HttpService::httpPost($requestUri ,json_encode($postData));
  232. $responseData = json_decode($response, true);
  233. Log::logInfo('修改客户备注信息', [
  234. 'params' => [
  235. 'corpid' => $corpid,
  236. 'userid' => $userid,
  237. 'external_userid' => $external_userid,
  238. 'remark' => $remark,
  239. 'description' => $description,
  240. 'remark_company' => $remark_company,
  241. 'remark_mobiles' => $remark_mobiles,
  242. 'remark_pic_mediaid' => $remark_pic_mediaid,
  243. ],
  244. 'response' => $responseData
  245. ], 'qyWechat');
  246. return $responseData;
  247. }
  248. /**
  249. * 创建企业标签
  250. * @param $corpid string 企微id
  251. * @param $group_id string 标签组id
  252. * @param $group_name string 标签组名称
  253. * @param $order integer 标签组排序值
  254. * @param $tag array 标签列表
  255. * @return mixed
  256. */
  257. public static function add_corp_tag($corpid, $group_id, $group_name, $order, $tag, $retry = 0)
  258. {
  259. $accessToken = AuthorizeCorp::getAccessToken($corpid, '创建企业标签');
  260. if(empty($accessToken)) {
  261. Log::logError('创建企业标签', [
  262. 'params' => ['corpid' => $corpid],
  263. 'errmsg' => '令牌获取失败'
  264. ], 'qyWechat');
  265. return false;
  266. }
  267. $requestUri = config('qyWechat.add_corp_tag');
  268. $requestUri .= $accessToken;
  269. $params['tag'] = $tag; // '[{"name": "TAG_NAME_1","order": 1},{"name": "TAG_NAME_2","order": 2}]';
  270. if (!empty($group_name)) {
  271. $params['group_name'] = $group_name;
  272. }
  273. if (!empty($group_id)) {
  274. $params['group_id'] = $group_id;
  275. }
  276. if (!empty($order)) {
  277. $params['order'] = $order;
  278. }
  279. $responseData = HttpService::httpPost($requestUri, json_encode($params), true);
  280. if($responseData === false && $retry < 5) {
  281. $retry++;
  282. return self::add_corp_tag($corpid, $group_id, $group_name, $order, $tag, $retry);
  283. }
  284. $responseData = json_decode($responseData, 1);
  285. Log::logInfo('创建企业标签', [
  286. 'params' => [
  287. 'corpid' => $corpid,
  288. 'group_id' => $group_id,
  289. 'group_name' => $group_name,
  290. 'order' => $order,
  291. 'tag' => $tag,
  292. ],
  293. 'response' => $responseData
  294. ], 'qyWechat');
  295. return $responseData;
  296. }
  297. /**
  298. * 编辑客户企业标签
  299. * @param $corpid string 企微id
  300. * @param $userid string 企微成员userid
  301. * @param $external_userid string 外部联系人userid
  302. * @param $add_tag array 要标记的企业标签列表
  303. * @param $remove_tag array 要移除的企业标签列表
  304. * @return bool|mixed
  305. */
  306. public static function mark_tag($corpid, $userid, $external_userid, $add_tag, $remove_tag, $retry=0)
  307. {
  308. $accessToken = AuthorizeCorp::getAccessToken($corpid, '编辑客户企业标签');
  309. if(empty($accessToken)) {
  310. Log::logError('编辑客户企业标签', [
  311. 'params' => ['corpid' => $corpid],
  312. 'errmsg' => '令牌获取失败',
  313. ], 'qyWechat');
  314. return false;
  315. }
  316. $requestUri = config('qyWechat.mark_tag');
  317. $requestUri .= $accessToken;
  318. $postData['userid'] = $userid;
  319. $postData['external_userid'] = $external_userid;
  320. if($add_tag) {
  321. $postData['add_tag'] = $add_tag;
  322. }
  323. if($remove_tag) {
  324. $postData['remove_tag'] = $remove_tag;
  325. }
  326. $responseData = HttpService::httpPost($requestUri, json_encode($postData), true);
  327. if($responseData === false && $retry < 5) {
  328. $retry++;
  329. return self::mark_tag($corpid, $userid, $external_userid, $add_tag, $remove_tag, $retry);
  330. }
  331. $responseData = json_decode($responseData, 1);
  332. Log::logInfo('编辑客户企业标签', [
  333. 'params' => [
  334. 'corpid' => $corpid,
  335. 'userid' => $userid,
  336. 'external_userid' => $external_userid,
  337. 'add_tag' => $add_tag,
  338. 'remove_tag' => $remove_tag,
  339. ],
  340. 'response' => $responseData,
  341. ], 'qyWechat');
  342. return $responseData;
  343. }
  344. /**
  345. * 获取企微所有已授权部门列表
  346. * @param $corpid
  347. * @return bool|mixed
  348. */
  349. public static function simplelist($corpid)
  350. {
  351. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企微所有已授权部门列表');
  352. if(empty($accessToken)) {
  353. Log::logError('获取企微所有已授权部门列表', [
  354. 'params' => ['corpid' => $corpid],
  355. 'errmsg' => '令牌获取失败'
  356. ], 'qyWechat');
  357. return false;
  358. }
  359. $requestUri = config('qyWechat.simplelist');
  360. $requestUri .= $accessToken.'&id=';
  361. $response = HttpService::httpGet($requestUri );
  362. $responseData = json_decode($response, true);
  363. Log::logInfo('获取企微所有已授权部门列表', [
  364. 'params' => ['corpid' => $corpid],
  365. 'response' => $responseData,
  366. ], 'qyWechat');
  367. return $responseData;
  368. }
  369. /**
  370. * 获取企业配置了联系我功能的客服列表
  371. * @param $corpid
  372. * @return bool|mixed
  373. */
  374. public static function get_follow_user_list($corpid)
  375. {
  376. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企业配置了联系我功能的客服列表');
  377. if(empty($accessToken)) {
  378. Log::logError('获取企业配置了联系我功能的客服列表', [
  379. 'params' => ['corpid' => $corpid],
  380. 'errmsg' => '令牌获取失败'
  381. ], 'qyWechat');
  382. return false;
  383. }
  384. $requestUri = config('qyWechat.get_follow_user_list');
  385. $requestUri .= $accessToken.'&id=';
  386. $response = HttpService::httpGet($requestUri );
  387. $responseData = json_decode($response, true);
  388. Log::logInfo('获取企业配置了联系我功能的客服列表', [
  389. 'params' => ['corpid' => $corpid],
  390. 'response' => $responseData,
  391. ], 'qyWechat');
  392. return $responseData;
  393. }
  394. /**
  395. * 获取部门详细信息
  396. * @param $corpid
  397. * @param $department_id
  398. * @return bool|mixed
  399. */
  400. public static function get_department_info($corpid, $department_id)
  401. {
  402. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取部门详细信息');
  403. if(empty($accessToken)) {
  404. Log::logError('获取部门详细信息', [
  405. 'params' => [
  406. 'corpid' => $corpid,
  407. 'depatment_id' => $department_id,
  408. ],
  409. 'errmsg' => '令牌获取失败'
  410. ], 'qyWechat');
  411. return false;
  412. }
  413. $requestUri = config('qyWechat.get_department_info');
  414. $requestUri .= $accessToken.'&id='.$department_id;
  415. $response = HttpService::httpGet($requestUri );
  416. $responseData = json_decode($response, true);
  417. Log::logInfo('获取部门详细信息', [
  418. 'params' => [
  419. 'corpid' => $corpid,
  420. 'depatment_id' => $department_id,
  421. ],
  422. 'response' => $responseData,
  423. ], 'qyWechat');
  424. return $responseData;
  425. }
  426. /**
  427. * 根据Oauth2授权code获取用户信息
  428. * */
  429. public static function getCustomerInfoByCode($corpid, $code)
  430. {
  431. # 获取SuiteAccessToken
  432. $accessToken = AuthorizeCorp::getAccessToken($corpid, '根据Oauth2授权code获取用户信息');
  433. if(empty($accessToken)) {
  434. Log::logError('根据Oauth2授权code获取用户信息时令牌获取失败', [
  435. 'corpid' => $corpid,
  436. 'code' => $code
  437. ], 'GetCustomerInfoByCode');
  438. return false;
  439. }
  440. # 获取用户信息API
  441. $getExternalContactUri = config('qyWechat.get_user_info');
  442. $getExternalContactUri .= $accessToken . '&code=' . $code;
  443. $response = HttpService::httpGet($getExternalContactUri);
  444. $responseData = json_decode($response, true);
  445. return $responseData;
  446. }
  447. /**
  448. * 分配在职成员客户
  449. * @param $corpid string 企微id
  450. * @param $handoverUserId string 被接替员工id
  451. * @param $takeoverUserId string 接替员工id
  452. * @param $externalUserIdList array 需要迁移的外部联系人id
  453. * @param $transferSuccessMsg string 迁移成功后为客户发送的提醒消息
  454. * @return mixed
  455. */
  456. public static function transferCustomerOnJob($corpid, $handoverUserId, $takeoverUserId, $externalUserIdList,
  457. $transferSuccessMsg)
  458. {
  459. $accessToken = AuthorizeCorp::getAccessToken($corpid, '分配在职成员客户');
  460. if(empty($accessToken)) {
  461. Log::logError('分配在职成员客户', [
  462. 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId,
  463. 'external_userid_list' => $externalUserIdList, 'transfer_success_msg' => $transferSuccessMsg],
  464. 'errmsg' => '令牌获取失败'
  465. ], 'qyWechat');
  466. return false;
  467. }
  468. $url = config('qyWechat.transfer_customer_on_job').$accessToken;
  469. $params['handover_userid'] = $handoverUserId;
  470. $params['takeover_userid'] = $takeoverUserId;
  471. if(!empty($transferSuccessMsg)) {
  472. $params['transfer_success_msg'] = $transferSuccessMsg;
  473. }
  474. $params['external_userid'] = $externalUserIdList;
  475. $result = HttpService::httpPost($url, json_encode($params), true);
  476. $result = json_decode($result, 1);
  477. Log::logInfo('分配在职成员客户', [
  478. 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId,
  479. 'external_userid_list' => $externalUserIdList, 'transfer_success_msg' => $transferSuccessMsg],
  480. 'responseData' => $result,
  481. ], 'qyWechat');
  482. return $result;
  483. }
  484. /**
  485. * 查询待分配的客户列表
  486. * @param $corpid
  487. * @param $cursor
  488. * @return mixed
  489. */
  490. public static function getUnassignedList($corpid, $cursor, $retry = 0)
  491. {
  492. $accessToken = AuthorizeCorp::getAccessToken($corpid, '查询待分配的离职成员列表');
  493. if(empty($accessToken)) {
  494. Log::logError('查询待分配的离职成员列表', [
  495. 'params' => ['corpid' => $corpid],
  496. 'errmsg' => '令牌获取失败'
  497. ], 'qyWechat');
  498. return false;
  499. }
  500. $url = config('qyWechat.get_unassigned_list').$accessToken;
  501. $params['cursor'] = $cursor;
  502. $data = HttpService::httpPost($url, json_encode($params), true);
  503. if($data === false && $retry < 3) {
  504. $retry++;
  505. return self::getUnassignedList($corpid, $cursor, $retry);
  506. }
  507. $data = json_decode($data, 1);
  508. Log::logInfo('查询待分配的离职成员列表', [
  509. 'params' => ['corpid' => $corpid, 'cursor' => $cursor],
  510. 'responseData' => $data,
  511. ], 'qyWechat');
  512. return $data;
  513. }
  514. /**
  515. * 分配离职成员的客户
  516. * @param $corpid string 企业id
  517. * @param $handoverUserId string 原跟进成员的userid
  518. * @param $takeoverUserId string 接替成员的userid
  519. * @param $externalUserIdList array 客户的external_userid列表,每次最多分配100个客户
  520. * @return mixed
  521. * 注意:
  522. * 1.external_userid必须是handover_userid的客户(即配置了客户联系功能的成员所添加的联系人)。
  523. * 2.handover_userid必须是已离职用户。
  524. */
  525. public static function transferCustomerQuit($corpid, $handoverUserId, $takeoverUserId, $externalUserIdList)
  526. {
  527. $accessToken = AuthorizeCorp::getAccessToken($corpid, '分配离职成员的客户');
  528. if(empty($accessToken)) {
  529. Log::logError('分配离职成员的客户', [
  530. 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId,
  531. 'external_userid_list' => $externalUserIdList],
  532. 'errmsg' => '令牌获取失败'
  533. ], 'qyWechat');
  534. return false;
  535. }
  536. $url = config('qyWechat.transfer_customer_quit').$accessToken;
  537. $params['handover_userid'] = $handoverUserId;
  538. $params['takeover_userid'] = $takeoverUserId;
  539. $params['external_userid'] = $externalUserIdList;
  540. $result = HttpService::httpPost($url, json_encode($params), true);
  541. $result = json_decode($result, 1);
  542. Log::logInfo('分配离职成员的客户', [
  543. 'params' => ['corpid' => $corpid, 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId,
  544. 'external_userid_list' => $externalUserIdList],
  545. 'responseData' => $result,
  546. ], 'qyWechat');
  547. return $result;
  548. }
  549. /**
  550. * 客户迁移结果查询
  551. * @param $type int 接替类型 1在职 2离职
  552. * @param $corpid
  553. * @param $handOverUserId string 原跟进员工user_id
  554. * @param $takeOverUserId string 接替成员的user_id
  555. * @param string $cursor string 分页查询的游标
  556. * @return mixed
  557. */
  558. public static function transferResult($type, $corpid, $handOverUserId, $takeOverUserId, $cursor)
  559. {
  560. $accessToken = AuthorizeCorp::getAccessToken($corpid, '客户迁移结果查询');
  561. if(empty($accessToken)) {
  562. Log::logError('客户迁移结果查询', [
  563. 'params' => [
  564. 'type' => $type,
  565. 'corpid' => $corpid,
  566. 'handover_userid' => $handOverUserId,
  567. 'takeover_userid' => $takeOverUserId,
  568. 'cursor' => $cursor,
  569. ],
  570. 'errmsg' => '令牌获取失败'
  571. ], 'qyWechat');
  572. return false;
  573. }
  574. if(1 == $type) {
  575. $url = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/transfer_result?access_token='.$accessToken;
  576. } else {
  577. $url = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/resigned/transfer_result?access_token='.$accessToken;
  578. }
  579. $params['handover_userid'] = $handOverUserId;
  580. $params['takeover_userid'] = $takeOverUserId;
  581. $params['cursor'] = $cursor;
  582. $result = HttpService::httpPost($url, json_encode($params), true);
  583. $result = json_decode($result, 1);
  584. Log::logInfo('客户迁移结果查询', [
  585. 'params' => [
  586. 'type' => $type,
  587. 'corpid' => $corpid,
  588. 'handover_userid' => $handOverUserId,
  589. 'takeover_userid' => $takeOverUserId,
  590. 'cursor' => $cursor,
  591. ],
  592. 'response' => $result,
  593. ], 'qyWechat');
  594. return $result;
  595. }
  596. /**
  597. * 客户群基本信息获取
  598. * */
  599. public static function getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter=0, $retry=0)
  600. {
  601. $accessToken = AuthorizeCorp::getAccessToken($corpid, '客户群基本信息获取');
  602. if(empty($accessToken)) {
  603. Log::logError('客户群基本信息获取', [
  604. 'params' => ['corpid' => $corpid],
  605. 'errmsg' => '令牌获取失败'
  606. ], 'qyWechat');
  607. return false;
  608. }
  609. $url = config('qyWechat.group_chat_list') . $accessToken;
  610. $postData = [
  611. 'status_filter' => $statusFilter,
  612. 'owner_filter' => [
  613. 'userid_list' => $ownerFilter
  614. ],
  615. 'cursor' => $cursor,
  616. 'limit' => $limit
  617. ];
  618. $response = HttpService::httpPost($url, json_encode($postData));
  619. if($response === false && $retry <=5) { // 发起重试
  620. $retry++;
  621. return self::getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter, $retry);
  622. }
  623. $responseData = json_decode($response, true);
  624. Log::logInfo('getGroupChatList', ['params' => [
  625. 'corpid' => $corpid, 'user_list' => $ownerFilter, 'cursor' => $cursor
  626. ], 'response' => $responseData], 'qyWechat');
  627. return $responseData;
  628. }
  629. /**
  630. * 获取客户群详情
  631. * */
  632. public static function getGroupChatDetail($corpid, $chatId, $needName=1, $retry=0)
  633. {
  634. $accessToken = AuthorizeCorp::getAccessToken($corpid, '客户群基本信息获取');
  635. if(empty($accessToken)) {
  636. Log::logError('客户群详情获取', [
  637. 'params' => ['corpid' => $corpid],
  638. 'errmsg' => '令牌获取失败'
  639. ], 'qyWechat');
  640. return false;
  641. }
  642. $url = config('qyWechat.group_chat_detail') . $accessToken;
  643. $postData = [
  644. 'chat_id' => $chatId,
  645. 'need_name' => $needName
  646. ];
  647. $response = HttpService::httpPost($url, json_encode($postData));
  648. if($response === false && $retry <=5) { // 发起重试
  649. $retry++;
  650. return self::getGroupChatDetail($corpid, $chatId, $needName, $retry);
  651. }
  652. $responseData = json_decode($response, true);
  653. Log::logInfo('getGroupChatDetail', ['params' => ['corpid' => $corpid, 'chat_id' => $chatId
  654. ], 'response' => $responseData], 'qyWechat');
  655. return $responseData;
  656. }
  657. /**
  658. * 企业成员user_id转换为open_userid
  659. * */
  660. public static function useridToOpenUserid($corpid, $userIdList)
  661. {
  662. $accessToken = AuthorizeCorp::getAccessToken($corpid, '企业成员user_id转换为open_userid');
  663. if(empty($accessToken)) {
  664. Log::logError('企业成员user_id转换为open_userid', [
  665. 'params' => ['corpid' => $corpid],
  666. 'errmsg' => '令牌获取失败'
  667. ], 'qyWechat');
  668. return false;
  669. }
  670. $url = config('qyWechat.userid_to_open_userid') . $accessToken;
  671. $postData = [
  672. 'userid_list' => $userIdList,
  673. ];
  674. $response = HttpService::httpPost($url, json_encode($postData));
  675. $responseData = json_decode($response, true);
  676. Log::logInfo('useridToOpenUserid', ['data' => $responseData, 'param' => $userIdList, 'corpid' => $corpid], 'qyWechat');
  677. return $responseData;
  678. }
  679. /**
  680. * 获取企微客服账号许可信息
  681. * */
  682. public static function getActiveUserList($corpid, $cursor, $limit, $suiteId, $suiteSecret)
  683. {
  684. # 获取SuiteAccessToken
  685. $accessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  686. // $accessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  687. if(empty($accessToken)) {
  688. Log::logError('获取企微客服账号许可信息', [
  689. 'params' => ['corpid' => $corpid],
  690. 'errmsg' => '服务商的access_token获取失败'
  691. ], 'qyWechat');
  692. return false;
  693. }
  694. $url = config('qyWechat.list_active_account') . $accessToken;
  695. $postData = [
  696. 'corpid' => $corpid,
  697. 'limit' => $limit,
  698. 'cursor' => $cursor
  699. ];
  700. $response = HttpService::httpPost($url, json_encode($postData));
  701. $responseData = json_decode($response, true);
  702. Log::logInfo('getActiveUserList', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  703. return $responseData;
  704. }
  705. /**
  706. * 获取指定客服账号许可信息
  707. * */
  708. public static function getUserActiveInfo($corpid, $userId, $suiteId, $suiteSecret)
  709. {
  710. # 获取SuiteAccessToken
  711. $accessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  712. // $accessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  713. if(empty($accessToken)) {
  714. Log::logError('获取企微指定客服账号许可信息', [
  715. 'params' => ['corpid' => $corpid],
  716. 'errmsg' => '服务商的access_token获取失败'
  717. ], 'qyWechat');
  718. return false;
  719. }
  720. $url = config('qyWechat.active_info_by_user') . $accessToken;
  721. $postData = [
  722. 'corpid' => $corpid,
  723. 'userid' => $userId,
  724. ];
  725. $response = HttpService::httpPost($url, json_encode($postData));
  726. $responseData = json_decode($response, true);
  727. Log::logInfo('getUserActiveInfo', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  728. return $responseData;
  729. }
  730. /**
  731. * 配置客户群进群方式
  732. * @param $corpid string 企微id
  733. * @param $scene integer 场景 1:群的小程序插件 2:群的二维码插件
  734. * @param $remark string 联系方式的备注信息,用于助记,超过30个字符将被截断
  735. * @param $autoCreateRoom int 当群满了后,是否自动新建群。0-否;1-是。 默认为1
  736. * @param $roomBaseName string 自动建群的群名前缀
  737. * @param $roomBaseId int 自动建群的起始序号
  738. * @param $chatIdList array 使用该配置的客户群ID列表,支持5个
  739. * @param $state string 企业自定义的state参数,用于区分不同的入群渠道
  740. * */
  741. public static function addGroupJoinWay(
  742. $corpid, $scene, $chatIdList, $state, $autoCreateRoom=1, $roomBaseName='', $roomBaseId=1, $remark=null
  743. )
  744. {
  745. $accessToken = AuthorizeCorp::getAccessToken($corpid, '配置客户群进群方式');
  746. if(empty($accessToken)) {
  747. Log::logError('配置客户群进群方式', [
  748. 'params' => [
  749. 'corpid' => $corpid, 'scene' => $scene, 'chat_id_list' => $chatIdList, 'state' => $state, 'auto_create_room' => $autoCreateRoom, 'room_base_name' => $roomBaseName, 'room_base_id' => $roomBaseId, 'remark' => $remark
  750. ],
  751. 'errmsg' => '令牌获取失败'
  752. ], 'qyWechat');
  753. return false;
  754. }
  755. $url = config('qyWechat.add_group_join_way') . $accessToken;
  756. $postData = [
  757. 'scene' => $scene,
  758. 'chat_id_list' => $chatIdList
  759. ];
  760. # 联系方式的备注信息
  761. if($remark)
  762. $postData['remark'] = $remark;
  763. # 当群满了后,是否自动新建群
  764. if(in_array($autoCreateRoom, [0, 1]))
  765. $postData['auto_create_room'] = $autoCreateRoom;
  766. # 自动建群规则
  767. if($roomBaseName)
  768. $postData['room_base_name'] = $roomBaseName;
  769. if(is_numeric($roomBaseId))
  770. $postData['room_base_id'] = $roomBaseId;
  771. # state
  772. if($state)
  773. $postData['state'] = $state;
  774. $response = HttpService::httpPost($url, json_encode($postData));
  775. $responseData = json_decode($response, true);
  776. Log::logInfo('addGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  777. return $responseData;
  778. }
  779. /**
  780. * 获取客户群进群方式配置
  781. * @param $corpid string 企微ID
  782. * @param $configId string 联系方式的配置id
  783. * */
  784. public static function getGroupJoinWay($corpid, $configId)
  785. {
  786. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取客户群进群方式配置');
  787. if(empty($accessToken)) {
  788. Log::logError('获取客户群进群方式配置', [
  789. 'params' => ['corpid' => $corpid, 'config_id' => $configId],
  790. 'errmsg' => '令牌获取失败'
  791. ], 'qyWechat');
  792. return false;
  793. }
  794. $url = config('qyWechat.get_group_join_way') . $accessToken;
  795. $postData = [
  796. 'config_id' => $configId,
  797. ];
  798. $response = HttpService::httpPost($url, json_encode($postData));
  799. $responseData = json_decode($response, true);
  800. Log::logInfo('getGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  801. return $responseData;
  802. }
  803. /**
  804. * 更新客户群进群方式配置
  805. * @param $corpid string 企微id
  806. * @param $configId string 联系方式的配置id
  807. * @param $scene integer 场景 1:群的小程序插件 2:群的二维码插件
  808. * @param $remark string 联系方式的备注信息,用于助记,超过30个字符将被截断
  809. * @param $autoCreateRoom int 当群满了后,是否自动新建群。0-否;1-是。 默认为1
  810. * @param $roomBaseName string 自动建群的群名前缀
  811. * @param $roomBaseId int 自动建群的起始序号
  812. * @param $chatIdList array 使用该配置的客户群ID列表,支持5个
  813. * @param $state string 企业自定义的state参数,用于区分不同的入群渠道
  814. * */
  815. public static function updateGroupJoinWay(
  816. $corpid, $configId, $scene, $chatIdList, $state, $autoCreateRoom=1, $roomBaseName='', $roomBaseId=1, $remark=null
  817. )
  818. {
  819. $accessToken = AuthorizeCorp::getAccessToken($corpid, '配置客户群进群方式');
  820. if(empty($accessToken)) {
  821. Log::logError('配置客户群进群方式', [
  822. 'params' => [
  823. 'corpid' => $corpid, 'scene' => $scene, 'chat_id_list' => $chatIdList, 'state' => $state, 'auto_create_room' => $autoCreateRoom, 'room_base_name' => $roomBaseName, 'room_base_id' => $roomBaseId, 'remark' => $remark
  824. ],
  825. 'errmsg' => '令牌获取失败'
  826. ], 'qyWechat');
  827. return false;
  828. }
  829. $url = config('qyWechat.update_group_join_way') . $accessToken;
  830. $postData = [
  831. 'config_id' => $configId,
  832. 'scene' => $scene,
  833. 'chat_id_list' => $chatIdList
  834. ];
  835. # 联系方式的备注信息
  836. if($remark)
  837. $postData['remark'] = $remark;
  838. # 当群满了后,是否自动新建群
  839. if(in_array($autoCreateRoom, [0, 1]))
  840. $postData['auto_create_room'] = $autoCreateRoom;
  841. # 自动建群规则
  842. if($roomBaseName)
  843. $postData['room_base_name'] = $roomBaseName;
  844. if(is_numeric($roomBaseId))
  845. $postData['room_base_id'] = $roomBaseId;
  846. # state
  847. if($state)
  848. $postData['state'] = $state;
  849. $response = HttpService::httpPost($url, json_encode($postData));
  850. $responseData = json_decode($response, true);
  851. Log::logInfo('updateGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  852. return $responseData;
  853. }
  854. /**
  855. * 删除客户群进群方式配置
  856. * @param $corpid string 企微ID
  857. * @param $configId string 联系方式的配置id
  858. * */
  859. public static function delGroupJoinWay($corpid, $configId)
  860. {
  861. $accessToken = AuthorizeCorp::getAccessToken($corpid, '删除客户群进群方式配置');
  862. if(empty($accessToken)) {
  863. Log::logError('删除客户群进群方式配置', [
  864. 'params' => ['corpid' => $corpid, 'config_id' => $configId],
  865. 'errmsg' => '令牌获取失败'
  866. ], 'qyWechat');
  867. return false;
  868. }
  869. $url = config('qyWechat.del_group_join_way') . $accessToken;
  870. $postData = [
  871. 'config_id' => $configId,
  872. ];
  873. $response = HttpService::httpPost($url, json_encode($postData));
  874. $responseData = json_decode($response, true);
  875. Log::logInfo('delGroupJoinWay', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  876. return $responseData;
  877. }
  878. /**
  879. * 获取订单中的帐号列表
  880. * @param $corpid string 企微ID
  881. * @param $configId string 联系方式的配置id
  882. * */
  883. public static function licenseListOfOrder($orderId, $limit=1000, $cursor=null)
  884. {
  885. $component = config('qyWechat.suite_info');
  886. $suiteId = $component['suite_id'];
  887. $suiteSecret = $component['suite_secret'];
  888. # 获取SuiteAccessToken
  889. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  890. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  891. if(!$suiteAccessToken) {
  892. Log::logError('获取订单中的帐号列表时获取suiteAccessToken异常', [
  893. 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret],
  894. 'errmsg' => '令牌获取失败'
  895. ], 'LicenseListOfOrder');
  896. return false;
  897. }
  898. $url = config('qyWechat.list_order_account') . $suiteAccessToken;
  899. $postData = [
  900. 'order_id' => $orderId,
  901. 'limit' => $limit,
  902. 'cursor' => $cursor
  903. ];
  904. $response = HttpService::httpPost($url, json_encode($postData));
  905. $responseData = json_decode($response, true);
  906. Log::logInfo('订单中的许可信息响应结果为:', ['data' => $responseData, 'param' => $postData], 'LicenseListOfOrder');
  907. return $responseData;
  908. }
  909. /**
  910. * 分配许可激活账号
  911. * */
  912. public static function activeAccountByLicense($activeCode, $corpid, $userid)
  913. {
  914. $component = config('qyWechat.suite_info');
  915. $suiteId = $component['suite_id'];
  916. $suiteSecret = $component['suite_secret'];
  917. # 获取SuiteAccessToken
  918. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  919. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  920. if(!$suiteAccessToken) {
  921. Log::logError('分配许可激活账号时获取suiteAccessToken异常', [
  922. 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret],
  923. 'errmsg' => '令牌获取失败'
  924. ], 'ActiveAccountByLicense');
  925. return false;
  926. }
  927. $url = config('qyWechat.active_by_license') . $suiteAccessToken;
  928. $postData = [
  929. 'active_code' => $activeCode,
  930. 'corpid' => $corpid,
  931. 'userid' => $userid
  932. ];
  933. $response = HttpService::httpPost($url, json_encode($postData));
  934. $responseData = json_decode($response, true);
  935. Log::logInfo('分配许可激活账号响应结果为:', ['data' => $responseData, 'param' => $postData], 'ActiveAccountByLicense');
  936. return $responseData;
  937. }
  938. /**
  939. * 批量分配许可激活账号
  940. * */
  941. public static function batchActiveAccountByLicense($corpid, $activeList)
  942. {
  943. $component = config('qyWechat.suite_info');
  944. $suiteId = $component['suite_id'];
  945. $suiteSecret = $component['suite_secret'];
  946. # 获取SuiteAccessToken
  947. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  948. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  949. if(!$suiteAccessToken) {
  950. Log::logError('批量分配许可激活账号时获取suiteAccessToken异常', [
  951. 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret],
  952. 'errmsg' => '令牌获取失败'
  953. ], 'BatchActiveAccountByLicense');
  954. return false;
  955. }
  956. $url = config('qyWechat.batch_active_by_license') . $suiteAccessToken;
  957. $postData = [
  958. 'corpid' => $corpid,
  959. 'active_list' => $activeList
  960. ];
  961. $response = HttpService::httpPost($url, json_encode($postData));
  962. $responseData = json_decode($response, true);
  963. Log::logInfo('批量分配许可激活账号响应结果为:', ['data' => $responseData, 'param' => $postData], 'BatchActiveAccountByLicense');
  964. return $responseData;
  965. }
  966. /**
  967. * 批量转移账号许可
  968. * */
  969. public static function batchTransferLicense($corpid, $transferList)
  970. {
  971. $component = config('qyWechat.suite_info');
  972. $suiteId = $component['suite_id'];
  973. $suiteSecret = $component['suite_secret'];
  974. # 获取SuiteAccessToken
  975. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  976. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  977. if(!$suiteAccessToken) {
  978. Log::logError('批量转移许可激活账号时获取suiteAccessToken异常', [
  979. 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret],
  980. 'errmsg' => '令牌获取失败'
  981. ], 'BatchTransferLicense');
  982. return false;
  983. }
  984. $url = config('qyWechat.batch_transfer_license') . $suiteAccessToken;
  985. $postData = [
  986. 'corpid' => $corpid,
  987. 'transfer_list' => $transferList
  988. ];
  989. $response = HttpService::httpPost($url, json_encode($postData));
  990. $responseData = json_decode($response, true);
  991. Log::logInfo('批量分配许可激活账号响应结果为:', ['data' => $responseData, 'param' => $postData], 'BatchTransferLicense');
  992. return $responseData;
  993. }
  994. # 生成客服许可续期任务
  995. public static function createRenewOrderJob($corpid, $accountList, $retry = 0) {
  996. $component = config('qyWechat.suite_info');
  997. $suiteId = $component['suite_id'];
  998. $suiteSecret = $component['suite_secret'];
  999. # 获取SuiteAccessToken
  1000. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  1001. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  1002. if(!$suiteAccessToken) {
  1003. Log::logError('创建客服许可续期任务时获取suiteAccessToken异常', [
  1004. 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret],
  1005. 'errmsg' => '令牌获取失败'
  1006. ], 'CreateRenewOrderJob');
  1007. return false;
  1008. }
  1009. $url = config('qyWechat.create_renew_order_job') . $suiteAccessToken;
  1010. $postData = [
  1011. 'corpid' => $corpid,
  1012. 'account_list' => $accountList
  1013. ];
  1014. $response = HttpService::httpPost($url, json_encode($postData));
  1015. if(empty($response) && $retry<5) {
  1016. $retry++;
  1017. sleep(1);
  1018. return self::createRenewOrderJob($corpid, $accountList, $retry);
  1019. }
  1020. $responseData = json_decode($response, true);
  1021. Log::logInfo('创建客服许可续期任务响应结果为:', ['data' => $responseData, 'param' => $postData, 'url' => $url], 'CreateRenewOrderJob');
  1022. return $responseData;
  1023. }
  1024. # 提交客服许可续期订单
  1025. public static function submitOrderJob($jobId, $buyerUserId, $accountDuration, $retry = 0) {
  1026. $component = config('qyWechat.suite_info');
  1027. $suiteId = $component['suite_id'];
  1028. $suiteSecret = $component['suite_secret'];
  1029. # 获取SuiteAccessToken
  1030. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  1031. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  1032. if(!$suiteAccessToken) {
  1033. Log::logError('提交客服许可续期任务订单时获取suiteAccessToken异常', [
  1034. 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret],
  1035. 'errmsg' => '令牌获取失败'
  1036. ], 'submitOrderJob');
  1037. return false;
  1038. }
  1039. $url = config('qyWechat.submit_order_job') . $suiteAccessToken;
  1040. $postData = [
  1041. 'jobid' => $jobId,
  1042. 'buyer_userid' => $buyerUserId,
  1043. 'account_duration' => $accountDuration
  1044. ];
  1045. $response = HttpService::httpPost($url, json_encode($postData));
  1046. if(empty($response) && $retry<5) {
  1047. $retry++;
  1048. sleep(1);
  1049. return self::submitOrderJob($jobId, $buyerUserId, $accountDuration, $retry);
  1050. }
  1051. $responseData = json_decode($response, true);
  1052. Log::logInfo('可续期任务订单响应结果为:', ['data' => $responseData, 'param' => $postData, 'url' => $url], 'submitOrderJob');
  1053. return $responseData;
  1054. }
  1055. # 取消订单
  1056. public static function cancelOrder($corpid, $orderId, $retry = 0) {
  1057. $component = config('qyWechat.suite_info');
  1058. $suiteId = $component['suite_id'];
  1059. $suiteSecret = $component['suite_secret'];
  1060. # 获取SuiteAccessToken
  1061. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  1062. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  1063. if(!$suiteAccessToken) {
  1064. Log::logError('取消订单时获取suiteAccessToken异常', [
  1065. 'params' => ['suite_id' => $suiteId, 'suite_secret' => $suiteSecret],
  1066. 'errmsg' => '令牌获取失败'
  1067. ], 'cancelOrder');
  1068. return false;
  1069. }
  1070. $url = config('qyWechat.cancel_order') . $suiteAccessToken;
  1071. $postData = [
  1072. 'corpid' => $corpid,
  1073. 'order_id' => $orderId
  1074. ];
  1075. $response = HttpService::httpPost($url, json_encode($postData));
  1076. if(empty($response) && $retry<5) {
  1077. $retry++;
  1078. sleep(1);
  1079. return self::cancelOrder($corpid, $orderId, $retry);
  1080. }
  1081. $responseData = json_decode($response, true);
  1082. Log::logInfo('取消订单响应结果为:', ['data' => $responseData, 'param' => $postData, 'url' => $url], 'cancelOrder');
  1083. return $responseData;
  1084. }
  1085. /**
  1086. * 停止企业群发
  1087. * */
  1088. public static function cancelMsg($corpid, $msgId)
  1089. {
  1090. $accessToken = AuthorizeCorp::getAccessToken($corpid);
  1091. if(empty($accessToken)) {
  1092. Log::logError('停止企业群发令牌获取失败', [
  1093. 'params' => ['corpid' => $corpid, 'msg_id' => $msgId],
  1094. ], 'qyWechat');
  1095. return false;
  1096. }
  1097. $url = config('qyWechat.cancel_mass_msg') . $accessToken;
  1098. $postData = [
  1099. 'msgid' => $msgId,
  1100. ];
  1101. $response = HttpService::httpPost($url, json_encode($postData));
  1102. $responseData = json_decode($response, true);
  1103. Log::logInfo('cancelMsg', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1104. return $responseData;
  1105. }
  1106. // 根据内部员工获取外部联系客户具体方法
  1107. public static function getExternalContactList($userId, $corpid, $retry = 0)
  1108. {
  1109. $accessToken = AuthorizeCorp::getAccessToken($corpid, '根据员工获取外部客户');
  1110. if(!$accessToken) {
  1111. Log::logError('获取客户列表时accessToken异常', [
  1112. 'params' => ['corpid' => $corpid],
  1113. 'errmsg' => '令牌获取失败'
  1114. ], 'getCustomerList');
  1115. return '令牌获取失败';
  1116. }
  1117. $url = config('qyWechat.external_contact_list').$accessToken.'&userid='.$userId;
  1118. $response = HttpService::HttpGet($url);
  1119. if(empty($response) && $retry<5) {
  1120. $retry++;
  1121. sleep(1);
  1122. return self::getExternalContactList($userId, $corpid, $retry);
  1123. }
  1124. $responseData = json_decode($response, true);
  1125. if(in_array($responseData['errcode'],[-1])) {
  1126. $retry++;
  1127. sleep(1);
  1128. return self::getExternalContactList($userId, $corpid, $retry);
  1129. }
  1130. Log::logInfo('获取客户列表响应结果为:', ['data' => $responseData, 'param' => ['corpid' => $corpid, 'user_id' => $userId]
  1131. , 'url' => $url], 'getExternalContactList');
  1132. return $responseData;
  1133. }
  1134. /**
  1135. * 客户群在职迁移
  1136. * */
  1137. public static function onJobTransfer($corpid, $chatIdList, $newOwner)
  1138. {
  1139. $accessToken = AuthorizeCorp::getAccessToken($corpid);
  1140. if(empty($accessToken)) {
  1141. Log::logError('【onJobTransfer】客户群在职迁移令牌获取失败', [
  1142. 'params' => ['corpid' => $corpid, 'chat_id_list' => $chatIdList, 'new_owner' => $newOwner],
  1143. ], 'qyWechat');
  1144. return false;
  1145. }
  1146. $url = config('qyWechat.chat_group_on_job_transfer') . $accessToken;
  1147. $postData = [
  1148. 'chat_id_list' => $chatIdList,
  1149. 'new_owner' => $newOwner
  1150. ];
  1151. $response = HttpService::httpPost($url, json_encode($postData));
  1152. $responseData = json_decode($response, true);
  1153. Log::logInfo('onJobTransfer', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1154. return $responseData;
  1155. }
  1156. /**
  1157. * 离职迁移
  1158. * */
  1159. public static function transfer($corpid, $chatIdList, $newOwner)
  1160. {
  1161. $accessToken = AuthorizeCorp::getAccessToken($corpid);
  1162. if(empty($accessToken)) {
  1163. Log::logError('【OffJobTransfer】客户群离职迁移令牌获取失败', [
  1164. 'params' => ['corpid' => $corpid, 'chat_id_list' => $chatIdList, 'new_owner' => $newOwner],
  1165. ], 'qyWechat');
  1166. return false;
  1167. }
  1168. $url = config('qyWechat.chat_group_transfer') . $accessToken;
  1169. $postData = [
  1170. 'chat_id_list' => $chatIdList,
  1171. 'new_owner' => $newOwner
  1172. ];
  1173. $response = HttpService::httpPost($url, json_encode($postData));
  1174. $responseData = json_decode($response, true);
  1175. Log::logInfo('OffJobTransfer', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1176. return $responseData;
  1177. }
  1178. /**
  1179. * 停止发表企业朋友圈
  1180. * */
  1181. public static function cancelMomentTask($corpid, $momentId)
  1182. {
  1183. $accessToken = AuthorizeCorp::getAccessToken($corpid);
  1184. if(empty($accessToken)) {
  1185. Log::logError('停止发表企业朋友圈令牌获取失败', [
  1186. 'params' => ['corpid' => $corpid, 'moment_id' => $momentId],
  1187. ], 'qyWechat');
  1188. return false;
  1189. }
  1190. $url = config('qyWechat.cancel_moment_task') . $accessToken;
  1191. $postData = [
  1192. 'moment_id' => $momentId,
  1193. ];
  1194. $response = HttpService::httpPost($url, json_encode($postData));
  1195. $responseData = json_decode($response, true);
  1196. Log::logInfo('cancelMomentTask', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1197. return $responseData;
  1198. }
  1199. /**
  1200. * 知识库分组管理-添加分组
  1201. * */
  1202. public static function addGroup($corpid, $name)
  1203. {
  1204. $accessToken = 'O2npAJry4n5WKw_a8-toxrRACiRDl7tEDrfY12PDNHXLALb6UfK8t_bmtQwPnmtIelZRJQLLrF0tQt2vD_6D0pxt9ZeJTr9hMMQkkd5goeTK2YqgLul3F7lqgIYNJJK7tYvTjGObPS7bY7hyUgnwGRsYpWCwzBMU3IUO59MrNMSaU9pcnwNxw7sC-_N7b2GEQcoIr7g1voYRF2m-uZRWLw';
  1205. $url = config('qyWechat.knowledge_add_group') . $accessToken;
  1206. $postData = [
  1207. 'name' => $name,
  1208. ];
  1209. $response = HttpService::httpPost($url, json_encode($postData));
  1210. $responseData = json_decode($response, true);
  1211. Log::logInfo('knowledgeAddGroup', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1212. return $responseData;
  1213. }
  1214. /**
  1215. * 知识库问答管理-添加问答
  1216. * */
  1217. public static function addIntent($corpid, $groupId, $question, $similarQuestions, $answers)
  1218. {
  1219. $accessToken = 'O2npAJry4n5WKw_a8-toxrRACiRDl7tEDrfY12PDNHXLALb6UfK8t_bmtQwPnmtIelZRJQLLrF0tQt2vD_6D0pxt9ZeJTr9hMMQkkd5goeTK2YqgLul3F7lqgIYNJJK7tYvTjGObPS7bY7hyUgnwGRsYpWCwzBMU3IUO59MrNMSaU9pcnwNxw7sC-_N7b2GEQcoIr7g1voYRF2m-uZRWLw';
  1220. $url = config('qyWechat.knowledge_add_intent') . $accessToken;
  1221. $postData = [
  1222. 'group_id' => $groupId,
  1223. 'question' => [
  1224. 'text' => [
  1225. 'content' => $question
  1226. ]
  1227. ],
  1228. 'similar_questions' => $similarQuestions,
  1229. 'answers' => json_decode($answers, true)
  1230. ];
  1231. $response = HttpService::httpPost($url, json_encode($postData));
  1232. $responseData = json_decode($response, true);
  1233. Log::logInfo('knowledgeAddGroup', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1234. return $responseData;
  1235. }
  1236. # 添加客户入群欢迎语素材
  1237. public static function addChatGroupWelcomeTemplate($corpid, $content, $attachments, $retry = 0) {
  1238. $accessToken = AuthorizeCorp::getAccessToken($corpid);
  1239. if(empty($accessToken)) {
  1240. Log::logError('添加客户入群欢迎语素材令牌获取失败', [
  1241. 'params' => ['corpid' => $corpid, 'content' => $content, 'attachments' => $attachments],
  1242. ], 'qyWechat');
  1243. return false;
  1244. }
  1245. $url = config('qyWechat.add_chat_group_welcome_template') . $accessToken;
  1246. $text = [
  1247. "notify" => 0,
  1248. 'text' => [
  1249. 'content' => $content
  1250. ],
  1251. ];
  1252. $postData = array_merge($text, $attachments);
  1253. $response = HttpService::httpPost($url, json_encode($postData));
  1254. if($response === false && $retry < 3) {
  1255. $retry++;
  1256. return self::addChatGroupWelcomeTemplate($corpid, $content, $attachments, $retry);
  1257. }
  1258. $responseData = json_decode($response, true);
  1259. Log::logInfo('addChatGroupWelcomeTemplate', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1260. return $responseData;
  1261. }
  1262. public static function editChatGroupWelcomeTemplate($corpid, $templateId, $content, $attachments, $retry = 0) {
  1263. $accessToken = AuthorizeCorp::getAccessToken($corpid);
  1264. if(empty($accessToken)) {
  1265. Log::logError('编辑客户入群欢迎语素材令牌获取失败', [
  1266. 'params' => ['corpid' => $corpid, 'content' => $content, 'attachments' => $attachments],
  1267. ], 'qyWechat');
  1268. return false;
  1269. }
  1270. $url = config('qyWechat.edit_chat_group_welcome_template') . $accessToken;
  1271. $text = [
  1272. "template_id" => $templateId,
  1273. "notify" => 0,
  1274. 'text' => [
  1275. 'content' => $content
  1276. ],
  1277. ];
  1278. $postData = array_merge($text, $attachments);
  1279. $response = HttpService::httpPost($url, json_encode($postData));
  1280. if($response === false && $retry < 3) {
  1281. $retry++;
  1282. return self::editChatGroupWelcomeTemplate($corpid, $templateId, $content, $attachments, $retry);
  1283. }
  1284. $responseData = json_decode($response, true);
  1285. Log::logInfo('editChatGroupWelcomeTemplate', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1286. return $responseData;
  1287. }
  1288. public static function delChatGroupWelcomeTemplate($corpid, $templateId, $retry = 0) {
  1289. $accessToken = AuthorizeCorp::getAccessToken($corpid);
  1290. if(empty($accessToken)) {
  1291. Log::logError('删除客户入群欢迎语素材令牌获取失败', [
  1292. 'params' => ['corpid' => $corpid, 'template_id' => $templateId],
  1293. ], 'qyWechat');
  1294. return false;
  1295. }
  1296. $url = config('qyWechat.del_chat_group_welcome_template') . $accessToken;
  1297. $postData = [
  1298. "template_id" => $templateId
  1299. ];
  1300. $response = HttpService::httpPost($url, json_encode($postData));
  1301. if($response === false && $retry < 3) {
  1302. $retry++;
  1303. return self::delChatGroupWelcomeTemplate($corpid, $templateId, $retry);
  1304. }
  1305. $responseData = json_decode($response, true);
  1306. Log::logInfo('delChatGroupWelcomeTemplate', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1307. return $responseData;
  1308. }
  1309. /**
  1310. * 获取应用的接口许可状态
  1311. * */
  1312. public static function getAppLicenseInfo($corpid, $suiteId, $suiteCorpid, $suiteSecret, $retry=0)
  1313. {
  1314. $accessToken = TokenService::getProviderToken($suiteCorpid, $suiteSecret);
  1315. $url = config('qyWechat.get_app_license_info') . $accessToken;
  1316. $postData = [
  1317. 'corpid' => $corpid,
  1318. 'suite_id' => $suiteId
  1319. ];
  1320. $response = HttpService::httpPost($url, json_encode($postData));
  1321. if($response === false && $retry < 3) {
  1322. $retry++;
  1323. return self::getAppLicenseInfo($corpid, $suiteCorpid, $suiteSecret, $retry);
  1324. }
  1325. $responseData = json_decode($response, true);
  1326. Log::logInfo('getAppLicenseInfo', ['data' => $responseData, 'param' => $postData, 'corpid' => $corpid], 'qyWechat');
  1327. return $responseData;
  1328. }
  1329. /**
  1330. * 同步企微信息
  1331. * */
  1332. public static function syncCorpData($corpid, $permanentCode, $retry=0)
  1333. {
  1334. $component = config('qyWechat.suite_info');
  1335. $suiteId = $component['app_template_id'];
  1336. $suiteSecret = $component['app_template_secret'];
  1337. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  1338. $url = config('qyWechat.get_corp_info') . $suiteAccessToken;
  1339. $params = [
  1340. 'auth_corpid' => $corpid,
  1341. 'permanent_code' => $permanentCode
  1342. ];
  1343. $response = HttpService::httpPost($url, json_encode($params), true);
  1344. if($response === false && $retry < 3) {
  1345. $retry++;
  1346. return self::syncCorpData($corpid, $permanentCode, $retry);
  1347. }
  1348. $responseData = json_decode($response, true);
  1349. Log::logInfo('syncCorpData', ['data' => $responseData, 'param' => $params, 'corpid' => $corpid], 'qyWechat');
  1350. return $responseData;
  1351. }
  1352. }