Cloudcall.php 4.7 KB
<?php

namespace app\admin\model\facrm;

use app\admin\library\Auth;
use think\Cache;
use think\Model;

/**
 * 坐席管理
 */
class Cloudcall extends Model
{
    protected $name = 'facrm_cloudcall';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';


    /**
     * 关联员工
     */
    public function admin()
    {
        return $this->hasOne('app\admin\model\Admin', 'id', 'admin_id');
    }


    /**
     * 云呼签入签出
     * @param $from_exten 坐席工号
     * @param $exten_type 云呼方式 Local/gateway
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function sign($from_exten, $exten_type, $sign = 'SignIn')
    {
        //获取云呼配置
        $all_types = \addons\facrm\library\cloudcall\Call::getProviders();
        $keys = "";
        foreach ($all_types as $v) {
            $keys .= $keys ? ',' . 'cloudcall' . $v : 'cloudcall' . $v;
        }
        $settingModel = new \app\admin\model\facrm\Setting();
        $setting = $settingModel->where('key', 'in', $keys)->where('status', 1)->find();//只找一条云呼通道
        if (!$setting) return true;


        try {
            $setting = $setting->toArray();
            $setting['key'] = $setting['describe'];
            $setting = array_merge($setting, $setting['values']);
            $call = \addons\facrm\library\cloudcall\Call::instance($setting);
            $send_data = [
                'sign' => $sign,//SignIn / SignOut
                'exten' => $from_exten,//座席号
                'exten_type' => $exten_type,//Local/sip/gateway外呼时强制坐席使用该接听方式进行外呼。Local为“手机”,”gateway为“语音网关”
            ];
            $result = $call->signInOrOut($send_data);
            if (!$result) {
                return false;
            }

            return true;

        } catch (\Exception $e) {
            return false;
        }
    }


    /**
     * 修改坐席号
     * @param $cloud
     * @param $admin_id
     * @param $from_exten
     * @param bool $is_add 如果不存在是否添加
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function edit($cloud, $admin_id, $from_exten, $is_add = false)
    {

        if (!$cloud) {
            //获取云呼配置
            $all_types = \addons\facrm\library\cloudcall\Call::getProviders();
            $keys = "";
            foreach ($all_types as $v) {
                $keys .= $keys ? ',' . 'cloudcall' . $v : 'cloudcall' . $v;
            }
            $settingModel = new \app\admin\model\facrm\Setting();
            $setting = $settingModel->where('key', 'in', $keys)->where('status', 1)->find();//只找一条云呼通道
            if (!$setting) return true;
            $cloud = $setting['describe'];
        }
        if (!$admin_id) {
            $auth = Auth::instance();

            if (!$auth->id) return false;

            $admin_id = $auth->id;

        }

        $model = new self();
        $row = $model->where('cloud', $cloud)->where('admin_id', $admin_id)->find();
        $exten_type = strip_tags(\think\Cookie::get("facrm_extentype")); //云呼通话方式
        $result = false;
        if (!$row && $is_add) {
            //添加
            $params = [
                'cloud' => $cloud,
                'admin_id' => $admin_id,
                'from_exten' => $from_exten,
                'create_time' => time(),
                'exten_type' => $exten_type
            ];
            $result = $model->allowField(true)->save($params);
        } else if ($row) {
            //修改
            $result = $row->save(['from_exten' => $from_exten, 'exten_type' => $exten_type]);
        }

        //签入
        try {
            $setting = $setting->toArray();
            $setting['key'] = $setting['describe'];
            $setting = array_merge($setting, $setting['values']);
            $call = \addons\facrm\library\cloudcall\Call::instance($setting);

            $send_data = array(
                'sign' => 'SignIn', //SignIn / SignOut
                'exten' => $from_exten, //座席号
                'exten_type' => $exten_type, //Local/sip/gateway外呼时强制坐席使用该接听方式进行外呼。Local为“手机”,”gateway为“语音网关”
            );

            $result = $call->signInOrOut($send_data);


        } catch (\Exception $e) {

        }


        return $result ? true : false;

    }


}