request.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. const devBaseUrl = "http://192.168.1.24:51106";
  2. const proBaseUrl = "http://192.168.1.24:51106";
  3. // const devBaseUrl = "http://61.163.210.204:51106";
  4. // // const proBaseUrl = "http://61.163.210.204:51106";
  5. const httpGet = (url, data, checkToken = true) => {
  6. uni.onNetworkStatusChange(function(res) {
  7. if (!res.isConnected) {
  8. uni.showToast({
  9. title: '网络连接不可用!',
  10. icon: 'none'
  11. });
  12. }
  13. return false
  14. });
  15. let token = uni.getStorageSync('erpToken');
  16. if (checkToken) {
  17. if (token == '' || token == undefined || token == null) {
  18. uni.showToast({
  19. title: '账号已过期,请重新登录',
  20. icon: 'none',
  21. complete: function() {
  22. uni.reLaunch({
  23. url: '/pages/login/index'
  24. });
  25. }
  26. });
  27. }
  28. }
  29. let baseUrl = devBaseUrl;
  30. if (process.env.NODE_ENV === 'production') {
  31. baseUrl = proBaseUrl
  32. }
  33. console.log(baseUrl);
  34. let httpDefaultOpts = {
  35. url: baseUrl + url,
  36. data: data,
  37. method: 'GET',
  38. header: {
  39. 'Access-Token': token,
  40. 'X-Requested-With': 'XMLHttpRequest',
  41. "Accept": "application/json",
  42. "Content-Type": "application/json; charset=UTF-8"
  43. },
  44. dataType: 'json',
  45. }
  46. let promise = new Promise(function(resolve, reject) {
  47. uni.request(httpDefaultOpts).then(
  48. res => {
  49. resolve(res[1].data);
  50. }
  51. ).catch(
  52. (response) => {
  53. reject(response)
  54. }
  55. )
  56. })
  57. return promise
  58. };
  59. const httpPost = (url, data, checkToken = true) => {
  60. uni.onNetworkStatusChange(function(res) {
  61. if (!res.isConnected) {
  62. uni.showToast({
  63. title: '网络连接不可用!',
  64. icon: 'none'
  65. });
  66. }
  67. return false
  68. });
  69. let token = uni.getStorageSync('erpToken');
  70. if (checkToken) {
  71. if (token == '' || token == undefined || token == null) {
  72. uni.showToast({
  73. title: '账号已过期,请重新登录',
  74. icon: 'none',
  75. complete: function() {
  76. uni.reLaunch({
  77. url: '/pages/login/login'
  78. });
  79. }
  80. });
  81. }
  82. }
  83. let baseUrl = devBaseUrl;
  84. if (process.env.NODE_ENV === 'production') {
  85. baseUrl = proBaseUrl
  86. }
  87. let httpDefaultOpts = {
  88. url: baseUrl + url,
  89. data: data,
  90. method: 'POST',
  91. header: {
  92. 'Access-Token': token,
  93. 'X-Requested-With': 'XMLHttpRequest',
  94. "Accept": "application/json",
  95. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  96. },
  97. dataType: 'json',
  98. }
  99. let promise = new Promise(function(resolve, reject) {
  100. uni.request(httpDefaultOpts).then(
  101. (res) => {
  102. if (res[1].data.httpCode == 401) {
  103. uni.reLaunch({
  104. url: '/pages/login/login'
  105. });
  106. }
  107. resolve(res[1].data);
  108. }
  109. ).catch(
  110. (response) => {
  111. reject(response)
  112. }
  113. )
  114. })
  115. return promise
  116. };
  117. const httpPostBody = (url, data, checkToken = true) => {
  118. uni.onNetworkStatusChange(function(res) {
  119. if (!res.isConnected) {
  120. uni.showToast({
  121. title: '网络连接不可用!',
  122. icon: 'none'
  123. });
  124. }
  125. return false
  126. });
  127. let token = uni.getStorageSync('erpToken');
  128. if (checkToken) {
  129. if (token == '' || token == undefined || token == null) {
  130. uni.showToast({
  131. title: '账号已过期,请重新登录',
  132. icon: 'none',
  133. complete: function() {
  134. uni.reLaunch({
  135. url: '/pages/login/index'
  136. });
  137. }
  138. });
  139. return;
  140. }
  141. }
  142. let baseUrl = devBaseUrl;
  143. if (process.env.NODE_ENV === 'production') {
  144. baseUrl = proBaseUrl
  145. }
  146. let httpDefaultOpts = {
  147. url: baseUrl + url,
  148. data: data,
  149. method: 'POST',
  150. header: {
  151. 'Access-Token': token,
  152. 'X-Requested-With': 'XMLHttpRequest',
  153. "Accept": "application/json",
  154. "Content-Type": "application/json; charset=UTF-8"
  155. },
  156. dataType: 'json',
  157. }
  158. let promise = new Promise(function(resolve, reject) {
  159. uni.request(httpDefaultOpts).then(
  160. (res) => {
  161. resolve(res[1].data);
  162. }
  163. ).catch(
  164. (response) => {
  165. reject(response)
  166. }
  167. )
  168. })
  169. return promise
  170. };
  171. export default {
  172. httpGet,
  173. httpPost,
  174. httpPostBody
  175. }