index.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <page title="收货地址" ref="pageRef" nav-color="transparent">
  3. <empty v-if="isEmpty" :top="100" />
  4. <view>
  5. <cell
  6. v-for="(item, index) in list"
  7. :data="item"
  8. :key="index"
  9. @delete="onDelete(item)"
  10. @setAddress="setAddress(item)"
  11. :type="type"
  12. />
  13. </view>
  14. <view class="marginX14 marginT30">
  15. <cm-button @click="showAdd">添加新地址</cm-button>
  16. </view>
  17. </page>
  18. </template>
  19. <script>
  20. import empty from '@/components/empty'
  21. import cell from './cell'
  22. import loginMixin from '@/mixin/login'
  23. export default {
  24. mixins: [loginMixin],
  25. components: { empty, cell },
  26. data() {
  27. return {
  28. list: [],
  29. isEmpty: false,
  30. type: 0, // 0正常进,1选择
  31. tradeNo: ''
  32. }
  33. },
  34. onLoad(options) {
  35. this.type = options.type
  36. this.tradeNo = options.tradeNo || ''
  37. this.$event.on(this.$event.key.ADDRESS, this.getData)
  38. },
  39. mounted() {
  40. setTimeout(() => {
  41. this.getData(true)
  42. }, 100)
  43. },
  44. onUnload() {
  45. this.$event.off(this.$event.key.ADDRESS)
  46. },
  47. onPullDownRefresh() {
  48. this.getData(false)
  49. },
  50. methods: {
  51. init() {
  52. this.getData()
  53. },
  54. async getData(loading = false) {
  55. const res = await this.$service.address.list(loading)
  56. this.list = res || []
  57. uni.stopPullDownRefresh()
  58. this.isEmpty = this.list.length === 0
  59. },
  60. showAdd() {
  61. this.$router.push('address_edit')
  62. },
  63. onDelete(item) {
  64. this.$message.confirm('确定删除吗?', async () => {
  65. const res = await this.$service.address.delete(item.id)
  66. res && this.getData(false)
  67. })
  68. },
  69. async setAddress(data) {
  70. if (this.tradeNo) {
  71. await this.$service.address
  72. .updateAddress({
  73. addressBookId: data.id,
  74. tradeNo: this.tradeNo
  75. })
  76. .then(res => {
  77. if (res.code === '0') {
  78. this.$message.success('修改成功!')
  79. this.$router.back()
  80. }
  81. })
  82. }
  83. }
  84. }
  85. }
  86. </script>