铝交易,微信公众号
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.

134 lines
2.4 KiB

4 months ago
4 months ago
4 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. getPrivacyPolicy: {
  28. url: '/login/getPrivacyPolicy',
  29. method: 'GET',
  30. },
  31. //用户协议
  32. getUserAgreement: {
  33. url: '/login/getUserAgreement',
  34. method: 'GET',
  35. },
  36. //用户注册
  37. registerUser: {
  38. url: '/aluminium-prod/alUser/regUesr',
  39. method: 'POST',
  40. },
  41. //用户登录
  42. loginUser: {
  43. url: '/aluminium-prod/alUser/login',
  44. method: 'POST',
  45. },
  46. // 发送短信接口
  47. sendSms: {
  48. url: '/aluminium-prod/alUser/sendSms',
  49. method: 'POST',
  50. },
  51. // // 我的挂单列表
  52. // getProductlist: {
  53. // url: '/aluminium-prod/product/myProductlist',
  54. // method: 'GET',
  55. // showLoading: true,
  56. // debounce : 300,
  57. // },
  58. }
  59. export function api(key, data, callback, loadingTitle) {
  60. let req = config[key]
  61. if (!req) {
  62. console.error('无效key' + key);
  63. return
  64. }
  65. if (typeof callback == 'string') {
  66. loadingTitle = callback
  67. }
  68. if (typeof data == 'function') {
  69. callback = data
  70. data = {}
  71. }
  72. // 接口限流
  73. if (req.limit) {
  74. let storageKey = req.url
  75. let storage = limit[storageKey]
  76. if (storage && new Date().getTime() - storage < req.limit) {
  77. return
  78. }
  79. limit[storageKey] = new Date().getTime()
  80. }
  81. //必须登录
  82. if (req.auth) {
  83. if (!uni.getStorageSync('token')) {
  84. uni.navigateTo({
  85. url: '/pages_order/auth/wxLogin'
  86. })
  87. console.error('需要登录')
  88. return
  89. }
  90. }
  91. // 接口防抖
  92. if(req.debounce){
  93. let storageKey = req.url
  94. let storage = debounce[storageKey]
  95. if (storage) {
  96. clearTimeout(storage)
  97. }
  98. debounce[storageKey] = setTimeout(() => {
  99. clearTimeout(storage)
  100. delete debounce[storageKey]
  101. http.http(req.url, data, callback, req.method,
  102. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  103. }, req.debounce)
  104. return
  105. }
  106. http.http(req.url, data, callback, req.method,
  107. loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
  108. }
  109. export default api