var businessT = require('business-tool.js') var businessType = require('../public/business-type.js') var sysT = require('sys-tool.js') var header = { 'content-type': 'application/x-www-form-urlencoded', 'X-UA': sysT.sysInfo().system.indexOf("iOS") == 0 ? 'xcx_iOS#22' : 'xcx_Android#22' } var header2 = { 'content-type': 'multipart/form-data', 'X-UA': sysT.sysInfo().system.indexOf("iOS") == 0 ? 'xcx_iOS#22' : 'xcx_Android#22' } var app = getApp() const timeOutMinute = 60; const networkCallbackSuccessCode = 200; const networkCallbackNeedLogin = 10004; const networkCallbackKeyInvalid = 601; class Net { constructor() { this.timeOut = 0 } uploadImage(obj, flag) { if (app.loginStatus.key == '') { this.waitLogin(false, obj, () => { this.uploadImages(obj, flag) }) } else { obj.data.key = app.loginStatus.key swan.uploadFile({ url: obj.url, filePath: obj.imgPath, name: obj.imgName, formData: obj.data, header: header2, success: res => { this.handleCallbackData(res, obj, flag, () => { this.waitLogin(true, obj, () => { this.uploadImage(obj, flag) }) }) }, fail: e => { this.handleFail(null, null, obj.fail) }, complete: obj.complete }) } } //obj:参数,flg:是否需检查数据,外部调用不用传 connectNeedLogin(obj, flag) { if (app.loginStatus.key == '') { this.waitLogin(false, obj, () => { this.connectNeedLogin(obj, flag) }) } else { obj.data.key = app.loginStatus.key swan.request({ url: obj.url, data: obj.data, method: 'POST', header: header, success: res => { businessT.debugLog('success') businessT.debugLog(obj.url) businessT.debugLog(res) this.handleCallbackData(res, obj, flag, () => { this.waitLogin(true, obj, () => { this.connectNeedLogin(obj, flag) }) }) }, fail: e => { this.handleFail(null, null, obj.fail) }, complete: obj.complete }) } } //obj:参数,flg:是否需检查数据 connect(obj, flag) { swan.request({ url: obj.url, data: obj.data, method: obj.method, header: header, success: res => { // businessT.debugLog('success') // businessT.debugLog(obj.url) // businessT.debugLog(res.data) this.handleCallbackData(res, obj, flag, () => { }) }, fail: e => { this.handleFail(null, null, obj.fail) }, complete: obj.complete }) } //flag是否触发自动登录,obj参数 waitLogin(flag, obj, circulationAction) { if (this.timeOut > timeOutMinute || !app.networkStatus.connected) { this.timeOut = 0 this.handleFail(null, null, obj.fail) } else { if (flag) { var login = require('../public/login.js') login.autoLogin() } this.timeOut = this.timeOut + 2 businessT.debugLog(this.timeOut) setTimeout(function () { circulationAction() }.bind(this), 2000) } } handleCallbackData(res, obj, flag, circulationAction) { //重算自动登录时间 // var login = require('../public/login.js') // login.recalculateAutoLoginTime() // businessT.debugLog(login) businessT.debugLog(res.data) if (res.data) { if (typeof (res.data) == 'string') { res.data = JSON.parse(res.data) } //改造数据 if (res.data.code == networkCallbackSuccessCode) { var currentPage = obj.data.page || '1' res.data.currentPage = currentPage if (!res.data.msg) { delete res.data.msg } if (!res.data.data) { delete res.data.data } } if (res.data.code == networkCallbackSuccessCode && (!flag || res.data.data)) { var err = { code: businessType.NetworkErrorCode.success, msg: '成功' } obj.success(err, res.data) } else if (res.data.code == networkCallbackNeedLogin || res.data.code == networkCallbackKeyInvalid) { circulationAction() } else { this.timeOut = 0 this.handleFail(res.data.msg, res.data, obj.fail) } } else { this.timeOut = 0 this.handleFail(null, null, obj.fail) } } handleFail(message, data, failAction) { if (!failAction) { return } var err if (!app.networkStatus.connected) { err = { code: businessType.NetworkErrorCode.netwrokMiss, msg: '网络连接不稳定' } } else if (message) { err = { code: businessType.NetworkErrorCode.connectErrorMessage, msg: message } } else { err = { code: businessType.NetworkErrorCode.connectError, msg: '' } } failAction(err, data) } } export default Net