ajax.js 4.0 KB

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