瑶都万能墙
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
2.5 KiB

4 months ago
4 months ago
4 months ago
4 months ago
  1. import http from './http.js'
  2. let limit = {}
  3. let debounce = {}
  4. const config = {
  5. // 示例
  6. // wxLogin : {url : '/api/wxLogin', method : 'POST',
  7. // auth : false, showLoading : true, loadingTitle : '加载中...',
  8. // limit : 1000
  9. // },
  10. getConfig : {url : '/api/getConfig', method : 'GET', limit : 500},
  11. // 微信登录接口
  12. wxLogin: {
  13. url: '/login/login',
  14. method: 'POST',
  15. limit : 500,
  16. showLoading : true,
  17. },
  18. // 修改个人信息接口
  19. updateInfo: {
  20. url: '/info/updateInfo',
  21. method: 'POST',
  22. auth: true,
  23. limit : 500,
  24. showLoading : true,
  25. },
  26. //隐私政策
  27. getPrivacyPolicy: {
  28. url: '/login/getPrivacyPolicy',
  29. method: 'GET',
  30. },
  31. //用户协议
  32. getUserAgreement: {
  33. url: '/login/getUserAgreement',
  34. method: 'GET',
  35. },
  36. // 不需要登录的接口
  37. //获取分类
  38. getClassInfo: {
  39. url: '/api/city/getClassInfo',
  40. method: 'GET',
  41. },
  42. //获取首页头部信息
  43. getIndexHeaderInfo: {
  44. url: '/api/city/getIndexHeaderInfo',
  45. method: 'GET',
  46. },
  47. //获取工作信息列表
  48. getJobPage: {
  49. url: '/api/city/getJobPage',
  50. method: 'GET',
  51. },
  52. //根据分类获取租房信息列表
  53. getRentPage: {
  54. url: '/api/city/getRentPage',
  55. method: 'GET',
  56. },
  57. //根据分类获取动态帖子列表带分页
  58. getPostPage: {
  59. url: '/api/city/getPostPage',
  60. method: 'GET',
  61. },
  62. }
  63. export function api(key, data, callback, loadingTitle) {
  64. let req = config[key]
  65. if (!req) {
  66. console.error('无效key--------' + key);
  67. return
  68. }
  69. if (typeof callback == 'string') {
  70. loadingTitle = callback
  71. }
  72. if (typeof data == 'function') {
  73. callback = data
  74. data = {}
  75. }
  76. // 接口限流
  77. if (req.limit) {
  78. let storageKey = req.url
  79. let storage = limit[storageKey]
  80. if (storage && new Date().getTime() - storage < req.limit) {
  81. return
  82. }
  83. limit[storageKey] = new Date().getTime()
  84. }
  85. //必须登录
  86. if (req.auth) {
  87. if (!uni.getStorageSync('token')) {
  88. uni.navigateTo({
  89. url: '/pages_order/auth/wxLogin'
  90. })
  91. console.error('需要登录')
  92. return
  93. }
  94. }
  95. // 接口防抖
  96. if(req.debounce){
  97. let storageKey = req.url
  98. let storage = debounce[storageKey]
  99. if (storage) {
  100. clearTimeout(storage)
  101. }
  102. debounce[storageKey] = setTimeout(() => {
  103. clearTimeout(storage)
  104. delete debounce[storageKey]
  105. http.http(req.url, data, callback, req.method,
  106. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  107. }, req.debounce)
  108. return
  109. }
  110. http.http(req.url, data, callback, req.method,
  111. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  112. }
  113. export default api