request.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // API配置信息
  2. import urls from '../common/config.js'
  3. /**
  4. * 构建完整请求URL
  5. * @param {Object} path
  6. */
  7. function buildUrl(path) {
  8. // 完整接口地址返回
  9. if (path.startsWith('http://') || path.startsWith('https://')) {
  10. return path;
  11. }
  12. return urls.api + path;
  13. }
  14. /**
  15. * 通用请求封装
  16. * @param {Object} path
  17. * @param {Object} data
  18. */
  19. function postData(path, data) {
  20. // 构建完整请求
  21. const url = buildUrl(path)
  22. return new Promise((resolve, reject) => {
  23. uni.request({
  24. url: url,
  25. data: data,
  26. method: "POST",
  27. header: {
  28. 'content-type': "application/json",
  29. 'token': uni.getStorageSync("token"),
  30. 'mac': uni.getStorageSync("mac") // APP.vue页面的mac地址
  31. },
  32. success: function(res) {
  33. // 错误500
  34. if (res.statusCode == 500) {
  35. uni.showToast({
  36. title: '糟糕,服务器开小差了~',
  37. icon: 'none',
  38. duration: 5000
  39. });
  40. return;
  41. }
  42. // 404
  43. if (res.statusCode == 404) {
  44. uni.showToast({
  45. title: '接口地址404,请联系管理员~',
  46. icon: 'none',
  47. duration: 5000
  48. });
  49. return;
  50. }
  51. // 成功才回调
  52. if (res.data.code === 0) {
  53. resolve(res.data.data);
  54. } else {
  55. // 后台错误信息
  56. uni.showToast({
  57. title: res.data.msg,
  58. icon: 'none',
  59. duration: 3000
  60. });
  61. if (res.data.code === 10010002) {
  62. // 跳转到登录
  63. uni.navigateTo({
  64. url: '/pages/login/login'
  65. });
  66. return;
  67. }
  68. reject(res.data)
  69. }
  70. },
  71. fail: function(e) {
  72. uni.showToast({
  73. title: '啊哦,与服务器失去连接了~',
  74. icon: 'none',
  75. duration: 10000
  76. });
  77. }
  78. });
  79. });
  80. }
  81. /**
  82. * 文件上传
  83. * @param {Object} url
  84. * @param {Object} file
  85. * @param {Object} data
  86. */
  87. function uploadData(path, file, formData) {
  88. // 构建完整请求地址
  89. let url = buildUrl(path)
  90. uni.showLoading({
  91. title: '正在上传...'
  92. })
  93. // 兼容chooseMedia
  94. const filePath = file.path || file.tempFilePath
  95. return new Promise((resolve, reject) => {
  96. uni.uploadFile({
  97. url: url,
  98. filePath: filePath,
  99. name: 'file',
  100. header: {
  101. 'token': uni.getStorageSync("token")
  102. },
  103. formData: formData,
  104. success: res => {
  105. let data = {}
  106. if (res.data) {
  107. data = JSON.parse(res.data)
  108. }
  109. resolve(data);
  110. uni.hideLoading()
  111. },
  112. fail: err => {
  113. reject()
  114. uni.hideLoading()
  115. }
  116. })
  117. });
  118. }
  119. module.exports = {
  120. post: postData,
  121. upload: uploadData
  122. };