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

381 lines
9.9 KiB

<template>
<view class="reader-card">
<image class="card-top-bg" src="@/static/images/card-img1.png" mode="widthFix" />
<view class="edit-btn" @click="toggleEditMode">
{{ isEditMode ? '完成' : '解绑' }}
</view>
<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">{{ formatCardNo(item.bindValue) }}</text>
<text class="default-tag" v-if="item.bindDefault">默认证</text>
</view>
<!-- 编辑模式显示单选框 -->
<radio v-if="isEditMode" :value="item.bindValue" :checked="selectedValue === item.bindValue"/>
<!-- 非编辑模式才显示:设置默认 + 二维码 -->
<view class="card-setting" v-else>
<button v-if="!item.bindDefault" type="default" size="mini" @click.stop="setDefaultCard(item.bindValue)">设置默认</button>
<uni-icons class="erweima-style" custom-prefix="iconfont" type="icon-erweima" size="28" @click.stop="showQrcode(item.bindValue)"></uni-icons>
</view>
</view>
<view class="add-card-btn" @click="toAddReaderCard" v-if="!isEditMode">
<uni-icons type="plus" size="20" color="#01a4fe"></uni-icons>
<text>新增读者证</text>
</view>
</view>
<!-- 编辑模式显示底部解绑按钮 -->
<button
v-if="isEditMode"
class="unbind-btn"
:disabled="!selectedValue"
:class="{disabled: !selectedValue}"
@click="handleUnbind"
>
确认解绑
</button>
<!-- 二维码弹窗 -->
<uni-popup ref="popup" type="center" @maskClick="closePopup">
<view class="qrcode-popup">
<view class="qrcode-title">读者证二维码</view>
<w-qrcode
v-if="qrcodeText"
:value="qrcodeText"
:size="160"
foreground="#333"
background="#f5f5f5"
/>
<view class="qrcode-num">{{ qrcodeText }}</view>
<view class="close-btn" @click="closePopup">关闭</view>
</view>
</uni-popup>
</view>
</template>
<script>
import {
FetchFindAllReaderBindByOpenId,
FetchSetDefaultReadCard,
FetchUnbindReadCard
} from '@/api/user';
const READLIST = 'reader-card-list';
export default {
data() {
return {
isEditMode: false,
selectedValue: '',
cardList: [],
qrcodeText: '' // 二维码内容
}
},
onShow() {
this.getBindReaderCardList();
},
methods: {
// 格式化读者证号
formatCardNo(cardNo) {
if (!cardNo) return '';
const str = String(cardNo);
const len = str.length;
if (len > 8) {
// 前2位 + 中间全部* + 后2位
const front = str.substring(0, 2);
const end = str.substring(len - 2);
const star = '*'.repeat(len - 4); // 中间全部隐藏
return front + star + end;
} else {
// 小于等于8:*** + 后2位
return '***' + str.substring(len - 2);
}
},
// 切换编辑模式 + 清空选中
toggleEditMode() {
this.isEditMode = !this.isEditMode;
this.selectedValue = '';
},
// 获取列表
async getBindReaderCardList() {
try {
const openId = uni.getStorageSync('wx_login_code');
if (!openId) return;
const res = await FetchFindAllReaderBindByOpenId({
libcode: '1201',
openId: openId
});
if (res.code === 200 && res.data.length > 0) {
this.cardList = res.data;
uni.setStorageSync(READLIST, res.data);
} else {
this.cardList = [];
this.selectedValue = '';
uni.setStorageSync(READLIST, []);
}
} catch (err) {
this.cardList = uni.getStorageSync(READLIST) || [];
}
},
// 点击卡片(仅编辑模式使用)
handleSelectItem(value) {
if (this.isEditMode) {
this.selectedValue = this.selectedValue === value ? '' : value;
}
},
async setDefaultCard(value) {
const currentDefault = this.cardList.find(item => item.bindDefault === true);
if (currentDefault && currentDefault.bindValue === value) {
uni.showToast({ title: '当前已是默认证', icon: 'none' });
return;
}
const openId = uni.getStorageSync('wx_login_code');
uni.showModal({
title: '提示',
content: '确定设为默认读者证吗?',
success: async (res) => {
if (!res.confirm) return;
try {
await FetchSetDefaultReadCard({
bindType: 'rdid',
bindValue: value,
libcode: '1201',
openid: openId
});
uni.setStorageSync('currentReaderCard', value);
await this.getBindReaderCardList();
uni.showToast({ title: '设置成功', icon: 'success' });
} catch (err) {
uni.showToast({ title: '设置失败', icon: 'none' });
}
}
});
},
// ========== 新功能:显示二维码 ==========
showQrcode(bindValue) {
this.qrcodeText = bindValue;
this.$refs.popup.open();
},
closePopup() {
console.log('关闭弹窗');
this.qrcodeText = '';
this.$refs.popup.close();
},
// 解绑(不变)
async handleUnbind() {
if (!this.selectedValue) {
uni.showToast({ title: '请选择要解绑的读者证', icon: 'none' });
return;
}
const openId = uni.getStorageSync('wx_login_code');
const unbindItem = this.cardList.find(item => item.bindValue === this.selectedValue);
const isUnbindDefault = unbindItem?.bindDefault === true;
uni.showModal({
title: '提示',
content: `确定要解绑读者证【${this.selectedValue}】吗?`,
success: async (res) => {
if (!res.confirm) return;
try {
const result = await FetchUnbindReadCard({
bindType: "rdid",
bindValue: this.selectedValue,
libcode: "1201",
openid: openId
});
if (result.code === 200) {
uni.showToast({ title: '解绑成功', icon: 'success' });
this.selectedValue = '';
await this.getBindReaderCardList();
if (isUnbindDefault && this.cardList.length > 0) {
const newValue = this.cardList[0].bindValue;
await FetchSetDefaultReadCard({
bindType: 'rdid',
bindValue: newValue,
libcode: '1201',
openid: openId
});
uni.setStorageSync('currentReaderCard', newValue);
await this.getBindReaderCardList();
}
if (this.cardList.length === 0) {
setTimeout(() => uni.navigateBack(), 1500);
}
}
} catch (err) {
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%;
}
.edit-btn{
position: absolute;
right: 20px;
top: 20px;
z-index: 100;
font-size: 15px;
}
// 读者证列表
.card-list{
position: absolute;
left: 0;
top: 60px;
width: calc(100% - 24px);
height: calc(100vh - 195px);
overflow-y: auto;
padding: 12px;
z-index: 999;
.card-list-item{
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
margin-bottom: 12px;
border: 2px solid #C6C6E2;
border-radius: 8px;
background: rgba(241,241,249,0.4);
cursor: pointer;
transition: all 0.2s;
&.active{
border-color: #01a4fe;
background: rgba(1, 164, 254, 0.1);
}
.card-left-img{
width: 80px;
margin-right: 12px;
display: block;
}
.card-right-info{
flex: 1;
display: flex;
flex-direction: column;
.info-title{
font-size: 16px;
color: #333;
font-weight: bold;
padding-bottom: 4px;
}
.info-num{
font-size: 14px;
color: #999;
}
.default-tag{
width: 46px;
font-size: 12px;
color: #01a4fe;
border: 1px solid #01a4fe;
border-radius: 4px;
padding: 2px 0;
text-align: center;
display: inline-block;
margin-top: 4px;
}
}
}
}
.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;
}
}
.unbind-btn{
position: fixed;
bottom: 40px;
left: 0;
right: 0;
width: calc(100% - 40px);
margin: 0 auto;
padding: 4px 0;
color: #fff;
background-color: #01a4fe;
border-radius: 23px;
font-size: 16px;
&.disabled{
background-color: #ccc;
}
}
}
.card-setting{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
button{
padding: 0 8px !important;
margin-bottom: 6px;
font-size: 12px;
}
::v-deep .icon-erweima{
color: #999 !important;
}
}
</style>