<?php

namespace App\Utils\API;

use GuzzleHttp\Client;

/**
 * 第三方api发送上号请求
 */
class SendCode
{
    /**
     * 发送验证码
     */
    public static function send_code($phone)
    {
        $client = new Client(); // 创建 Guzzle 客户端

        // 请求数据
        $data = [
            "token" => "druid_6aa47680-56e6-423f-866d-e190700725fd",
            "phoneNumber" => $phone,
            "business" => 0,
        ];

        // 发送 POST 请求
        $response = $client->post('https://dx1.rocketgo.vip/ex-api/biz/api/merchant/scanCode', [
            'json' => $data, // 以 JSON 格式发送数据
        ]);

        // 获取响应体内容
        $body = $response->getBody();
        $responseData = json_decode($body, true); // 如果返回的是 JSON 格式,解析它
        return $responseData;
    }
    /**
     * 获取验证码
     */
    public static function get_code($phone)
    {
        $client = new Client(); // 创建 Guzzle 客户端

        // 请求数据
        $data = [
            "token" => "druid_6aa47680-56e6-423f-866d-e190700725fd",
            "phoneNumber" => $phone,
        ];

        // 发送 POST 请求
        $response = $client->post('https://dx1.rocketgo.vip/ex-api/biz/api/merchant/getScanCode', [
            'json' => $data, // 以 JSON 格式发送数据
        ]);

        // 获取响应体内容
        $body = $response->getBody();
        $responseData = json_decode($body, true); // 如果返回的是 JSON 格式,解析它
        return $responseData;
    }
    /**
     * 检测是否在线
     * https://apifox.com/apidoc/shared-c08671dd-4e9d-44dd-a2a1-ec4b8316a81f
     * 0 在线 1不在线 2token无效 3WS号不存在
     * code
     */
    public static function get_ws_status($phone)
    {

        $client = new Client(); // 创建 Guzzle 客户端


        $url = "https://dx1.rocketgo.vip/ex-api/biz/api/wsStatus?wsNumber=" . $phone . "&token=druid_6aa47680-56e6-423f-866d-e190700725fd";
        // 发送 POST 请求
        $response = $client->get("$url");
        $body = $response->getBody();
        $responseData = json_decode($body, true); // 如果返回的是 JSON 格式,解析它
        if ($responseData['code'] == 0) {
            return $responseData['status'];
        }
        if($responseData['code'] == 1){
            echo('token无效');
            return 2;
        }
        if($responseData['code'] == 2){
            echo('2WS号不存在');
            return 3;
        }
        return -1;
    }
}