requestList.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import request from "@/common/request/index.js";
  2. // 请求前拦截
  3. const BEFOREINTERCEPT = (axios,currentPage, totalPage) => {
  4. return {}
  5. }
  6. //参数处理
  7. const AFTERINTERCEPT=(result,currentPage, totalPage)=>{
  8. return {}
  9. }
  10. // 报错拦截
  11. const ERRINTERCPTOR = function(err) {
  12. uni.showModal({
  13. title:"警告",
  14. content:err.message || JSON.stringify(err),
  15. confirmText:"关闭",
  16. showCancel:false,
  17. confirmColor:"#fbbd08"
  18. })
  19. return err
  20. }
  21. export default {
  22. namespaced: true,
  23. state: {
  24. value: [], //数据
  25. currentPage: 1, // 当前页
  26. totalPage: 1, //总页数
  27. },
  28. mutations: {
  29. increment(state, options) {
  30. state = {
  31. ...state,
  32. ...options
  33. }
  34. }
  35. },
  36. actions: {
  37. async requestFunc({
  38. commit,
  39. state
  40. }, options) {
  41. try{
  42. let {
  43. axios = {}, reset = false
  44. } = options;
  45. let {
  46. value,
  47. currentPage,
  48. totalPage
  49. } = state;
  50. // true 重置数据
  51. if (reset) {
  52. value = [];
  53. currentPage = 1;
  54. totalPage = 1;
  55. }
  56. if (currentPage > totalPage) return false;
  57. axios = BEFOREINTERCEPT(axios, currentPage, totalPage);
  58. var result =await request(axios);
  59. result==AFTERINTERCEPT(result);
  60. commit('increment',result,currentPage, totalPage)
  61. }catch(err){
  62. ERRINTERCPTOR(err)
  63. }
  64. }
  65. }
  66. }