request.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-01-25 11:32:04
  4. * @LastEditTime: 2021-01-25 13:33:39
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \rebatedine-mini\src\api\request.js
  8. */
  9. /**
  10. * 通用uni-app网络请求
  11. * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
  12. */
  13. import { USER_TOKEN } from '../store/storageKeys'
  14. export default {
  15. config: {
  16. baseUrl:
  17. process.env.NODE_ENV === 'development'
  18. ? 'https://apitest.topboom0912.com/bwc/'
  19. : 'https://apitest.topboom0912.com/bwc/',
  20. data: '',
  21. header: {
  22. 'Content-Type': 'application/x-www-form-urlencoded'
  23. },
  24. method: 'GET',
  25. timeout: 60000,
  26. dataType: 'json',
  27. responseType: '',
  28. success: () => {},
  29. fail: () => {},
  30. complete: () => {}
  31. },
  32. request (options = {}) {
  33. options = Object.assign({}, this.config, options)
  34. options.url = options.baseUrl + options.url
  35. if (uni.getStorageSync(USER_TOKEN)) {
  36. options.header.token = uni.getStorageSync(USER_TOKEN)
  37. }
  38. return new Promise(resolve => {
  39. options.complete = ({ statusCode, data }) => {
  40. if (statusCode !== 200) {
  41. uni.showToast({
  42. title: '系统异常',
  43. icon: 'none'
  44. })
  45. return
  46. }
  47. data.code = data.code * 1
  48. if (data.code === 200) {
  49. resolve(data)
  50. } else if (data.code === 1000) {
  51. uni.navigateTo({
  52. url: '/pagesSub/login/login'
  53. })
  54. } else {
  55. resolve(data)
  56. }
  57. }
  58. uni.request(options)
  59. })
  60. },
  61. get (options) {
  62. options.method = 'GET'
  63. return this.request(options)
  64. },
  65. post (options) {
  66. options.method = 'POST'
  67. return this.request(options)
  68. }
  69. }