艺易修小程序24.08.21
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.

58 lines
1.1 KiB

8 months ago
  1. import http from './http.js'
  2. const config = {
  3. // 示例
  4. // wxLogin : {url : '/api/wxLogin', method : 'POST',
  5. // auth : false, showLoading : true, loadingTitle : '加载中...',
  6. // limit : 1000
  7. // },
  8. getConfig : {url : '/api/getConfig', method : 'GET', limit : 500},
  9. }
  10. export function api(key, data, callback, loadingTitle){
  11. let req = config[key]
  12. if (!req) {
  13. console.error('无效key' + key);
  14. return
  15. }
  16. if(typeof callback == 'string'){
  17. loadingTitle = callback
  18. }
  19. if(typeof data == 'function'){
  20. callback = data
  21. data = {}
  22. }
  23. // 接口限流
  24. if(req.limit){
  25. let storageKey = 'limit:' + req.url
  26. let storage = uni.getStorageSync(storageKey)
  27. if(storage && new Date().getTime() - parseInt(storage) < req.limit){
  28. return
  29. }
  30. uni.setStorageSync(storageKey, new Date().getTime())
  31. }
  32. //必须登录
  33. if (req.auth) {
  34. if (!uni.getStorageSync('token')) {
  35. uni.navigateTo({
  36. url: '/pages/login/mobile'
  37. })
  38. console.error('需要登录')
  39. return
  40. }
  41. }
  42. http.http(req.url, data, callback, req.method,
  43. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  44. }
  45. export default api