123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- import _data from './_data';
- import _onSocket from './_onSocket';
- function stringSource(s) {
- let i = 0;
- return () => {
- return i < s.length ? s.charCodeAt(i++) : null;
- };
- }
- let params = {
- timeout: 18000,
- timeout2: 22000,
- timeoutObj: null,
- serverTimeoutObj: null,
- lockReconnect: false,
- tt: false,
- maxConnect: 3,
- status: false,
- lockSend: false
- }
- export default {
-
- methods: {
-
- getCurPage() {
- let pages = getCurrentPages();
- let curPage = pages[pages.length - 1];
- return curPage
- },
- $reconnect(cb, sendData) {
- if (params.lockReconnect || params.maxConnect <= 0 || _data.data("socket_state") > 0) {
- return false;
- }
- _data.data('socket_state', 0);
- clearTimeout(params.timeoutObj);
- clearTimeout(params.serverTimeoutObj);
- uni.closeSocket();
- params.lockReconnect = true;
- params.status = false;
- params.maxConnect--;
- if (sendData != undefined) {
- this.$socketSend(sendData, cb);
- } else {
- this.$sendWithPingToken(cb);
- }
- },
- $sendWithPingToken(cb) {
- this.$socketSend({
- 'action': 'ping',
- 'data': _data.localData('token')
- }, cb);
- },
- $reset() {
- params.status = false;
- params.lockReconnect = false;
- params.maxConnect = 3;
- params.lockSend = false;
- clearTimeout(params.timeoutObj);
- clearTimeout(params.serverTimeoutObj);
- return this;
- },
- $sendWithToken(cb) {
- if (params.lockSend) {
- return false;
- }
- params.lockSend = true;
- this.$socketSend({
- action: 'checkToken',
- data: _data.localData('token'),
- }, cb);
- },
- $start() {
- if (params.status) {
- return false;
- }
- params.status = true;
- let self = this;
- clearTimeout(params.serverTimeoutObj);
- clearTimeout(params.timeoutObj);
- params.timeoutObj = setTimeout(function() {
-
-
- self.$sendWithPingToken();
- params.serverTimeoutObj = setTimeout(function() {
- uni
- .closeSocket();;
- }, params.timeout2)
- }, params.timeout)
- },
- $requestSend(config) {
- let header = {
-
- 'Content-Type': 'application/x-www-form-urlencoded',
- };
- let send_data = ('data' in config ? config.data : {}),
- url = _data.data('http_url') + config.path;
- console.log(send_data['users']);
- send_data['_token'] = _data.localData('token');
- send_data['_agent_id'] = _data.data('agent_id');
- let _this = this;
- console.log(url)
- console.log(send_data)
- uni.request({
- url: url,
- data: send_data,
- method: ('type' in config ? config.type : 'POST'),
- header: header,
-
- success(res) {
- console.log(res);
- if (res.statusCode == 200) {
- if (('success_action' in config) && config.success_action) {
- config.success(res.data);
- } else {
- if (res.data.err) {
-
- console.log(config.path);
- if (send_data['_token'] || config.path.indexOf('/in/') > -1 || config.path
- .indexOf('/App/') > -1) {
-
- console.log(JSON.stringify(res));
- if (('fail_callback' in config) && config.fail_callback) {
- config.fail_callback(res.data);
- } else {
- uni.showToast({
- title: res.data.msg,
- duration: 1000,
- icon: "none"
- });
- }
- }
- } else {
- if (config.path.indexOf('/im/video.Share/') > -1) {
- config.success(res.data);
- return
- }
- if (config.path.indexOf('/im/remove/del_log') > -1) {
- config.success(res.data);
- return
- }
- if (config.path.indexOf('/im/circle/del') > -1) {
- config.success(res.data);
- return
- }
- if (config.path.indexOf('/im/remove/del_all') > -1) {
- config.success(res.data);
- return
- }
- if (config.path.indexOf('/im/remove/sign_out') > -1) {
- config.success(res.data);
- return
- }
- if ('success' in config) {
- config.success(res.data.data);
- }
- }
- }
- } else {
-
-
-
-
-
-
-
-
-
-
-
- }
- },
- fail(err) {
- console.log(err,'链接失败');
- if ('fail' in config) {
-
- } else {
- return;
- uni.showModal({
- content: JSON.stringify(err),
- });
- }
- }
- });
- },
- $httpSend(config) {
- let _this = this;
- _this.$requestSend(config);
- },
-
- $socketSend(send_data, cb) {
- let self = this;
-
- ((callback1, callback2) => {
- if (send_data && _data.data('socket_state')) {
- callback2(send_data);
- } else {
- callback1(callback2, send_data);
- }
- })((callback, send_data) => {
- uni.connectSocket({
- url: _data.data('socket_url'),
- header: {
- 'content-type': 'application/json',
- },
-
- method: 'GET',
- success() {
- _data.data('socket_state', 1);
- },
- fail(err) {
- self.$reset().$reconnect();
- }
- });
- uni.onSocketOpen((res) => {
- self.$reset().$start();
-
- uni.onSocketMessage((res) => {
- self.$reset().$start();
- res = JSON.parse(res.data);
- console.log("res其他消息", res);
- if (!(res.action in _onSocket)) {
- if (res.action != 'ping' && res.type != 'ping') {
-
-
-
- }
- } else {
- console.log("接收到消息:", res.action,res)
-
- _onSocket[res.action](res.data);
- }
- return;
-
- if (res.data instanceof Blob) {
-
- let reader = new FileReader();
- reader.readAsDataURL(res.data);
-
- reader.onload = function(evt) {
- let data = JSON.parse(((str) => {
-
- if (str.indexOf(',') > -1) {
- str = str.split(',')[1];
- }
- return decodeURIComponent(atob(str).split('')
- .map((c) => {
- return '%' + ('00' + c
- .charCodeAt(0).toString(
- 16)).slice(-2);
- }).join(''));
- })(evt.target.result));
- if (!(data.action in _onSocket)) {
- if (data.action != 'ping') {}
- return;
- }
- _onSocket[data.action](data.data);
- }
- }
- });
-
- callback({
- action: 'checkToken',
- data: _data.localData('token'),
- });
-
- if (send_data) {
- if (_data.localData('token')) {
- callback(send_data);
- } else {
- callback({
- action: 'checkToken',
- data: _data.localData('token'),
- });
- }
- }
- });
- uni.onSocketClose((err) => {
- _data.data('socket_state', 0);
- params.lockReconnect = false;
- self.$reconnect();
- });
- uni.onSocketError((err) => {
- _data.data('socket_state', 0);
- params.lockReconnect = false;
- self.$reconnect();
- });
- },
- (send_data) => {
- uni.sendSocketMessage({
- data: JSON.stringify(send_data),
- fail(err) {
- return;
- uni.showModal({
- content: JSON.stringify(err) + '---发送消息失败',
- });
- },
- success(res) {
- if (cb != undefined) {
- cb();
- }
- }
- });
- return true;
- });
- },
-
- $httpSendFile(config) {
- if (!config) {
- config = {};
- }
- let send_data = ('data' in config ? config.data : {});
- send_data['_token'] = _data.localData('token');
- ((callback) => {
- switch (config.type) {
-
- case 0:
- callback(config.local_url, '/im/upload/chat');
- break;
-
- case 1:
- callback(config.local_url, '/im/upload/photo');
- break;
-
- case 2:
- callback(config.local_url, '/im/upload/circle');
- break;
-
- case 3:
- callback(config.local_url, '/im/upload/circleImg');
- break;
-
- case 4:
- callback(config.local_url, '/im/upload/groupPhoto');
- break;
-
- case 6:
- callback(config.local_url, '/im/video.Share/upload');
- break;
- default:
-
-
-
- break;
- }
- })((local_url, action_path) => {
- console.log("local_url", local_url)
- let uploadTask = uni.uploadFile({
- url: (_data.data('static_url') + action_path),
- filePath: local_url,
- name: 'file',
-
- formData: send_data,
- success: (res) => {
- if (res.statusCode == 200) {
- if ('success' in config) {
- console.log(res.data);
- res.data = JSON.parse(res.data);
- if (res.data.err) {
- if ('fail' in config) {
-
- } else {
- uni.showModal({
- content: res.data.msg,
- });
- }
- } else {
- config.success(res.data.data);
- }
- }
- }
- },
- fail(err) {
- if ('fail' in config) {
-
- } else {
- uni.showModal({
- content: JSON.stringify(err),
- });
- }
- }
- });
- uploadTask.onProgressUpdate((res) => {
- if ('onProgressUpdate' in config) {
- config.onProgressUpdate();
- }
- return;
- });
- });
- },
- }
- }
|