鸿宇研学生前端代码
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.

111 lines
2.7 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 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', 'image', 'activity', 'coupon', 'order', 'comment', 'medal', 'bind', 'experience', 'partner', 'info', 'notice']
  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/queryConfigList', method : 'GET', limit : 500},
  13. queryConfigByParamCode : {url : '/config/queryConfigByParamCode', method : 'GET'},
  14. queryPeriodList : {url : '/config/queryPeriodList', method : 'GET'},
  15. queryCommentOptionList : {url : '/config/queryCommentOptionList', method : 'GET'},
  16. queryExperienceQuestionList : {url : '/config/queryExperienceQuestionList', method : 'GET'},
  17. queryAddressList : {url : '/config/queryAddressList', method : 'GET'},
  18. queryAgeList : {url : '/config/queryAgeList', method : 'GET'},
  19. queryTimeList : {url : '/config/queryTimeList', method : 'GET'},
  20. }
  21. export function api(key, data, callback, loadingTitle, disabledToast) {
  22. let req = config[key]
  23. if (!req) {
  24. console.error('无效key' + key);
  25. return Promise.reject()
  26. }
  27. if (typeof callback == 'string') {
  28. loadingTitle = callback
  29. }
  30. if (typeof data == 'function') {
  31. callback = data
  32. data = {}
  33. }
  34. // 接口限流
  35. if (req.limit) {
  36. let storageKey = req.url
  37. let storage = limit[storageKey]
  38. if (storage && new Date().getTime() - storage < req.limit) {
  39. return Promise.reject()
  40. }
  41. limit[storageKey] = new Date().getTime()
  42. }
  43. //必须登录
  44. if (req.auth) {
  45. if (!uni.getStorageSync('token')) {
  46. utils.toLogin()
  47. console.error('需要登录', req.url)
  48. return Promise.reject()
  49. }
  50. }
  51. // 接口防抖
  52. if(req.debounce){
  53. let storageKey = req.url
  54. let storage = debounce[storageKey]
  55. if (storage) {
  56. clearTimeout(storage)
  57. }
  58. debounce[storageKey] = setTimeout(() => {
  59. clearTimeout(storage)
  60. delete debounce[storageKey]
  61. http.http(req.url, data, callback, req.method,
  62. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  63. }, req.debounce)
  64. return Promise.reject()
  65. }
  66. return http.http(req.url, data, callback, req.method,
  67. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle, disabledToast)
  68. }
  69. function addApiModel(model, key){
  70. for(let k in model){
  71. if(config[`${k}`]){
  72. console.error(`重名api------model=${key},key=${k}`);
  73. uni.showModal({
  74. title: `重名api`,
  75. content: `model=${key},key=${k}`
  76. })
  77. continue
  78. }
  79. config[`${k}`] = model[k]
  80. // config[`${key}_${k}`] = model[k]
  81. }
  82. }
  83. models.forEach(key => {
  84. addApiModel(require(`./model/${key}.js`).default, key)
  85. })
  86. export default api