index.vue 11 KB

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