request.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-01-25 11:32:04
  4. * @LastEditTime: 2021-03-08 21:22:17
  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 === 'development1'
  18. ? 'http://localhost:18333/'
  19. : 'https://api.qcbwc.cn/',
  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. console.log(data)
  41. if (
  42. statusCode !== 200 ||
  43. !Object.prototype.hasOwnProperty.call(data, 'code')
  44. ) {
  45. uni.showToast({
  46. title: '系统异常',
  47. icon: 'none'
  48. })
  49. resolve(false)
  50. return
  51. }
  52. data.code = data.code * 1
  53. if (data.code === 200) {
  54. resolve(data)
  55. } else if (data.code === 1000) {
  56. console.log(1111)
  57. uni.navigateTo({
  58. url: '/pagesSub/other/login'
  59. })
  60. } else {
  61. resolve(data)
  62. }
  63. }
  64. uni.request(options)
  65. })
  66. },
  67. get (options) {
  68. options.method = 'GET'
  69. return this.request(options)
  70. },
  71. post (options) {
  72. options.method = 'POST'
  73. return this.request(options)
  74. }
  75. }