request.js 4.6 KB

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