request.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // const server = 'http://127.0.0.1:8080';
  2. const server = 'http://192.168.88.34:8080';
  3. import common from '../common/js/common.js';
  4. function get(url, data) {
  5. return uni.request({
  6. method: 'get',
  7. url: server + url,
  8. data: data || {},
  9. header: {
  10. 'Content-type': 'application/x-www-form-urlencoded',
  11. 'satoken': uni.getStorageSync('token')
  12. }
  13. }).then(data => {
  14. common.hidingLoading();
  15. var [err, res] = data
  16. if (err) {
  17. return err
  18. } else {
  19. let resp=res.data;
  20. if(resp.code==401){
  21. uni.removeStorageSync('token');
  22. common.to('/pages/login/login');
  23. return ;
  24. }
  25. return res.data
  26. }
  27. })
  28. }
  29. function postForm(url, data) {
  30. common.showLoading('正在提交...')
  31. return uni.request({
  32. method: 'post',
  33. url: server + url,
  34. data: data || {},
  35. dataType: 'json',
  36. header: {
  37. 'Content-type': 'application/x-www-form-urlencoded',
  38. 'satoken': uni.getStorageSync('token')
  39. }
  40. }).then(data => {
  41. common.hidingLoading();
  42. var [err, res] = data
  43. if (err) {
  44. return err
  45. } else {
  46. let reData = res.data;
  47. if (reData.code !== 200) {
  48. uni.showToast({
  49. "icon": 'none',
  50. "title": reData.msg,
  51. "mask": false,
  52. "duration": 3000,
  53. "position": 'bottom'
  54. })
  55. return res.data;
  56. }
  57. return res.data
  58. }
  59. })
  60. }
  61. function postJson(url, data) {
  62. common.showLoading('正在提交...')
  63. return uni.request({
  64. method: 'post',
  65. url: server + url,
  66. data: JSON.stringify(data),
  67. dataType: 'json',
  68. header: {
  69. 'Content-type': 'application/json; charset=utf-8',
  70. 'satoken': uni.getStorageSync('token')
  71. }
  72. }).then(data => {
  73. common.hidingLoading();
  74. var [err, res] = data
  75. if (err) {
  76. return err
  77. } else {
  78. let reData = res.data;
  79. if (reData.code !== 200) {
  80. uni.showToast({
  81. "icon": 'none',
  82. "title": reData.msg,
  83. "mask": false,
  84. "duration": 3000,
  85. "position": 'bottom'
  86. })
  87. return res.data;
  88. }
  89. return res.data
  90. }
  91. })
  92. }
  93. function postJsonNotLoading(url, data) {
  94. return uni.request({
  95. method: 'post',
  96. url: server + url,
  97. data: JSON.stringify(data),
  98. dataType: 'json',
  99. header: {
  100. 'Content-type': 'application/json; charset=utf-8',
  101. 'satoken': uni.getStorageSync('token')
  102. }
  103. }).then(data => {
  104. common.hidingLoading();
  105. var [err, res] = data
  106. if (err) {
  107. return err
  108. } else {
  109. let reData = res.data;
  110. if (reData.code !== 200) {
  111. uni.showToast({
  112. "icon": 'none',
  113. "title": reData.msg,
  114. "mask": false,
  115. "duration": 3000,
  116. "position": 'bottom'
  117. })
  118. return res.data;
  119. }
  120. return res.data
  121. }
  122. })
  123. }
  124. function deleteFn(url, data) {
  125. common.showLoading('正在删除...')
  126. return uni.request({
  127. method: 'delete',
  128. url: server + url,
  129. data: data || {},
  130. dataType: 'json',
  131. header: {
  132. 'Content-type': 'application/x-www-form-urlencoded',
  133. 'satoken': uni.getStorageSync('token')
  134. }
  135. }).then(data => {
  136. common.hidingLoading();
  137. var [err, res] = data
  138. if (err) {
  139. return err
  140. } else {
  141. return res.data
  142. }
  143. })
  144. }
  145. function put(url, data) {
  146. common.showLoading('正在提交...')
  147. return uni.request({
  148. method: 'put',
  149. url: server + url,
  150. data: JSON.stringify(data),
  151. dataType: 'json',
  152. header: {
  153. 'Content-type': 'application/json; charset=utf-8',
  154. 'satoken': uni.getStorageSync('token')
  155. }
  156. }).then(data => {
  157. common.hidingLoading();
  158. var [err, res] = data
  159. if (err) {
  160. return err
  161. } else {
  162. return res.data
  163. }
  164. })
  165. }
  166. export default {
  167. get,
  168. deleteFn,
  169. put,
  170. postForm,
  171. postJson,
  172. postJsonNotLoading,
  173. server
  174. }