风险测评小程序前端代码仓库
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.

98 lines
1.9 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month 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', 'exam', 'report', 'userInfo']
  6. const config = {
  7. getConfig : {url : '/config/queryConfigList', method : 'GET', limit : 500},
  8. }
  9. export function api(key, data, callback, loadingTitle, disabledToast) {
  10. let req = config[key]
  11. if (!req) {
  12. console.error('无效key' + key);
  13. return Promise.reject()
  14. }
  15. if (typeof callback == 'string') {
  16. loadingTitle = callback
  17. }
  18. if (typeof data == 'function') {
  19. callback = data
  20. data = {}
  21. }
  22. // 接口限流
  23. if (req.limit) {
  24. let storageKey = req.url
  25. let storage = limit[storageKey]
  26. if (storage && new Date().getTime() - storage < req.limit) {
  27. return Promise.reject()
  28. }
  29. limit[storageKey] = new Date().getTime()
  30. }
  31. //必须登录
  32. if (req.auth) {
  33. if (!uni.getStorageSync('token')) {
  34. utils.toLogin()
  35. console.error('需要登录', req.url)
  36. return Promise.reject()
  37. }
  38. }
  39. // 接口防抖
  40. if(req.debounce){
  41. let storageKey = req.url
  42. let storage = debounce[storageKey]
  43. if (storage) {
  44. clearTimeout(storage)
  45. }
  46. debounce[storageKey] = setTimeout(() => {
  47. clearTimeout(storage)
  48. delete debounce[storageKey]
  49. http.http(req.url, data, callback, req.method,
  50. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  51. }, req.debounce)
  52. return Promise.reject()
  53. }
  54. return http.http(req.url, data, callback, req.method,
  55. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle, disabledToast)
  56. }
  57. function addApiModel(model, key){
  58. for(let k in model){
  59. if(config[`${k}`]){
  60. console.error(`重名api------model=${key},key=${k}`);
  61. uni.showModal({
  62. title: `重名api`,
  63. content: `model=${key},key=${k}`
  64. })
  65. continue
  66. }
  67. config[`${k}`] = model[k]
  68. // config[`${key}_${k}`] = model[k]
  69. }
  70. }
  71. models.forEach(key => {
  72. addApiModel(require(`./model/${key}.js`).default, key)
  73. })
  74. export default api