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

378 lines
9.8 KiB

2 months ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
2 months 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
2 months 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 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
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
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months 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="edit-btn" @click="toggleEditMode">
  5. {{ isEditMode ? '完成' : '解绑' }}
  6. </view>
  7. <view class="card-list">
  8. <view
  9. class="card-list-item"
  10. v-for="(item, index) in cardList"
  11. :key="item.id"
  12. @click="handleSelectItem(item.bindValue)"
  13. :class="{ active: selectedValue === item.bindValue }"
  14. >
  15. <image class="card-left-img" src="@/static/images/card-img2.png" mode="widthFix" />
  16. <view class="card-right-info">
  17. <text class="info-title">读者证号</text>
  18. <text class="info-num">{{ formatCardNo(item.bindValue) }}</text>
  19. <text class="default-tag" v-if="item.bindDefault">默认证</text>
  20. </view>
  21. <!-- 编辑模式显示单选框 -->
  22. <radio v-if="isEditMode" :value="item.bindValue" :checked="selectedValue === item.bindValue"/>
  23. <!-- 非编辑模式才显示设置默认 + 二维码 -->
  24. <view class="card-setting" v-else>
  25. <button v-if="!item.bindDefault" type="default" size="mini" @click.stop="setDefaultCard(item.bindValue)">设置默认</button>
  26. <uni-icons class="erweima-style" custom-prefix="iconfont" type="icon-erweima" size="28" @click.stop="showQrcode(item.bindValue)"></uni-icons>
  27. </view>
  28. </view>
  29. <view class="add-card-btn" @click="toAddReaderCard" v-if="!isEditMode">
  30. <uni-icons type="plus" size="20" color="#01a4fe"></uni-icons>
  31. <text>新增读者证</text>
  32. </view>
  33. </view>
  34. <!-- 编辑模式显示底部解绑按钮 -->
  35. <button
  36. v-if="isEditMode"
  37. class="unbind-btn"
  38. :disabled="!selectedValue"
  39. :class="{disabled: !selectedValue}"
  40. @click="handleUnbind"
  41. >
  42. 确认解绑
  43. </button>
  44. <!-- 二维码弹窗 -->
  45. <uni-popup ref="popup" type="center" @maskClick="closePopup">
  46. <view class="qrcode-popup">
  47. <view class="qrcode-title">读者证二维码</view>
  48. <w-qrcode
  49. v-if="qrcodeText"
  50. :value="qrcodeText"
  51. :size="160"
  52. foreground="#333"
  53. background="#f5f5f5"
  54. />
  55. <view class="qrcode-num">{{ qrcodeText }}</view>
  56. <view class="close-btn" @click="closePopup">关闭</view>
  57. </view>
  58. </uni-popup>
  59. </view>
  60. </template>
  61. <script>
  62. import {
  63. FetchFindAllReaderBindByOpenId,
  64. FetchSetDefaultReadCard,
  65. FetchUnbindReadCard
  66. } from '@/api/user';
  67. import { getReaderCardList, getOpenId, setCurrentReaderCard, STORAGE_KEYS } from '@/utils/storage';
  68. export default {
  69. data() {
  70. return {
  71. isEditMode: false,
  72. selectedValue: '',
  73. cardList: [],
  74. qrcodeText: '',
  75. loading: false,
  76. }
  77. },
  78. onShow() {
  79. this.getBindReaderCardList();
  80. },
  81. methods: {
  82. formatCardNo(cardNo) {
  83. if (!cardNo) return '';
  84. const str = String(cardNo);
  85. const len = str.length;
  86. if (len > 8) {
  87. const front = str.substring(0, 2);
  88. const end = str.substring(len - 2);
  89. const star = '*'.repeat(len - 4);
  90. return front + star + end;
  91. } else {
  92. return '***' + str.substring(len - 2);
  93. }
  94. },
  95. toggleEditMode() {
  96. this.isEditMode = !this.isEditMode;
  97. this.selectedValue = '';
  98. },
  99. async getBindReaderCardList(forceRefresh = false) {
  100. if (this.loading) return;
  101. this.loading = true;
  102. try {
  103. const readerList = await getReaderCardList(forceRefresh);
  104. this.cardList = readerList;
  105. } catch (err) {
  106. console.error('[ReaderCard] Failed to load reader card list:', err);
  107. const cachedList = uni.getStorageSync(STORAGE_KEYS.READER_CARD_LIST) || [];
  108. this.cardList = cachedList;
  109. } finally {
  110. this.loading = false;
  111. }
  112. },
  113. handleSelectItem(value) {
  114. if (this.isEditMode) {
  115. this.selectedValue = this.selectedValue === value ? '' : value;
  116. }
  117. },
  118. async setDefaultCard(value) {
  119. const currentDefault = this.cardList.find(item => item.bindDefault === true);
  120. if (currentDefault && currentDefault.bindValue === value) {
  121. uni.showToast({ title: '当前已是默认证', icon: 'none' });
  122. return;
  123. }
  124. const openId = await getOpenId();
  125. if (!openId) {
  126. uni.showToast({ title: '获取用户信息失败', icon: 'none' });
  127. return;
  128. }
  129. uni.showModal({
  130. title: '提示',
  131. content: '确定设为默认读者证吗?',
  132. success: async (res) => {
  133. if (!res.confirm) return;
  134. try {
  135. await FetchSetDefaultReadCard({
  136. bindType: 'rdid',
  137. bindValue: value,
  138. libcode: '1201',
  139. openid: openId
  140. });
  141. setCurrentReaderCard(value);
  142. await this.getBindReaderCardList(true);
  143. uni.showToast({ title: '设置成功', icon: 'success' });
  144. } catch (err) {
  145. console.error('[ReaderCard] Failed to set default card:', err);
  146. uni.showToast({ title: '设置失败', icon: 'none' });
  147. }
  148. }
  149. });
  150. },
  151. showQrcode(bindValue) {
  152. this.qrcodeText = bindValue;
  153. this.$refs.popup.open();
  154. },
  155. closePopup() {
  156. console.log('关闭弹窗');
  157. this.qrcodeText = '';
  158. this.$refs.popup.close();
  159. },
  160. async handleUnbind() {
  161. if (!this.selectedValue) {
  162. uni.showToast({ title: '请选择要解绑的读者证', icon: 'none' });
  163. return;
  164. }
  165. const openId = await getOpenId();
  166. if (!openId) {
  167. uni.showToast({ title: '获取用户信息失败', icon: 'none' });
  168. return;
  169. }
  170. const unbindItem = this.cardList.find(item => item.bindValue === this.selectedValue);
  171. const isUnbindDefault = unbindItem?.bindDefault === true;
  172. uni.showModal({
  173. title: '提示',
  174. content: `确定要解绑读者证【${this.selectedValue}】吗?`,
  175. success: async (res) => {
  176. if (!res.confirm) return;
  177. try {
  178. const result = await FetchUnbindReadCard({
  179. bindType: "rdid",
  180. bindValue: this.selectedValue,
  181. libcode: "1201",
  182. openid: openId
  183. });
  184. if (result.code === 200) {
  185. uni.showToast({ title: '解绑成功', icon: 'success' });
  186. this.selectedValue = '';
  187. await this.getBindReaderCardList(true);
  188. if (isUnbindDefault && this.cardList.length > 0) {
  189. const newValue = this.cardList[0].bindValue;
  190. await FetchSetDefaultReadCard({
  191. bindType: 'rdid',
  192. bindValue: newValue,
  193. libcode: '1201',
  194. openid: openId
  195. });
  196. setCurrentReaderCard(newValue);
  197. await this.getBindReaderCardList(true);
  198. }
  199. if (this.cardList.length === 0) {
  200. setTimeout(() => uni.navigateBack(), 1500);
  201. }
  202. }
  203. } catch (err) {
  204. console.error('[ReaderCard] Failed to unbind card:', err);
  205. uni.showToast({ title: '解绑失败', icon: 'none' });
  206. }
  207. }
  208. });
  209. },
  210. toAddReaderCard() {
  211. uni.navigateTo({ url: "/pages/login/login" });
  212. },
  213. }
  214. }
  215. </script>
  216. <style lang="scss" scoped>
  217. .reader-card{
  218. position: relative;
  219. min-height: 100vh;
  220. background-color: #f5f5f5;
  221. .card-top-bg{
  222. position: absolute;
  223. left: 0;
  224. top: 0;
  225. width: 100%;
  226. }
  227. .edit-btn{
  228. position: absolute;
  229. right: 20px;
  230. top: 20px;
  231. z-index: 100;
  232. font-size: 15px;
  233. }
  234. .card-list{
  235. position: absolute;
  236. left: 0;
  237. top: 60px;
  238. width: calc(100% - 24px);
  239. height: calc(100vh - 195px);
  240. overflow-y: auto;
  241. padding: 12px;
  242. z-index: 999;
  243. .card-list-item{
  244. display: flex;
  245. justify-content: space-between;
  246. align-items: center;
  247. padding: 10px;
  248. margin-bottom: 12px;
  249. border: 2px solid #C6C6E2;
  250. border-radius: 8px;
  251. background: rgba(241,241,249,0.4);
  252. cursor: pointer;
  253. transition: all 0.2s;
  254. &.active{
  255. border-color: #01a4fe;
  256. background: rgba(1, 164, 254, 0.1);
  257. }
  258. .card-left-img{
  259. width: 80px;
  260. margin-right: 12px;
  261. display: block;
  262. }
  263. .card-right-info{
  264. flex: 1;
  265. display: flex;
  266. flex-direction: column;
  267. .info-title{
  268. font-size: 16px;
  269. color: #333;
  270. font-weight: bold;
  271. padding-bottom: 4px;
  272. }
  273. .info-num{
  274. font-size: 14px;
  275. color: #999;
  276. }
  277. .default-tag{
  278. width: 46px;
  279. font-size: 12px;
  280. color: #01a4fe;
  281. border:1px solid #01a4fe;
  282. border-radius: 4px;
  283. padding: 2px 0;
  284. text-align: center;
  285. display: inline-block;
  286. margin-top: 4px;
  287. }
  288. }
  289. }
  290. }
  291. .add-card-btn {
  292. display: flex;
  293. align-items: center;
  294. justify-content: center;
  295. margin-top: 15px;
  296. height: 44px;
  297. border: 1px dashed #01a4fe;
  298. border-radius: 8px;
  299. color: #01a4fe;
  300. font-size: 15px;
  301. uni-icons {
  302. margin-right: 6px;
  303. }
  304. }
  305. .unbind-btn{
  306. position: fixed;
  307. bottom: 40px;
  308. left: 0;
  309. right: 0;
  310. width: calc(100% - 40px);
  311. margin: 0 auto;
  312. padding: 4px 0;
  313. color: #fff;
  314. background-color: #01a4fe;
  315. border-radius: 23px;
  316. font-size: 16px;
  317. &.disabled{
  318. background-color: #ccc;
  319. }
  320. }
  321. }
  322. .card-setting{
  323. display: flex;
  324. flex-direction: column;
  325. align-items: center;
  326. justify-content: center;
  327. button{
  328. padding: 0 8px !important;
  329. margin-bottom: 6px;
  330. font-size: 12px;
  331. }
  332. ::v-deep .icon-erweima{
  333. color: #999 !important;
  334. }
  335. }
  336. </style>