商城类、订单类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.

76 lines
1.4 KiB

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