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.
|
|
<template> <view class="reader-card"> <image class="card-top-bg" src="@/static/images/card-img1.png" mode="widthFix" /> <view class="card-list"> <view class="card-list-item" v-for="(item, index) in cardList" :key="item.id" @click="handleSelectItem(item.bindValue)" :class="{ active: selectedValue === item.bindValue }" > <image class="card-left-img" src="@/static/images/card-img2.png" mode="widthFix" /> <view class="card-right-info"> <text class="info-title">读者证号</text> <text class="info-num">{{ item.bindValue }}</text> </view> <radio :value="item.bindValue" :checked="selectedValue === item.bindValue"/> </view>
<view class="add-card-btn" @click="toAddReaderCard"> <uni-icons type="plus" size="20" color="#01a4fe"></uni-icons> <text>新增读者证</text> </view> </view> </view></template>
<script>import { FetchFindAllReaderBindByOpenId, FetchSetDefaultReadCard } from '@/api/user';const READLIST = 'reader-card-list';
export default { data() { return { selectedValue: '', cardList: [], } }, onLoad() {}, onShow() { this.getBindReaderCardList(); }, methods: { async getBindReaderCardList() { try { const openId = uni.getStorageSync('wx_login_code'); if (!openId) { uni.showToast({ title: '未获取到用户信息', icon: 'none' }); return; }
const data = { libcode: '1201', openId: openId }; const res = await FetchFindAllReaderBindByOpenId(data);
if (res.code === 200 && res.data.length > 0) { this.cardList = res.data; uni.setStorageSync(READLIST, res.data);
// 优先 bindDefault = true
const defaultCard = this.cardList.find(item => item.bindDefault === true); this.selectedValue = defaultCard ? defaultCard.bindValue : this.cardList[0].bindValue; } else { this.cardList = []; uni.setStorageSync(READLIST, []); this.selectedValue = ''; } } catch (err) { this.cardList = uni.getStorageSync(READLIST) || []; const defaultCard = this.cardList.find(item => item.bindDefault === true); this.selectedValue = defaultCard ? defaultCard.bindValue : this.cardList[0]?.bindValue || ''; } },
// 点击整个卡片 → 触发切换
handleSelectItem(value) { const oldValue = this.selectedValue; const openId = uni.getStorageSync('wx_login_code');
if (value === oldValue) return;
uni.showModal({ title: '提示', content: '确定切换默认读者证吗?', success: async (res) => { if (!res.confirm) { this.selectedValue = oldValue; return; }
try { const result = await FetchSetDefaultReadCard({ bindType: 'rdid', bindValue: value, libcode: '1201', openid: openId });
if (result.code === 200) { this.selectedValue = value; uni.setStorageSync('currentReaderCard', value); uni.showToast({ title: '默认证切换成功', icon: 'success' }); } else { this.selectedValue = oldValue; uni.showToast({ title: result.msg || '切换失败', icon: 'none' }); } } catch (err) { this.selectedValue = oldValue; uni.showToast({ title: '切换失败', icon: 'none' }); } } }); }, toAddReaderCard() { uni.navigateTo({ url: "/pages/login/login" }); }, }}</script>
<style lang="scss" scoped>.reader-card{ position: relative; min-height: 100vh; background-color: #f5f5f5; .card-top-bg{ position: absolute; left: 0; top: 0; width: 100%; display: block; }
.add-card-btn { display: flex; align-items: center; justify-content: center; margin-top: 15px; height: 44px; border: 1px dashed #01a4fe; border-radius: 8px; color: #01a4fe; font-size: 15px;
uni-icons { margin-right: 6px; } }}</style>
|