Commit 948fdad3 authored by 陈冲's avatar 陈冲

commit

parent 285e540b
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
declare module 'crypto-js'; declare module 'crypto-js';
declare const __APP_DOMAIN__: string;
interface ImportMetaEnv { interface ImportMetaEnv {
readonly VITE_ASSET_BASE_URL?: string; readonly VITE_ASSET_BASE_URL?: string;
readonly VITE_ASSET_DEV_LOCAL?: string; readonly VITE_ASSET_DEV_LOCAL?: string;
......
...@@ -4,4 +4,6 @@ import { createApp } from 'vue' ...@@ -4,4 +4,6 @@ import { createApp } from 'vue'
import App from './App.vue' import App from './App.vue'
import router from './router' import router from './router'
localStorage.setItem('domain', __APP_DOMAIN__)
createApp(App).use(router).mount('#app') createApp(App).use(router).mount('#app')
...@@ -40,7 +40,7 @@ const loginHandler = async () => { ...@@ -40,7 +40,7 @@ const loginHandler = async () => {
telephone: '13111223344', telephone: '13111223344',
avatar: '13111223344', avatar: '13111223344',
}); });
await router.replace('/h5/game1') await router.replace('/game1')
} }
</script> </script>
......
...@@ -12,15 +12,15 @@ const router = createRouter({ ...@@ -12,15 +12,15 @@ const router = createRouter({
routes: [ routes: [
{ {
path: '/', path: '/',
redirect: () => (getWechatId() ? '/h5/game1' : '/h5/login'), redirect: () => (getWechatId() ? '/game1' : '/login'),
}, },
{ {
path: '/h5/login', path: '/login',
name: 'Login', name: 'Login',
component: Login, component: Login,
}, },
{ {
path: '/h5/game1', path: '/game1',
name: 'Game1', name: 'Game1',
component: Game1, component: Game1,
meta: { meta: {
...@@ -38,7 +38,7 @@ router.beforeEach((to) => { ...@@ -38,7 +38,7 @@ router.beforeEach((to) => {
const wechatId = getWechatId() const wechatId = getWechatId()
if (to.meta.requiresAuth && !wechatId) { if (to.meta.requiresAuth && !wechatId) {
return '/h5/login' return '/login'
} }
return true return true
......
...@@ -6,6 +6,8 @@ import { defineConfig } from 'vite' ...@@ -6,6 +6,8 @@ import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools' import vueDevTools from 'vite-plugin-vue-devtools'
const buildDomain = 'https://hddpserver.guocai365.org.cn'
function copyImageAssets() { function copyImageAssets() {
return { return {
name: 'copy-image-assets', name: 'copy-image-assets',
...@@ -25,6 +27,9 @@ function copyImageAssets() { ...@@ -25,6 +27,9 @@ function copyImageAssets() {
export default defineConfig(({ command }) => ({ export default defineConfig(({ command }) => ({
base: command === 'build' ? '/cc/h5/' : '/', base: command === 'build' ? '/cc/h5/' : '/',
define: {
__APP_DOMAIN__: JSON.stringify(command === 'build' ? buildDomain : ''),
},
plugins: [ plugins: [
vue(), vue(),
vueDevTools(), vueDevTools(),
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
declare module 'crypto-js'; declare module 'crypto-js';
declare const __APP_DOMAIN__: string;
interface ImportMetaEnv { interface ImportMetaEnv {
readonly VITE_ASSET_BASE_URL?: string; readonly VITE_ASSET_BASE_URL?: string;
readonly VITE_ASSET_DEV_LOCAL?: string; readonly VITE_ASSET_DEV_LOCAL?: string;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>小游戏-大屏</title> <title>小游戏-大屏</title>
<style>body{background: #FA6047;}</style> <style>body{background: #FA6047;}</style>
<script>localStorage.setItem('domain', '');</script>
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
......
...@@ -74,8 +74,9 @@ export function $read_socket_storge(): any { ...@@ -74,8 +74,9 @@ export function $read_socket_storge(): any {
let token = $read('token', ''); let token = $read('token', '');
const socket_path = $read('socket_path', ''); const socket_path = $read('socket_path', '');
const client_ns = $read('client_ns', '') const client_ns = $read('client_ns', '')
const domain = localStorage.getItem('domain') || ''
return { return {
passphrase, token, socket_path, client_ns passphrase, token, socket_path, client_ns, domain
} }
} }
...@@ -91,4 +92,4 @@ export function $is_run_local(): boolean { ...@@ -91,4 +92,4 @@ export function $is_run_local(): boolean {
return true; return true;
} }
return false; return false;
} }
\ No newline at end of file
...@@ -34,8 +34,12 @@ export function initSocket(args: SocketInitArgs): Socket | null { ...@@ -34,8 +34,12 @@ export function initSocket(args: SocketInitArgs): Socket | null {
if (socket) { if (socket) {
if (socket.connected) { if (socket.connected) {
args.onConnect?.(socket); args.onConnect?.(socket);
return socket;
} }
return socket;
socket.removeAllListeners();
socket.disconnect();
socket = null;
} }
socket = io(args.client_ns, { socket = io(args.client_ns, {
......
...@@ -58,7 +58,7 @@ export function useAdminGameSocket(options: AdminGameSocketOptions) { ...@@ -58,7 +58,7 @@ export function useAdminGameSocket(options: AdminGameSocketOptions) {
offSocketMessage = onSocketMessage(async (evt, payload: SocketPayload) => { offSocketMessage = onSocketMessage(async (evt, payload: SocketPayload) => {
const { msg, state, data } = payload const { msg, state, data } = payload
if ($goto_login(evt, router, options.loginRedirectPath ?? '/pc/game1')) { if ($goto_login(evt, router, options.loginRedirectPath ?? '/game1')) {
return return
} }
......
...@@ -4,4 +4,6 @@ import { createApp } from 'vue' ...@@ -4,4 +4,6 @@ import { createApp } from 'vue'
import App from './App.vue' import App from './App.vue'
import router from './router' import router from './router'
localStorage.setItem('domain', __APP_DOMAIN__)
createApp(App).use(router).mount('#app') createApp(App).use(router).mount('#app')
<script setup lang="ts"> <script setup lang="ts">
import CryptoJS from 'crypto-js';
import { $save } from '@/commons/utils.ts'; import { $save } from '@/commons/utils.ts';
import { ref, onUnmounted } from 'vue' import { ref } from 'vue'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import DesignStage from '@/components/DesignStage.vue' import DesignStage from '@/components/DesignStage.vue'
import { initSocket, onSocketMessage, sendSocketMessage } from '@/commons/ws.ts';
let offSocketMessage: (() => void) | undefined;
const router = useRouter() const router = useRouter()
const username = ref('') const username = ref('')
const password = ref('') const password = ref('')
...@@ -40,38 +36,8 @@ const loginHandler = async () => { ...@@ -40,38 +36,8 @@ const loginHandler = async () => {
for (let i in data) { for (let i in data) {
$save(i, data[i]); $save(i, data[i]);
} }
let domain = localStorage.getItem('domain') || ''; await router.replace('/game1')
initSocket({
socket_path: `${domain}${data.socket_path}`,
client_ns: data.client_ns,
auth: {
userid: data.token,
validuser: CryptoJS.AES.encrypt(data.token, data.passphrase).toString(),
},
onConnect: async () => {
setTimeout(() => {
sendSocketMessage('room_create', 1);
}, 200);
},
onDisconnect: (reason: string) => { },
onError: (error: any) => { },
});
} }
offSocketMessage = onSocketMessage(async (evt, { msg, state, data }) => {
if (state == 0) {
alert(msg);
return;
}
switch (evt) {
case 'room_create_result': {
await router.replace('/pc/game1')
break;
}
}
});
onUnmounted(() => {
offSocketMessage?.();
});
</script> </script>
<template> <template>
......
...@@ -97,7 +97,7 @@ function handleRoomState(data: any) { ...@@ -97,7 +97,7 @@ function handleRoomState(data: any) {
const { startGame, createRoom, requestRoomState } = useAdminGameSocket({ const { startGame, createRoom, requestRoomState } = useAdminGameSocket({
gameId: 'game1', gameId: 'game1',
loginRedirectPath: '/pc/game1', // loginRedirectPath: '/game1',
onGameStartRejected: gotoLoading, onGameStartRejected: gotoLoading,
onRoomState: handleRoomState, onRoomState: handleRoomState,
onRoomJoin: (data) => { onRoomJoin: (data) => {
......
...@@ -8,15 +8,15 @@ const router = createRouter({ ...@@ -8,15 +8,15 @@ const router = createRouter({
routes: [ routes: [
{ {
path: '/', path: '/',
redirect: () => ($read('token', '') ? '/pc/game1' : '/pc/login'), redirect: () => ($read('token', '') ? '/game1' : '/login'),
}, },
{ {
path: '/pc/login', path: '/login',
name: 'Login', name: 'Login',
component: Login, component: Login,
}, },
{ {
path: '/pc/game1', path: '/game1',
name: 'Game1', name: 'Game1',
component: Game1, component: Game1,
meta: { meta: {
...@@ -34,7 +34,7 @@ router.beforeEach((to) => { ...@@ -34,7 +34,7 @@ router.beforeEach((to) => {
const token = $read('token', '') const token = $read('token', '')
if (to.meta.requiresAuth && !token) { if (to.meta.requiresAuth && !token) {
return '/pc/login' return '/login'
} }
return true return true
......
...@@ -6,7 +6,7 @@ import { defineConfig } from 'vite' ...@@ -6,7 +6,7 @@ import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools' import vueDevTools from 'vite-plugin-vue-devtools'
const buildBase = '/cc/pc/' const buildDomain = 'https://hddpserver.guocai365.org.cn'
function copyImageAssets() { function copyImageAssets() {
return { return {
...@@ -25,11 +25,10 @@ function copyImageAssets() { ...@@ -25,11 +25,10 @@ function copyImageAssets() {
} }
} }
// https://vite.dev/config/
export default defineConfig(({ command }) => ({ export default defineConfig(({ command }) => ({
base: command === 'build' ? buildBase : '/', base: command === 'build' ? '/cc/pc/' : '/',
build: { define: {
emptyOutDir: false, __APP_DOMAIN__: JSON.stringify(command === 'build' ? buildDomain : ''),
}, },
plugins: [ plugins: [
vue(), vue(),
...@@ -51,7 +50,7 @@ export default defineConfig(({ command }) => ({ ...@@ -51,7 +50,7 @@ export default defineConfig(({ command }) => ({
}, },
resolve: { resolve: {
alias: { alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)) '@': fileURLToPath(new URL('./src', import.meta.url)),
}, },
}, },
})) }))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment