<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2022/03/22 1847
 * Time: 18:47
 */

namespace app\api\controller\alibaba;

use app\common\controller\Api;
use think\Db;
use think\Request;

/**
 * 阿里云语音服务
 * @package app\api\controller\core
 */
class Call extends Base
{
    /**
     * 1. 向指定号码发起语音验证码
     */
    public function singleCallByTts()
    {
        $param = $this->request->param();
        $mobile = $param['mobile'];//手机号
        if (empty($mobile)) {
            $this->error("无效的手机号");
        }
        $from_screen = $param['from_screen'];//控制台名称
        $type = $param['type'];//语音 、视频
        $outId = "CALL_" . rand('1000000000', '9999999999');//发起请求时预留给调用方的自定义ID,最终会通过在回执消息中将此ID带回给调用方。字符串类型,长度为1~15个字节。

        $data = [
            "calledNumber" => $mobile,
            "ttsCode" => "TTS_237056642",
            "ttsParam" => json_encode(["name" => $from_screen, "type" => $type]),//"{\"name\":\"zhong\",\"type\":\"语音\"}",
            "playTimes" => 3,
            "volume" => 100,
            "speed" => -200,
            "outId" => $outId,
        ];
        $res = $this->main($data);
        $log_data = [
            "calledNumber" => $mobile,
            "name" => $from_screen,
            "type" => $type,
            "outId" => $outId,
            "createtime" => time(),
            "backcontent" => $res
        ];
        Db::name("alibaba_call")->insert($log_data);
        $result = json_decode($res, true);
        if (!empty($result)) {
            if ($result['Code'] == "OK") {
                $this->success("电话语音通知推送成功", $result);//状态,online在线 offline离线
            } else {
                $this->error("电话语音通知推送失败[" . $result['Code'] . $result['Message'] . "]");
            }
        } else {
            $this->error("语音通知推送服务器请求异常");
        }
    }

}