特易招,招聘小程序
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.

133 lines
2.1 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 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. * 登录的接口
  13. */
  14. // 微信登录接口
  15. wxLogin: {
  16. url: '/login/login',
  17. method: 'POST',
  18. limit : 500,
  19. showLoading : true,
  20. },
  21. // 修改个人信息接口
  22. updateInfo: {
  23. url: '/info/updateInfo',
  24. method: 'POST',
  25. auth: true,
  26. limit : 500,
  27. showLoading : true,
  28. },
  29. //隐私政策
  30. getPrivacyPolicy: {
  31. url: '/login/getPrivacyPolicy',
  32. method: 'GET',
  33. },
  34. //用户协议
  35. getUserAgreement: {
  36. url: '/login/getUserAgreement',
  37. method: 'GET',
  38. },
  39. /**
  40. * 公共的接口
  41. */
  42. /**
  43. * 求职者的接口
  44. */
  45. /**
  46. * boss的接口
  47. */
  48. }
  49. export function api(key, data, callback, loadingTitle) {
  50. let req = config[key]
  51. if (!req) {
  52. console.error('无效key' + key);
  53. return
  54. }
  55. if (typeof callback == 'string') {
  56. loadingTitle = callback
  57. }
  58. if (typeof data == 'function') {
  59. callback = data
  60. data = {}
  61. }
  62. // 接口限流
  63. if (req.limit) {
  64. let storageKey = req.url
  65. let storage = limit[storageKey]
  66. if (storage && new Date().getTime() - storage < req.limit) {
  67. return
  68. }
  69. limit[storageKey] = new Date().getTime()
  70. }
  71. //必须登录
  72. if (req.auth) {
  73. if (!uni.getStorageSync('token')) {
  74. uni.navigateTo({
  75. url: '/pages_order/auth/wxLogin'
  76. })
  77. console.error('需要登录')
  78. return
  79. }
  80. }
  81. // 接口防抖
  82. if(req.debounce){
  83. let storageKey = req.url
  84. let storage = debounce[storageKey]
  85. if (storage) {
  86. clearTimeout(storage)
  87. }
  88. debounce[storageKey] = setTimeout(() => {
  89. clearTimeout(storage)
  90. delete debounce[storageKey]
  91. http.http(req.url, data, callback, req.method,
  92. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  93. }, req.debounce)
  94. return
  95. }
  96. http.http(req.url, data, callback, req.method,
  97. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  98. }
  99. export default api