net.js 4.9 KB

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