index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import request from './request.js' // 文档地址:https://github.com/jerry-9527/uni_request
  2. import config from '@/config/config.js'
  3. // import store from '@/store'
  4. import cache from '@/utils/cache'
  5. import {
  6. LOGIN_TOKEN
  7. } from '@/config/cache'
  8. // 创建request实例
  9. const service = request({
  10. baseURL: config.baseUrl,
  11. timeout: 5000, // 请求超时,
  12. header: {
  13. 'Content-Type': 'application/x-www-form-urlencoded',
  14. 'Accept': 'application/json'
  15. }
  16. });
  17. // 请求拦截器
  18. service.interceptors.request.use(async (config, ...args) => {
  19. // // 判断token是否存在
  20. // let token = cache.get(LOGIN_TOKEN) || '';
  21. // // token为空已过期 刷新token
  22. // if (token) {
  23. // config.body.token = token;
  24. // }
  25. config.body.token = '1LghTlDdt7dBVyRIyadh2lmA3y9JmD9Cbtc3e57GUic='
  26. return config
  27. })
  28. // response interceptor
  29. service.interceptors.response.use((response, ...args) => { // 响应拦截器(可以设置多个, 同时可以也可以使用异步方法)
  30. if (response.data.message) {
  31. uni.showToast({
  32. title: response.data.message,
  33. duration: 1500,
  34. mask: true,
  35. icon: 'none'
  36. });
  37. }
  38. return response
  39. })
  40. export default service
  41. // export const myRequest = (options) => {
  42. // let BASE_URL = 请求地址
  43. // console.log(BASE_URL)
  44. // var session_key = ""
  45. // session_key = uni.getStorageSync('session_key')
  46. // let url = ""
  47. // let data = options.data || {}
  48. // if (options.url.split('=')[1]) {
  49. // url = options.url.split('=')[1]
  50. // } else {
  51. // url = options.url.split('api/')[1]
  52. // }
  53. // console.log(url)
  54. // if (url != 'user.login.login_user') {
  55. // data.token = session_key
  56. // }
  57. // return new Promise((resolve, reject) => {
  58. // uni.request({
  59. // url: BASE_URL + options.url,
  60. // method: options.method || 'POST',
  61. // header: {
  62. // "Content-Type": "application/x-www-form-urlencoded",
  63. // },
  64. // data: data,
  65. // success: (res) => {
  66. // if (res.data.message != "") {
  67. // if (res.data.message == "识别无结果") {
  68. // resolve(res.data.data)
  69. // } else {
  70. // return uni.showToast({
  71. // title: res.data.message,
  72. // icon: "none"
  73. // })
  74. // }
  75. // }
  76. // resolve(res.data.data)
  77. // },
  78. // fail: (err) => {
  79. // uni.showToast({
  80. // title: '请求接口失败'
  81. // })
  82. // reject(err)
  83. // }
  84. // })
  85. // })
  86. // }