// CDEK
import axios from 'axios'
router.get("/service", async (req, res) => {
console.log("REQUEST прилетел", req.body)
console.log("REQUEST прилетел", req.query)
const baseURL = 'https://api.cdek.ru/v2';
const client_credentials = 'client_credentials';
const clientId = 'LOGIN';
const clientSecret = 'PASS';
// Get authToken
async function getAuthToken() {
try {
await axios({
method: "post",
baseURL: baseURL,
url: "oauth/token",
params: {
client_id: clientId,
client_secret: clientSecret,
grant_type: client_credentials,
},
}).then(function (response) {
console.log("API TOKEN:", response.data.access_token);
getOffices(response.data.access_token)
return response.data.access_token
}).catch(function (error) {
console.log("error", error);
return null;
});
} catch (error) {
console.error(error);
}
}
// Получить все отделения ПВЗ
async function getOffices(authToken) {
try {
await axios({
method: "get",
baseURL: baseURL,
url: "deliverypoints",
params: {
client_id: clientId,
client_secret: clientSecret,
grant_type: client_credentials,
is_handout: true,
weight: 1000,
},
headers: {
Authorization: "Bearer " + authToken,
},
})
.then(function (response) {
// console.log("Отделения ПВЗ:", response.data);
return res.json(response.data);
})
.catch(function (error) {
console.log("error", error);
return null;
});
} catch (error) {
console.error(error);
}
}
if(req.query){
getAuthToken();
if(req.query.action == "offices"){
await getOffices()
console.log("FUNCTION offices")
console.log("req.query.action", req.query.action)
} else {
res.json("Action require")
}
} else{
console.error('NO actions', req.query);
}
})
router.post("/service", async (req, res) => {
console.log("REQUEST body прилетел", req.body)
console.log("REQUEST query прилетел", req.query)
const baseURL = 'https://api.cdek.ru/v2';
const client_credentials = 'client_credentials';
const clientId = 'LOGIN';
const clientSecret = 'PASS';
// Get authToken
async function getAuthToken() {
try {
await axios({
method: "post",
baseURL: baseURL,
url: "oauth/token",
params: {
client_id: clientId,
client_secret: clientSecret,
grant_type: client_credentials,
},
}).then(function (response) {
console.log("API TOKEN:", response.data.access_token);
calculate(response.data.access_token)
return response.data.access_token
}).catch(function (error) {
console.log("error", error);
return null;
});
} catch (error) {
console.error(error);
}
}
// Получить все отделения ПВЗ
async function calculate(authToken) {
try {
await axios({
method: "post",
baseURL: baseURL,
url: "calculator/tarifflist",
params: {
client_id: clientId,
client_secret: clientSecret,
grant_type: client_credentials,
is_handout: true,
weight: 1000,
},
headers: {
Authorization: "Bearer " + authToken,
},
})
.then(function (response) {
console.log("Отделения ПВЗ:", response);
return res.json(response.data);
})
.catch(function (error) {
console.log("error", error);
return null;
});
} catch (error) {
console.error(error);
}
}
if(req.query){
getAuthToken();
if(req.query.action == "calculate"){
console.log("FUNCTION calculate")
await calculate()
} else {
res.json("Action require")
}
} else{
console.error('NO actions', req.query);
}
})
windows1251
фиг знает Привет!