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

379 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
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, clearReaderCardCache, STORAGE_KEYS } from '@/utils/storage';
  68. import config from '@/utils/config';
  69. export default {
  70. data() {
  71. return {
  72. isEditMode: false,
  73. selectedValue: '',
  74. cardList: [],
  75. qrcodeText: '',
  76. loading: false,
  77. }
  78. },
  79. onShow() {
  80. this.getBindReaderCardList();
  81. },
  82. methods: {
  83. formatCardNo(cardNo) {
  84. if (!cardNo) return '';
  85. const str = String(cardNo);
  86. const len = str.length;
  87. if (len > 8) {
  88. const front = str.substring(0, 2);
  89. const end = str.substring(len - 2);
  90. const star = '*'.repeat(len - 4);
  91. return front + star + end;
  92. } else {
  93. return '***' + str.substring(len - 2);
  94. }
  95. },
  96. toggleEditMode() {
  97. this.isEditMode = !this.isEditMode;
  98. this.selectedValue = '';
  99. },
  100. async getBindReaderCardList(forceRefresh = false) {
  101. if (this.loading) return;
  102. this.loading = true;
  103. try {
  104. const readerList = await getReaderCardList(forceRefresh);
  105. this.cardList = readerList;
  106. } catch (err) {
  107. console.error(err);
  108. const cachedList = uni.getStorageSync(STORAGE_KEYS.READER_CARD_LIST) || [];
  109. this.cardList = cachedList;
  110. } finally {
  111. this.loading = false;
  112. }
  113. },
  114. handleSelectItem(value) {
  115. if (this.isEditMode) {
  116. this.selectedValue = this.selectedValue === value ? '' : value;
  117. }
  118. },
  119. async setDefaultCard(value) {
  120. const currentDefault = this.cardList.find(item => item.bindDefault === true);
  121. if (currentDefault && currentDefault.bindValue === value) {
  122. uni.showToast({ title: '当前已是默认证', icon: 'none' });
  123. return;
  124. }
  125. const openId = await getOpenId();
  126. if (!openId) {
  127. uni.showToast({ title: '获取用户信息失败', icon: 'none' });
  128. return;
  129. }
  130. uni.showModal({
  131. title: '提示',
  132. content: `确定设置【${value}】为默认读者证吗?`,
  133. success: async (res) => {
  134. if (!res.confirm) return;
  135. try {
  136. await FetchSetDefaultReadCard({
  137. bindType: 'rdid',
  138. bindValue: value,
  139. libcode: config.LIB_CODE,
  140. openid: openId
  141. });
  142. setCurrentReaderCard(value);
  143. await this.getBindReaderCardList(true);
  144. uni.showToast({ title: '设置成功', icon: 'success' });
  145. } catch (err) {
  146. console.error(err);
  147. uni.showToast({ title: '设置失败', icon: 'none' });
  148. }
  149. }
  150. });
  151. },
  152. showQrcode(bindValue) {
  153. this.qrcodeText = bindValue;
  154. this.$refs.popup.open();
  155. },
  156. closePopup() {
  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: config.LIB_CODE,
  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: config.LIB_CODE,
  194. openid: openId
  195. });
  196. setCurrentReaderCard(newValue);
  197. await this.getBindReaderCardList(true);
  198. }
  199. if (this.cardList.length === 0) {
  200. clearReaderCardCache();
  201. setTimeout(() => uni.navigateBack(), 1000);
  202. }
  203. }
  204. } catch (err) {
  205. console.error(err);
  206. uni.showToast({ title: '解绑失败', icon: 'none' });
  207. }
  208. }
  209. });
  210. },
  211. toAddReaderCard() {
  212. uni.navigateTo({ url: "/pages/login/login" });
  213. },
  214. }
  215. }
  216. </script>
  217. <style lang="scss" scoped>
  218. .reader-card{
  219. position: relative;
  220. min-height: 100vh;
  221. background-color: #f5f5f5;
  222. .card-top-bg{
  223. position: absolute;
  224. left: 0;
  225. top: 0;
  226. width: 100%;
  227. }
  228. .edit-btn{
  229. position: absolute;
  230. right: 20px;
  231. top: 20px;
  232. z-index: 100;
  233. font-size: 15px;
  234. }
  235. .card-list{
  236. position: absolute;
  237. left: 0;
  238. top: 60px;
  239. width: calc(100% - 24px);
  240. height: calc(100vh - 195px);
  241. overflow-y: auto;
  242. padding: 12px;
  243. z-index: 999;
  244. .card-list-item{
  245. display: flex;
  246. justify-content: space-between;
  247. align-items: center;
  248. padding: 10px;
  249. margin-bottom: 12px;
  250. border: 2px solid #C6C6E2;
  251. border-radius: 8px;
  252. background: rgba(241,241,249,0.4);
  253. cursor: pointer;
  254. transition: all 0.2s;
  255. &.active{
  256. border-color: #01a4fe;
  257. background: rgba(1, 164, 254, 0.1);
  258. }
  259. .card-left-img{
  260. width: 80px;
  261. margin-right: 12px;
  262. display: block;
  263. }
  264. .card-right-info{
  265. flex: 1;
  266. display: flex;
  267. flex-direction: column;
  268. .info-title{
  269. font-size: 16px;
  270. color: #333;
  271. font-weight: bold;
  272. padding-bottom: 4px;
  273. }
  274. .info-num{
  275. font-size: 14px;
  276. color: #999;
  277. }
  278. .default-tag{
  279. width: 46px;
  280. font-size: 12px;
  281. color: #01a4fe;
  282. border:1px solid #01a4fe;
  283. border-radius: 4px;
  284. padding: 2px 0;
  285. text-align: center;
  286. display: inline-block;
  287. margin-top: 4px;
  288. }
  289. }
  290. }
  291. }
  292. .add-card-btn {
  293. display: flex;
  294. align-items: center;
  295. justify-content: center;
  296. margin-top: 15px;
  297. height: 44px;
  298. border: 1px dashed #01a4fe;
  299. border-radius: 8px;
  300. color: #01a4fe;
  301. font-size: 15px;
  302. uni-icons {
  303. margin-right: 6px;
  304. }
  305. }
  306. .unbind-btn{
  307. position: fixed;
  308. bottom: 40px;
  309. left: 0;
  310. right: 0;
  311. width: calc(100% - 40px);
  312. margin: 0 auto;
  313. padding: 4px 0;
  314. color: #fff;
  315. background-color: #01a4fe;
  316. border-radius: 23px;
  317. font-size: 16px;
  318. &.disabled{
  319. background-color: #ccc;
  320. }
  321. }
  322. }
  323. .card-setting{
  324. display: flex;
  325. flex-direction: column;
  326. align-items: center;
  327. justify-content: center;
  328. button{
  329. padding: 0 8px !important;
  330. margin-bottom: 6px;
  331. font-size: 12px;
  332. }
  333. ::v-deep .icon-erweima{
  334. color: #999 !important;
  335. }
  336. }
  337. </style>