request.js 3.5 KB

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