敢为人鲜小程序前端代码仓库
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.

106 lines
2.1 KiB

11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
11 months ago
10 months ago
11 months ago
11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
  1. import http from './http.js'
  2. import utils from '../utils/utils.js'
  3. let limit = {}
  4. let debounce = {}
  5. const models = ['login', 'index', 'vip', 'info']
  6. const config = {
  7. // 示例
  8. // wxLogin : {url : '/api/wxLogin', method : 'POST',
  9. // auth : false, showLoading : true, loadingTitle : '加载中...',
  10. // limit : 1000
  11. // },
  12. getConfig : {url : '/config_common/getConfig', method : 'GET', limit : 500},
  13. }
  14. export function api(key, data, callback, loadingTitle) {
  15. let req = config[key]
  16. if (!req) {
  17. console.error('无效key' + key);
  18. return Promise.reject()
  19. }
  20. if (typeof callback == 'string') {
  21. loadingTitle = callback
  22. }
  23. if (typeof data == 'function') {
  24. callback = data
  25. data = {}
  26. }
  27. // 接口限流
  28. if (req.limit) {
  29. let storageKey = req.url
  30. let storage = limit[storageKey]
  31. if (storage && new Date().getTime() - storage < req.limit) {
  32. return Promise.reject()
  33. }
  34. limit[storageKey] = new Date().getTime()
  35. }
  36. //必须登录 - 注释掉登录检查
  37. /*
  38. if (req.auth) {
  39. if (!uni.getStorageSync('token')) {
  40. utils.toLogin()
  41. console.error('需要登录', req.url)
  42. return Promise.reject()
  43. }
  44. }
  45. */
  46. // 接口防抖
  47. if(req.debounce){
  48. let storageKey = req.url
  49. let storage = debounce[storageKey]
  50. if (storage) {
  51. clearTimeout(storage)
  52. }
  53. debounce[storageKey] = setTimeout(() => {
  54. clearTimeout(storage)
  55. delete debounce[storageKey]
  56. http.http(req.url, data, callback, req.method,
  57. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  58. }, req.debounce)
  59. return Promise.reject()
  60. }
  61. return http.http(req.url, data, callback, req.method,
  62. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  63. }
  64. function addApiModel(model, key){
  65. for(let k in model){
  66. if(config[`${k}`]){
  67. console.error(`重名api------model=${key},key=${k}`);
  68. uni.showModal({
  69. title: `重名api`,
  70. content: `model=${key},key=${k}`
  71. })
  72. continue
  73. }
  74. config[`${k}`] = model[k]
  75. // config[`${key}_${k}`] = model[k]
  76. }
  77. }
  78. models.forEach(key => {
  79. addApiModel(require(`./model/${key}.js`).default, key)
  80. })
  81. export default api