mycar.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import HTTP from "../../../../requestFn/Api";
  2. const app = getApp();
  3. Page({
  4. data: {
  5. car_list: []
  6. },
  7. onLoad() {
  8. },
  9. onShow(options) {
  10. const {
  11. garden_id
  12. } = app.globalData;
  13. if (garden_id) {
  14. this.setData({
  15. garden_id
  16. })
  17. this.getMyCar()
  18. }
  19. },
  20. getMyCar() {
  21. const that = this;
  22. const {
  23. garden_id
  24. } = that.data;
  25. HTTP.GetuserCar({
  26. garden_id
  27. }).then(res => {
  28. wx.getStorage({
  29. key: "car_list",
  30. success(e) {
  31. if (res.list.length > e.data.length) {
  32. let id;
  33. e.data.map(item => {
  34. if (item.selected) {
  35. id = item.id
  36. }
  37. })
  38. const result = res.list.map(item => {
  39. if (item.id == id) {
  40. return {
  41. ...item,
  42. selected: true,
  43. detail: `${item.brand}${item.seat}坐(${item.displacement}${item.displacement_unit})`
  44. }
  45. } else {
  46. return {
  47. ...item,
  48. selected: false,
  49. detail: `${item.brand}${item.seat}坐(${item.displacement}${item.displacement_unit})`
  50. }
  51. }
  52. })
  53. that.setData({
  54. car_list: result
  55. })
  56. wx.setStorage({
  57. data: result,
  58. key: "car_list"
  59. })
  60. } else {
  61. that.setData({
  62. car_list: e.data
  63. })
  64. }
  65. },
  66. fail() {
  67. const result = res.list.map((item, index) => {
  68. if (index == 0) {
  69. return {
  70. ...item,
  71. selected: true,
  72. detail: `${item.brand}${item.seat}坐(${item.displacement}${item.displacement_unit})`
  73. }
  74. } else {
  75. return {
  76. ...item,
  77. selected: false,
  78. detail: `${item.brand}${item.seat}坐(${item.displacement}${item.displacement_unit})`
  79. }
  80. }
  81. })
  82. that.setData({
  83. car_list: result
  84. })
  85. wx.setStorage({
  86. data: result,
  87. key: "car_list"
  88. })
  89. }
  90. })
  91. })
  92. },
  93. myCar(e) {
  94. const {
  95. id
  96. } = e.currentTarget;
  97. const {
  98. car_list
  99. } = this.data;
  100. let newArr = car_list.map(item => {
  101. if (item.id == id) {
  102. return {
  103. ...item,
  104. selected: true
  105. }
  106. } else {
  107. return {
  108. ...item,
  109. selected: false
  110. }
  111. }
  112. })
  113. wx.setStorage({
  114. data: newArr,
  115. key: "car_list"
  116. })
  117. this.setData({
  118. car_list: newArr
  119. })
  120. },
  121. })