1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = undefined;
|
7 |
|
8 | var _stringify = require('babel-runtime/core-js/json/stringify');
|
9 |
|
10 | var _stringify2 = _interopRequireDefault(_stringify);
|
11 |
|
12 | var _typeof2 = require('babel-runtime/helpers/typeof');
|
13 |
|
14 | var _typeof3 = _interopRequireDefault(_typeof2);
|
15 |
|
16 | var _promise = require('babel-runtime/core-js/promise');
|
17 |
|
18 | var _promise2 = _interopRequireDefault(_promise);
|
19 |
|
20 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
21 |
|
22 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
23 |
|
24 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
25 |
|
26 | var _createClass3 = _interopRequireDefault(_createClass2);
|
27 |
|
28 | var _superagent = require('superagent');
|
29 |
|
30 | var _superagent2 = _interopRequireDefault(_superagent);
|
31 |
|
32 | var _Cookies = require('../common/Cookies');
|
33 |
|
34 | var _Cookies2 = _interopRequireDefault(_Cookies);
|
35 |
|
36 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
37 |
|
38 | (function () {
|
39 | var enterModule = require('react-hot-loader').enterModule;
|
40 |
|
41 | enterModule && enterModule(module);
|
42 | })();
|
43 |
|
44 | var cookie = '';
|
45 | if (!__SERVER__) {
|
46 | cookie = new _Cookies2.default();
|
47 | }
|
48 | var methods = ['get', 'post', 'put', 'patch', 'del'];
|
49 | function getProtocol(req) {
|
50 | return "http:";
|
51 |
|
52 | }
|
53 | function formatUrl(path, config, req) {
|
54 | if (/^(https?:)|^(\/\/)/.test(path)) return path;
|
55 | var adjustedPath = path[0] !== '/' ? '/' + path : path;
|
56 | if (__SERVER__) {
|
57 | var protocol = getProtocol(req);
|
58 |
|
59 | if (config.apiPort != "" && config.apiPort != "80") {
|
60 | return protocol + '//' + config.apiHost + ':' + config.apiPort + adjustedPath;
|
61 | } else {
|
62 | return protocol + '//' + config.apiHost + adjustedPath;
|
63 | }
|
64 |
|
65 | }
|
66 |
|
67 | return '/proxy' + adjustedPath;
|
68 | }
|
69 |
|
70 | var ApiClient = function () {
|
71 | function ApiClient(config, req, res, operating) {
|
72 | var _this = this;
|
73 |
|
74 | (0, _classCallCheck3.default)(this, ApiClient);
|
75 |
|
76 | methods.forEach(function (method) {
|
77 | return _this[method] = function (path) {
|
78 | var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
79 | params = _ref.params,
|
80 | data = _ref.data;
|
81 |
|
82 | var noLogin = arguments[2];
|
83 | var jsonFlag = arguments[3];
|
84 | var shopFlag = arguments[4];
|
85 | var companyIdFlag = arguments[5];
|
86 | return new _promise2.default(function (resolve, reject) {
|
87 | var url = formatUrl(path, config, req);
|
88 | var request = _superagent2.default[method](url);
|
89 | console.log(__SERVER__ ? "Server" : "Client", "---ajax:", url);
|
90 | params = params || {};
|
91 |
|
92 | |
93 |
|
94 |
|
95 | if (__SERVER__) {
|
96 |
|
97 | var userAgent = req.headers['user-agent'];
|
98 | request.set("user-agent", userAgent);
|
99 |
|
100 |
|
101 | if (!params.companyId) {
|
102 |
|
103 | if (!req.cookies['companyId']) {
|
104 | params.companyId = req.session && req.session.companyId || undefined;
|
105 | } else {
|
106 | params.companyId = req.cookies['companyId'];
|
107 | }
|
108 | }
|
109 | }
|
110 | if (!__SERVER__ && !data) {
|
111 | |
112 |
|
113 |
|
114 |
|
115 | if (!shopFlag && !params.shopId) {
|
116 | params.shopId = cookie.get('shopId');
|
117 | }
|
118 | |
119 |
|
120 |
|
121 |
|
122 | if (!companyIdFlag && !params.companyId) {
|
123 | params.companyId = cookie.get('companyId');
|
124 | }
|
125 | }
|
126 |
|
127 | params._ = +new Date();
|
128 | request.query(params);
|
129 |
|
130 | if (__SERVER__ && req.get('cookie')) {
|
131 | request.set('cookie', req.get('cookie'));
|
132 | }
|
133 | if (__SERVER__) {
|
134 | request.set('original-host', req.get("host"));
|
135 | console.log("host:---------------->", request.get("original-host"));
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | }
|
161 | if (data) {
|
162 | |
163 |
|
164 |
|
165 | if (__SERVER__) {
|
166 |
|
167 | if ((typeof data === 'undefined' ? 'undefined' : (0, _typeof3.default)(data)) === 'object') {
|
168 | if (!data.companyId) {
|
169 | if (!req.cookies['companyId']) {
|
170 | data.companyId = req.session && req.session.companyId || undefined;
|
171 | } else {
|
172 | data.companyId = req.cookies['companyId'];
|
173 | }
|
174 | }
|
175 | }
|
176 | }
|
177 | if (!__SERVER__) {
|
178 | if ((typeof data === 'undefined' ? 'undefined' : (0, _typeof3.default)(data)) === 'object') {
|
179 | |
180 |
|
181 |
|
182 |
|
183 | if (!shopFlag && !data.shopId) {
|
184 | data.shopId = cookie.get('shopId');
|
185 | }
|
186 | |
187 |
|
188 |
|
189 |
|
190 | if (!companyIdFlag && !data.companyId) {
|
191 | data.companyId = cookie.get('companyId');
|
192 | }
|
193 | }
|
194 | }
|
195 | if (jsonFlag) {
|
196 | request.type("json").send(data);
|
197 | } else {
|
198 | request.type("form").send(data);
|
199 | }
|
200 | }
|
201 | function sso(body) {
|
202 | var ssoSystem = operating ? "passport-platform" : "passport";
|
203 | var service = operating ? "passport-operating-view" : "service-passport-view";
|
204 | var redirectUrl = '//@/' + service + '/determine/login?return_url=';
|
205 | if (noLogin) {
|
206 | resolve(body);
|
207 | } else {
|
208 | if (__SERVER__ && body.code == "0-0004") {
|
209 | var urls = req.get('host').replace("www.", "").split(".");
|
210 | if (urls.length > 2) {
|
211 | if (urls[0] == "mall" || urls[0] == "shop" || urls[0] == "buyer" || urls[0] == "passport" || urls[0] == "platform" || urls[0] == "passport-platform" || urls[0] == "view-zone") {
|
212 | urls.splice(0, 1, ssoSystem);
|
213 | } else {
|
214 | urls.unshift(ssoSystem);
|
215 | }
|
216 | } else {
|
217 | urls.unshift(ssoSystem);
|
218 | }
|
219 | var protocol = getProtocol(req);
|
220 | redirectUrl = protocol + redirectUrl.replace("@", urls.join("."));
|
221 | try {
|
222 | var fullUrl = protocol + '//' + req.get('host') + req.originalUrl;
|
223 | res && res.redirect(redirectUrl + encodeURIComponent(fullUrl));
|
224 | } catch (e) {}
|
225 | } else if (body.code == "0-0004") {
|
226 | var hostMatch = window.location.href.replace("www.", "").match(/.*?\/\/(.*?)\/|.*?\/\/(.*)/);
|
227 | var _urls = hostMatch[1] || hostMatch[2];
|
228 | _urls = _urls.split(".");
|
229 | if (_urls.length > 2) {
|
230 | if (_urls[0] == "mall" || _urls[0] == "shop" || _urls[0] == "buyer" || _urls[0] == "passport" || _urls[0] == "platform" || _urls[0] == "passport-platform" || _urls[0] == "view-zone") {
|
231 | _urls.splice(0, 1, ssoSystem);
|
232 | } else {
|
233 | _urls.unshift(ssoSystem);
|
234 | }
|
235 | } else {
|
236 | _urls.unshift(ssoSystem);
|
237 | }
|
238 | redirectUrl = redirectUrl.replace("@", _urls.join("."));
|
239 | window.location.href = redirectUrl + encodeURIComponent(window.location.href);
|
240 | } else if (!!!__SERVER__ && body.code == "000001020001") {
|
241 | var _hostMatch = window.location.href.match(/.*?\/\/(.*?)\/|.*?\/\/(.*)/);
|
242 | var _urls2 = _hostMatch[1] || _hostMatch[2];
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | var redirectUrls = "/user-shop-view/sellerinfo/defaultsupplyhome";
|
248 | window.location.href = '//' + _urls2 + redirectUrls;
|
249 | } else {
|
250 | resolve(body);
|
251 | }
|
252 | }
|
253 | }
|
254 |
|
255 | function error(err) {
|
256 | reject(err);
|
257 | }
|
258 |
|
259 |
|
260 | request.end(function (err, response) {
|
261 | try {
|
262 | if (response) {
|
263 | response = response || {};
|
264 | var body = response.body || {},
|
265 | text = response.text || "";
|
266 | err ? error(body || err) : sso((0, _stringify2.default)(body) == "{}" ? text : body);
|
267 | } else {
|
268 | __SERVER__ && res.send("请求后端接口:" + url + ",请求参数:" + (0, _stringify2.default)(params) + ",返回异常" + err + ",response:" + response);
|
269 | }
|
270 | } catch (e) {
|
271 | __SERVER__ && res.send("请求后端接口:" + url + ",请求参数:" + (0, _stringify2.default)(params) + ",返回异常" + err + ",response:" + response);
|
272 | }
|
273 | });
|
274 | });
|
275 | };
|
276 | });
|
277 | }
|
278 |
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | (0, _createClass3.default)(ApiClient, [{
|
292 | key: 'empty',
|
293 | value: function empty() {}
|
294 | }, {
|
295 | key: '__reactstandin__regenerateByEval',
|
296 | value: function __reactstandin__regenerateByEval(key, code) {
|
297 | this[key] = eval(code);
|
298 | }
|
299 | }]);
|
300 | return ApiClient;
|
301 | }();
|
302 |
|
303 | exports.default = ApiClient;
|
304 | ;
|
305 |
|
306 | (function () {
|
307 | var reactHotLoader = require('react-hot-loader').default;
|
308 |
|
309 | var leaveModule = require('react-hot-loader').leaveModule;
|
310 |
|
311 | if (!reactHotLoader) {
|
312 | return;
|
313 | }
|
314 |
|
315 | reactHotLoader.register(cookie, 'cookie', '../../src/apiClient/ApiClient.js');
|
316 | reactHotLoader.register(methods, 'methods', '../../src/apiClient/ApiClient.js');
|
317 | reactHotLoader.register(getProtocol, 'getProtocol', '../../src/apiClient/ApiClient.js');
|
318 | reactHotLoader.register(formatUrl, 'formatUrl', '../../src/apiClient/ApiClient.js');
|
319 | reactHotLoader.register(ApiClient, 'ApiClient', '../../src/apiClient/ApiClient.js');
|
320 | leaveModule(module);
|
321 | })();
|
322 |
|
323 | ;
|
324 | module.exports = exports['default'];
|
325 |
|
\ | No newline at end of file |