request.js 3.8 KB

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