index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <script setup>
  2. import { requestClient } from '#/api/request';
  3. import { Button, message } from 'ant-design-vue';
  4. import { ref, reactive, computed, onMounted } from 'vue';
  5. import dayjs from 'dayjs';
  6. import MatchItem from '../components/match_item.vue';
  7. const gamesList = reactive({});
  8. const gamesRelations = reactive({});
  9. const currentRelation = reactive({});
  10. const selectedInfo = ref(null);
  11. const formatDate = (timestamp) => {
  12. return dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss');
  13. }
  14. const formatGameItem = (game, platform) => {
  15. const selected = currentRelation[platform]?.eventId == game.eventId;
  16. const { timestamp } = game;
  17. const dateTime = formatDate(timestamp);
  18. return { ...game, dateTime, selected };
  19. }
  20. const setGameOrderWeight= (game) => {
  21. const { t, l, h, a } = selectedInfo.value ?? {};
  22. game.orderWeight = 0;
  23. const { leagueName, teamHomeName, teamAwayName, timestamp } = game;
  24. if (timestamp != t) {
  25. game.orderWeight = -99;
  26. }
  27. else {
  28. game.orderWeight += 1;
  29. }
  30. if (leagueName.startsWith(l)) {
  31. game.orderWeight += 1;
  32. }
  33. if (teamHomeName.startsWith(h)) {
  34. game.orderWeight += 1;
  35. }
  36. if (teamAwayName.startsWith(a)) {
  37. game.orderWeight += 1;
  38. }
  39. return game;
  40. }
  41. const getGameOrderList = (platform) => {
  42. let games = gamesList[platform]?.games ?? [];
  43. const relatedGames = new Set(Object.values(gamesRelations).map(item => item[platform]?.eventId));
  44. games = games.map(game => formatGameItem(game, platform));
  45. if (platform == 'jc') {
  46. return games.filter(game => !relatedGames.has(game.eventId))
  47. .sort((a, b) => {
  48. if (a.selected) {
  49. return -1;
  50. }
  51. return 1;
  52. });
  53. }
  54. return games.map(setGameOrderWeight)
  55. .sort((a, b) => b.orderWeight - a.orderWeight)
  56. .filter(game => {
  57. if (game.orderWeight > 0 && !relatedGames.has(game.eventId)) {
  58. return true;
  59. }
  60. return false;
  61. });
  62. }
  63. const showRelationButton = computed(() => {
  64. return currentRelation.ps || currentRelation.ob;
  65. });
  66. const relationsList = computed(() => {
  67. return Object.keys(gamesRelations).map(id => {
  68. const rel = gamesRelations[id];
  69. Object.values(rel).forEach(item => {
  70. item.dateTime = formatDate(item.timestamp);
  71. });
  72. return { id, rel };
  73. });
  74. });
  75. const jcGamesList = computed(() => {
  76. return getGameOrderList('jc');
  77. });
  78. const psGamesList = computed(() => {
  79. return getGameOrderList('ps');
  80. });
  81. const obGamesList = computed(() => {
  82. return getGameOrderList('ob');
  83. });
  84. const getGamesList = async () => {
  85. try {
  86. const data = await requestClient.get('/triangle/get_games_list');
  87. return data;
  88. }
  89. catch (error) {
  90. console.error('Failed to fetch games info:', error);
  91. message.error('获取比赛信息失败');
  92. return [];
  93. }
  94. }
  95. const getGamesRelations = async () => {
  96. try {
  97. const data = await requestClient.get('/triangle/get_games_relation');
  98. return data;
  99. }
  100. catch (error) {
  101. console.error('Failed to fetch game relations:', error);
  102. message.error('获取比赛关系失败');
  103. return [];
  104. }
  105. }
  106. const setGamesRelation = () => {
  107. const rel = currentRelation;
  108. Object.keys(rel).forEach(key => {
  109. if (!rel[key]) {
  110. delete rel[key];
  111. }
  112. });
  113. Object.values(rel).forEach(item => {
  114. delete item.orderWeight;
  115. });
  116. const id = rel['jc']?.eventId;
  117. if (!id) {
  118. console.log('没有选择竞彩的比赛');
  119. message.warn('设置比赛关系失败');
  120. }
  121. requestClient.post('/triangle/update_games_relation', { id, rel })
  122. .then(res => {
  123. console.log('设置比赛关系成功', res);
  124. message.success('设置比赛关系成功');
  125. selectedInfo.value = null;
  126. currentRelation.jc = currentRelation.ps = currentRelation.ob = null;
  127. updateGamesRelations();
  128. })
  129. .catch(error => {
  130. console.error('Failed to set game relation:', error);
  131. message.error('设置比赛关系失败');
  132. });
  133. }
  134. const removeGamesRelation = (id) => {
  135. requestClient.post('/triangle/remove_games_relation', { id })
  136. .then(res => {
  137. console.log('删除比赛关系成功', res);
  138. message.success('删除比赛关系成功');
  139. updateGamesRelations();
  140. })
  141. .catch(error => {
  142. console.error('Failed to remove game relation:', error);
  143. message.error('删除比赛关系失败');
  144. });
  145. }
  146. const openRelationModal = () => {
  147. relationModalVisible.value = true;
  148. }
  149. const updateGamesList = async () => {
  150. const data = await getGamesList();
  151. Object.keys(data).forEach(key => {
  152. gamesList[key] = data[key];
  153. });
  154. }
  155. const updateGamesRelations = async () => {
  156. const data = await getGamesRelations();
  157. data.forEach(item => {
  158. const { id, rel } = item;
  159. gamesRelations[id] = rel;
  160. });
  161. const newIds = new Set(data.map(item => item.id));
  162. Object.keys(gamesRelations).forEach(id => {
  163. if (!newIds.has(id)) {
  164. delete gamesRelations[id];
  165. }
  166. });
  167. }
  168. const selectGame = (platform, game) => {
  169. const { leagueId, eventId, timestamp, leagueName, teamHomeName, teamAwayName, selected, matchNumStr } = game;
  170. if (selected) {
  171. currentRelation[platform] = null;
  172. }
  173. else {
  174. currentRelation[platform] = { leagueId, eventId, timestamp, leagueName, teamHomeName, teamAwayName, matchNumStr };
  175. }
  176. if (platform == 'jc') {
  177. currentRelation.ps = null;
  178. currentRelation.ob = null;
  179. if (selected) {
  180. selectedInfo.value = null;
  181. }
  182. else {
  183. selectedInfo.value = {
  184. t: timestamp,
  185. l: leagueName,
  186. h: teamHomeName,
  187. a: teamAwayName,
  188. }
  189. }
  190. }
  191. }
  192. onMounted(() => {
  193. updateGamesList();
  194. updateGamesRelations();
  195. });
  196. </script>
  197. <template>
  198. <div class="relation-container">
  199. <div class="top-panel" ref="topPanel">
  200. <span>竞彩</span>
  201. <span>平博</span>
  202. <span>OB</span>
  203. <i>{{ relationsList.length }}</i>
  204. </div>
  205. <div class="match-list" v-if="relationsList.length">
  206. <div class="match-row" v-for="({ id, rel }) in relationsList" :key="id">
  207. <MatchItem
  208. :eventId="rel.jc.eventId"
  209. :leagueName="rel.jc.leagueName"
  210. :teamHomeName="rel.jc.teamHomeName"
  211. :teamAwayName="rel.jc.teamAwayName"
  212. :dateTime="rel.jc.dateTime"
  213. :matchNumStr="rel.jc.matchNumStr" />
  214. <MatchItem v-if="rel.ps"
  215. :eventId="rel.ps.eventId"
  216. :leagueName="rel.ps.leagueName"
  217. :teamHomeName="rel.ps.teamHomeName"
  218. :teamAwayName="rel.ps.teamAwayName"
  219. :dateTime="rel.ps.dateTime" />
  220. <div class="match-item match-item-holder" v-else></div>
  221. <MatchItem v-if="rel.ob"
  222. :eventId="rel.ob.eventId"
  223. :leagueName="rel.ob.leagueName"
  224. :teamHomeName="rel.ob.teamHomeName"
  225. :teamAwayName="rel.ob.teamAwayName"
  226. :dateTime="rel.ob.dateTime" />
  227. <div class="match-item match-item-holder" v-else></div>
  228. <div class="match-action">
  229. <Button type="link" class="action-btn" @click="removeGamesRelation(id)">
  230. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>
  231. </Button>
  232. </div>
  233. </div>
  234. </div>
  235. <div class="match-list col-list" v-if="jcGamesList.length">
  236. <div class="match-col">
  237. <MatchItem v-for="game in jcGamesList" :key="game.id"
  238. :eventId="game.eventId"
  239. :leagueName="game.leagueName"
  240. :teamHomeName="game.teamHomeName"
  241. :teamAwayName="game.teamAwayName"
  242. :dateTime="game.dateTime"
  243. :matchNumStr="game.matchNumStr"
  244. :selected="game.selected"
  245. @click="selectGame('jc', game)" />
  246. </div>
  247. <div class="match-col">
  248. <MatchItem v-for="game in psGamesList" :key="game.id"
  249. :eventId="game.eventId"
  250. :leagueName="game.leagueName"
  251. :teamHomeName="game.teamHomeName"
  252. :teamAwayName="game.teamAwayName"
  253. :dateTime="game.dateTime"
  254. :selected="game.selected"
  255. @click="selectGame('ps', game)" />
  256. </div>
  257. <div class="match-col">
  258. <MatchItem v-for="game in obGamesList" :key="game.id"
  259. :eventId="game.eventId"
  260. :leagueName="game.leagueName"
  261. :teamHomeName="game.teamHomeName"
  262. :teamAwayName="game.teamAwayName"
  263. :dateTime="game.dateTime"
  264. :selected="game.selected"
  265. @click="selectGame('ob', game)" />
  266. </div>
  267. <div class="match-action">
  268. <Button type="link" class="action-btn" v-if="showRelationButton" @click="setGamesRelation">
  269. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
  270. </Button>
  271. </div>
  272. </div>
  273. <div class="list-empty" v-if="!relationsList.length && !jcGamesList.length">暂无数据</div>
  274. </div>
  275. </template>
  276. <style lang="scss" scoped>
  277. .relation-container {
  278. padding: 10px;
  279. }
  280. .top-panel {
  281. display: flex;
  282. margin-bottom: 5px;
  283. border: 1px solid hsl(var(--border));
  284. border-radius: 6px;
  285. background-color: hsl(var(--card));
  286. span, i {
  287. display: block;
  288. }
  289. span {
  290. flex: 1;
  291. text-align: center;
  292. &:not(:last-child) {
  293. border-right: 1px solid hsl(var(--border));
  294. }
  295. }
  296. i {
  297. width: 50px;
  298. text-align: center;
  299. font-style: normal;
  300. }
  301. }
  302. .match-list {
  303. margin-bottom: 5px;
  304. border: 1px solid hsl(var(--border));
  305. border-radius: 6px;
  306. background-color: hsl(var(--card));
  307. overflow: hidden;
  308. &.col-list {
  309. display: flex;
  310. }
  311. }
  312. .match-row {
  313. display: flex;
  314. flex-wrap: wrap;
  315. &:not(:last-child) {
  316. border-bottom: 1px solid hsl(var(--border));
  317. }
  318. .match-item {
  319. &:not(:last-child) {
  320. border-right: 1px solid hsl(var(--border));
  321. }
  322. }
  323. }
  324. .match-col {
  325. flex: 1;
  326. &:not(:last-child) {
  327. border-right: 1px solid hsl(var(--border));
  328. }
  329. .match-item {
  330. border-bottom: 1px solid hsl(var(--border));
  331. &:last-child {
  332. margin-bottom: -1px;
  333. }
  334. }
  335. }
  336. .match-item-holder {
  337. flex: 1;
  338. padding: 10px 15px;
  339. }
  340. .match-action {
  341. display: flex;
  342. align-items: center;
  343. justify-content: center;
  344. width: 50px;
  345. }
  346. .action-btn {
  347. padding: 4px;
  348. display: flex;
  349. align-items: center;
  350. justify-content: center;
  351. color: hsl(var(--foreground) / 0.7);
  352. &:hover {
  353. color: hsl(var(--foreground));
  354. }
  355. .col-list & {
  356. position: relative;
  357. top: 40px;
  358. align-self: flex-start;
  359. }
  360. svg {
  361. width: 16px;
  362. height: 16px;
  363. }
  364. }
  365. .list-empty {
  366. text-align: center;
  367. padding: 10px;
  368. font-size: 18px;
  369. color: hsl(var(--foreground) / 0.7);
  370. }
  371. @media (max-width: 768px) {
  372. .match-item {
  373. min-width: calc(50% - 16px);
  374. }
  375. }
  376. @media (max-width: 480px) {
  377. .match-item {
  378. min-width: calc(100% - 16px);
  379. }
  380. }
  381. </style>