商城类、订单类uniapp模板,多角色
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.

103 lines
1.8 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. import http from './http.js'
  2. let limit = {}
  3. let debounce = {}
  4. const config = {
  5. // 示例
  6. // wxLogin : {url : '/api/wxLogin', method : 'POST',
  7. // auth : false, showLoading : true, loadingTitle : '加载中...',
  8. // limit : 1000
  9. // },
  10. getConfig : {url : '/api/getConfig', method : 'GET', limit : 500},
  11. // 微信登录接口
  12. wxLogin: {
  13. url: '/login/login',
  14. method: 'POST',
  15. limit : 500,
  16. showLoading : true,
  17. },
  18. // 修改个人信息接口
  19. updateInfo: {
  20. url: '/info/updateInfo',
  21. method: 'POST',
  22. auth: true,
  23. limit : 500,
  24. showLoading : true,
  25. },
  26. }
  27. export function api(key, data, callback, loadingTitle) {
  28. let req = config[key]
  29. if (!req) {
  30. console.error('无效key' + key);
  31. return
  32. }
  33. if (typeof callback == 'string') {
  34. loadingTitle = callback
  35. }
  36. if (typeof data == 'function') {
  37. callback = data
  38. data = {}
  39. }
  40. // 接口限流
  41. if (req.limit) {
  42. let storageKey = req.url
  43. let storage = limit[storageKey]
  44. if (storage && new Date().getTime() - storage < req.limit) {
  45. return
  46. }
  47. limit[storageKey] = new Date().getTime()
  48. }
  49. //必须登录
  50. if (req.auth) {
  51. if (!uni.getStorageSync('token')) {
  52. uni.navigateTo({
  53. url: '/pages_order/auth/wxLogin'
  54. })
  55. console.error('需要登录')
  56. return
  57. }
  58. }
  59. // 接口防抖
  60. if(req.debounce){
  61. let storageKey = req.url
  62. let storage = debounce[storageKey]
  63. if (storage) {
  64. clearTimeout(storage)
  65. }
  66. debounce[storageKey] = setTimeout(() => {
  67. clearTimeout(storage)
  68. delete debounce[storageKey]
  69. http.http(req.url, data, callback, req.method,
  70. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  71. }, req.debounce)
  72. return
  73. }
  74. http.http(req.url, data, callback, req.method,
  75. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  76. }
  77. export default api