request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import util from '@/common/we7_js/util.js'
  2. import App from '../App'
  3. module.exports = {
  4. // get请求后台数据
  5. get: function(url, data) {
  6. return new Promise((resolve, reject) => {
  7. util.request({
  8. 'url': 'entry/wxapp/' + url,
  9. 'showLoading': data ? data.showLoading : false,
  10. 'data': data,
  11. 'method': 'get',
  12. 'cachetime': '30',
  13. success: function(res) {
  14. resolve(res.data)
  15. }
  16. });
  17. })
  18. },
  19. // post请求后台数据
  20. post: function(url, data) {
  21. return new Promise((resolve, reject) => {
  22. util.request({
  23. 'url': 'entry/wxapp/' + url,
  24. 'data': data,
  25. 'showLoading': data ? data.showLoading : false,
  26. 'method': 'post',
  27. 'cachetime': '30',
  28. success: function(res) {
  29. resolve(res.data)
  30. }
  31. });
  32. })
  33. },
  34. uploadFile: function(path) {
  35. var formvar = 'wxapp';
  36. var url = '';
  37. //#ifdef H5
  38. formvar = 'mp';
  39. //#endif
  40. url = App.siteInfo.siteroot + '?i=' + App.siteInfo.uniacid +
  41. '&c=utility&a=file&do=upload&thumb=0&from=' + formvar;
  42. //#ifdef H5
  43. let urlquery = getQuery(window.location.href);
  44. if (urlquery.length > 0) {
  45. var urli = '';
  46. for (let i = 0; i < urlquery.length; i++) {
  47. if (urlquery[i] && urlquery[i].name && urlquery[i].value) {
  48. if (urlquery[i].name == "i") {
  49. urli = urlquery[i].name + '=' + urlquery[i].value;
  50. }
  51. }
  52. }
  53. if (urli) {
  54. url = window.location.protocol + '//' + window.location.host +
  55. '/app/index.php?c=utility&a=file&do=upload&thumb=0&from=' + formvar + '&' + urli;
  56. }
  57. }
  58. //#endif
  59. url = url + '&m=' + App.module;
  60. return new Promise((resolve, reject) => {
  61. var FilePaths = path;
  62. console.log(FilePaths);
  63. uni.uploadFile({
  64. url: url,
  65. method: 'POST',
  66. // #ifdef MP-WEIXIN
  67. filePath: FilePaths,
  68. formData: {
  69. file: FilePaths
  70. },
  71. header: {
  72. "Content-Type": "multipart/form-data"
  73. },
  74. // #endif
  75. // #ifdef H5
  76. filePath: FilePaths,
  77. formData: {
  78. file: FilePaths
  79. },
  80. // #endif
  81. name: 'file',
  82. success: (res) => {
  83. resolve(JSON.parse(res.data));
  84. //resolve(res)
  85. console.info("服务器返回的图片路径是:" + res.data)
  86. }
  87. });
  88. });
  89. }
  90. }
  91. function getQuery(url) {
  92. var theRequest = [];
  93. if (url.indexOf("?") != -1) {
  94. var str = url.split('?')[1];
  95. if (str.indexOf("#") != -1) {
  96. str = str.split('#')[0]
  97. }
  98. var strs = str.split("&");
  99. for (var i = 0; i < strs.length; i++) {
  100. if (strs[i].split("=")[0] && unescape(strs[i].split("=")[1])) {
  101. theRequest[i] = {
  102. 'name': strs[i].split("=")[0],
  103. 'value': unescape(strs[i].split("=")[1])
  104. }
  105. }
  106. }
  107. }
  108. return theRequest;
  109. }