net.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var businessT = require('business-tool.js')
  2. var businessType = require('../public/business-type.js')
  3. var sysT = require('sys-tool.js')
  4. var header = {
  5. 'content-type': 'application/x-www-form-urlencoded',
  6. 'X-UA': sysT.sysInfo().system.indexOf("iOS") == 0 ? 'xcx_iOS#22' : 'xcx_Android#22'
  7. }
  8. var app = getApp()
  9. const timeOutMinute = 60;
  10. const networkCallbackSuccessCode = 200;
  11. const networkCallbackNeedLogin = 10004;
  12. const networkCallbackKeyInvalid = 601;
  13. class Net {
  14. constructor() {
  15. this.timeOut = 0
  16. }
  17. uploadImage(obj, flag) {
  18. if (app.loginStatus.key == '') {
  19. this.waitLogin(false, obj, () => {
  20. this.uploadImages(obj, img, flag)
  21. })
  22. } else {
  23. obj.data.key = app.loginStatus.key
  24. wx.uploadFile({
  25. url: obj.url,
  26. filePath: obj.imgPath,
  27. name: obj.imgName,
  28. formData: obj.data,
  29. header: header,
  30. success: res => {
  31. this.handleCallbackData(res, obj, flag, () => {
  32. this.waitLogin(true, obj, () => {
  33. this.uploadImage(obj, flag)
  34. })
  35. })
  36. },
  37. fail: e => {
  38. this.handleFail(null, null, obj.fail)
  39. },
  40. complete: obj.complete
  41. })
  42. }
  43. }
  44. //obj:参数,flg:是否需检查数据,外部调用不用传
  45. connectNeedLogin(obj, flag) {
  46. if (app.loginStatus.key == '') {
  47. this.waitLogin(false, obj, () => {
  48. this.connectNeedLogin(obj, flag)
  49. })
  50. } else {
  51. obj.data.key = app.loginStatus.key
  52. wx.request({
  53. url: obj.url,
  54. data: obj.data,
  55. method: 'POST',
  56. header: header,
  57. success: res => {
  58. businessT.debugLog('success')
  59. businessT.debugLog(obj.url)
  60. businessT.debugLog(res)
  61. this.handleCallbackData(res, obj, flag, () => {
  62. this.waitLogin(true, obj, () => {
  63. this.connectNeedLogin(obj, flag)
  64. })
  65. })
  66. },
  67. fail: e => {
  68. this.handleFail(null, null, obj.fail)
  69. },
  70. complete: obj.complete
  71. })
  72. }
  73. }
  74. //obj:参数,flg:是否需检查数据
  75. connect(obj, flag) {
  76. wx.request({
  77. url: obj.url,
  78. data: obj.data,
  79. method: obj.method,
  80. header: header,
  81. success: res => {
  82. businessT.debugLog('success')
  83. businessT.debugLog(obj.url)
  84. businessT.debugLog(res.data)
  85. this.handleCallbackData(res, obj, flag, () => {
  86. })
  87. },
  88. fail: e => {
  89. this.handleFail(null, null, obj.fail)
  90. },
  91. complete: obj.complete
  92. })
  93. }
  94. //flag是否触发自动登录,obj参数
  95. waitLogin(flag, obj, circulationAction) {
  96. if (this.timeOut > timeOutMinute || !app.networkStatus.connected) {
  97. this.timeOut = 0
  98. this.handleFail(null, null, obj.fail)
  99. } else {
  100. if (flag) {
  101. var login = require('../public/login.js')
  102. login.autoLogin()
  103. }
  104. this.timeOut = this.timeOut + 2
  105. businessT.debugLog(this.timeOut)
  106. setTimeout(function () {
  107. circulationAction()
  108. }.bind(this), 2000)
  109. }
  110. }
  111. handleCallbackData(res, obj, flag, circulationAction) {
  112. //重算自动登录时间
  113. var login = require('../public/login.js')
  114. login.recalculateAutoLoginTime()
  115. businessT.debugLog(login)
  116. businessT.debugLog(res.data)
  117. if (res.data) {
  118. if (typeof (res.data) == 'string') {
  119. res.data = JSON.parse(res.data)
  120. }
  121. //改造数据
  122. if (res.data.code == networkCallbackSuccessCode) {
  123. var currentPage = obj.data.page || '1'
  124. res.data.currentPage = currentPage
  125. if (!res.data.msg) {
  126. delete res.data.msg
  127. }
  128. if (!res.data.data) {
  129. delete res.data.data
  130. }
  131. }
  132. if (res.data.code == networkCallbackSuccessCode && (!flag || res.data.data)) {
  133. var err = {
  134. code: businessType.NetworkErrorCode.success,
  135. msg: '成功'
  136. }
  137. obj.success(err, res.data)
  138. } else if (res.data.code == networkCallbackNeedLogin || res.data.code == networkCallbackKeyInvalid) {
  139. circulationAction()
  140. } else {
  141. this.timeOut = 0
  142. this.handleFail(res.data.msg, res.data, obj.fail)
  143. }
  144. } else {
  145. this.timeOut = 0
  146. this.handleFail(null, null, obj.fail)
  147. }
  148. }
  149. handleFail(message, data, failAction) {
  150. if (!failAction) {
  151. return
  152. }
  153. var err
  154. if (!app.networkStatus.connected) {
  155. err = {
  156. code: businessType.NetworkErrorCode.netwrokMiss,
  157. msg: '网络连接不稳定'
  158. }
  159. } else if (message) {
  160. err = {
  161. code: businessType.NetworkErrorCode.connectErrorMessage,
  162. msg: message
  163. }
  164. } else {
  165. err = {
  166. code: businessType.NetworkErrorCode.connectError,
  167. msg: ''
  168. }
  169. }
  170. businessT.debugLog('dddd')
  171. failAction(err, data)
  172. }
  173. }
  174. export default Net