net.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var businessT = require('business-tool.js')
  2. var businessType = require('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(obj.data)
  85. businessT.debugLog(res.data)
  86. this.handleCallbackData(res, obj, flag, () => {
  87. })
  88. },
  89. fail: e => {
  90. this.handleFail(null, null, obj.fail)
  91. },
  92. complete: obj.complete
  93. })
  94. }
  95. //flag是否触发自动登录,obj参数
  96. waitLogin(flag, obj, circulationAction) {
  97. if (this.timeOut > timeOutMinute || !app.networkStatus.connected) {
  98. this.timeOut = 0
  99. this.handleFail(null, null, obj.fail)
  100. } else {
  101. if (flag) {
  102. var login = require('../public/login.js')
  103. login.autoLogin()
  104. }
  105. this.timeOut = this.timeOut + 2
  106. businessT.debugLog(this.timeOut)
  107. setTimeout(function () {
  108. circulationAction()
  109. }.bind(this), 2000)
  110. }
  111. }
  112. handleCallbackData(res, obj, flag, circulationAction) {
  113. //重算自动登录时间
  114. // var login = require('../public/login.js')
  115. // login.recalculateAutoLoginTime()
  116. // businessT.debugLog(login)
  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