Files
xk-api-yii/service/modules/v1/controllers/OrderVideoController.php

176 lines
6.0 KiB
PHP

<?php
namespace service\modules\v1\controllers;
use Carbon\Carbon;
use common\core\BaseAppController;
use common\enums\OrderAcceptEnum;
use common\models\oldDb\Order;
use common\models\oldDb\OrderVideoInfo;
use yii\base\Exception;
/**
* @doc-module 视频问诊
*/
class OrderVideoController extends BaseAppController
{
/**
* @doc-name 视频问诊-UserSig
* @throws \Exception
*/
public function actionGetKey(): array
{
$config = \Yii::$app->params['tencent_video'];
$api = new \Tencent\TLSSigAPIv2($config['appid'], $config['secret']);
$user_id = 'service_' . \Yii::$app->user->identity->getId();
$key = $api->genUserSig($user_id);
return [
'appid' => $config['appid'],
'key' => $key,
'user_id' => $user_id,
];
}
/**
* @doc-name 视频基础信息
* @doc-param order_id int 订单id
* @doc-return is_limit int 0不限制1限制
* @doc-return left_minutes int 剩余分钟数
*/
public function actionVideoInfo()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['order_id', 'required']
]);
/* @var Order $order */
$order = Order::find()->where([
'id' => $post['order_id'],
'su_id' => \Yii::$app->user->identity->id,
])->with('video')->one();
if (!$order || !$order['video']) {
throw new Exception('订单不存在');
}
if ($order->accept_status != OrderAcceptEnum::ACCEPTING) {
throw new Exception('订单状态非接诊中');
}
if ($order->type != 2) {
throw new Exception('您的订单非视频问诊订单');
}
/* @var \common\models\oldDb\OrderVideoInfo $video */
$video = $order['video'];
return [
'is_limit' => $video->is_limit,
'left_minutes' => $video->left_minutes,
];
}
/**
* @doc-name 视频通话处理
* @doc-param order_id int 订单id
* @doc-param type string 类型start开始视频sign上报扣除end结束视频
* @doc-return is_limit int 0不限制1限制
* @doc-return left_minutes int 剩余分钟数
*/
public function actionVideoSign()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['order_id', 'required'],
['type', 'required'],
]);
/* @var Order $order */
$order = Order::find()->where([
'id' => $post['order_id'],
'su_id' => \Yii::$app->user->identity->id,
])->with('video')->one();
if (!$order || !$order['video']) {
throw new Exception('订单不存在');
}
if ($order->accept_status != OrderAcceptEnum::ACCEPTING) {
throw new Exception('订单状态非接诊中');
}
if ($order->type != 2) {
throw new Exception('您的订单非视频问诊订单');
}
/* @var \common\models\oldDb\OrderVideoInfo $video */
$video = $order['video'];
if ($video->is_limit == 0) {
return [
'is_limit' => $video->is_limit,
'left_minutes' => $video->left_minutes,
];
}
switch ($post['type']) {
// 开始视频
case 'start':
if ($video->left_minutes <= 0) {
throw new Exception('通话时长不足');
}
$now = Carbon::now()->toDateTimeString();
if (!$video->start_at) {
$video->start_at = $now;
}
$video->last_start_left_minutes = $video->left_minutes;
$video->last_start_at = $now;
$info = $video->info ? json_decode($video->info, true) : [];
$info[] = [
'time' => $now,
'desc' => '开始视频通话',
'left_minutes' => $video->left_minutes,
];
$video->info = json_encode($info);
$video->save();
return [
'is_limit' => $video->is_limit,
'left_minutes' => $video->left_minutes,
];
break;
// 上报扣除
case 'sign':
if ($video->left_minutes <= 0) {
throw new Exception('通话时长不足');
}
$now = Carbon::now()->toDateTimeString();
$video->start_at = $now;
$left_minutes = $video->last_start_left_minutes - Carbon::now()->diffInRealMinutes($video->last_start_at);
$video->left_minutes = max($left_minutes, 0);
$video->last_limit_at = $now;
$info = $video->info ? json_decode($video->info, true) : [];
$info[] = [
'time' => $now,
'desc' => '扣除时长',
'left_minutes' => $video->left_minutes,
];
$video->info = json_encode($info);
$video->save();
return [
'is_limit' => $video->is_limit,
'left_minutes' => $video->left_minutes,
];
break;
// 结束视频
case 'end':
$now = Carbon::now()->toDateTimeString();
$video->end_at = $now;
$left_minutes = $video->last_start_left_minutes - Carbon::now()->diffInRealMinutes($video->last_start_at);
$video->left_minutes = max($left_minutes, 0);
$info = $video->info ? json_decode($video->info, true) : [];
$info[] = [
'time' => $now,
'desc' => '结束视频通话',
'left_minutes' => $video->left_minutes,
];
$video->info = json_encode($info);
$video->save();
return ['通话已结束'];
break;
}
}
}