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

112 lines
2.0 KiB

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. export function api(key, data, callback, loadingTitle) {
  38. let req = config[key]
  39. if (!req) {
  40. console.error('无效key' + key);
  41. return
  42. }
  43. if (typeof callback == 'string') {
  44. loadingTitle = callback
  45. }
  46. if (typeof data == 'function') {
  47. callback = data
  48. data = {}
  49. }
  50. // 接口限流
  51. if (req.limit) {
  52. let storageKey = req.url
  53. let storage = limit[storageKey]
  54. if (storage && new Date().getTime() - storage < req.limit) {
  55. return
  56. }
  57. limit[storageKey] = new Date().getTime()
  58. }
  59. //必须登录
  60. if (req.auth) {
  61. if (!uni.getStorageSync('token')) {
  62. uni.navigateTo({
  63. url: '/pages_order/auth/wxLogin'
  64. })
  65. console.error('需要登录')
  66. return
  67. }
  68. }
  69. // 接口防抖
  70. if(req.debounce){
  71. let storageKey = req.url
  72. let storage = debounce[storageKey]
  73. if (storage) {
  74. clearTimeout(storage)
  75. }
  76. debounce[storageKey] = setTimeout(() => {
  77. clearTimeout(storage)
  78. delete debounce[storageKey]
  79. http.http(req.url, data, callback, req.method,
  80. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  81. }, req.debounce)
  82. return
  83. }
  84. http.http(req.url, data, callback, req.method,
  85. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  86. }
  87. export default api