图书馆小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.2 KiB

1 month ago
  1. <template>
  2. <view class="reader-card">
  3. <image class="card-top-bg" src="@/static/images/card-img1.png" mode="widthFix" />
  4. <view class="card-list">
  5. <view
  6. class="card-list-item"
  7. v-for="(item, index) in cardList"
  8. :key="item.id"
  9. @click="handleSelectItem(item.bindValue)"
  10. :class="{ active: selectedValue === item.bindValue }"
  11. >
  12. <image class="card-left-img" src="@/static/images/card-img2.png" mode="widthFix" />
  13. <view class="card-right-info">
  14. <text class="info-title">读者证号</text>
  15. <text class="info-num">{{ item.bindValue }}</text>
  16. </view>
  17. <radio :value="item.bindValue" :checked="selectedValue === item.bindValue"/>
  18. </view>
  19. <view class="add-card-btn" @click="toAddReaderCard">
  20. <uni-icons type="plus" size="20" color="#01a4fe"></uni-icons>
  21. <text>新增读者证</text>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import { FetchFindAllReaderBindByOpenId, FetchSetDefaultReadCard } from '@/api/user';
  28. const READLIST = 'reader-card-list';
  29. export default {
  30. data() {
  31. return {
  32. selectedValue: '',
  33. cardList: [],
  34. }
  35. },
  36. onLoad() {},
  37. onShow() {
  38. this.getBindReaderCardList();
  39. },
  40. methods: {
  41. async getBindReaderCardList() {
  42. try {
  43. const openId = uni.getStorageSync('wx_login_code');
  44. if (!openId) {
  45. uni.showToast({ title: '未获取到用户信息', icon: 'none' });
  46. return;
  47. }
  48. const data = { libcode: '1201', openId: openId };
  49. const res = await FetchFindAllReaderBindByOpenId(data);
  50. if (res.code === 200 && res.data.length > 0) {
  51. this.cardList = res.data;
  52. uni.setStorageSync(READLIST, res.data);
  53. // 优先 bindDefault = true
  54. const defaultCard = this.cardList.find(item => item.bindDefault === true);
  55. this.selectedValue = defaultCard ? defaultCard.bindValue : this.cardList[0].bindValue;
  56. } else {
  57. this.cardList = [];
  58. uni.setStorageSync(READLIST, []);
  59. this.selectedValue = '';
  60. }
  61. } catch (err) {
  62. this.cardList = uni.getStorageSync(READLIST) || [];
  63. const defaultCard = this.cardList.find(item => item.bindDefault === true);
  64. this.selectedValue = defaultCard ? defaultCard.bindValue : this.cardList[0]?.bindValue || '';
  65. }
  66. },
  67. // 点击整个卡片 → 触发切换
  68. handleSelectItem(value) {
  69. const oldValue = this.selectedValue;
  70. const openId = uni.getStorageSync('wx_login_code');
  71. if (value === oldValue) return;
  72. uni.showModal({
  73. title: '提示',
  74. content: '确定切换默认读者证吗?',
  75. success: async (res) => {
  76. if (!res.confirm) {
  77. this.selectedValue = oldValue;
  78. return;
  79. }
  80. try {
  81. const result = await FetchSetDefaultReadCard({
  82. bindType: 'rdid',
  83. bindValue: value,
  84. libcode: '1201',
  85. openid: openId
  86. });
  87. if (result.code === 200) {
  88. this.selectedValue = value;
  89. uni.setStorageSync('currentReaderCard', value);
  90. uni.showToast({ title: '默认证切换成功', icon: 'success' });
  91. } else {
  92. this.selectedValue = oldValue;
  93. uni.showToast({ title: result.msg || '切换失败', icon: 'none' });
  94. }
  95. } catch (err) {
  96. this.selectedValue = oldValue;
  97. uni.showToast({ title: '切换失败', icon: 'none' });
  98. }
  99. }
  100. });
  101. },
  102. toAddReaderCard() {
  103. uni.navigateTo({ url: "/pages/login/login" });
  104. },
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .reader-card{
  110. position: relative;
  111. min-height: 100vh;
  112. background-color: #f5f5f5;
  113. .card-top-bg{
  114. position: absolute;
  115. left: 0;
  116. top: 0;
  117. width: 100%;
  118. display: block;
  119. }
  120. .add-card-btn {
  121. display: flex;
  122. align-items: center;
  123. justify-content: center;
  124. margin-top: 15px;
  125. height: 44px;
  126. border: 1px dashed #01a4fe;
  127. border-radius: 8px;
  128. color: #01a4fe;
  129. font-size: 15px;
  130. uni-icons {
  131. margin-right: 6px;
  132. }
  133. }
  134. }
  135. </style>