http.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. export default {
  2. //通用Post请求方法
  3. Post: function(url, data = {}, isloading = false) {
  4. var promise = new Promise(function(resolve, reject) {
  5. //判断是否需要显示加载状态
  6. if (isloading) {
  7. uni.showLoading({
  8. title: '加载中...'
  9. })
  10. }
  11. uni.request({
  12. url: url,
  13. data: data,
  14. method: 'POST',
  15. sslVerify: false,
  16. header: {
  17. 'content-type': 'application/json',
  18. // 'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : '',
  19. },
  20. success: function(data) {
  21. if (isloading) {
  22. uni.hideLoading()
  23. }
  24. if (data.data.code == 1) {
  25. resolve(data.data);
  26. } else if (data.data.code == 0) {
  27. uni.showModal({
  28. title: '系统提示',
  29. content: data.data.msg,
  30. showCancel: false,
  31. success() {
  32. resolve(data.data);
  33. }
  34. })
  35. }
  36. },
  37. fail: function(data) {
  38. if (isloading) {
  39. uni.hideLoading();
  40. }
  41. uni.showModal({
  42. title: '系统提示',
  43. content: data.errMsg,
  44. showCancel: false
  45. });
  46. reject(data.errMsg);
  47. }
  48. });
  49. });
  50. return promise;
  51. },
  52. //通用Get请求方法
  53. Get: function(url, data = {}, isloading = false) {
  54. var promise = new Promise(function(resolve, reject) {
  55. //判断是否需要显示加载状态
  56. if (isloading) {
  57. uni.showLoading({
  58. title: '加载中...'
  59. })
  60. }
  61. uni.request({
  62. url: url,
  63. data: data,
  64. method: 'GET',
  65. header: {
  66. 'content-type': 'application/json',
  67. 'Authorization': uni.getStorageSync('userinfo').token
  68. },
  69. success: function(data) {
  70. if (isloading) {
  71. uni.hideLoading();
  72. }
  73. if (data.statusCode == 200) {
  74. resolve(data.data);
  75. } else {
  76. uni.showModal({
  77. title: '系统提示',
  78. content: '服务器请求错误',
  79. showCancel: false,
  80. });
  81. reject('服务器请求错误');
  82. }
  83. },
  84. fail: function(data) {
  85. if (isloading) {
  86. uni.hideLoading();
  87. }
  88. uni.showModal({
  89. title: '系统提示',
  90. content: data.errMsg,
  91. showCancel: false
  92. });
  93. reject(data.errMsg);
  94. }
  95. })
  96. });
  97. return promise;
  98. },
  99. //下载文件接口
  100. DownLoad: function(url, isloading = false) {
  101. var promise = new Promise(function(resolve, reject) {
  102. //判断是否需要显示加载状态
  103. if (isloading) {
  104. uni.showLoading({
  105. title: '加载中...'
  106. })
  107. }
  108. uni.downloadFile({
  109. url: url,
  110. success(res) {
  111. if (res.statusCode === 200) {
  112. if (isloading) {
  113. uni.hideLoading();
  114. }
  115. resolve(res.tempFilePath);
  116. } else {
  117. if (isloading) {
  118. uni.hideLoading();
  119. }
  120. reject('服务器请求错误');
  121. }
  122. }
  123. });
  124. });
  125. return promise;
  126. },
  127. //发起支付
  128. uniPay: function(data) {
  129. var promise = new Promise(function(resolve, reject) {
  130. //开始发起支付
  131. uni.requestPayment({
  132. timeStamp: String(data.timeStamp),
  133. nonceStr: data.nonceStr,
  134. package: data.package,
  135. signType: data.signType,
  136. paySign: data.paySign,
  137. success: function(res) {
  138. resolve(res);
  139. },
  140. fail: function(res) {
  141. reject(res);
  142. },
  143. complete: function(res) {}
  144. });
  145. });
  146. return promise;
  147. }
  148. }