changeChain.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div class="changeChain">
  3. <van-nav-bar class="navBar" title="切换经销商" left-arrow @click-left="onClickLeft" />
  4. <div class="content">
  5. <div class="module">
  6. <div
  7. class="box"
  8. v-if="userInfo && userInfo.userMultipleChains && userInfo.userMultipleChains.length > 0">
  9. <van-radio-group v-model="selectedChainIndex" @change="chainChange">
  10. <van-radio
  11. v-for="(value, index) in userInfo.userMultipleChains"
  12. :key="`${value.chainCode}-${index}`"
  13. :name="index">
  14. {{ value.chainName }}
  15. </van-radio>
  16. </van-radio-group>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. import { mapState } from 'vuex';
  24. import { switchChainIdentity } from '@/api/week';
  25. import store from '@/store';
  26. export default {
  27. name: 'changeChain',
  28. computed: {
  29. ...mapState({
  30. userInfo: (state) => state.user.userInfo,
  31. }),
  32. },
  33. data() {
  34. return {
  35. selectedChainIndex: null,
  36. };
  37. },
  38. watch: {
  39. 'userInfo.userMultipleChains'(val) {
  40. this.initSelectedChain(val);
  41. },
  42. },
  43. created() {
  44. this.initSelectedChain(this.userInfo && this.userInfo.userMultipleChains);
  45. },
  46. methods: {
  47. initSelectedChain(val) {
  48. if (val && val.length > 0) {
  49. const currentIndex = val.findIndex((item) => item.check);
  50. this.selectedChainIndex = currentIndex >= 0 ? currentIndex : 0;
  51. }
  52. },
  53. chainChange(index) {
  54. const chain = this.userInfo.userMultipleChains[index];
  55. if (!chain) {
  56. return;
  57. }
  58. this.toastLoading(0, '切换中,请稍候...', true);
  59. switchChainIdentity({ userMultipleChainId: chain.multipleChainId }).then((res) => {
  60. this.toastLoading().clear();
  61. if (res.code === 200) {
  62. store.dispatch('getUserInfo').then(() => {
  63. this.$toast('切换成功');
  64. });
  65. } else {
  66. this.$toast(res.message || '切换失败');
  67. }
  68. });
  69. },
  70. onClickLeft() {
  71. this.$router.go(-1);
  72. },
  73. },
  74. };
  75. </script>
  76. <style lang="scss" scoped>
  77. .changeChain {
  78. width: 100%;
  79. height: 100%;
  80. .content {
  81. margin: 10px 16px;
  82. .module {
  83. margin-bottom: 10px;
  84. .title {
  85. font-size: 16px;
  86. padding: 10px 0;
  87. }
  88. .box {
  89. background: #fff;
  90. display: flex;
  91. align-items: center;
  92. justify-content: space-between;
  93. padding: 10px;
  94. font-size: 16px;
  95. .van-radio {
  96. padding: 8px 0;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. </style>