ajax.js 3.9 KB

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