UNPKG

13.2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = undefined;
7
8var _stringify = require('babel-runtime/core-js/json/stringify');
9
10var _stringify2 = _interopRequireDefault(_stringify);
11
12var _typeof2 = require('babel-runtime/helpers/typeof');
13
14var _typeof3 = _interopRequireDefault(_typeof2);
15
16var _promise = require('babel-runtime/core-js/promise');
17
18var _promise2 = _interopRequireDefault(_promise);
19
20var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
21
22var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
23
24var _createClass2 = require('babel-runtime/helpers/createClass');
25
26var _createClass3 = _interopRequireDefault(_createClass2);
27
28var _superagent = require('superagent');
29
30var _superagent2 = _interopRequireDefault(_superagent);
31
32var _Cookies = require('../common/Cookies');
33
34var _Cookies2 = _interopRequireDefault(_Cookies);
35
36function _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
44var cookie = '';
45if (!__SERVER__) {
46 cookie = new _Cookies2.default();
47}
48var methods = ['get', 'post', 'put', 'patch', 'del'];
49function getProtocol(req) {
50 return "http:";
51 //return req.headers["x-proto"]=="http"?"http:":"https:";
52}
53function 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 //var protocol = req.headers["x-proto"]=="http"?"http":"https";
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 // Prepend host and port of the API server to the path.
65 }
66 // Prepend `/api` to relative URL, to proxy to API server.
67 return '/proxy' + adjustedPath;
68}
69
70var 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 * node端判断companyId
94 * */
95 if (__SERVER__) {
96
97 var userAgent = req.headers['user-agent'];
98 request.set("user-agent", userAgent);
99
100 // 判断params中是否存在companyId,如果不存在则添加
101 if (!params.companyId) {
102 // 如果cookie里不存在companyId,读取session里的companyId,否则从cookie读取
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 * 获取cookie中的shopId,通过为空判断,将shopId添加到params对象中
113 * 实现为每个请求添加shopId参数
114 * */
115 if (!shopFlag && !params.shopId) {
116 params.shopId = cookie.get('shopId');
117 }
118 /*
119 * 获取cookie中的companyId,通过为空判断,将companyId添加到params对象中
120 * 实现为每个请求添加companyId参数
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 // // 根据店铺用户权限,跳转到查询出的菜单第1个链接
138 // const _params = {};
139 // const menuUrl = '/proxy/authority-service/mall/resource/queryResourceMenuBySellerId';
140 // const request1 = superagent[method](menuUrl);
141 // request1.set('original-host', req.get('host'));
142 // request1.set('cookie', req.get('cookie'));
143 // _params.shopType = req.cookies['shopType'];
144 // _params.shopId = req.cookies['shopId'];
145 // _params.companyId = req.cookies['companyId'];
146 // _params._ = +new Date();
147 // request1.query(_params);
148 // request1.end((err, response) => {
149 // if (response.body.code == 0) {
150 // // 从返回的数据中截取第1个有效url做为跳转路径
151 // const _data = JSON.stringify(response.body.data);
152 // const urlArr1 = _data.substr(_data.indexOf('"url":"/') + 7);
153 // const urlArr2 = urlArr1.substr(0, urlArr1.indexOf('"'));
154 // // 如果当前url中含有shop(即认为是在店铺页面下),则重定向url到第1个有并向路径
155 // if (req.headers.host.indexOf('shop.') >= 0) {
156 // req.redirect(302, '//' + req.headers.host + urlArr2);
157 // }
158 // }
159 // });
160 }
161 if (data) {
162 /*
163 * node端判断companyId
164 * */
165 if (__SERVER__) {
166 // 判断params中是否存在companyId,如果不存在则添加
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 * 获取cookie中的shopId,通过为空判断,将shopId添加到params对象中
181 * 实现为每个请求添加shopId参数
182 * */
183 if (!shopFlag && !data.shopId) {
184 data.shopId = cookie.get('shopId');
185 }
186 /*
187 * 获取cookie中的companyId,通过为空判断,将companyId添加到params对象中
188 * 实现为每个请求添加companyId参数
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 // console.log("hostMatch")
244 // console.log(hostMatch)
245 // console.log("hostMatch")
246 // console.log("2222222222")
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 //request.end((err, { body , text }) => err ? error(body||err) : sso(JSON.stringify(body)=="{}"?text:body));
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 * There's a V8 bug where, when using Babel, exporting classes with only
281 * constructors sometimes fails. Until it's patched, this is a solution to
282 * "ApiClient is not defined" from issue #14.
283 * https://github.com/erikras/react-redux-universal-hot-example/issues/14
284 *
285 * Relevant Babel bug (but they claim it's V8): https://phabricator.babeljs.io/T2455
286 *
287 * Remove it at your own risk.
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
303exports.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;
324module.exports = exports['default'];
325//# sourceMappingURL=ApiClient.js.map
\No newline at end of file