当前位置: 首页 > news >正文

开发体育直播即时比分系统:赛事收藏功能的技术实现方案

以下是基于“东莞梦幻网络科技”的体育即时比分系统收藏界面的技术实现方案,包括后端(PHP + ThinkPHP)、安卓(Java)、iOS(Objective-C)和PC/H5前端(Vue.js)的代码示例。

技术架构

后端(PHP + ThinkPHP):提供API接口,处理数据存储、用户管理、比赛数据获取等功能。

安卓端(Java):调用后端API,展示比赛列表,并实现收藏功能。

iOS端(Objective-C):实现与安卓端类似的功能,提供比赛数据展示和收藏功能。

PC/H5前端(Vue.js):基于Vue3 + Element UI 实现收藏比赛列表、赛事展示等功能。

代码实现

(1)后端(ThinkPHP)

<?php
namespace app\controller;

use think\Request;
use think\Db;

class MatchController {
    // 获取比赛数据(支持收藏筛选)
    public function getMatchList(Request $request) {
        $userId = $request->param('user_id');
        $isFavorite = $request->param('favorite', 0); // 0: 全部 1: 仅收藏

        $query = Db::table('matches')
            ->alias('m')
            ->field('m.id, m.time, m.team_home, m.team_away, m.status, f.id as favorite')
            ->leftJoin('favorites f', 'm.id = f.match_id AND f.user_id = '.$userId);

        if ($isFavorite) {
            $query->whereNotNull('f.id');
        }

        $matches = $query->order('m.time', 'asc')->select();
        return json(['code' => 200, 'data' => $matches]);
    }

    // 收藏比赛
    public function toggleFavorite(Request $request) {
        $userId = $request->param('user_id');
        $matchId = $request->param('match_id');

        $exists = Db::table('favorites')->where('user_id', $userId)->where('match_id', $matchId)->find();
        if ($exists) {
            Db::table('favorites')->where('user_id', $userId)->where('match_id', $matchId)->delete();
            return json(['code' => 200, 'message' => '取消收藏']);
        } else {
            Db::table('favorites')->insert(['user_id' => $userId, 'match_id' => $matchId]);
            return json(['code' => 200, 'message' => '收藏成功']);
        }
    }
}

(2)安卓端(Java)

public void fetchMatchList(boolean favoriteOnly) {
    ApiService apiService = RetrofitClient.getInstance().create(ApiService.class);
    Call<ApiResponse<List<Match>>> call = apiService.getMatchList(userId, favoriteOnly ? 1 : 0);

    call.enqueue(new Callback<ApiResponse<List<Match>>>() {
        @Override
        public void onResponse(Call<ApiResponse<List<Match>>> call, Response<ApiResponse<List<Match>>> response) {
            if (response.isSuccessful() && response.body() != null) {
                matchListAdapter.updateData(response.body().getData());
            }
        }

        @Override
        public void onFailure(Call<ApiResponse<List<Match>>> call, Throwable t) {
            Log.e("API_ERROR", "Failed to fetch matches");
        }
    });
}

public void toggleFavorite(int matchId) {
    ApiService apiService = RetrofitClient.getInstance().create(ApiService.class);
    Call<ApiResponse<String>> call = apiService.toggleFavorite(userId, matchId);

    call.enqueue(new Callback<ApiResponse<String>>() {
        @Override
        public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
            if (response.isSuccessful() && response.body() != null) {
                fetchMatchList(false); // 刷新比赛列表
            }
        }

        @Override
        public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
            Log.e("API_ERROR", "Failed to update favorite");
        }
    });
}

(3)iOS端(Objective-C )

- (void)fetchMatchList:(BOOL)favoriteOnly {
    NSDictionary *params = @{@"user_id": userId, @"favorite": @(favoriteOnly ? 1 : 0)};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://yourserver.com/api/match/list" parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.matches = responseObject[@"data"];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"API Error: %@", error.localizedDescription);
    }];
}

- (void)toggleFavorite:(NSInteger)matchId {
    NSDictionary *params = @{@"user_id": userId, @"match_id": @(matchId)};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://yourserver.com/api/match/toggleFavorite" parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self fetchMatchList:NO]; // 刷新列表
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"API Error: %@", error.localizedDescription);
    }];
}

(4)PC/H5前端(Vue.js)

<template>
  <div>
    <el-switch v-model="favoriteOnly" active-text="仅显示收藏" @change="fetchMatches" />
    <el-table :data="matches">
      <el-table-column prop="time" label="时间" />
      <el-table-column prop="team_home" label="主队" />
      <el-table-column prop="team_away" label="客队" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button :type="row.favorite ? 'danger' : 'primary'" @click="toggleFavorite(row.id)">
            {{ row.favorite ? '取消收藏' : '收藏' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      matches: [],
      favoriteOnly: false,
    };
  },
  mounted() {
    this.fetchMatches();
  },
  methods: {
    fetchMatches() {
      axios
        .get("/api/match/list", { params: { user_id: 1, favorite: this.favoriteOnly ? 1 : 0 } })
        .then((res) => {
          this.matches = res.data.data;
        });
    },
    toggleFavorite(matchId) {
      axios.post("/api/match/toggleFavorite", { user_id: 1, match_id: matchId }).then(() => {
        this.fetchMatches();
      });
    },
  },
};
</script>

在这里插入图片描述
在这里插入图片描述

相关文章:

  • vscode集成deepseek实现辅助编程(银河麒麟系统)【详细自用版】
  • Android学习总结之Kotlin 协程
  • presto行转列
  • Vulnhub-zico2靶机打靶记录
  • ML 聚类算法 dbscan|| OPTICS|mean-shift
  • C语言基础系列【32】指针进阶5:指针与常量
  • 上市电子制造企业如何实现合规的质量文件管理?
  • 0301-组件基础-react-仿低代码平台项目
  • 【AI微信小程序开发】AI减脂菜谱小程序项目代码:根据用户身高/体重等信息定制菜谱(含完整前端+后端代码)
  • 爱心计算器:用 Python 创建带有动态爱心效果的计算器
  • Ubuntu20.04安装OpenVINO环境以及YOLOv8 C++部署测试
  • Android里面内存优化
  • 【Redis】基础1:基本概念,基本数据结构
  • Git操作指南
  • Python数据类型-int
  • JavaScript基础-触屏事件
  • 加密钱包助记词丢失后的一天
  • 在MFC中使用Qt(三):通过编辑项目文件(.vcxproj)实现Qt的自动化编译流程
  • 循环神经网络 - 通用近似定理 图灵完备
  • 昇腾CANN算子共建仓CANN-Ops正式上线Gitee,首批算子已合入
  • 香港发生车祸致22人受伤,4人伤势严重
  • 泽连斯基:美乌矿产协议将提交乌拉达批准
  • 净海护渔,中国海警局直属第一局开展伏季休渔普法宣传活动
  • 关于“十五五”,在上海召开的这场座谈会释放最新信号
  • 全国人民代表大会常务委员会公告〔十四届〕第十号
  • 比黄油年糕热量还高,这个火爆全网的甜品劝你慎吃