图书馆小程序
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.

209 lines
5.4 KiB

2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months 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. <!-- 点击整个item触发切换 -->
  6. <view
  7. class="card-list-item"
  8. v-for="(item, index) in cardList"
  9. :key="item.id"
  10. @click="handleSelectItem(item.bindValue)"
  11. :class="{ active: selectedValue === item.bindValue }"
  12. >
  13. <image class="card-left-img" src="@/static/images/card-img2.png" mode="widthFix" />
  14. <view class="card-right-info">
  15. <text class="info-title">读者证号</text>
  16. <text class="info-num">{{ item.bindValue }}</text>
  17. </view>
  18. <radio :value="item.bindValue" :checked="selectedValue === item.bindValue"/>
  19. </view>
  20. <view class="add-card-btn" @click="toAddReaderCard">
  21. <uni-icons type="plus" size="20" color="#01a4fe"></uni-icons>
  22. <text>新增读者证</text>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import { FetchFindAllReaderBindByOpenId, FetchSetDefaultReadCard } from '@/api/user';
  29. const READLIST = 'reader-card-list';
  30. export default {
  31. data() {
  32. return {
  33. selectedValue: '',
  34. cardList: [],
  35. }
  36. },
  37. onLoad() {},
  38. onShow() {
  39. this.getBindReaderCardList();
  40. },
  41. methods: {
  42. async getBindReaderCardList() {
  43. try {
  44. const openId = uni.getStorageSync('wx_login_code');
  45. if (!openId) {
  46. uni.showToast({ title: '未获取到用户信息', icon: 'none' });
  47. return;
  48. }
  49. const data = { libcode: '1201', openId: openId };
  50. const res = await FetchFindAllReaderBindByOpenId(data);
  51. if (res.code === 200 && res.data.length > 0) {
  52. this.cardList = res.data;
  53. uni.setStorageSync(READLIST, res.data);
  54. // 优先 bindDefault = true
  55. const defaultCard = this.cardList.find(item => item.bindDefault === true);
  56. this.selectedValue = defaultCard ? defaultCard.bindValue : this.cardList[0].bindValue;
  57. } else {
  58. this.cardList = [];
  59. uni.setStorageSync(READLIST, []);
  60. this.selectedValue = '';
  61. }
  62. } catch (err) {
  63. this.cardList = uni.getStorageSync(READLIST) || [];
  64. const defaultCard = this.cardList.find(item => item.bindDefault === true);
  65. this.selectedValue = defaultCard ? defaultCard.bindValue : this.cardList[0]?.bindValue || '';
  66. }
  67. },
  68. // 点击整个卡片 → 触发切换
  69. handleSelectItem(value) {
  70. const oldValue = this.selectedValue;
  71. const openId = uni.getStorageSync('wx_login_code');
  72. // 如果点击的是已经选中的,不处理
  73. if (value === oldValue) return;
  74. uni.showModal({
  75. title: '提示',
  76. content: '确定切换默认读者证吗?',
  77. success: async (res) => {
  78. if (!res.confirm) {
  79. this.selectedValue = oldValue;
  80. return;
  81. }
  82. try {
  83. const result = await FetchSetDefaultReadCard({
  84. bindType: 'rdid',
  85. bindValue: value,
  86. libcode: '1201',
  87. openid: openId
  88. });
  89. if (result.code === 200) {
  90. this.selectedValue = value;
  91. uni.setStorageSync('currentReaderCard', value);
  92. uni.showToast({ title: '默认证切换成功', icon: 'success' });
  93. } else {
  94. this.selectedValue = oldValue;
  95. uni.showToast({ title: result.msg || '切换失败', icon: 'none' });
  96. }
  97. } catch (err) {
  98. this.selectedValue = oldValue;
  99. uni.showToast({ title: '切换失败', icon: 'none' });
  100. }
  101. }
  102. });
  103. },
  104. // 废弃不用
  105. radioChange() {},
  106. toAddReaderCard() {
  107. uni.navigateTo({ url: "/pages/login/login" });
  108. },
  109. }
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .reader-card{
  114. position: relative;
  115. min-height: 100vh;
  116. background-color: #f5f5f5;
  117. .card-top-bg{
  118. position: absolute;
  119. left: 0;
  120. top: 0;
  121. width: 100%;
  122. display: block;
  123. }
  124. .card-list{
  125. position: absolute;
  126. left: 0;
  127. top: 60px;
  128. width: calc(100% - 24px);
  129. height: calc(100vh - 195px);
  130. overflow-y: auto;
  131. padding: 12px;
  132. z-index: 999;
  133. .card-list-item{
  134. display: flex;
  135. justify-content: space-between;
  136. align-items: center;
  137. padding: 10px;
  138. margin-bottom: 12px;
  139. border: 2px solid #C6C6E2;
  140. border-radius: 8px;
  141. background: rgba(241,241,249,0.4);
  142. cursor: pointer;
  143. transition: all 0.2s;
  144. &.active{
  145. border-color: #01a4fe;
  146. background: rgba(1, 164, 254, 0.1);
  147. }
  148. .card-left-img{
  149. width: 80px;
  150. margin-right: 12px;
  151. display: block;
  152. }
  153. .card-right-info{
  154. flex: 1;
  155. display: flex;
  156. flex-direction: column;
  157. .info-title{
  158. font-size: 16px;
  159. color: #333;
  160. font-weight: bold;
  161. padding-bottom: 12px;
  162. }
  163. .info-num{
  164. font-size: 14px;
  165. color: #999;
  166. }
  167. }
  168. }
  169. }
  170. .add-card-btn {
  171. display: flex;
  172. align-items: center;
  173. justify-content: center;
  174. margin-top: 15px;
  175. height: 44px;
  176. border: 1px dashed #01a4fe;
  177. border-radius: 8px;
  178. color: #01a4fe;
  179. font-size: 15px;
  180. uni-icons {
  181. margin-right: 6px;
  182. }
  183. }
  184. }
  185. </style>