request.js 3.5 KB

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