request.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import http from './http.js'
  2. export default {
  3. // 请求方法
  4. requestEn(url, data = {}, methods) {
  5. return new Promise((resolve, reject) => {
  6. uni.request({
  7. url: http.webUrl + url,
  8. method: methods ? methods : 'GET',
  9. data: data,
  10. success: res => {
  11. if (res.data.code === 200) {
  12. resolve(res.data.data === undefined ? null : res.data.data)
  13. } else if (res.data.code === 403 ||res.data.code === 401 ) {
  14. uni.showToast({
  15. title: 'Token超时,请重新登录',
  16. icon: 'none'
  17. })
  18. if (uni.getStorageSync('token')) {
  19. uni.removeStorage({
  20. key: 'token',
  21. success: () => {
  22. uni.navigateTo({
  23. url: '/pages/login/login'
  24. })
  25. }
  26. })
  27. } else {
  28. uni.navigateTo({
  29. url: '/pages/login/login'
  30. })
  31. }
  32. } else {
  33. uni.showToast({
  34. title: res.data.msg ? res.data.msg : '系统异常',
  35. icon: 'none'
  36. })
  37. }
  38. },
  39. fail: () => {
  40. uni.showToast({
  41. title: '网络异常',
  42. icon: 'none'
  43. })
  44. }
  45. })
  46. })
  47. },
  48. // 请求方法
  49. requestFn(url, data = {}, methods) {
  50. return new Promise((resolve, reject) => {
  51. console.log(http.webUrl + url +'---'+ uni.getStorageSync('token'))
  52. uni.request({
  53. url: http.webUrl + url,
  54. method: methods ? methods : 'GET',
  55. data: data,
  56. header: {
  57. "WxAuthorization": `wxBearer ${uni.getStorageSync('token') ? uni.getStorageSync('token') : ''}`,
  58. "userType":'MOdBILE'
  59. },
  60. success: res => {
  61. console.log(res)
  62. if (res.data.code === 200) {
  63. resolve(res.data.data === undefined ? null : res.data.data)
  64. } else if (res.data.code === 403 ||res.data.code === 401 ) {
  65. uni.showToast({
  66. title: 'Token超时,请重新登录',
  67. icon: 'none'
  68. })
  69. if (uni.getStorageSync('token')) {
  70. uni.removeStorage({
  71. key: 'token',
  72. success: () => {
  73. uni.navigateTo({
  74. url: '/pages/login/login'
  75. })
  76. }
  77. })
  78. } else {
  79. uni.navigateTo({
  80. url: '/pages/login/login'
  81. })
  82. }
  83. } else {
  84. uni.showToast({
  85. title: res.data.msg ? res.data.msg : '系统异常',
  86. icon: 'none'
  87. })
  88. }
  89. },
  90. fail: () => {
  91. uni.showToast({
  92. title: '网络异常',
  93. icon: 'none'
  94. })
  95. }
  96. })
  97. })
  98. },
  99. requestLo(url, data = {}, methods) {
  100. return new Promise((resolve, reject) => {
  101. console.log(http.webUrl + url)
  102. uni.request({
  103. url: http.webUrl + url,
  104. method: methods ? methods : 'GET',
  105. data: data,
  106. header: {
  107. "userType":'MOBILE'
  108. },
  109. success: res => {
  110. console.log(res)
  111. if (res.data.code === 200 || res.data.code === 500) {
  112. resolve(res.data === undefined ? null : res.data)
  113. } else if (res.data.code === 403) {
  114. uni.showToast({
  115. title: res.data.msg ? res.data.msg : 'Token超时,请重新登录',
  116. icon: 'none'
  117. })
  118. if (uni.getStorageSync('token')) {
  119. uni.removeStorage({
  120. key: 'token',
  121. success: () => {
  122. uni.navigateTo({
  123. url: '/pages/login/login'
  124. })
  125. }
  126. })
  127. } else {
  128. uni.navigateTo({
  129. url: '/pages/login/login'
  130. })
  131. }
  132. } else {
  133. uni.showToast({
  134. title: res.data.msg ? res.data.msg : '系统异常',
  135. icon: 'none'
  136. })
  137. }
  138. },
  139. fail: () => {
  140. uni.showToast({
  141. title: '网络异常',
  142. icon: 'none'
  143. })
  144. }
  145. })
  146. })
  147. }
  148. }