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

129 lines
2.3 KiB

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 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. queryNewsList: {
  29. url: '/workorder/news/queryNewsList',
  30. method: 'GET',
  31. },
  32. // 公告信息-根据id查询公告信息
  33. queryNewsById: {
  34. url: '/workorder/news/queryNewsById',
  35. method: 'GET',
  36. },
  37. }
  38. export function api(key, data, callback, loadingTitle) {
  39. let req = config[key]
  40. if (!req) {
  41. console.error('无效key--------' + key);
  42. return
  43. }
  44. if (typeof callback == 'string') {
  45. loadingTitle = callback
  46. }
  47. if (typeof data == 'function') {
  48. callback = data
  49. data = {}
  50. }
  51. // 接口限流
  52. if (req.limit) {
  53. let storageKey = req.url
  54. let storage = limit[storageKey]
  55. if (storage && new Date().getTime() - storage < req.limit) {
  56. return
  57. }
  58. limit[storageKey] = new Date().getTime()
  59. }
  60. //必须登录
  61. if (req.auth) {
  62. if (!uni.getStorageSync('token')) {
  63. utils.toLogin()
  64. console.error('需要登录')
  65. return
  66. }
  67. }
  68. // 接口防抖
  69. if (req.debounce) {
  70. let storageKey = req.url
  71. let storage = debounce[storageKey]
  72. if (storage) {
  73. clearTimeout(storage)
  74. }
  75. debounce[storageKey] = setTimeout(() => {
  76. clearTimeout(storage)
  77. delete debounce[storageKey]
  78. http.http(req.url, data, callback, req.method,
  79. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  80. }, req.debounce)
  81. return
  82. }
  83. http.http(req.url, data, callback, req.method,
  84. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  85. }
  86. function addApiModel(model, key) {
  87. for (let k in model) {
  88. if (config[`${k}`]) {
  89. console.error(`重名api------model=${key},key=${k}`);
  90. continue
  91. }
  92. config[`${k}`] = model[k]
  93. // config[`${key}_${k}`] = model[k]
  94. }
  95. }
  96. models.forEach(key => {
  97. addApiModel(require(`./model/${key}.js`).default, key)
  98. })
  99. export default api