You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.6 KiB

<?php
namespace App\Repositories;
use App\Models\AppointmentConfig;
use App\Models\ThirdAppointmentLog;
/**
* 会议第三方
*/
class MeetRepository
{
public $baseUrl;
public $account;
public $password;
public $companyId;
public function __construct()
{
$this->baseUrl = 'https://www.aiyuyue.cn';
$this->account = '13771971407';
$this->password = '123456';
$this->companyId = 'b6fbd3f82db94dd5bb7f8e5768540528';
}
/**
* 获取公司 Id
* @return false|mixed
*/
public function login()
{
$url = $this->baseUrl . '/services/showInfo.ashx';
$header[] = 'Content-Type: application/json';
$params = [
'method' => 'login',
'account' => $this->account,
'password' => $this->password
];
try {
$result = httpCurl($url, 'GET', $params, $header);
$result = json_decode($result, true);
if ($result['success']) {
return $result['companyId'];
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
/**
* 会议室列表
*/
public function index()
{
$url = $this->baseUrl . '/services/api/MeetingRoomService.aspx';
$header[] = 'Content-Type: application/json';
$params = [
'method' => 'find',
'account' => $this->account,
'password' => md5($this->password)
];
try {
$result = httpCurl($url, 'GET', $params, $header);
$result = json_decode($result, true);
if ($result['success']) {
// 更新下config配置
foreach ($result['data'] as $item) {
$where = ['value' => $item['id']];
$data = ['remark' => $item['name'], 'value' => $item['id'], 'room' => json_encode($item, JSON_UNESCAPED_UNICODE)];
AppointmentConfig::updateOrCreate($where, $data);
}
return $result['data'];
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
/**
* 会议室预约
*/
public function appointment($model, $appointmentConfig, &$out)
{
$startDateTime = new \DateTime($model->start_time);
$endDateTime = new \DateTime($model->end_time);
$meetOther = json_decode($appointmentConfig->room, true);
$url = $this->baseUrl . '/services/api/BookingService.aspx';
$header[] = 'Content-Type: application/json';
$allMeetIds = [];
$allResults = [];
$currentDate = clone $startDateTime;
while ($currentDate->format('Y-m-d') <= $endDateTime->format('Y-m-d')) {
$dayStart = clone $currentDate;
$dayStart->setTime(5, 0, 0);
$dayEnd = clone $currentDate;
$dayEnd->setTime(23, 0, 0);
if ($currentDate->format('Y-m-d') === $startDateTime->format('Y-m-d')) {
$dayStart = clone $startDateTime;
}
if ($currentDate->format('Y-m-d') === $endDateTime->format('Y-m-d')) {
$dayEnd = clone $endDateTime;
}
$params = [
'account' => $this->account,
'password' => md5($this->password),
'roomName' => $appointmentConfig->name,
'roomCode' => $meetOther['code'],
'roomAddress' => $meetOther['address'],
'beginTime' => $dayStart->format('Y-m-d H:i:s'),
'endTime' => $dayEnd->format('Y-m-d H:i:s'),
'speaker' => $model->name,
'subject' => empty($model->content) ? '预约会议室' : $model->content,
'booker' => "{$model->name};{$model->mobile};",
'attendance' => "{$model->name};{$model->mobile};",
'signUrl' => ''
];
$result = [];
$finally = 0;
try {
$resultJson = httpCurl($url, 'GET', $params, $header);
$result = json_decode($resultJson, true);
if ($result['success']) {
$finally = 1;
$allMeetIds[] = $result['data'];
$allResults[] = $result;
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '会议室预约-' . $currentDate->format('Y-m-d'));
} else {
$out = $resultJson;
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '会议室预约-' . $currentDate->format('Y-m-d'));
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '会议室预约-' . $currentDate->format('Y-m-d'));
return false;
}
$currentDate->modify('+1 day');
}
if (!empty($allMeetIds)) {
$meetIds = explode(',', $model->meet_id);
$meetIds = array_merge($meetIds, $allMeetIds);
$meetIds = array_filter($meetIds);
$model->meet_id = implode(',', $meetIds);
$model->save();
$out = json_encode($allResults, JSON_UNESCAPED_UNICODE);
return true;
}
return false;
}
/**
* 会议室删除
*/
public function cancelAppointment($model, $meetId, &$out)
{
if (empty($meetId)) return true;
$url = $this->baseUrl . '/services/api/BookingService.aspx';
$header[] = 'Content-Type: application/json';
$params = [
'account' => $this->account,
'password' => md5($this->password),
'method' => 'delete',
'id' => $meetId
];
$result = [];
$finally = 0;
try {
$resultJson = httpCurl($url, 'GET', $params, $header);
$out = $resultJson;
$result = json_decode($resultJson, true);
if ($result['success']) {
$finally = 1;
return true;
} else {
$out = $resultJson;
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
return false;
} finally {
// 写日志
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '会议室预约取消');
}
}
/**
* 会议室信息
*/
public function meetingService($roomId, $startDate, $endDate, &$out)
{
$url = $this->baseUrl . '/services/api/MeetingService.aspx';
$header[] = 'Content-Type: application/json';
$params = [
'account' => $this->account,
'password' => md5($this->password),
'method' => 'find',
'roomId' => $roomId,
'begin' => $startDate,
'end' => $endDate,
];
try {
$resultJson = httpCurl($url, 'GET', $params, $header);
$out = $resultJson;
$result = json_decode($resultJson, true);
if ($result['success']) {
$out = $result['data'];
return true;
} else {
$out = $resultJson;
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
return false;
}
}
}