request.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import useUserStore from '@/stores/user.js';
  2. import config from '@/config'
  3. import {
  4. getToken
  5. } from '@/utils/auth'
  6. import errorCode from '@/utils/errorCode'
  7. import {
  8. toast,
  9. showConfirm,
  10. tansParams
  11. } from '@/utils/common'
  12. let timeout = 10000
  13. const baseUrl = config.baseUrl
  14. const request = config => {
  15. uni.showLoading({
  16. title: '加载中'
  17. });
  18. // 是否需要设置 token
  19. const isToken = (config.headers || {}).isToken === false
  20. config.header = config.header || {}
  21. if (getToken() && !isToken) {
  22. config.header['satoken'] = getToken()
  23. config.header['sysId'] = uni.getStorageSync('sysId')
  24. }
  25. // get请求映射params参数
  26. if (config.params) {
  27. let url = config.url + '?' + tansParams(config.params)
  28. url = url.slice(0, -1)
  29. config.url = url
  30. }
  31. return new Promise((resolve, reject) => {
  32. uni.request({
  33. method: config.method || 'get',
  34. timeout: config.timeout || timeout,
  35. url: config.baseUrl || baseUrl + config.url,
  36. data: config.data,
  37. header: config.header,
  38. dataType: 'json'
  39. }).then(response => {
  40. uni.hideLoading();
  41. let data=response.data;
  42. saveLogs(config.url, config.data, data.data)
  43. const code = data.code || 404
  44. const msg = errorCode[code] || data.msg || errorCode['default']
  45. if (code === 501) {
  46. let pages = getCurrentPages()
  47. let currentPage = pages[pages.length - 1].route
  48. if (currentPage != '/pages/login') {
  49. toast('登录状态已过期,请重新登陆!')
  50. reject('无效的会话,或者会话已过期,请重新登录。')
  51. useUserStore().LogOut().then(res => {
  52. uni.reLaunch({
  53. url: '/pages/login'
  54. })
  55. })
  56. }
  57. } else if (code === 500) {
  58. toast(msg.substr(0, 50))
  59. reject(msg)
  60. } else if (code !== 200) {
  61. toast(msg)
  62. reject(code)
  63. } else if (code === 404) {
  64. toast('请求超时')
  65. reject(code)
  66. }
  67. resolve(data.data)
  68. })
  69. .catch(error => {
  70. saveLogs(config.url, config.data, error)
  71. uni.hideLoading();
  72. let {
  73. message
  74. } = error
  75. if (message === 'Network Error') {
  76. message = '网络连接超时'
  77. } else if (message.includes('timeout')) {
  78. message = '网络连接超时'
  79. } else if (message.includes('Request failed with status code')) {
  80. message = '系统接口' + message.substr(message.length - 3) + '异常'
  81. }
  82. toast(message)
  83. reject(error)
  84. })
  85. })
  86. }
  87. function saveLogs(url, params, res) {
  88. let arr = []
  89. let obj = {
  90. url: url,
  91. time: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),
  92. params: JSON.stringify(params) || '无',
  93. res: JSON.stringify(res).substr(0, 1000)
  94. }
  95. if (uni.getStorageSync('logs')) {
  96. let arr = JSON.parse(uni.getStorageSync('logs'))
  97. arr.unshift(obj)
  98. if (arr.length > 30) {
  99. arr.pop()
  100. }
  101. uni.setStorageSync('logs', JSON.stringify(arr))
  102. } else {
  103. arr.unshift(obj)
  104. uni.setStorageSync('logs', JSON.stringify(arr))
  105. }
  106. }
  107. // 验证是否为blob格式
  108. async function blobValidate(data) {
  109. try {
  110. const text = await data.text();
  111. JSON.parse(text);
  112. return false;
  113. } catch (error) {
  114. return true;
  115. }
  116. }
  117. export function downLoadReq(config) {
  118. uni.showLoading({
  119. title: '加载中'
  120. });
  121. // 是否需要设置 token
  122. const isToken = (config.headers || {}).isToken === false
  123. config.header = config.header || {}
  124. if (getToken() && !isToken) {
  125. config.header['satoken'] = getToken()
  126. config.header['sysId'] = uni.getStorageSync('sysId')
  127. }
  128. // get请求映射params参数
  129. if (config.params) {
  130. let url = config.url + '?' + tansParams(config.params)
  131. url = url.slice(0, -1)
  132. config.url = url
  133. }
  134. return new Promise((resolve, reject) => {
  135. uni.request({
  136. method: config.method || 'get',
  137. timeout: config.timeout || timeout,
  138. url: config.baseUrl || baseUrl + config.url,
  139. data: config.data,
  140. header: config.header,
  141. dataType: 'json'
  142. }).then(async (response) => {
  143. let data =response.data
  144. uni.hideLoading();
  145. const isLogin = await blobValidate(data)
  146. if (isLogin) {
  147. wx.getFileSystemManager().readFile({
  148. filePath: data, //选择图片返回的相对路径
  149. encoding: "base64", //这个是很重要的
  150. success: res => { //成功的回调
  151. },
  152. fail: err => {
  153. }
  154. })
  155. } else {
  156. }
  157. resolve(data)
  158. }).catch((r) => {
  159. reject(r)
  160. })
  161. })
  162. }
  163. export default request