工单小程序2024-11-20
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.

118 lines
2.1 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 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 = []
  6. const config = {
  7. // 示例
  8. // wxLogin : {url : '/api/wxLogin', method : 'POST',
  9. // auth : false, showLoading : true, loadingTitle : '加载中...',
  10. // limit : 1000
  11. // },
  12. getConfig: {
  13. url: '/api/getConfig',
  14. method: 'GET',
  15. limit: 500
  16. },
  17. //轮播图-轮播图列表
  18. queryBannerList: {
  19. url: '/workorder/banner/queryBannerList',
  20. method: 'GET',
  21. },
  22. //工单信息-查询工单列表
  23. queryTemplateList: {
  24. url: '/workorder/template/queryTemplateList',
  25. method: 'GET',
  26. },
  27. }
  28. export function api(key, data, callback, loadingTitle) {
  29. let req = config[key]
  30. if (!req) {
  31. console.error('无效key--------' + key);
  32. return
  33. }
  34. if (typeof callback == 'string') {
  35. loadingTitle = callback
  36. }
  37. if (typeof data == 'function') {
  38. callback = data
  39. data = {}
  40. }
  41. // 接口限流
  42. if (req.limit) {
  43. let storageKey = req.url
  44. let storage = limit[storageKey]
  45. if (storage && new Date().getTime() - storage < req.limit) {
  46. return
  47. }
  48. limit[storageKey] = new Date().getTime()
  49. }
  50. //必须登录
  51. if (req.auth) {
  52. if (!uni.getStorageSync('token')) {
  53. utils.toLogin()
  54. console.error('需要登录')
  55. return
  56. }
  57. }
  58. // 接口防抖
  59. if (req.debounce) {
  60. let storageKey = req.url
  61. let storage = debounce[storageKey]
  62. if (storage) {
  63. clearTimeout(storage)
  64. }
  65. debounce[storageKey] = setTimeout(() => {
  66. clearTimeout(storage)
  67. delete debounce[storageKey]
  68. http.http(req.url, data, callback, req.method,
  69. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  70. }, req.debounce)
  71. return
  72. }
  73. http.http(req.url, data, callback, req.method,
  74. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  75. }
  76. function addApiModel(model, key) {
  77. for (let k in model) {
  78. if (config[`${k}`]) {
  79. console.error(`重名api------model=${key},key=${k}`);
  80. continue
  81. }
  82. config[`${k}`] = model[k]
  83. // config[`${key}_${k}`] = model[k]
  84. }
  85. }
  86. models.forEach(key => {
  87. addApiModel(require(`./model/${key}.js`).default, key)
  88. })
  89. export default api