list.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <view class="page">
  3. <NoData v-if="!loading&&list.length<1"></NoData>
  4. <view class="list" v-else>
  5. <!-- @open="open" -->
  6. <u-swipe-action :show="item.show" :index="index"
  7. v-for="(item, index) in list" :key="item.id"
  8. @click="click"
  9. :options="options"
  10. @open="open"
  11. >
  12. <AddressCard :isBack="isBack" :data="item" :showEdit="!isSelect" :showBorderBottom="index != list.length - 1"></AddressCard>
  13. </u-swipe-action>
  14. </view>
  15. <view class="btn">
  16. <u-button type="primary" shape="circle" @click="$u.route({ url: '/pages-mine/pages/address/add-or-update' })">
  17. <u-icon name="plus"></u-icon>
  18. <text>新建收货地址</text>
  19. </u-button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import AddressCard from '@/pages/mine/components/address-card.vue';
  25. export default {
  26. components: {
  27. AddressCard
  28. },
  29. data() {
  30. return {
  31. isSelect: false,
  32. isBack: false,
  33. loading:false,
  34. list: [],
  35. options: [
  36. {
  37. text: '设为默认',
  38. style: {
  39. backgroundColor: '#22ac38'
  40. }
  41. },
  42. {
  43. text: '删除',
  44. style: {
  45. backgroundColor: '#dd524d'
  46. }
  47. }
  48. ]
  49. };
  50. },
  51. onLoad(ops) {
  52. if (ops.isSelect) {
  53. this.isSelect = ops.isSelect;
  54. this.isBack = ops.isBack;
  55. }
  56. // this.getAddressList();
  57. },
  58. onShow(ops) {
  59. this.getAddressList();
  60. },
  61. methods:{
  62. click(index, index1) {
  63. if(index1 == 1) {
  64. this.delFun(index);
  65. } else {
  66. this.setDefaultAddress(index);
  67. }
  68. },
  69. // 删除
  70. delFun(index) {
  71. this.$u.api.delAddressAjax(this.list[index].id).then(({code,data})=>{
  72. if(code==1){
  73. this.list.splice(index, 1);
  74. this.list[index].show = false;
  75. }else{
  76. this.$u.toast(`删除失败`);
  77. }
  78. })
  79. },
  80. // 设为默认
  81. setDefaultAddress(index) {
  82. this.$u.api.setDefaultAddressAjax(this.list[index].id).then(({code,data})=>{
  83. uni.hideLoading();
  84. if(code==1){
  85. this.list.forEach(e=>{
  86. e.is_default = 0;
  87. e.show = false;
  88. })
  89. this.list[index].is_default = 1;
  90. }
  91. })
  92. },
  93. open(index) {
  94. // 先将正在被操作的swipeAction标记为打开状态,否则由于props的特性限制,
  95. // 原本为'false',再次设置为'false'会无效
  96. this.list[index].show = true;
  97. this.list.map((val, idx) => {
  98. if(index != idx) this.list[idx].show = false;
  99. })
  100. },
  101. getAddressList(){
  102. this.loading = true;
  103. this.$u.api.getAddressListAjax().then(({code,data})=>{
  104. this.loading = false;
  105. if(code==1){
  106. data.forEach(e=>{
  107. e.show = false;
  108. })
  109. this.list = data;
  110. console.log(this.list);
  111. }
  112. }).catch(()=>{
  113. this.loading = false;
  114. })
  115. },
  116. },
  117. };
  118. </script>
  119. <style lang="scss" scoped>
  120. .page {
  121. background-color: $app-theme-bg-color;
  122. }
  123. .list {
  124. padding: 0 30rpx 180rpx;
  125. // height: calc(100vh - 200rpx);
  126. }
  127. .btn {
  128. position: fixed;
  129. bottom: 40rpx;
  130. left: 0;
  131. padding: 30rpx;
  132. width: 100%;
  133. text {
  134. margin-left: 8rpx;
  135. }
  136. }
  137. </style>