12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034 |
- <?php
- namespace weworkapi\api;
- use app\model\Company;
- use app\model\WeworksingleContactWay;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Messages\TextCard;
- use think\facade\Config;
- use think\facade\Log;
- use \WxWorkFinanceSdk;
- use \WxworkFinanceSdkException;
- class apiSingle {
- protected $app = null;
- public $error = null;
- protected $config = null;
- private $errorCode = 1;
- private $errorMsg = ['errcode'=> 1, 'errmsg'=> 'error'];
- public function __construct($config='')
- {
- if ($config){
- $this->config = $config;
- $this->app = Factory::work($config);
- }
- }
- /**
- * 获取部门列表
- * @return mixed
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- */
- public function getDepartmentList(){
- return $this->app->department->list();
- }
- /**
- * 根据部门ID获取用户列表
- * @param int $departmentId 部门ID
- * @param bool $fetchChild 是否查询子部门
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- */
- public function getUserList($departmentId = 1, $fetchChild = false){
- $list_result = $this->app->user->getDetailedDepartmentUsers($departmentId, $fetchChild);
- $list = $list_result['userlist'];
- return $list;
- }
- /**
- * 根据用户ID获取用户详情
- */
- public function getUserById($userId){
- return $this->app->user->get($userId);
- }
- /**
- * 开启外部联系功能的成员
- */
- public function getExternalContactUser(){
- return $this->app->external_contact->getFollowUsers();
- // $result['follow_user'];
- }
- /**
- * 根据客户id获取客户详情
- * @param $customer_id
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getCustomerDetailById($customer_id){
- return $this->app->external_contact->get($customer_id);
- }
- /**
- * 根据用户ID批量获取外部联系人详情
- * @param string $user_id 内部人员ID
- * @param string $cursor 请求分页
- * @param int $limit 单次条数
- */
- public function getCustomerDetailByUser($user_id = [], $cursor = '', $limit = 100, $list = []){
- $result = $this->app->external_contact->batchGetByUser($user_id, $cursor, $limit);
- $list = array_merge($list, $result['external_contact_list']);
- if ($result['next_cursor']) {
- return $this->getCustomerDetailByUser($user_id, $result['next_cursor'], $limit, $list);
- }
- return $list;
- }
- /**
- * 根据用户ID获取外部联系人列表
- * @param string $user_id
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getCustomerListByUser($user_id = ''){
- $result = $this->app->external_contact->list($user_id);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- // $result['external_userid'];
- }
- /**
- * 拉取会话记录
- * @param int $start_seq
- * @return bool
- */
- public function getTalkRecord($start_seq = 0, $config = []){
- try {
- //$config = Config::get('app.wework_talk_record');
- $obj = new \WxworkFinanceSdk($config['corp_id'], $config['secret'], [
- "proxy_password" => "world",
- "timeout" => -2,
- ]);
- // 私钥地址
- $privateKey = file_get_contents($config['private_key']);
- $chats = json_decode($obj->getChatData($start_seq, 1000), true);
- $msg_data = []; //消息数组
- $file_data = []; // 文件数组
- $recall_data = []; // 撤回消息数据
- foreach ($chats['chatdata'] as $val) {
- // 单条消息数组
- unset($once_msg);
- $once_msg = [];
- //单条存储文件信息
- unset($once_file);
- $once_file = [];
- $once_file['is_local'] = 1; // 1文件在本地 0 文件在OSS
- $once_file['local_delete'] = 1; // 1文件在本地未删除
- $decryptRandKey = null;
- openssl_private_decrypt(base64_decode($val['encrypt_random_key']), $decryptRandKey, $privateKey, OPENSSL_PKCS1_PADDING);
- $str = $obj->decryptData($decryptRandKey, $val['encrypt_chat_msg']);
- $msg = json_decode($str, true);
- // 切换企业单独处理
- if ($msg['action'] == 'switch') {
- $once_msg['company_id'] = $config['company_id'];
- $once_msg['msgid'] = $msg['msgid'];
- $once_msg['action'] = 'switch';
- $once_msg['msgfrom'] = $msg['user'];
- $once_msg['msgtime'] = strtotime(get_microtime_format($msg['time']));
- $once_msg['msgtype'] = 'switch';
- $once_msg['content'] = '切换企业';
- $once_msg['json_content'] = $str;
- $msg_data[] = $once_msg;
- continue;
- }
- $once_msg = $msg;
- $once_msg['company_id'] = $config['company_id'];
- $once_msg['msgfrom'] = !empty($msg['from']) ? $msg['from'] : '';
- $once_msg['tolist'] = !empty($msg['tolist']) ? json_encode($msg['tolist']) : '';
- $once_msg['json_content'] = $str;
- if (!empty($msg['msgtype'])) {
- switch ($msg['msgtype']) {
- case 'text':
- // 文本
- $once_msg['content'] = $msg['text']['content'];
- break;
- case 'image':
- // 图片
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------image error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------image error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, 'jpg');
- try {
- $obj->downloadMedia($msg['image']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------image error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------image error---------')->save();
- break;
- }
-
- $once_msg['content'] = $file_name;
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $file_name;
- $once_file['sdkfileid'] = $msg['image']['sdkfileid'];
- $once_file['filesize'] = $msg['image']['filesize'];
- $once_file['md5sum'] = $msg['image']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'revoke':
- // 撤回消息
- $recall_data[] = $msg['revoke']['pre_msgid'];
- break;
- case 'agree':
- // 同意会话聊天内容
- break;
- case 'voice':
- // 语音消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------voice error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------voice error---------')->save();
- break;
- }
- $amr = make_file_name($dir_path, 'amr');
- $mp3 = make_file_name($dir_path, 'mp3');
- try {
- $obj->downloadMedia($msg['voice']['sdkfileid'], $dir_path . $amr);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------voice download error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------voice download error---------')->save();
- break;
- }
-
- // amr2mp3
- $amr_path = $dir_path . $amr;
- $mp3_path = $dir_path . $mp3;
- if (file_exists($amr_path)) {
- try {
- $command = "/usr/local/bin/ffmpeg -i $amr_path $mp3_path";
- exec($command, $error);
- Log::record('----------arm2mp3 $error---------')->save();
- Log::record($error)->save();
- Log::record('----------arm2mp3 $error---------')->save();
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------arm2mp3 error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------arm2mp3 error---------')->save();
- }
- }
- $once_msg['content'] = $mp3;
- $once_file['other_file'] = $amr;
- $once_file['other_path'] = $path . $amr;
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $mp3;
- $once_file['filename'] = $mp3;
- $once_file['sdkfileid'] = $msg['voice']['sdkfileid'];
- $once_file['filesize'] = $msg['voice']['voice_size'];
- $once_file['md5sum'] = $msg['voice']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'video':
- // 视频消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------video error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------video error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, 'mp4');
-
- try {
- $obj->downloadMedia($msg['video']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------video error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------video error---------')->save();
- break;
- }
- $once_msg['content'] = $file_name;
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $file_name;
- $once_file['sdkfileid'] = $msg['video']['sdkfileid'];
- $once_file['filesize'] = $msg['video']['filesize'];
- $once_file['md5sum'] = $msg['video']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'card':
- // 名片消息
- break;
- case 'location':
- // 位置消息
- break;
- case 'emotion':
- // 表情消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------emotion error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------emotion error---------')->save();
- break;
- }
- $fileext = $msg['emotion']['type'] == 1 ? 'gif' : 'png';
- $file_name = make_file_name($dir_path, $fileext);
- try {
- $obj->downloadMedia($msg['emotion']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------emotion error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------emotion error---------')->save();
- break;
- }
- $once_msg['content'] = $file_name;
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $file_name;
- $once_file['sdkfileid'] = $msg['emotion']['sdkfileid'];
- $once_file['filesize'] = $msg['emotion']['imagesize'];
- $once_file['md5sum'] = $msg['emotion']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'file':
- // 文件消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------file error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------file error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, $msg['file']['fileext']);
- try {
- $obj->downloadMedia($msg['file']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------file error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------file error---------')->save();
- break;
- }
- $once_msg['content'] = $msg['file']['filename'];
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $msg['file']['filename'];
- $once_file['sdkfileid'] = $msg['file']['sdkfileid'];
- $once_file['filesize'] = $msg['file']['filesize'];
- $once_file['md5sum'] = $msg['file']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'link':
- // 链接消息
- break;
- case 'weapp':
- // 小程序消息
- break;
- case 'chatrecord':
- // 会话记录消息
- break;
- case 'todo':
- // 待办消息
- break;
- case 'vote':
- // 投票消息
- break;
- case 'collect':
- // 填表消息
- break;
- case 'redpacket':
- // 红包消息
- break;
- case 'meeting':
- // 会议邀请消息
- break;
- case 'docmsg':
- // 在线文档消息
- break;
- case 'markdown':
- // 系统消息
- break;
- case 'news':
- // 图文消息
- break;
- case 'calendar':
- // 日程消息
- break;
- case 'mixed':
- // 混合消息
- $item = $msg['mixed']['item'];
- $once_msg['content'] = json_encode($item);
- foreach ($item as $k => $v){
- switch ($v['type']){
- case 'image':
- // 图片
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------img error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------img error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, 'jpg');
- try {
- $obj->downloadMedia($msg['image']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------img error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------img error---------')->save();
- break;
- }
- $once_msg['content'] = $path . $file_name;
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $file_name;
- $once_file['sdkfileid'] = $msg['image']['sdkfileid'];
- $once_file['filesize'] = $msg['image']['filesize'];
- $once_file['md5sum'] = $msg['image']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'voice':
- // 语音消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------voice error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------voice error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, 'mp3');
- try {
- $obj->downloadMedia($msg['voice']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------voice error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------voice error---------')->save();
- break;
- }
-
- $once_msg['content'] = $path . $file_name;
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $file_name;
- $once_file['sdkfileid'] = $msg['voice']['sdkfileid'];
- $once_file['filesize'] = $msg['voice']['voice_size'];
- $once_file['md5sum'] = $msg['voice']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'video':
- // 视频消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------video error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------video error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, 'mp4');
- try {
- $obj->downloadMedia($msg['video']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------video error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------video error---------')->save();
- break;
- }
- $once_msg['content'] = $path . $file_name;
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $file_name;
- $once_file['sdkfileid'] = $msg['video']['sdkfileid'];
- $once_file['filesize'] = $msg['video']['filesize'];
- $once_file['md5sum'] = $msg['video']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'file':
- // 文件消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------file error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------file error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, $msg['file']['fileext']);
- try {
- $obj->downloadMedia($msg['file']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------file error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------file error---------')->save();
- break;
- }
- $once_msg['content'] = $msg['file']['filename'];
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $msg['file']['filename'];
- $once_file['sdkfileid'] = $msg['file']['sdkfileid'];
- $once_file['filesize'] = $msg['file']['filesize'];
- $once_file['md5sum'] = $msg['file']['md5sum'];
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- default:
- break;
- }
- }
- break;
- case 'meeting_voice_call':
- // 音频存档消息
- $path = 'weworksingle_file/'.$config['company_id'].'/' . date('Ymd') . '/';
- $dir_path = public_path() . $path;
- if (!mkdirs($dir_path)) {
- Log::record('----------meeting_voice_call error---------')->save();
- Log::record('创建' . $dir_path . '失败')->save();
- Log::record('----------meeting_voice_call error---------')->save();
- break;
- }
- $file_name = make_file_name($dir_path, 'mp3');
- try {
- $obj->downloadMedia($msg['meeting_voice_call']['sdkfileid'], $dir_path . $file_name);
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------meeting_voice_call error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------meeting_voice_call error---------')->save();
- break;
- }
- $once_file['msgid'] = $msg['msgid'];
- $once_file['path'] = $path . $file_name;
- $once_file['filename'] = $file_name;
- $once_file['sdkfileid'] = $msg['meeting_voice_call']['sdkfileid'];
- $once_file['filesize'] = 0;
- $once_file['md5sum'] = '';
- $once_file['cretetime'] = time();
- $file_data[] = $once_file;
- break;
- case 'voip_doc_share':
- // 音频共享文档消息
- break;
- case 'external_redpacket':
- // 互通红包消息
- break;
- case 'sphfeed':
- // 视频号消息
- break;
- case 'voiptext':
- // 音视频通话
- break;
- case 'qydiskfile':
- // 微盘文件
- $once_msg['content'] = $msg['info']['filename'];
- break;
- default:
- break;
- }
- $msg_data[] = $once_msg;
- }
- }
- $return_data['record'] = $msg_data;
- $return_data['file'] = $file_data;
- $return_data['recall'] = $recall_data;
- //最后一条消息的seq
- if (!empty($msg_data)) {
- $end_chats = end($chats['chatdata']);
- $return_data['seq'] = $end_chats['seq'];
- } else {
- $return_data['seq'] = '';
- }
- return $return_data;
- } catch (\WxworkFinanceSdkException $e) {
- Log::record('----------WxworkFinanceSdkException error---------')->save();
- Log::record($e->getMessage())->save();
- Log::record('----------WxworkFinanceSdkException error---------')->save();
- $this->error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取开启会话存储功能的用户列表
- */
- public function getPermitUsers(){
- return $this->app->msg_audit->getPermitUsers();
- // $result['ids'];
- }
- /**
- * 根据用户ID获取用户群列表
- */
- public function getGroupChatsByUser($userId = [], $cursor = ''){
- $params['status_filter'] = 0; // 客户群跟进状态过滤。0 - 所有列表(即不过滤)1 - 离职待继承2 - 离职继承中3 - 离职继承完成
- $params['owner_filter']['userid_list'] = $userId;
- $params['cursor'] = $cursor;
- $params['limit'] = 1000;
- $result = $this->app->external_contact->getGroupChats($params);
- return $result;
- }
- /**
- * 获取客户群列表
- * @param int $status 客户群跟进状态过滤。 0 - 所有列表(即不过滤) 1 - 离职待继承 2 - 离职继承中 3 - 离职继承完成
- * @param array $owner 群主过滤。
- * @param int $cursor 用于分页查询的游标,字符串类型,由上一次调用返回,首次调用不填
- * @param int $limit 分页,预期请求的数据量,取值范围 1 ~ 1000
- */
- public function getGroupChats($cursor = '', $limit = 100, $status = 0, $owner = []){
- $params = [
- "status_filter" => $status,
- "owner_filter" => [
- "userid_list" => $owner,
- ],
- "cursor" => $cursor,
- "limit" => $limit
- ];
- return $this->app->external_contact->getGroupChats($params);
- }
- /**
- * 获取所有客户群列表
- * @param int $status
- * @param array $owner
- */
- public function getAllGroupChats($cursor = '', $limit = 100, $status = 0, $owner = [], $list = []){
- $result = $this->getGroupChats($cursor, $limit, $status, $owner);
- if ($result['errcode'] == 0) {
- $list = array_merge($list, $result['group_chat_list']);
- if (!empty($result['next_cursor'])){
- return $this->getAllGroupChats($result['next_cursor'], $limit, $status, $owner, $list);
- }
- } else {
- Log::record($result['errmsg'])->save();
- }
- return $list;
- }
- /**
- * 获取群详情
- * @param string $chatId
- */
- public function getGroupChat($chatId = ''){
- $result = $this->app->external_contact->getGroupChat($chatId);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- }
- /**
- * 给企业成员发送消息
- * @param string $content
- * @param string $touser
- */
- public function sendMessageToUser($content = '', $touser = ''){
- $result = $this->app->messenger->message($content)->toUser($touser)->send();
- if ($result['errcode'] == 0) {
- return true;
- } else {
- Log::record($result)->save();
- return false;
- }
- }
- /**
- * 企业群发消息
- */
- public function sendExternalMessage($content = []){
- $result = $this->app->external_contact_message->submit($content);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($content)->save();
- Log::record($result['errmsg'])->save();
- return false;
- }
- }
- /**
- * 根据群发消息id获取群发详情
- * @param $msgId
- */
- public function getExternalMessage($msgId){
- $result = $this->app->external_contact_message->get($msgId);
- return $result;
- }
- /**
- * 根据群发消息id及群发人员获取群发执行结果
- */
- public function getExternalMessageSendResult($msgid, $userid, $limit = 500, $cursor = '', $list = []){
- $result = $this->app->external_contact_message->getGroupmsgSendResult($msgid, $userid, $limit, $cursor);
- if ($result['errcode'] == 0){
- $list = array_merge($list, $result['send_list']);
- if (!empty($result['next_cursor'])){
- $list = $this->getExternalMessageSendResult($msgid, $userid, $limit, $result['next_cursor'], $list);
- }
- }
- return $list;
- }
- /**
- * 根据群发id获取群发任务详情
- * @param $msgid
- * @param $limit
- * @param $cursor
- */
- public function getExternalMessageTask($msgid, $limit = 500, $cursor = ''){
- $result = $this->app->external_contact_message->getGroupmsgTask($msgid);
- return $result;
- }
- /**
- * 获取企业已配置的【联系我】方式列表
- * @param string $cursor
- * @param int $limit
- * @param string $startTime
- * @param string $ednTime
- * @return array|bool|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getContactWayList($cursor = '', $limit = 100, $startTime = '', $ednTime = ''){
- $result = $this->app->contact_way->list($cursor, $limit, $startTime, $ednTime);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return false;
- }
- }
- /**
- * 根据配置ID获取联系我配置详情
- * @param string $configId
- * @return bool|mixed
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getContactWay($configId = ''){
- $result = $this->app->contact_way->get($configId);
- if ($result['errcode'] == 0) {
- return $result['contact_way'];
- } else {
- Log::record($result['errmsg'])->save();
- return false;
- }
- }
- /**
- * 设置【联系我】方式
- * @param int $type 联系方式类型,1-单人,2-多人
- * @param int $scene 场景,1-在小程序中联系,2-通过二维码联系
- * @param array $config 配置项
- * @return bool
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function addContactWay($type = 1, $scene = 2, $config = []){
- if (empty($config)) {
- return false;
- } else {
- $result = $this->app->contact_way->create($type, $scene, $config);
- Log::record($result)->save();
- if ($result['errcode'] == 0) {
- $data['company_id'] = Company::where('root_id', '=', request()->token['root_org'])->value('id');
- $data['type'] = $type;
- $data['scene'] = $scene;
- $data['style'] = !empty($config['style']) ? $config['style'] : '';
- $data['remark'] = !empty($config['remark']) ? $config['remark'] : '';
- $data['skip_verify'] = !empty($config['skip_verify']) ? $config['skip_verify'] : '';
- $data['state'] = !empty($config['state']) ? $config['state'] : '';
- $data['user'] = !empty($config['user']) ? $config['user'] : '';
- $data['party'] = !empty($config['party']) ? $config['party'] : '';
- $data['is_temp'] = !empty($config['is_temp']) ? $config['is_temp'] : '';
- $data['expires_in'] = !empty($config['expires_in']) ? $config['expires_in'] : '';
- $data['chat_expires_in'] = !empty($config['chat_expires_in']) ? $config['chat_expires_in'] : '';
- $data['unionid'] = !empty($config['unionid']) ? $config['unionid'] : '';
- $data['conclusions'] = !empty($config['conclusions']) ? $config['conclusions'] : '';
- $data['config_id'] = $result['config_id'];
- WeworksingleContactWay::create($data);
- return true;
- } else {
- Log::record($result['errmsg'])->save();
- return false;
- }
- }
- }
- /**
- * 调整【联系我】方式
- * @param string $configId 配置ID
- * @param array $config 配置项
- * @return bool
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function editContactWay($configId = '', $config = []){
- $result = $this->app->contact_way->update($configId, $config);
- if ($result['errcode'] == 0) {
- $data['style'] = !empty($config['style']) ? $config['style'] : '';
- $data['remark'] = !empty($config['remark']) ? $config['remark'] : '';
- $data['skip_verify'] = !empty($config['skip_verify']) ? $config['skip_verify'] : '';
- $data['state'] = !empty($config['state']) ? $config['state'] : '';
- $data['user'] = !empty($config['user']) ? $config['user'] : '';
- $data['party'] = !empty($config['party']) ? $config['party'] : '';
- $data['expires_in'] = !empty($config['expires_in']) ? $config['expires_in'] : '';
- $data['chat_expires_in'] = !empty($config['chat_expires_in']) ? $config['chat_expires_in'] : '';
- $data['unionid'] = !empty($config['unionid']) ? $config['unionid'] : '';
- $data['conclusions'] = !empty($config['conclusions']) ? $config['conclusions'] : '';
- WeworksingleContactWay::where('config_id', '=', $configId)->save($data);
- return true;
- } else {
- Log::record($result['errmsg'])->save();
- return false;
- }
- }
- /**
- * 删除【联系我】方式
- * @param string $configId
- * @return bool
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function deleteContactWay($configId = ''){
- $result = $this->app->contact_way->delete($configId);
- if ($result['errcode'] == 0) {
- return true;
- } else {
- Log::record($result['errmsg'])->save();
- return false;
- }
- }
- /**
- * 上传图片临时素材
- */
- public function uploadMediaImage($path){
- $result = $this->app->media->uploadImage($path);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- }
- /**
- * 上传声音临时素材
- */
- public function uploadMediaVoice($path){
- $result = $this->app->media->uploadVoice($path);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- }
- /**
- * 上传视频临时素材
- */
- public function uploadMediaVideo($path){
- $result = $this->app->media->uploadVideo($path);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- }
- /**
- * 上传普通文件临时素材
- */
- public function uploadMediaFile($path){
- $result = $this->app->media->uploadFile($path);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- }
- /**
- * 获取临时素材
- */
- public function getMediaById($mediaId){
- $result = $this->app->media->get($mediaId);
- return $result;
- }
- /**
- * 获取accessToken
- * @return array|string
- * @throws \EasyWeChat\Kernel\Exceptions\HttpException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function getToken(){
- $config = $this->config;
- if ($config) {
- $app = Factory::officialAccount($config);
- $accessToken = $app->access_token;
- $token = $accessToken->getToken(); // token 数组 token['access_token'] 字符串
- return $token;
- } else {
- return '';
- }
- }
- /**
- * 上传永久素材
- * @param $path
- */
- public function uploadMaterialImage($path){
- $result = $this->app->media->uploadImg($path);
- if ($result['errcode'] == 0) {
- return $result;
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- }
- /**
- * 客户联系
- * 员工行为数据
- * return ["errcode": 0, "errmsg": "ok", "behavior_data": []]
- * @param $userIds 查询用户id
- * @param $from 开始时间
- * @param $to 结束时间
- * @param $partyid 部门ID列表
- * 支持的最大查询跨度为30天;用户最多可获取最近180天内的数据;
- */
- public function getUserBehaviorData($userIds = [], $from = '', $to = '', $partyid=[]){
- $result = $this->app->external_contact_statistics->userBehavior($userIds, $from, $to, $partyid);
- if ($result['errcode'] == 0) {
- return $result['behavior_data'];
- } else {
- Log::record($result['errmsg'])->save();
- return $this->errorMsg;
- }
- }
- /**
- * 获取「群聊数据统计」数据
- * 获取指定日期的统计数据。注意,企业微信仅存储180天的数据。
- * @param array $param
- * @param array $list
- * @return array|mixed
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getGroupChatStatistic($param = [], $list = []){
- $param['limit'] = 1000;
- $result = $this->app->external_contact_statistics->groupChatStatistic($param);
- Log::record($param)->save();
- Log::record($result)->save();
- if ($result['errcode'] == 0){
- $list = array_merge($list, $result['items']);
- if (!empty($result['total']) && !empty($result['next_offset']) && $result['total'] != $result['next_offset']){
- $param['offset'] = $result['next_offset'];
- $list = $this->getGroupChatStatistic($param, $list);
- }
- }
- return $list;
- }
- }
|