| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- Ext.define('uas.override.data.Connection', {
- override: 'Ext.data.Connection',
- urlRegexp: /(http|ftp|https):\/\//,
- privates: {
- setupServerOptions: function (options) {
- var serverOptions = Ext.manifest.server, originUrl = options.url;
- if (serverOptions && serverOptions.url && !this.urlRegexp.test(originUrl) &&
- (!serverOptions.urlPattern || new RegExp(serverOptions.urlPattern).test(originUrl))) {
- Ext.Object.merge(options, {
- url: serverOptions.url + (originUrl.indexOf('/') == 0 ? '' : '/') + originUrl
- });
- }
- },
- parseResponseStatus: function (status) {
- switch (status) {
- case 0:
- return "无法连接服务";
- }
- }
- },
- request: function (options) {
- if (options && options.url) {
- this.setupServerOptions(options);
- }
- return this.callParent([options]);
- },
- promise: function (config) {
- var me = this, url = config.url,
- params = config.params,
- async = (Ext.isBoolean(config.async) ? config.async : true),
- method = config.method || 'GET',
- timeout = config.timeout || 8000,
- defaultHeaders = {
- 'Access-Control-Allow-Origin': '*',
- 'Content-Type': 'application/json;charset=UTF-8'
- };
- return new Ext.Promise(function (resolve, reject) {
- Ext.Ajax.request({
- url: url,
- params: params,
- async: async,
- method: method,
- timeout: timeout,
- headers: Ext.apply(defaultHeaders, config.headers),
- success: function (response) {
- var res = Ext.decode(response.responseText);
- if (res.success) {
- return resolve(res);
- } else {
- res.message = res.message || '未知错误';
- return reject(res);
- }
- },
- failure: function (response) {
- var res, status = response.status;
- try {
- res = JSON.parse(response.responseText);
- } catch (e) {
- res = response.responseJson || {
- message: me.parseResponseStatus(status)
- };
- }
- res.message = res.message || '未知错误';
- reject(res);
- }
- });
- })
- },
- /**
- * form post
- *
- * @param {*} config
- */
- form: function (config) {
- config.headers = config.headers || {};
- Ext.apply(config.headers, {
- 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
- });
- config.method = 'POST';
- return this.promise(config);
- },
- /**
- * payload
- *
- * @param {*} config
- */
- payload: function (config) {
- if (Ext.isObject(config.params)) {
- config.params = JSON.stringify(config.params);
- }
- config.method = 'POST';
- return this.promise(config);
- }
- });
|