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

328 lines
8.8 KiB

2 months 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
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
2 months 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
2 months 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
1 month ago
1 month ago
2 months ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. <template>
  2. <view style="padding-bottom: 20px;">
  3. <view class="top-user-bar">
  4. <image class="top-bar-bg" src="@/static/images/mingqi-beij@2x.png" mode="aspectFill"></image>
  5. <view class="user-info">
  6. <view class="avatar-btn" @click="toUserInfoPage">
  7. <image v-if="userInfo.avatarUrl" :src="userInfo.avatarUrl" class="avatar-img"></image>
  8. <image v-else src="@/static/images/avatar.png" class="avatar-img"></image>
  9. </view>
  10. <view class="user-info-text">
  11. <text class="user-name" @click="toUserInfoPage">{{ userInfo.nickName || '未登录' }}</text>
  12. <text class="user-card" v-if="cardNo">读者证{{ cardNo }}</text>
  13. </view>
  14. </view>
  15. <view class="user-menu">
  16. <view class="menu-item" @click="toCheckLogin('收藏')">
  17. <image class="menu-icon" src="@/static/images/menu-sc.png" mode="scaleToFill" />
  18. <text class="menu-txt">收藏</text>
  19. </view>
  20. <view class="menu-item" @click="toCheckLogin('借阅')">
  21. <image class="menu-icon" src="@/static/images/menu-jy.png" mode="scaleToFill" />
  22. <text class="menu-txt">借阅</text>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="submenu-box">
  27. <view class="submenu-item" @click="toCheckLogin('我的留言')">
  28. <uni-icons custom-prefix="iconfont" type="icon-liuyan" size="20"></uni-icons>
  29. <text class="left-txt">我的留言</text>
  30. </view>
  31. <view class="submenu-item" @click="toUserInfoPage">
  32. <uni-icons custom-prefix="iconfont" type="icon-shezhi" size="20"></uni-icons>
  33. <text class="left-txt">个人资料</text>
  34. </view>
  35. <!-- <view class="submenu-item" @click="toCheckLogin('修改密码')">
  36. <uni-icons custom-prefix="iconfont" type="icon-xiugai" size="20"></uni-icons>
  37. <text class="left-txt">修改密码</text>
  38. </view> -->
  39. <view class="submenu-item" @click="toCheckLogin('解绑读者证')">
  40. <uni-icons custom-prefix="iconfont" type="icon-UIsheji_menjinxitong-28" size="20"></uni-icons>
  41. <text class="left-txt">解绑读者证</text>
  42. </view>
  43. <!-- <view v-if="isBindLibraryCard" class="submenu-item" @click="toLogOut()">
  44. <uni-icons custom-prefix="iconfont" type="icon-tuichu" size="20"></uni-icons>
  45. <text class="left-txt">退出账号</text>
  46. </view> -->
  47. </view>
  48. <view>
  49. <uni-popup ref="alertDialog" type="dialog">
  50. <uni-popup-dialog
  51. type="info" cancelText="取消" confirmText="确定"
  52. title="提示" content="请您绑定读者证!"
  53. @confirm="dialogConfirm" @close="dialogClose">
  54. </uni-popup-dialog>
  55. </uni-popup>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import config from '@/utils/config'
  61. import { FetchBindRead, FetchFindAllReaderByOpenId } from '@/api/user';
  62. const USER_KEY = 'user-info';
  63. const READLIST = 'reader-card-list';
  64. export default {
  65. data() {
  66. return {
  67. userInfo: {},
  68. cardNo: "",
  69. isBindLibraryCard: false,
  70. };
  71. },
  72. onLoad() {
  73. this.loadUserInfo();
  74. },
  75. onShow() {
  76. this.loadUserInfo();
  77. },
  78. methods:{
  79. toUserInfoPage() {
  80. uni.navigateTo({
  81. url: '/subpkg/pages/user-info/user-info'
  82. });
  83. },
  84. // 调用接口:头像+昵称 → 成功后统一保存缓存
  85. async bindUserInfo(avatar, nickname) {
  86. const openId = uni.getStorageSync('wx_login_code') || '';
  87. const params = {
  88. avatar,
  89. libcode: "1201",
  90. nickname,
  91. openid: openId
  92. };
  93. const res = await FetchBindRead(params);
  94. const userInfo = {
  95. nickName: nickname,
  96. avatarUrl: avatar
  97. };
  98. uni.setStorageSync(USER_KEY, userInfo);
  99. this.userInfo = userInfo;
  100. },
  101. // 加载用户信息(从接口获取)
  102. async loadUserInfo() {
  103. try {
  104. const openId = uni.getStorageSync('wx_login_code');
  105. const readerList = uni.getStorageSync(READLIST) || [];
  106. // 从接口获取用户信息(昵称、头像)
  107. if (openId) {
  108. const res = await FetchFindAllReaderByOpenId({
  109. libcode: '1201',
  110. openId: openId
  111. });
  112. if (res.code === 200 && res.data) {
  113. // 只赋值用户信息,不处理读者证
  114. this.userInfo = {
  115. nickName: res.data.nickName || res.data.nickname || '',
  116. avatarUrl: res.data.avatarUrl || res.data.avatar || ''
  117. };
  118. uni.setStorageSync(USER_KEY, this.userInfo);
  119. } else {
  120. this.userInfo = uni.getStorageSync(USER_KEY) || {};
  121. }
  122. } else {
  123. this.userInfo = uni.getStorageSync(USER_KEY) || {};
  124. }
  125. // 找到默认读者证
  126. const defaultCard = readerList.find(item => item.bindDefault === true);
  127. this.cardNo = defaultCard ? defaultCard.bindValue : (readerList[0]?.bindValue || '');
  128. this.isBindLibraryCard = readerList.length > 0;
  129. } catch (err) {
  130. // 异常降级:读缓存
  131. console.error('加载用户信息失败:', err)
  132. const readerList = uni.getStorageSync(READLIST) || [];
  133. this.userInfo = uni.getStorageSync(USER_KEY) || {};
  134. const defaultCard = readerList.find(item => item.bindDefault === true);
  135. this.cardNo = defaultCard ? defaultCard.bindValue : (readerList[0]?.bindValue || '');
  136. this.isBindLibraryCard = readerList.length > 0;
  137. }
  138. },
  139. // 校验登录:只根据 有没有读者证 判断
  140. toCheckLogin(pageName) {
  141. const readerList = uni.getStorageSync(READLIST) || [];
  142. // 没有绑定读者证 → 弹窗
  143. if (readerList.length === 0) {
  144. this.dialogToggle();
  145. return;
  146. }
  147. // 已绑定 → 正常跳转
  148. const routeMap = {
  149. '借阅': '/subpkg/pages/myLending/myLending',
  150. '我的留言': '/subpkg/pages/feedback-list/feedback-list',
  151. // '修改密码': '/subpkg/pages/change-password/change-password',
  152. '收藏': '/subpkg/pages/collect-list/collect-list',
  153. // '个人资料': '/subpkg/pages/user-info/user-info',
  154. '解绑读者证': '/subpkg/pages/reader-card/reader-card'
  155. }
  156. const url = routeMap[pageName]
  157. if (url) {
  158. uni.navigateTo({ url })
  159. }
  160. },
  161. dialogToggle() {
  162. this.$refs.alertDialog.open();
  163. },
  164. dialogConfirm() {
  165. uni.navigateTo({ url: "/pages/login/login" });
  166. },
  167. dialogClose() {},
  168. // 退出登录
  169. toLogOut() {
  170. uni.showModal({
  171. title: '确认退出',
  172. content: '确定要退出当前账号吗?',
  173. success: (res) => {
  174. if (res.confirm) {
  175. // 清空缓存
  176. uni.removeStorageSync(USER_KEY);
  177. uni.removeStorageSync(READLIST);
  178. // 重置
  179. this.userInfo = {};
  180. this.cardNo = "";
  181. this.isBindLibraryCard = false;
  182. uni.showToast({ title: '退出成功', icon: 'success' });
  183. uni.switchTab({ url: "/pages/home/home" });
  184. }
  185. }
  186. });
  187. },
  188. }
  189. }
  190. </script>
  191. <style lang="scss" scoped>
  192. .top-user-bar{
  193. position: relative;
  194. width: 100%;
  195. height: 150px;
  196. display: flex;
  197. align-items: center;
  198. color: $uni-white;
  199. .top-bar-bg{
  200. position: absolute;
  201. left: 0;
  202. top: 0;
  203. display: block;
  204. width: 100%;
  205. height: 100%;
  206. z-index: 1;
  207. }
  208. .user-info{
  209. position: absolute;
  210. left: 0;
  211. top: 0;
  212. z-index: 99;
  213. display: flex;
  214. justify-content: flex-start;
  215. .avatar-btn {
  216. background: transparent;
  217. margin-top: 19px;
  218. margin-left: 28px;
  219. &::after{ border: none !important; }
  220. .avatar-img {
  221. width: 62px;
  222. height: 62px;
  223. border-radius: 50%;
  224. object-fit: cover;
  225. }
  226. }
  227. .user-info-text{
  228. display: flex;
  229. flex-direction: column;
  230. align-items: flex-start;
  231. justify-content: flex-start;
  232. margin: 26px 16px 0 16px;
  233. .user-name{
  234. font-size: 20px;
  235. font-weight: 500;
  236. color: #fff;
  237. line-height: 28px;
  238. }
  239. .user-card{
  240. margin-top: 5px;
  241. font-size: 12px;
  242. font-weight: 40;
  243. color: #fff;
  244. line-height: 17px;
  245. }
  246. }
  247. }
  248. .user-menu{
  249. position: absolute;
  250. bottom: -60px;
  251. left: 0;
  252. background-color: #fff;
  253. z-index: 99;
  254. display: flex;
  255. justify-content: flex-start;
  256. width: calc(100% - 40px);
  257. margin: 0 20px;
  258. border-radius: 6px;
  259. box-shadow: 0px 2px 15px 0px rgba(0, 0, 0, .1);
  260. .menu-item{
  261. display: flex;
  262. flex-direction: column;
  263. align-items: center;
  264. justify-content: center;
  265. padding: 0 30px;
  266. height: 100px;
  267. .menu-icon{
  268. width: 32px;
  269. height: 32px;
  270. margin-bottom: 10px;
  271. }
  272. .menu-txt{
  273. color: #636365;
  274. font-size: 13px;
  275. }
  276. }
  277. }
  278. }
  279. .submenu-box{
  280. margin-top: 80px;
  281. background-color: #fff;
  282. display: flex;
  283. flex-direction: column;
  284. align-items: center;
  285. justify-content: center;
  286. .submenu-item{
  287. display: flex;
  288. align-items: center;
  289. justify-content: flex-start;
  290. width: calc(100% - 40px);
  291. background-color: #fff;
  292. border-bottom: 1px solid #f4f4f4;
  293. height: 45px;
  294. padding: 0 20px;
  295. ::v-deep .uni-icons{
  296. color: #343434 !important;
  297. }
  298. .left-txt{
  299. color: #343434;
  300. font-size: 14px;
  301. width: calc(100% - 50px);
  302. white-space: nowrap;
  303. overflow: hidden;
  304. text-overflow: ellipsis;
  305. margin-left: 10px;
  306. }
  307. }
  308. }
  309. </style>