request.js 3.5 KB

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