ajax.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import tools from "@/common/js/tools";
  2. /**
  3. * post请求封装
  4. */
  5. export const request = (url, method, data, show = false) => {
  6. let token=uni.getStorageSync('token')
  7. if (show) {
  8. tools.showLoading();
  9. }
  10. if (data === undefined) {
  11. data = {};
  12. }
  13. data.language = uni.getLocale();
  14. return new Promise((resolve, reject) => {
  15. // 封装主体:网络请求
  16. console.log(url)
  17. uni.request({
  18. url: url,
  19. data: data,
  20. method: method || 'post',
  21. header: {
  22. 'content-type': 'application/json',
  23. 'hversion': '1.0',
  24. 'Authorization': token
  25. },
  26. success: (res) => {
  27. if (res.statusCode === 200) {
  28. // console.log('应答信息-----------------------')
  29. // console.log(res.data); // 控制台显示数据信息
  30. tools.hideLoading();
  31. if (res.data.code * 1 === 401) {
  32. uni.clearStorageSync()
  33. uni.reLaunch({
  34. url: '/pages/login/index'
  35. });
  36. } else {
  37. resolve(res.data)
  38. }
  39. }else if(res.statusCode === 401){
  40. uni.clearStorageSync()
  41. uni.reLaunch({
  42. url: '/pages/login/index'
  43. });
  44. } else{
  45. tools.error('error!!')
  46. }
  47. },
  48. fail: (err) => {
  49. tools.error('error!!')
  50. // 返回错误消息
  51. reject(err)
  52. tools.hideLoading();
  53. }
  54. })
  55. })
  56. }
  57. /**
  58. * 文件上传
  59. */
  60. export const uploadImage = (filePath, imageName, data) => {
  61. tools.showLoading();
  62. return new Promise((resolve, reject) => {
  63. uni.uploadFile({
  64. url: '/api/common/upload',
  65. // header: {
  66. // 'Authorization': JSON.parse(uni.getStorageSync('userInfo')).token
  67. // },
  68. filePath: filePath,
  69. name: imageName,
  70. formData: data,
  71. success: (res) => {
  72. tools.hideLoading();
  73. resolve(res)
  74. },
  75. fail(err) {
  76. tools.hideLoading();
  77. reject(err)
  78. }
  79. });
  80. })
  81. }
  82. export const uploadFile = (url, file) => {
  83. tools.showLoading();
  84. let token = uni.getStorageSync('token') || '';
  85. // data.app_version = plus.runtime.version;
  86. // console.log('请求地址-----------------------')
  87. // console.log('请求参数-----------------------')
  88. // console.log(data)
  89. console.log(file)
  90. return new Promise((resolve, reject) => {
  91. // 封装主体:网络请求
  92. uni.uploadFile({
  93. url: url, //仅为示例,非真实的接口地址
  94. filePath: file,
  95. name: 'file',
  96. header: {
  97. 'content-type': 'application/json',
  98. 'apikey': '01ABA77B8BA6F26243D85B5A50FFB88624951C7B3A4078B3F9971983E667D5B8',
  99. 'hversion': '1.0',
  100. 'Authorization': token
  101. },
  102. success: (res) => {
  103. tools.hideLoading();
  104. if (res.statusCode === 200) {
  105. // console.log('应答信息-----------------------')
  106. res.data = JSON.parse(res.data)
  107. // console.log(res.data); // 控制台显示数据信息
  108. if (res.data.code * 1 === 401) {
  109. uni.reLaunch({
  110. url: '/pages/login/login'
  111. });
  112. } else {
  113. resolve(res.data)
  114. }
  115. } else {
  116. tools.error('网络错误!!')
  117. }
  118. },
  119. fail: (err) => {
  120. tools.error('网络错误!!')
  121. // 返回错误消息
  122. reject(err)
  123. tools.hideLoading();
  124. }
  125. });
  126. })
  127. }