пятница, 7 марта 2025 г.

Получаем цены на автозапчасти через REST API moskvorechie.ru



HTML

<div class="replacement " value="<?=$value->search_number.";".$value->brand_name?>">

                <div  class="daydeliverysupplier">

                            Доставка:

                </div>

                <div class="kolrest">

                            В наличии

                </div>

                <div class="price">

                              Цена

                </div>

</div>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

JS

 $(document).ready(function(){

    var Arr='';

    $(".replacement").each(

        function( index, element){

            $(this).on( "getprice"+index, function(event, data) { //добавить событие к каждому .replacement

                if ( typeof(data) !== "undefined" && data !== null && Object.keys(data).length !== 0 ) {

                    if('price' in data){

                        $( this ).find(".price").text( Intl.NumberFormat("ru-RU", { style: "currency", currency: "RUB" }).format(Number(data['price']),) );

                        $( this ).find(".spinner-border").hide();

                        $( this ).find(".kolrest").text("На складе: " + data['kolrest'] + "шт." );

                        $( this ).find(".daydeliverysupplier").text("Доставка: " + (Number(data['daydeliverysupplier']) + 2) + "д." );

                    } else $( this ).hide();

                }else $( this ).hide();

            });

            xData = $.ajax({

                type: 'POST',

                cache: false,

                url: '/services/suppliers/moskvorechie/?'+$( this ).attr("value"),

                data: {partnumbrand: $( this ).attr("value")},

                success: function (data) {

console.log("mos data: ", data);

                    $('.replacement').trigger('getprice'+index, data);

                }

            });

        }

    );

});


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/services/suppliers/moskvorechie/index.php

<?php

if ((include $_SERVER["DOCUMENT_ROOT"].'/services/include.php') != TRUE) { exit;}


function price_by_nr_firm($part_number, $brand_name){

    global $config;

    $url = $config->suppliers->moskvorechie->urlAPI

        . "l=".$config->suppliers->moskvorechie->login_name

        . '&p='.$config->suppliers->moskvorechie->login_password

        . '&act=price_by_nr_firm&v=1'

        . '&nr='.urlencode($part_number)

        . '&f='.urlencode($brand_name)

        . '&cs=utf8';

    $json = get_content($url);

    if (false === $json) {

        echo "error";

        return false;

    }

    $part = json_decode($json);

    return $json;

}


if(isset($_POST['partnumbrand'])) {

    header("Content-type: application/json; charset=UTF-8");

    header("Cache-Control: must-revalidate");

    header("Pragma: no-cache");

    header("Expires: -1");


    $array=array();

    $arr = explode(';', $_POST['partnumbrand']);

    if(count($arr)<2){

        print json_encode($array);

        exit();

    }

    error_log ("moskvorechie arr:  ".var_export($arr, true), 0);

    $c = price_by_nr_firm($arr[0], $arr[1]);

    error_log ("moskvorechie c:  ".var_export($c, true), 0);


    if($c->idtovoemshort!=null){


        if(strcasecmp($c->idtovoemshort, $arr[0]) == 0 and strcasecmp($c->nbrand, $arr[1]) == 0){

            $array[]=array(

                'value'=>$_POST['partnumbrand'],

                'kolrest'=>(string)$c->kolrest,

                'price'=>(string)$c->price,

                'daydeliverysupplier'=>(string)$c->daydeliverysupplier,

                'minpart'=>(string)$c->minpart);

        }

    }else $array[]=array('value'=>$_POST['partnumbrand']);

    print json_encode($array);

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/services/include.php

<?php


class ConfigClass {

    public $levam;

    public $db;

    public $suppliers;


    private function array2obj($data) {

        $object = new stdClass();

        foreach ($data as $key => $val) {

            if (is_array($val)){

                $val = $this->array2obj($val);

            }

            $object->$key = $val;

        }

        return $object;

    }

    public function __construct(Array $data ) {

        foreach ($data as $key => $val){

            if (is_array($val)){

                $this->{$key} = $this->array2obj($val);

            } else 

                $this->{$key} = $val;

        }

    }

}

if(file_exists($_SERVER["DOCUMENT_ROOT"].'/services/.config.php')){

    $config = new ConfigClass(include ($_SERVER["DOCUMENT_ROOT"].'/services/.config.php'));

}

function get_content($URL){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_URL, $URL);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/services/.config.php

<?php


return array(

     'db' =>

    array(

        'host' => 'localhost',

        'database' => 'bitrix',

        'login' => 'root',

        'password' => 'password',

    ),

    'suppliers' =>

        'moskvorechie' =>

        array(

            'urlAPI' => 'http://portal.moskvorechie.ru/portal.api?',

            'login_name' => 'lunevstes',

            'login_password' => 'hrRqxsfPYr5HARELrEzhkw9ecYwyTa9RPExCGIdeSS9GTSDOeTnx5q4CHzhtAED8',

            'margin'=>'',

            'deliverydays'=>'',

        ),

    ),

);

Комментариев нет:

Отправить комментарий

Split / разбить dump

//Разбить дамп по 50000 строк for i in `ls *.sql`; do split -d -l 50000 -a 4 --additional-suffix=.sql $i "$(basename "$i" .sq...