order.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. export default{
  2. //开启命名空间
  3. namespaced:true,
  4. //数据
  5. state:()=>({
  6. // orders:JSON.parse(uni.getStorageSync('orders')||'[]'),
  7. orders:[],
  8. //重定向Object对象
  9. redirectInfo:null,
  10. }),
  11. //方法
  12. mutations:{
  13. //更新收货地址
  14. async putOrder(state,order){
  15. try {
  16. const response = await uni.request({
  17. url: uni.$http.baseUrl + 'orders/createOrder',
  18. method: 'POST',
  19. header: {
  20. 'content-type': 'application/json',
  21. 'token': uni.$http.testToken,
  22. },
  23. data: order
  24. });
  25. console.log(response,"什么吗是什么三十年")
  26. if (response.data && response.data.code === '200') {
  27. // 如果请求成功并且返回的数据中包含 code 为 '200',则提交 mutation 更新店面数据
  28. // commit('setShops', response.data.data);
  29. console.log(response.data.data,"aaa")
  30. } else {
  31. console.error('请求失败或返回的数据不符合预期');
  32. }
  33. } catch (error) {
  34. console.error('请求失败:', error);
  35. }
  36. state.orders=[...state.orders,order]
  37. // this.commit('m_user/saveOrderToStorage')
  38. },
  39. setOrders(state,orders){
  40. state.orders=orders
  41. },
  42. //持久化储存orders
  43. saveOrderToStorage(state){
  44. uni.setStorageSync('orders',JSON.stringify(state.orders))
  45. },
  46. updateRedirectInfo(state,info){
  47. state.redirectInfo=info
  48. console.log(state.redirectInfo);
  49. },
  50. },
  51. actions: {
  52. //后端请求得到所有的店面的数据,保存入shops
  53. async fetchOrder({ commit }) {
  54. // return
  55. console.log('哈哈哈哈哈')
  56. try {
  57. const response = await uni.request({
  58. url: uni.$http.baseUrl + 'orders/selectAll',
  59. method: 'GET',
  60. header: {
  61. 'content-type': 'application/json',
  62. 'token': uni.$http.testToken
  63. }
  64. });
  65. if (response.data && response.data.code === '200') {
  66. // 如果请求成功并且返回的数据中包含 code 为 '200',则提交 mutation 更新店面数据
  67. commit('setOrders', response.data.data);
  68. console.log(response.data.data,"aaa啊怎么办呢")
  69. } else {
  70. console.error('请求失败或返回的数据不符合预期');
  71. }
  72. } catch (error) {
  73. console.error('请求失败:', error);
  74. }
  75. }
  76. },
  77. getters:{
  78. //收货地址
  79. getOrdersByType(state,type){
  80. return state.orders.filter(order=>order.orderType===type)
  81. },
  82. getOrders(state){
  83. return state.orders
  84. }
  85. }
  86. }