ajax.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. } else{
  63. tools.error('网络错误!!')
  64. }
  65. },
  66. fail: (err) => {
  67. tools.error('网络错误!!')
  68. // 返回错误消息
  69. reject(err)
  70. tools.hideLoading();
  71. }
  72. })
  73. })
  74. }
  75. /**
  76. * 文件上传
  77. */
  78. export const uploadImage = (filePath, imageName, data) => {
  79. tools.showLoading();
  80. return new Promise((resolve, reject) => {
  81. uni.uploadFile({
  82. url: BASE_URL + '/api/common/upload',
  83. // header: {
  84. // 'Authorization': JSON.parse(uni.getStorageSync('userInfo')).token
  85. // },
  86. filePath: filePath,
  87. name: imageName,
  88. formData: data,
  89. success: (res) => {
  90. tools.hideLoading();
  91. resolve(res)
  92. },
  93. fail(err) {
  94. tools.hideLoading();
  95. reject(err)
  96. }
  97. });
  98. })
  99. }
  100. export const uploadFile = (url, file) => {
  101. tools.showLoading();
  102. let token = uni.getStorageSync('token') || '';
  103. // data.app_version = plus.runtime.version;
  104. // console.log('请求地址-----------------------')
  105. // console.log(BASE_URL + url)
  106. // console.log('请求参数-----------------------')
  107. // console.log(data)
  108. console.log(file)
  109. return new Promise((resolve, reject) => {
  110. // 封装主体:网络请求
  111. console.log(BASE_URL + url)
  112. uni.uploadFile({
  113. url: BASE_URL + url, //仅为示例,非真实的接口地址
  114. filePath: file,
  115. name: 'file',
  116. header: {
  117. 'content-type': 'application/json',
  118. 'apikey': '01ABA77B8BA6F26243D85B5A50FFB88624951C7B3A4078B3F9971983E667D5B8',
  119. 'hversion': '1.0',
  120. 'Authorization': token
  121. },
  122. success: (res) => {
  123. tools.hideLoading();
  124. if (res.statusCode === 200) {
  125. // console.log('应答信息-----------------------')
  126. res.data = JSON.parse(res.data)
  127. // console.log(res.data); // 控制台显示数据信息
  128. if (res.data.code * 1 === 401) {
  129. uni.reLaunch({
  130. url: '/pages/login/login'
  131. });
  132. } else {
  133. resolve(res.data)
  134. }
  135. } else {
  136. tools.error('网络错误!!')
  137. }
  138. },
  139. fail: (err) => {
  140. tools.error('网络错误!!')
  141. // 返回错误消息
  142. reject(err)
  143. tools.hideLoading();
  144. }
  145. });
  146. })
  147. }