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

483 lines
12 KiB

1 month ago
  1. <template>
  2. <view class="activity-list">
  3. <!-- tab栏 -->
  4. <view class="tab-box">
  5. <view
  6. class="tab-item"
  7. v-for="(tab,idx) in tabList"
  8. :key="idx"
  9. :class="{active:activeTab === idx}"
  10. @click="switchTab(idx)"
  11. >
  12. {{tab.label}}
  13. </view>
  14. </view>
  15. <scroll-view
  16. scroll-y
  17. @scrolltolower="onScrollLower"
  18. lower-threshold="150"
  19. class="scroll-view"
  20. >
  21. <view v-if="loading" class="loading-box">
  22. <text class="loading-text">加载中...</text>
  23. </view>
  24. <!-- 左滑组件 uni-swipe-action -->
  25. <!-- :disabled="!(item.status === '未开始' && !item.isCancel)" -->
  26. <uni-swipe-action>
  27. <uni-swipe-action-item
  28. v-for="item in activityList"
  29. :key="item.signId"
  30. >
  31. <!-- 左侧主体内容 -->
  32. <view class="activity-item" @click="toActivityDetail(item)">
  33. <view class="activity-info">
  34. <view class="title-row">
  35. <text class="title">{{ item.title }}</text>
  36. <text class="status-tag" :class="getStatusClass(item.status)">
  37. {{ item.status }}
  38. </text>
  39. </view>
  40. <view class="item-info">
  41. <text class="label">开始时间</text>
  42. <text class="value">{{ formatTime(item.start_time) }}</text>
  43. </view>
  44. <view class="item-info">
  45. <text class="label">结束时间</text>
  46. <text class="value">{{ formatTime(item.end_time) }}</text>
  47. </view>
  48. <view class="item-info">
  49. <text class="label">预约人</text>
  50. <text class="value">{{ item.signName }}</text>
  51. </view>
  52. <view class="item-info">
  53. <text class="label">参加人数</text>
  54. <text class="value">{{ item.signNum }}</text>
  55. </view>
  56. <!-- 未开始 && 未取消预约 才显示取消预约按钮 -->
  57. <view class="btn-box" v-if="item.status === '未开始' && !item.isCancel">
  58. <button class="activity-btn" type="primary" @click.stop="cancelAppoint(item)"> 取消预约 </button>
  59. </view>
  60. </view>
  61. </view>
  62. <!-- 右滑出来的按钮插槽 -->
  63. <!-- v-if="!(item.status === '未开始' && !item.isCancel)" -->
  64. <!-- v-if="item.status !== '进行中'" -->
  65. <template #right >
  66. <view class="swipe-right-wrap">
  67. <view class="swipe-btn swipe-btn-delete" @click.stop="deleteSignActivity(item)">
  68. <uni-icons type="trash" size="24"></uni-icons>
  69. </view>
  70. </view>
  71. </template>
  72. </uni-swipe-action-item>
  73. </uni-swipe-action>
  74. <!-- 非loading有数据是最后一页哪怕仅1条也展示没有更多 -->
  75. <view v-if="!loading && noMore && activityList.length > 0" class="nomore-box">
  76. <text class="nomore-text">没有更多数据了</text>
  77. </view>
  78. <view class="empty-box" v-if="!loading && activityList.length === 0">
  79. <uni-icons type="empty" size="80" color="#ccc"></uni-icons>
  80. <text class="empty-text">暂无预约活动~</text>
  81. </view>
  82. </scroll-view>
  83. </view>
  84. </template>
  85. <script>
  86. import config from '@/utils/config';
  87. import { FetchSessionKey } from '@/api/user.js';
  88. import { FetchMySignActivityInfo, FetchCancelSignActivity,FetchDeleteSignActivity } from '@/api/activity.js'
  89. export default {
  90. data() {
  91. return {
  92. tabList:[
  93. {label:'全部',value:null},
  94. {label:'预约成功',value:false},
  95. {label:'已取消',value:true}
  96. ],
  97. activeTab:0,
  98. activityList: [],
  99. refreshing: false,
  100. loading: false,
  101. noMore: false,
  102. page: 0,
  103. pageSize: 20,
  104. sessionKey: '',
  105. openid: ''
  106. };
  107. },
  108. async onLoad() {
  109. await this.getSessionKey();
  110. this.getActivityList(true);
  111. uni.$on('refreshMySignList',()=>{
  112. this.getActivityList(true)
  113. })
  114. },
  115. onUnload(){
  116. uni.$off('refreshMySignList')
  117. },
  118. onPullDownRefresh() {
  119. if(this.loading){
  120. uni.stopPullDownRefresh()
  121. return
  122. }
  123. this.getActivityList(true).finally(()=>{
  124. uni.stopPullDownRefresh()
  125. })
  126. },
  127. methods: {
  128. switchTab(index){
  129. this.activeTab = index
  130. this.page = 0
  131. this.noMore = false
  132. this.activityList = []
  133. this.getActivityList(true)
  134. },
  135. formatTime(timestamp) {
  136. if (!timestamp) return '';
  137. const date = new Date(timestamp);
  138. const y = date.getFullYear();
  139. const m = String(date.getMonth() + 1).padStart(2, '0');
  140. const d = String(date.getDate()).padStart(2, '0');
  141. const h = String(date.getHours()).padStart(2, '0');
  142. const min = String(date.getMinutes()).padStart(2, '0');
  143. return `${y}-${m}-${d} ${h}:${min}`;
  144. },
  145. getStatusClass(status) {
  146. if (status === '未开始') return 'status-0';
  147. if (status === '进行中') return 'status-1';
  148. if (status === '已结束') return 'status-2';
  149. return '';
  150. },
  151. async getSessionKey() {
  152. return new Promise((resolve) => {
  153. uni.login({
  154. success: async (loginRes) => {
  155. console.log('loginRes', loginRes)
  156. if (loginRes.code) {
  157. const res = await FetchSessionKey({
  158. libcode: config.LIB_CODE,
  159. code: loginRes.code
  160. });
  161. if (res.code === 200) {
  162. this.sessionKey = res.data.session_key;
  163. this.openid = res.data.openid;
  164. }
  165. }
  166. resolve()
  167. },
  168. fail: () => {
  169. resolve()
  170. }
  171. });
  172. })
  173. },
  174. async getActivityList(isRefresh = false) {
  175. if (!this.openid) {
  176. uni.showToast({ title: '获取用户信息失败', icon: 'none' })
  177. return
  178. }
  179. if (this.loading) return;
  180. if (isRefresh) {
  181. this.page = 0;
  182. this.noMore = false;
  183. this.refreshing = true;
  184. } else {
  185. if (this.noMore) return;
  186. }
  187. this.loading = true;
  188. const currentTabItem = this.tabList[this.activeTab]
  189. const params = {
  190. openId: this.openid,
  191. page: this.page,
  192. pageSize: this.pageSize
  193. }
  194. if(currentTabItem.value !== null){
  195. params.isCancel = currentTabItem.value
  196. }
  197. try {
  198. const res = await FetchMySignActivityInfo(params);
  199. console.log('FetchMySignActivityInfo返回结果', res)
  200. if (res.code === 200) {
  201. const pageData = res.data;
  202. const list = pageData.content || [];
  203. const currentPage = pageData.number ?? 0;
  204. const totalPage = pageData.totalPages ?? 0;
  205. this.noMore = currentPage >= totalPage - 1;
  206. if (isRefresh) {
  207. this.activityList = list;
  208. } else {
  209. this.activityList = [...this.activityList, ...list];
  210. }
  211. } else {
  212. if (isRefresh) this.activityList = [];
  213. }
  214. } catch (err) {
  215. console.error('请求活动接口异常', err)
  216. uni.showToast({ title: '网络请求失败', icon: 'none' })
  217. } finally {
  218. this.refreshing = false;
  219. this.loading = false;
  220. }
  221. },
  222. onRefresh() {
  223. if(this.loading) return
  224. this.getActivityList(true);
  225. },
  226. onScrollLower() {
  227. if(this.loading || this.noMore) return;
  228. this.page += 1;
  229. this.getActivityList(false);
  230. },
  231. cancelAppoint(item) {
  232. uni.showModal({
  233. title: '提示',
  234. content: `确定要取消活动【${item.title}】的预约吗?`,
  235. success: async (res) => {
  236. if (res.confirm) {
  237. try {
  238. const apiRes = await FetchCancelSignActivity({
  239. id: item.signId
  240. })
  241. if(apiRes.code === 200){
  242. uni.showToast({ title: '取消成功', icon: 'success' })
  243. this.getActivityList(true)
  244. }else{
  245. uni.showToast({ title: apiRes.message || '取消失败', icon: 'none' })
  246. }
  247. }catch(err){
  248. console.error('取消预约失败', err)
  249. uni.showToast({ title: '网络异常,取消失败', icon: 'none' })
  250. }
  251. }
  252. }
  253. })
  254. },
  255. deleteSignActivity(item){
  256. uni.showModal({
  257. title: '提示',
  258. content: `确定要删除活动【${item.title}】的预约吗?`,
  259. success: async (res) => {
  260. if (res.confirm) {
  261. try {
  262. const apiRes = await FetchDeleteSignActivity({
  263. id: item.signId
  264. })
  265. if(apiRes.code === 200){
  266. uni.showToast({ title: '删除成功', icon: 'success' })
  267. this.getActivityList(true)
  268. }else{
  269. uni.showToast({ title: apiRes.message || '删除失败', icon: 'none' })
  270. }
  271. }catch(err){
  272. console.error('删除预约失败', err)
  273. uni.showToast({ title: '网络异常,删除失败', icon: 'none' })
  274. }
  275. }
  276. }
  277. })
  278. },
  279. toActivityDetail(item) {
  280. console.log('toActivityDetail',item);
  281. uni.navigateTo({
  282. url: "/subpkg/pages/activity-detail/activity-detail?item=" + encodeURIComponent(JSON.stringify(item))
  283. });
  284. }
  285. },
  286. };
  287. </script>
  288. <style lang="scss" scoped>
  289. .activity-list {
  290. padding: 0;
  291. height: 100vh;
  292. box-sizing: border-box;
  293. background-color: #f5f5f5;
  294. display:flex;
  295. flex-direction:column;
  296. }
  297. .tab-box {
  298. display: flex;
  299. background-color: #fff;
  300. margin-bottom: 10px;
  301. position: sticky;
  302. top: 0;
  303. z-index: 99;
  304. }
  305. .tab-item{
  306. flex:1;
  307. text-align:center;
  308. padding:12px 0;
  309. font-size:14px;
  310. color:#666;
  311. position:relative;
  312. }
  313. .tab-item.active{
  314. color:#01a4fe;
  315. font-weight:bold;
  316. }
  317. .tab-item.active::after{
  318. content:"";
  319. position:absolute;
  320. bottom:0;
  321. left:50%;
  322. transform:translateX(-50%);
  323. width:28px;
  324. height:2px;
  325. background:#01a4fe;
  326. }
  327. .scroll-view {
  328. flex:1;
  329. height: 0;
  330. padding: 0 12px;
  331. box-sizing: border-box;
  332. }
  333. .loading-box{
  334. padding:20px 0;
  335. text-align:center;
  336. }
  337. .loading-text{
  338. font-size:14px;
  339. color:#999;
  340. }
  341. .nomore-box{
  342. padding:15px 0;
  343. text-align:center;
  344. }
  345. .nomore-text{
  346. font-size:13px;
  347. color:#999;
  348. }
  349. .empty-box {
  350. height: calc(100vh - 180px);
  351. display: flex;
  352. flex-direction: column;
  353. align-items: center;
  354. justify-content: center;
  355. }
  356. .empty-text {
  357. margin-top: 16px;
  358. font-size: 14px;
  359. color: #999;
  360. }
  361. .activity-item {
  362. background: #fff;
  363. border-radius: 12px;
  364. overflow: hidden;
  365. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  366. .activity-info {
  367. display: flex;
  368. flex-direction: column;
  369. align-items: flex-start;
  370. padding: 16px;
  371. .title-row{
  372. width:100%;
  373. display:flex;
  374. justify-content: space-between;
  375. align-items:flex-start;
  376. margin-bottom:12px;
  377. }
  378. .title {
  379. flex: 1;
  380. font-size: 16px;
  381. font-weight: bold;
  382. color: #222;
  383. }
  384. .item-info {
  385. display: flex;
  386. justify-content: space-between;
  387. padding: 4px 0;
  388. font-size: 14px;
  389. width:100%;
  390. .label {
  391. color: #666;
  392. width: 72px;
  393. }
  394. .value {
  395. color: #333;
  396. flex: 1;
  397. text-align: right;
  398. }
  399. }
  400. }
  401. }
  402. .btn-box {
  403. width: 100%;
  404. display: flex;
  405. justify-content: flex-end;
  406. margin-top: 10px;
  407. padding-top: 10px;
  408. border-top: 1px solid #e5e5e5;
  409. }
  410. .status-tag {
  411. padding: 2px 8px;
  412. border-radius: 4px;
  413. font-size: 12px;
  414. }
  415. .status-tag.status-0 {
  416. color: #01a4fe;
  417. background: #e6f7ff;
  418. }
  419. .status-tag.status-1 {
  420. color: #00b42a;
  421. background: #e6ffed;
  422. }
  423. .status-tag.status-2 {
  424. color: #999;
  425. background: #f5f5f5;
  426. }
  427. /* 👉 插槽外层容器:占满高度,垂直水平居中 */
  428. .swipe-right-wrap {
  429. width: 80px;
  430. height: calc(100% - 20px);
  431. display: flex;
  432. align-items: center; /* 垂直居中 */
  433. justify-content: center; /*水平居中*/
  434. // background-color: #f5222d;
  435. }
  436. /* 圆形按钮,不再写height:100%,固定大小做圆圈 */
  437. .swipe-btn {
  438. width: 48px;
  439. height: 48px;
  440. border-radius: 50%;
  441. display: flex;
  442. align-items: center;
  443. justify-content: center;
  444. color: #ffffff !important;
  445. }
  446. .swipe-btn-delete {
  447. background-color: #f5222d;
  448. }
  449. /* 深度修改uni-icons图标颜色,scoped必须加 ::v‑deep */
  450. ::v-deep .swipe-btn .uni-icons {
  451. color: #ffffff !important;
  452. }
  453. </style>