request.js 3.4 KB

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