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

495 lines
12 KiB

1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
3 weeks ago
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. baseUrl: config.baseUrl,
  100. refreshing: false,
  101. loading: false,
  102. noMore: false,
  103. page: 0,
  104. pageSize: 20,
  105. sessionKey: '',
  106. openid: ''
  107. };
  108. },
  109. async onLoad() {
  110. await this.getSessionKey();
  111. this.getActivityList(true);
  112. uni.$on('refreshMySignList',()=>{
  113. this.getActivityList(true)
  114. })
  115. },
  116. onUnload(){
  117. uni.$off('refreshMySignList')
  118. },
  119. onPullDownRefresh() {
  120. if(this.loading){
  121. uni.stopPullDownRefresh()
  122. return
  123. }
  124. this.getActivityList(true).finally(()=>{
  125. uni.stopPullDownRefresh()
  126. })
  127. },
  128. methods: {
  129. switchTab(index){
  130. this.activeTab = index
  131. this.page = 0
  132. this.noMore = false
  133. this.activityList = []
  134. this.getActivityList(true)
  135. },
  136. formatTime(timestamp) {
  137. if (!timestamp) return '';
  138. const date = new Date(timestamp);
  139. const y = date.getFullYear();
  140. const m = String(date.getMonth() + 1).padStart(2, '0');
  141. const d = String(date.getDate()).padStart(2, '0');
  142. const h = String(date.getHours()).padStart(2, '0');
  143. const min = String(date.getMinutes()).padStart(2, '0');
  144. return `${y}-${m}-${d} ${h}:${min}`;
  145. },
  146. getStatusClass(status) {
  147. if (status === '未开始') return 'status-0';
  148. if (status === '进行中') return 'status-1';
  149. if (status === '已结束') return 'status-2';
  150. return '';
  151. },
  152. async getSessionKey() {
  153. return new Promise((resolve) => {
  154. uni.login({
  155. success: async (loginRes) => {
  156. console.log('loginRes', loginRes)
  157. if (loginRes.code) {
  158. const res = await FetchSessionKey({
  159. libcode: config.LIB_CODE,
  160. code: loginRes.code
  161. });
  162. if (res.code === 200) {
  163. this.sessionKey = res.data.session_key;
  164. this.openid = res.data.openid;
  165. }
  166. }
  167. resolve()
  168. },
  169. fail: () => {
  170. resolve()
  171. }
  172. });
  173. })
  174. },
  175. async getActivityList(isRefresh = false) {
  176. if (!this.openid) {
  177. uni.showToast({ title: '获取用户信息失败', icon: 'none' })
  178. return
  179. }
  180. if (this.loading) return;
  181. if (isRefresh) {
  182. this.page = 0;
  183. this.noMore = false;
  184. this.refreshing = true;
  185. } else {
  186. if (this.noMore) return;
  187. }
  188. this.loading = true;
  189. const currentTabItem = this.tabList[this.activeTab]
  190. const params = {
  191. openId: this.openid,
  192. page: this.page,
  193. pageSize: this.pageSize
  194. }
  195. if(currentTabItem.value !== null){
  196. params.isCancel = currentTabItem.value
  197. }
  198. try {
  199. const res = await FetchMySignActivityInfo(params);
  200. console.log('FetchMySignActivityInfo返回结果', res)
  201. if (res.code === 200) {
  202. const pageData = res.data;
  203. const list = pageData.content || [];
  204. // ==========新增:统一处理图片地址,和第一个接口逻辑保持一致==========
  205. list.forEach(item => {
  206. if(item.cover_image){
  207. const imgId = encodeURIComponent(item.cover_image);
  208. item.cover_image = `${this.baseUrl}/files/${imgId}`;
  209. }else{
  210. item.cover_image = ''
  211. }
  212. })
  213. const currentPage = pageData.number ?? 0;
  214. const totalPage = pageData.totalPages ?? 0;
  215. this.noMore = currentPage >= totalPage - 1;
  216. if (isRefresh) {
  217. this.activityList = list;
  218. } else {
  219. this.activityList = [...this.activityList, ...list];
  220. }
  221. } else {
  222. if (isRefresh) this.activityList = [];
  223. }
  224. } catch (err) {
  225. console.error('请求活动接口异常', err)
  226. uni.showToast({ title: '网络请求失败', icon: 'none' })
  227. } finally {
  228. this.refreshing = false;
  229. this.loading = false;
  230. }
  231. },
  232. onRefresh() {
  233. if(this.loading) return
  234. this.getActivityList(true);
  235. },
  236. onScrollLower() {
  237. if(this.loading || this.noMore) return;
  238. this.page += 1;
  239. this.getActivityList(false);
  240. },
  241. cancelAppoint(item) {
  242. uni.showModal({
  243. title: '提示',
  244. content: `确定要取消活动【${item.title}】的预约吗?`,
  245. success: async (res) => {
  246. if (res.confirm) {
  247. try {
  248. const apiRes = await FetchCancelSignActivity({
  249. id: item.signId
  250. })
  251. if(apiRes.code === 200){
  252. uni.showToast({ title: '取消成功', icon: 'success' })
  253. this.getActivityList(true)
  254. }else{
  255. uni.showToast({ title: apiRes.message || '取消失败', icon: 'none' })
  256. }
  257. }catch(err){
  258. console.error('取消预约失败', err)
  259. uni.showToast({ title: '网络异常,取消失败', icon: 'none' })
  260. }
  261. }
  262. }
  263. })
  264. },
  265. deleteSignActivity(item){
  266. uni.showModal({
  267. title: '提示',
  268. content: `确定要删除活动【${item.title}】的预约吗?`,
  269. success: async (res) => {
  270. if (res.confirm) {
  271. try {
  272. const apiRes = await FetchDeleteSignActivity({
  273. id: item.signId
  274. })
  275. if(apiRes.code === 200){
  276. uni.showToast({ title: '删除成功', icon: 'success' })
  277. this.getActivityList(true)
  278. }else{
  279. uni.showToast({ title: apiRes.message || '删除失败', icon: 'none' })
  280. }
  281. }catch(err){
  282. console.error('删除预约失败', err)
  283. uni.showToast({ title: '网络异常,删除失败', icon: 'none' })
  284. }
  285. }
  286. }
  287. })
  288. },
  289. toActivityDetail(item) {
  290. console.log('toActivityDetail',item);
  291. uni.navigateTo({
  292. url: "/subpkg/pages/activity-detail/activity-detail?item=" + encodeURIComponent(JSON.stringify(item))
  293. });
  294. }
  295. },
  296. };
  297. </script>
  298. <style lang="scss" scoped>
  299. .activity-list {
  300. padding: 0;
  301. height: 100vh;
  302. box-sizing: border-box;
  303. background-color: #f5f5f5;
  304. display:flex;
  305. flex-direction:column;
  306. }
  307. .tab-box {
  308. display: flex;
  309. background-color: #fff;
  310. margin-bottom: 10px;
  311. position: sticky;
  312. top: 0;
  313. z-index: 99;
  314. }
  315. .tab-item{
  316. flex:1;
  317. text-align:center;
  318. padding:12px 0;
  319. font-size:14px;
  320. color:#666;
  321. position:relative;
  322. }
  323. .tab-item.active{
  324. color:#01a4fe;
  325. font-weight:bold;
  326. }
  327. .tab-item.active::after{
  328. content:"";
  329. position:absolute;
  330. bottom:0;
  331. left:50%;
  332. transform:translateX(-50%);
  333. width:28px;
  334. height:2px;
  335. background:#01a4fe;
  336. }
  337. .scroll-view {
  338. flex:1;
  339. height: 0;
  340. padding: 0 12px;
  341. box-sizing: border-box;
  342. }
  343. .loading-box{
  344. padding:20px 0;
  345. text-align:center;
  346. }
  347. .loading-text{
  348. font-size:14px;
  349. color:#999;
  350. }
  351. .nomore-box{
  352. padding:15px 0;
  353. text-align:center;
  354. }
  355. .nomore-text{
  356. font-size:13px;
  357. color:#999;
  358. }
  359. .empty-box {
  360. height: calc(100vh - 180px);
  361. display: flex;
  362. flex-direction: column;
  363. align-items: center;
  364. justify-content: center;
  365. }
  366. .empty-text {
  367. margin-top: 16px;
  368. font-size: 14px;
  369. color: #999;
  370. }
  371. .activity-item {
  372. background: #fff;
  373. border-radius: 12px;
  374. overflow: hidden;
  375. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  376. .activity-info {
  377. display: flex;
  378. flex-direction: column;
  379. align-items: flex-start;
  380. padding: 16px;
  381. .title-row{
  382. width:100%;
  383. display:flex;
  384. justify-content: space-between;
  385. align-items:flex-start;
  386. margin-bottom:12px;
  387. }
  388. .title {
  389. flex: 1;
  390. font-size: 16px;
  391. font-weight: bold;
  392. color: #222;
  393. }
  394. .item-info {
  395. display: flex;
  396. justify-content: space-between;
  397. padding: 4px 0;
  398. font-size: 14px;
  399. width:100%;
  400. .label {
  401. color: #666;
  402. width: 72px;
  403. }
  404. .value {
  405. color: #333;
  406. flex: 1;
  407. text-align: right;
  408. }
  409. }
  410. }
  411. }
  412. .btn-box {
  413. width: 100%;
  414. display: flex;
  415. justify-content: flex-end;
  416. margin-top: 10px;
  417. padding-top: 10px;
  418. border-top: 1px solid #e5e5e5;
  419. }
  420. .status-tag {
  421. padding: 2px 8px;
  422. border-radius: 4px;
  423. font-size: 12px;
  424. }
  425. .status-tag.status-0 {
  426. color: #01a4fe;
  427. background: #e6f7ff;
  428. }
  429. .status-tag.status-1 {
  430. color: #00b42a;
  431. background: #e6ffed;
  432. }
  433. .status-tag.status-2 {
  434. color: #999;
  435. background: #f5f5f5;
  436. }
  437. /* 👉 插槽外层容器:占满高度,垂直水平居中 */
  438. .swipe-right-wrap {
  439. width: 80px;
  440. height: calc(100% - 20px);
  441. display: flex;
  442. align-items: center; /* 垂直居中 */
  443. justify-content: center; /*水平居中*/
  444. // background-color: #f5222d;
  445. }
  446. /* 圆形按钮,不再写height:100%,固定大小做圆圈 */
  447. .swipe-btn {
  448. width: 48px;
  449. height: 48px;
  450. border-radius: 50%;
  451. display: flex;
  452. align-items: center;
  453. justify-content: center;
  454. color: #ffffff !important;
  455. }
  456. .swipe-btn-delete {
  457. background-color: #f5222d;
  458. }
  459. /* 深度修改uni-icons图标颜色,scoped必须加 ::v‑deep */
  460. ::v-deep .swipe-btn .uni-icons {
  461. color: #ffffff !important;
  462. }
  463. </style>