<?php
/**
 * 云客服，访客名片
 *
 * 接入流程
 * 在线服务接入访客名片的操作流程分为 2 步：
 * 1. 访客端聊天窗数据埋点
 * 2. 客服端工作台访客信息展示
 */

namespace wap\controllers;

// 这里替换一下公钥路径 ( 由云客服提供 )
defined('VISITOR_CARD_PUB_KEY') or define('VISITOR_CARD_PUB_KEY', dirname(__DIR__) . '/租户公钥.pem');

class VisitorCardController
{
    /**
     * 1. 访客端聊天窗数据埋点
     * 埋点主要是构造带有访客信息的加密链接
     * 云客服在线服务聊天窗 URL 示例如下：
     * https://cschat-ccs.aliyun.com/index.htm?tntInstId=xxxx&scene=xxxx
     * cinfo：将要传给访客名片的查询参数加密后的内容
     * key：通过非对称加密算法加密后的对称加密密钥
     * 最终的聊天窗 URL 示例如下：
     * https://cschat-ccs.aliyun.com/index.htm?tntInstId=xxxx&scene=xxxx&cinfo=xxxxx&key=yyyyy
     * @return string
     */
    public static function CustomerInfoCryptoUtil()
    {
        // 替换这里的tntInstId和scene的参数值
        // 
        // 1. 金融云租户, 支付宝小程序租户, 钉钉租户
        // $baseUrl = 'https://cschat.antcloud.com.cn/index.htm?tntInstId=xxxx&scene=xxxx';
        // 2. 阿里云租户
        $baseUrl = 'https://cschat-ccs.aliyun.com/index.htm?tntInstId=xxxx&scene=xxxx'

        //生成随即16位AES密钥
        $aesKey = md5(time(), true);

        //需要根据实际系统用户的信息修改
        $cInfoArray = [
            'extInfo' => [],
            'userId' => 123456,
            'uname' => '张三',
        ];

        $jsonStr = json_encode($cInfoArray, 1);

        $cInfo = self::AESEncrypt($jsonStr, $aesKey);

        //RSA 公钥加密aesKey
        openssl_public_encrypt($aesKey, $encrypted, file_get_contents(VISITOR_CARD_PUB_KEY));

        //构造带有访客信息的加密链接
        return $baseUrl . '&cinfo=' . urlencode(base64_encode($cInfo)) . '&key=' . urlencode(base64_encode($encrypted));
    }

    //页面嵌入 访客名片
    public function actionIframeVisitorCard()
    {
        $params = $_GET['params'];
        $key = $_GET['key'];

        //RSA 公钥解密
        openssl_public_decrypt(base64_decode($key), $decrypted, file_get_contents(VISITOR_CARD_PUB_KEY));

        //AES解密 params
        $aesParams = self::AESDecrypt($params, $decrypted);

        //json数据转为数组
        $resData = json_decode($aesParams, 1);
        //结果示例
        //array (
        //  'extInfo' =>
        //  array (),
        //  'userId' => '123456',
        //  'sourceType' => 'online',
        //  'contactId' => '201906101113010100000003521590',
        //  'agentId' => '0001',
        //  'tntInstId' => 'BNABCD',
        //  'key' => '201906101113010526000001847436',
        //)

        //可以自定义 渲染视图
        return $resData;
    }


    /**
     * AES-128-ECB 加密算法
     * @param String $str 需要加密字符串
     * @param String $key 密钥
     * @return string
     */
    private static function AESEncrypt($str, $key)
    {
        return openssl_encrypt($str, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
    }

    /**
     * AES-128-ECB 解密算法
     * @param String $strEncode base64Encode之后的加密串
     * @param String $key 密钥
     * @return string
     */
    private static function AESDecrypt($strEncode, $key)
    {
        return openssl_decrypt(base64_decode($strEncode), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
    }

}
