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

188 lines
4.8 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
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>
  20. <button
  21. class="unbind-btn"
  22. :disabled="!selectedValue"
  23. :class="{disabled: !selectedValue}"
  24. @click="handleUnbind"
  25. >
  26. 确认解绑
  27. </button>
  28. </view>
  29. </template>
  30. <script>
  31. import {
  32. FetchFindAllReaderBindByOpenId,
  33. FetchUnbindReadCard,
  34. FetchSetDefaultReadCard
  35. } from '@/api/user';
  36. const READLIST = 'reader-card-list';
  37. export default {
  38. data() {
  39. return {
  40. selectedValue: '',
  41. cardList: [],
  42. }
  43. },
  44. onShow() {
  45. this.getBindReaderCardList();
  46. },
  47. methods: {
  48. async getBindReaderCardList() {
  49. try {
  50. const openId = uni.getStorageSync('wx_login_code');
  51. if (!openId) return;
  52. const res = await FetchFindAllReaderBindByOpenId({
  53. libcode: '1201',
  54. openId: openId
  55. });
  56. if (res.code === 200) {
  57. this.cardList = res.data;
  58. uni.setStorageSync(READLIST, res.data);
  59. } else {
  60. this.cardList = [];
  61. uni.setStorageSync(READLIST, []);
  62. }
  63. } catch (err) {
  64. this.cardList = uni.getStorageSync(READLIST) || [];
  65. }
  66. },
  67. handleSelectItem(value) {
  68. if (this.selectedValue === value) {
  69. this.selectedValue = '';
  70. } else {
  71. this.selectedValue = value;
  72. }
  73. },
  74. // 确认解绑
  75. async handleUnbind() {
  76. if (!this.selectedValue) {
  77. uni.showToast({ title: '请选择要解绑的读者证', icon: 'none' });
  78. return;
  79. }
  80. const openId = uni.getStorageSync('wx_login_code');
  81. if (!openId) return;
  82. // 判断:当前解绑的是不是【默认证】
  83. const unbindItem = this.cardList.find(item => item.bindValue === this.selectedValue);
  84. const isUnbindDefault = unbindItem?.bindDefault === true;
  85. uni.showModal({
  86. title: '提示',
  87. content: `确定要解绑读者证【${this.selectedValue}】吗?`,
  88. success: async (res) => {
  89. if (!res.confirm) return;
  90. try {
  91. // 1. 执行解绑
  92. const result = await FetchUnbindReadCard({
  93. bindType: "rdid",
  94. bindValue: this.selectedValue,
  95. libcode: "1201",
  96. openid: openId
  97. });
  98. if (result.code !== 200) {
  99. uni.showToast({ title: result.msg || '解绑失败', icon: 'none' });
  100. return;
  101. }
  102. uni.showToast({ title: '解绑成功', icon: 'success' });
  103. this.selectedValue = '';
  104. // 刷新列表
  105. await this.getBindReaderCardList();
  106. if (isUnbindDefault && this.cardList.length > 0) {
  107. const newDefaultCard = this.cardList[0];
  108. const newValue = newDefaultCard.bindValue;
  109. const setResult = await FetchSetDefaultReadCard({
  110. bindType: 'rdid',
  111. bindValue: newValue,
  112. libcode: '1201',
  113. openid: openId
  114. });
  115. if (setResult.code === 200) {
  116. uni.setStorageSync('currentReaderCard', newValue);
  117. // 刷新列表,让界面显示新默认
  118. await this.getBindReaderCardList();
  119. uni.showToast({ title: '已自动设置默认证', icon: 'success' });
  120. }
  121. }
  122. if (this.cardList.length === 0) {
  123. setTimeout(() => {
  124. uni.navigateBack();
  125. }, 1500);
  126. }
  127. } catch (err) {
  128. uni.showToast({ title: '解绑失败', icon: 'none' });
  129. }
  130. }
  131. });
  132. },
  133. }
  134. }
  135. </script>
  136. <style lang="scss" scoped>
  137. .reader-card{
  138. position: relative;
  139. min-height: 100vh;
  140. background-color: #f5f5f5;
  141. .card-top-bg{
  142. position: absolute;
  143. left: 0;
  144. top: 0;
  145. width: 100%;
  146. display: block;
  147. }
  148. .unbind-btn{
  149. position: fixed;
  150. bottom: 40px;
  151. left: 0;
  152. right: 0;
  153. width: calc(100% - 40px);
  154. margin: 0 auto;
  155. padding: 4px 0;
  156. color: #fff;
  157. background-color: #01a4fe;
  158. border-radius: 23px;
  159. font-size: 16px;
  160. &.disabled{
  161. background-color: #ccc;
  162. }
  163. }
  164. }
  165. </style>