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

375 lines
10 KiB

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