@cdek-it/widget 3.13.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta content="width=device-width, initial-scale=1.0" name="viewport" />
6
+ <title>Виджет ПВЗ</title>
7
+ </head>
8
+ <body>
9
+ <div id="cdek-map" style="max-width: 56rem; height: 600px"></div>
10
+ <script src="./cdek-widget.umd.js"></script>
11
+ <script>
12
+ document.addEventListener('DOMContentLoaded', function() {
13
+ window.widget = new window.CDEKWidget({
14
+ defaultLocation: 'Новосибирск',
15
+ debug: true,
16
+ popup: false,
17
+ fixBounds: 'country',
18
+ from: 'Новосибирск',
19
+ goods: [
20
+ {
21
+ width: 10,
22
+ height: 10,
23
+ length: 10,
24
+ weight: 10,
25
+ },
26
+ ],
27
+ });
28
+ });
29
+ </script>
30
+ </body>
31
+ </html>
package/dist/service.php CHANGED
@@ -13,10 +13,13 @@ $service->process($_GET, file_get_contents('php://input'));
13
13
 
14
14
  class service
15
15
  {
16
+ const WIDGET_VERSION = '4.0.0';
17
+
16
18
  /**
17
19
  * @var string Auth login
18
20
  */
19
21
  private $login;
22
+
20
23
  /**
21
24
  * @var string Auth pwd
22
25
  */
@@ -54,15 +57,25 @@ class service
54
57
  $this->sendValidationError('Action is required');
55
58
  }
56
59
 
57
- $this->getAuthToken();
58
-
59
60
  switch ($this->requestData['action']) {
60
61
  case 'offices':
61
- $this->sendResponse($this->getOffices(), $time);
62
- break;
62
+ $this->getAuthToken();
63
+
64
+ $result = $this->getOffices();
65
+
66
+ $this->sendResponse($result, $time);
63
67
  case 'calculate':
64
- $this->sendResponse($this->calculate(), $time);
65
- break;
68
+ $this->getAuthToken();
69
+
70
+ $result = $this->calculate();
71
+
72
+ $this->sendResponse($result, $time);
73
+ case 'byCoordinate':
74
+ $this->getAuthToken();
75
+
76
+ $result = $this->getOfficesByCoordinates();
77
+
78
+ $this->sendResponse($result, $time);
66
79
  default:
67
80
  $this->sendValidationError('Unknown action');
68
81
  }
@@ -70,136 +83,13 @@ class service
70
83
 
71
84
  private function sendValidationError($message)
72
85
  {
73
- $this->http_response_code(400);
86
+ http_response_code(400);
74
87
  header('Content-Type: application/json');
75
- header('X-Service-Version: 3.13.0');
88
+ header('X-Service-Version: ' . self::WIDGET_VERSION);
76
89
  echo json_encode(array('message' => $message));
77
90
  exit();
78
91
  }
79
92
 
80
- private function http_response_code($code)
81
- {
82
- switch ($code) {
83
- case 100:
84
- $text = 'Continue';
85
- break;
86
- case 101:
87
- $text = 'Switching Protocols';
88
- break;
89
- case 200:
90
- $text = 'OK';
91
- break;
92
- case 201:
93
- $text = 'Created';
94
- break;
95
- case 202:
96
- $text = 'Accepted';
97
- break;
98
- case 203:
99
- $text = 'Non-Authoritative Information';
100
- break;
101
- case 204:
102
- $text = 'No Content';
103
- break;
104
- case 205:
105
- $text = 'Reset Content';
106
- break;
107
- case 206:
108
- $text = 'Partial Content';
109
- break;
110
- case 300:
111
- $text = 'Multiple Choices';
112
- break;
113
- case 301:
114
- $text = 'Moved Permanently';
115
- break;
116
- case 302:
117
- $text = 'Moved Temporarily';
118
- break;
119
- case 303:
120
- $text = 'See Other';
121
- break;
122
- case 304:
123
- $text = 'Not Modified';
124
- break;
125
- case 305:
126
- $text = 'Use Proxy';
127
- break;
128
- case 400:
129
- $text = 'Bad Request';
130
- break;
131
- case 401:
132
- $text = 'Unauthorized';
133
- break;
134
- case 402:
135
- $text = 'Payment Required';
136
- break;
137
- case 403:
138
- $text = 'Forbidden';
139
- break;
140
- case 404:
141
- $text = 'Not Found';
142
- break;
143
- case 405:
144
- $text = 'Method Not Allowed';
145
- break;
146
- case 406:
147
- $text = 'Not Acceptable';
148
- break;
149
- case 407:
150
- $text = 'Proxy Authentication Required';
151
- break;
152
- case 408:
153
- $text = 'Request Time-out';
154
- break;
155
- case 409:
156
- $text = 'Conflict';
157
- break;
158
- case 410:
159
- $text = 'Gone';
160
- break;
161
- case 411:
162
- $text = 'Length Required';
163
- break;
164
- case 412:
165
- $text = 'Precondition Failed';
166
- break;
167
- case 413:
168
- $text = 'Request Entity Too Large';
169
- break;
170
- case 414:
171
- $text = 'Request-URI Too Large';
172
- break;
173
- case 415:
174
- $text = 'Unsupported Media Type';
175
- break;
176
- case 500:
177
- $text = 'Internal Server Error';
178
- break;
179
- case 501:
180
- $text = 'Not Implemented';
181
- break;
182
- case 502:
183
- $text = 'Bad Gateway';
184
- break;
185
- case 503:
186
- $text = 'Service Unavailable';
187
- break;
188
- case 504:
189
- $text = 'Gateway Time-out';
190
- break;
191
- case 505:
192
- $text = 'HTTP Version not supported';
193
- break;
194
- default:
195
- exit('Unknown http status code "' . htmlentities($code) . '"');
196
- }
197
-
198
- $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
199
- header($protocol . ' ' . $code . ' ' . $text);
200
- $GLOBALS['http_response_code'] = $code;
201
- }
202
-
203
93
  private function getAuthToken()
204
94
  {
205
95
  $time = $this->startMetrics();
@@ -233,7 +123,7 @@ class service
233
123
  $headers = array(
234
124
  'Accept: application/json',
235
125
  'X-App-Name: widget_pvz',
236
- 'X-App-Version: 3.13.0'
126
+ 'X-App-Version: ' . self::WIDGET_VERSION
237
127
  );
238
128
 
239
129
  if ($this->authToken) {
@@ -256,24 +146,26 @@ class service
256
146
  }
257
147
 
258
148
  curl_setopt_array($ch, array(
259
- CURLOPT_USERAGENT => 'widget/3.13.0',
149
+ CURLOPT_USERAGENT => 'widget/' . self::WIDGET_VERSION,
260
150
  CURLOPT_HTTPHEADER => $headers,
261
151
  CURLOPT_RETURNTRANSFER => true,
262
152
  CURLOPT_HEADER => true,
263
153
  ));
264
154
 
265
155
  $response = curl_exec($ch);
156
+
157
+ if ($response === false) {
158
+ throw new RuntimeException(curl_error($ch), curl_errno($ch));
159
+ }
160
+
161
+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
266
162
  $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
267
163
  $headers = substr($response, 0, $headerSize);
268
164
  $result = substr($response, $headerSize);
269
165
 
270
166
  $addedHeaders = $this->getHeaderValue($headers);
271
167
 
272
- if ($result === false) {
273
- throw new RuntimeException(curl_error($ch), curl_errno($ch));
274
- }
275
-
276
- return array('result' => $result, 'addedHeaders' => $addedHeaders);
168
+ return array('result' => $result, 'addedHeaders' => $addedHeaders, 'httpCode' => $httpCode);
277
169
  }
278
170
 
279
171
  private function getHeaderValue($headers)
@@ -296,9 +188,9 @@ class service
296
188
 
297
189
  private function sendResponse($data, $start)
298
190
  {
299
- $this->http_response_code(200);
191
+ http_response_code(!empty($data['httpCode']) ? $data['httpCode'] : 200);
300
192
  header('Content-Type: application/json');
301
- header('X-Service-Version: 3.13.0');
193
+ header('X-Service-Version: ' . self::WIDGET_VERSION);
302
194
  if (!empty($data['addedHeaders'])) {
303
195
  foreach ($data['addedHeaders'] as $header) {
304
196
  header($header);
@@ -335,4 +227,13 @@ class service
335
227
  $this->endMetrics('calc', 'Calculate Request', $time);
336
228
  return $result;
337
229
  }
230
+
231
+ protected function getOfficesByCoordinates()
232
+ {
233
+ $time = $this->startMetrics();
234
+ $result = $this->httpRequest('deliverypoints/byPolygons', $this->requestData);
235
+
236
+ $this->endMetrics('officeByCoordinate', 'Offices By Coordinates Request', $time);
237
+ return $result;
238
+ }
338
239
  }
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@cdek-it/widget",
3
- "version": "3.13.1",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "license": "LGPL-3.0-only",
6
+ "packageManager": "yarn@4.1.1",
6
7
  "files": [
7
8
  "dist"
8
9
  ],