|
@@ -26,24 +26,25 @@ var http = {
|
|
|
},
|
|
|
|
|
|
|
|
|
- request: function(vue, url, options, callback, err_callback) {
|
|
|
- var self = this;
|
|
|
- if (vue == 'post') {
|
|
|
+ request: function(vue, url, options, callback, err_callback, type) {
|
|
|
+ var self = this;
|
|
|
+ if (vue == 'post') {
|
|
|
config.request.method = 'POST';
|
|
|
- config.request.method = 'GET';
|
|
|
- vue = false;
|
|
|
- } else {
|
|
|
- config.request.method = 'GET';
|
|
|
- }
|
|
|
- if (typeof(url) == 'object') {
|
|
|
- vue = url[1];
|
|
|
- url = url[0];
|
|
|
- }
|
|
|
- if (typeof(vue) == 'object') {
|
|
|
- vue.isDisabled = true;
|
|
|
+
|
|
|
+ vue = false;
|
|
|
+ } else {
|
|
|
+ config.request.method = 'GET';
|
|
|
+ }
|
|
|
+ var method = config.request.method;
|
|
|
+ if (typeof(url) == 'object') {
|
|
|
+ vue = url[1];
|
|
|
+ url = url[0];
|
|
|
+ }
|
|
|
+ if (typeof(vue) == 'object') {
|
|
|
+ vue.isDisabled = true;
|
|
|
}
|
|
|
|
|
|
- self.core(url, options).then((result, state) => {
|
|
|
+ self.core(url, options, type, method).then((result, state) => {
|
|
|
if (typeof(vue) == 'object') {
|
|
|
if (!vue.fetch) {
|
|
|
vue.fetch = {}
|
|
@@ -71,7 +72,7 @@ var http = {
|
|
|
})
|
|
|
},
|
|
|
|
|
|
- core : function(url, options) {
|
|
|
+ core : function(url, options, type, method) {
|
|
|
var self = this;
|
|
|
options = this.option(options);
|
|
|
if (!options.noconcat) {
|
|
@@ -88,7 +89,11 @@ var http = {
|
|
|
loading_method = 'page';
|
|
|
}
|
|
|
if (config.request.url.indexOf('http') == -1) {
|
|
|
- config.request.url = config.request.host + config.request.url;
|
|
|
+ if (type == 'im') {
|
|
|
+ config.request.url = config.im.api + config.request.url;
|
|
|
+ } else {
|
|
|
+ config.request.url = config.request.host + config.request.url;
|
|
|
+ }
|
|
|
}
|
|
|
config.request.data = options;
|
|
|
return new Promise((resolve, reject) => {
|
|
@@ -221,49 +226,100 @@ var http = {
|
|
|
}
|
|
|
|
|
|
var im = {
|
|
|
- socket : false,
|
|
|
- url : 'ws://me.5dev.cn:8282',
|
|
|
- init : function(url, callback, connect) {
|
|
|
- if (url) {
|
|
|
- this.url = url;
|
|
|
- } else {
|
|
|
- this.url = config.im.host;
|
|
|
+ socket : {},
|
|
|
+ status : false,
|
|
|
+
|
|
|
+ url : 'ws://me.5dev.cn:8282',
|
|
|
+
|
|
|
+ push_path : '',
|
|
|
+ init : function(key, url, callback, connect) {
|
|
|
+ if (url) {
|
|
|
+ this.url = url;
|
|
|
+ } else {
|
|
|
+ this.url = config.im.host;
|
|
|
}
|
|
|
- this.connect(connect);
|
|
|
- this.info(callback);
|
|
|
+ this.connect(key, callback, connect);
|
|
|
},
|
|
|
|
|
|
- connect : function(connect) {
|
|
|
- uni.connectSocket({
|
|
|
+ connect : function(key, callback, connect) {
|
|
|
+ var self = this;
|
|
|
+ this.close(key);
|
|
|
+ this.socket[key] = uni.connectSocket({
|
|
|
url: this.url,
|
|
|
- complete: (res)=> {
|
|
|
+ complete: (res)=> {
|
|
|
|
|
|
}
|
|
|
});
|
|
|
- uni.onSocketOpen(function (res) {
|
|
|
- if (connect) {
|
|
|
- connect(true, res);
|
|
|
- }
|
|
|
+ this.socket[key].onOpen(function (res) {
|
|
|
+ if (connect) {
|
|
|
+ connect(true, res);
|
|
|
+ }
|
|
|
+ console.info('open');
|
|
|
+ self.status = true;
|
|
|
+
|
|
|
+ self.socket[key].onMessage(function (res) {
|
|
|
+ if (res.data.indexOf('{') != -1) {
|
|
|
+ var data = eval("("+res.data+")");
|
|
|
+ } else {
|
|
|
+ var data = res.data;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (callback) {
|
|
|
+ callback(data);
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
- uni.onSocketError(function (res) {
|
|
|
- if (connect) {
|
|
|
- connect(false, res);
|
|
|
- }
|
|
|
+ this.socket[key].onClose(function (res) {
|
|
|
+ if (connect) {
|
|
|
+ connect(false, res);
|
|
|
+ }
|
|
|
+ console.info('close');
|
|
|
+ self.status = false;
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
+ this.socket[key].onError(function (res) {
|
|
|
+ console.info('error');
|
|
|
+ self.connect(callback, connect);
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ close : function(key) {
|
|
|
+ var self = this;
|
|
|
+ if (this.socket[key]) {
|
|
|
+ this.socket[key].close({
|
|
|
+ success(res) {
|
|
|
+ self.status = false;
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ self.status = true;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
- info : function(callback) {
|
|
|
- uni.onSocketMessage(function (res) {
|
|
|
- if (res.data.indexOf('{') != -1) {
|
|
|
- var data = eval("("+res.data+")");
|
|
|
- } else {
|
|
|
- var data = res.data;
|
|
|
- }
|
|
|
-
|
|
|
- if (callback) {
|
|
|
- callback(data);
|
|
|
+ get : function(vue, url, options, callback, err_callback) {
|
|
|
+ this.request(vue, url, options, callback, err_callback);
|
|
|
+ },
|
|
|
+
|
|
|
+ post : function(url, options, callback, err_callback) {
|
|
|
+ this.request('post', url, options, callback, err_callback);
|
|
|
+ },
|
|
|
+
|
|
|
+ request : function(vue, url, options, callback, err_callback) {
|
|
|
+ if (!options) {
|
|
|
+ options = {};
|
|
|
+ }
|
|
|
+ var param = options;
|
|
|
+ param.noloading = 1;
|
|
|
+ dever.post(this.push_path, param, function(t) {
|
|
|
+ url += '?timestamp=' + t.timestamp + '&pop_token=' + t.token + '&login_uid=' + t.login_uid;
|
|
|
+ dever.config.im = t;
|
|
|
+ options.login_uid = t.login_uid;
|
|
|
+ if (t.friend_uid) {
|
|
|
+ options.friend_uid = t.friend_uid;
|
|
|
}
|
|
|
+ delete options.noloading;
|
|
|
+ http.request(vue, url, options, callback, err_callback, 'im');
|
|
|
});
|
|
|
}
|
|
|
}
|
|
@@ -1198,9 +1254,24 @@ var dever = {
|
|
|
return upload;
|
|
|
},
|
|
|
|
|
|
-
|
|
|
- im : function(url, callback, connect) {
|
|
|
- im.init(url, callback, connect);
|
|
|
+
|
|
|
+ im : function(key, url, callback, connect) {
|
|
|
+ im.init(key, url, callback, connect);
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ im_close : function(key) {
|
|
|
+ im.close(key);
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ im_get : function(vue, url, options, callback, err_callback) {
|
|
|
+ im.get(vue, url, options, callback, err_callback);
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ im_post : function(url, options, callback, err_callback) {
|
|
|
+ im.post(url, options, callback, err_callback);
|
|
|
},
|
|
|
|
|
|
|