mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-06 10:07:36 +08:00
Merge pull request #446 from nagisa77/codex/fix-ismobile-check-for-initial-data
This commit is contained in:
@@ -147,7 +147,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { ref, computed, watch, onMounted } from "vue"
|
import { ref, computed, watch, onMounted } from "vue"
|
||||||
import { isMobile } from "~/utils/screen"
|
import { useIsMobile } from "~/utils/screen"
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -176,6 +176,7 @@ export default {
|
|||||||
const loaded = ref(false)
|
const loaded = ref(false)
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const wrapper = ref(null)
|
const wrapper = ref(null)
|
||||||
|
const isMobile = useIsMobile()
|
||||||
|
|
||||||
const toggle = () => {
|
const toggle = () => {
|
||||||
open.value = !open.value
|
open.value = !open.value
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ import { watch, nextTick } from 'vue'
|
|||||||
import { fetchUnreadCount, notificationState } from '~/utils/notification'
|
import { fetchUnreadCount, notificationState } from '~/utils/notification'
|
||||||
import DropdownMenu from '~/components/DropdownMenu.vue'
|
import DropdownMenu from '~/components/DropdownMenu.vue'
|
||||||
import SearchDropdown from '~/components/SearchDropdown.vue'
|
import SearchDropdown from '~/components/SearchDropdown.vue'
|
||||||
import { isMobile } from '~/utils/screen'
|
import { useIsMobile } from '~/utils/screen'
|
||||||
import { ClientOnly } from '#components'
|
import { ClientOnly } from '#components'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -71,7 +71,7 @@ export default {
|
|||||||
},
|
},
|
||||||
setup() {
|
setup() {
|
||||||
const isLogin = computed(() => authState.loggedIn)
|
const isLogin = computed(() => authState.loggedIn)
|
||||||
const isMobile = computed(() => isMobile.value)
|
const isMobile = useIsMobile()
|
||||||
const unreadCount = computed(() => notificationState.unreadCount)
|
const unreadCount = computed(() => notificationState.unreadCount)
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { isMobile } from '../utils/screen'
|
import { useIsMobile } from '../utils/screen'
|
||||||
export default {
|
export default {
|
||||||
name: 'NotificationContainer',
|
name: 'NotificationContainer',
|
||||||
props: {
|
props: {
|
||||||
@@ -21,6 +21,7 @@ export default {
|
|||||||
markRead: { type: Function, required: true }
|
markRead: { type: Function, required: true }
|
||||||
},
|
},
|
||||||
setup() {
|
setup() {
|
||||||
|
const isMobile = useIsMobile()
|
||||||
return {
|
return {
|
||||||
isMobile
|
isMobile
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { ref, watch } from 'vue'
|
import { ref, watch } from 'vue'
|
||||||
import { isMobile } from '~/utils/screen'
|
import { useIsMobile } from '~/utils/screen'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import Dropdown from '~/components/Dropdown.vue'
|
import Dropdown from '~/components/Dropdown.vue'
|
||||||
import { API_BASE_URL } from '~/main'
|
import { API_BASE_URL } from '~/main'
|
||||||
@@ -40,6 +40,7 @@ export default {
|
|||||||
const selected = ref(null)
|
const selected = ref(null)
|
||||||
const results = ref([])
|
const results = ref([])
|
||||||
const dropdown = ref(null)
|
const dropdown = ref(null)
|
||||||
|
const isMobile = useIsMobile()
|
||||||
|
|
||||||
const toggle = () => {
|
const toggle = () => {
|
||||||
dropdown.value.toggle()
|
dropdown.value.toggle()
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ import TagSelect from '~/components/TagSelect.vue'
|
|||||||
import ArticleTags from '~/components/ArticleTags.vue'
|
import ArticleTags from '~/components/ArticleTags.vue'
|
||||||
import ArticleCategory from '~/components/ArticleCategory.vue'
|
import ArticleCategory from '~/components/ArticleCategory.vue'
|
||||||
import SearchDropdown from '~/components/SearchDropdown.vue'
|
import SearchDropdown from '~/components/SearchDropdown.vue'
|
||||||
import { isMobile } from '~/utils/screen'
|
import { useIsMobile } from '~/utils/screen'
|
||||||
|
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
scrollToTop: false,
|
scrollToTop: false,
|
||||||
@@ -170,6 +170,7 @@ export default {
|
|||||||
const pageSize = 10
|
const pageSize = 10
|
||||||
const allLoaded = useState('index-allLoaded', () => false)
|
const allLoaded = useState('index-allLoaded', () => false)
|
||||||
const initialized = useState('index-initialized', () => false)
|
const initialized = useState('index-initialized', () => false)
|
||||||
|
const isMobile = useIsMobile()
|
||||||
|
|
||||||
const loadOptions = async () => {
|
const loadOptions = async () => {
|
||||||
if (selectedCategory.value && !isNaN(selectedCategory.value)) {
|
if (selectedCategory.value && !isNaN(selectedCategory.value)) {
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ import { API_BASE_URL, toast } from '../../../main'
|
|||||||
import { getToken, authState } from '../../../utils/auth'
|
import { getToken, authState } from '../../../utils/auth'
|
||||||
import TimeManager from '../../../utils/time'
|
import TimeManager from '../../../utils/time'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { isMobile } from '../../../utils/screen'
|
import { useIsMobile } from '../../../utils/screen'
|
||||||
import Dropdown from '../../../components/Dropdown.vue'
|
import Dropdown from '../../../components/Dropdown.vue'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -149,6 +149,7 @@ export default {
|
|||||||
const subscribed = ref(false)
|
const subscribed = ref(false)
|
||||||
const commentSort = ref('NEWEST')
|
const commentSort = ref('NEWEST')
|
||||||
const isFetchingComments = ref(false)
|
const isFetchingComments = ref(false)
|
||||||
|
const isMobile = useIsMobile()
|
||||||
|
|
||||||
// record default metadata from the main document (client only)
|
// record default metadata from the main document (client only)
|
||||||
const defaultTitle = process.client ? document.title : ''
|
const defaultTitle = process.client ? document.title : ''
|
||||||
|
|||||||
@@ -1,45 +1,46 @@
|
|||||||
import { ref, computed } from 'vue'
|
import { ref, computed, onUnmounted } from 'vue'
|
||||||
import { useRequestHeaders } from 'nuxt/app'
|
import { useRequestHeaders } from 'nuxt/app'
|
||||||
|
|
||||||
const width = ref(0)
|
export const useIsMobile = () => {
|
||||||
const isClient = ref(false)
|
const width = ref(0)
|
||||||
|
const isClient = ref(false)
|
||||||
|
|
||||||
// 检测移动设备的用户代理字符串
|
const isMobileUserAgent = () => {
|
||||||
const isMobileUserAgent = () => {
|
let userAgent = ''
|
||||||
let userAgent = ''
|
|
||||||
|
|
||||||
if (typeof navigator !== 'undefined') {
|
if (typeof navigator !== 'undefined') {
|
||||||
userAgent = navigator.userAgent.toLowerCase()
|
userAgent = navigator.userAgent.toLowerCase()
|
||||||
} else {
|
} else {
|
||||||
// 服务端:从请求头获取用户代理字符串
|
const headers = useRequestHeaders(['user-agent'])
|
||||||
const headers = useRequestHeaders(['user-agent'])
|
userAgent = (headers['user-agent'] || '').toLowerCase()
|
||||||
userAgent = (headers['user-agent'] || '').toLowerCase()
|
}
|
||||||
|
|
||||||
|
const mobileKeywords = [
|
||||||
|
'android', 'iphone', 'ipad', 'ipod', 'blackberry', 'windows phone',
|
||||||
|
'mobile', 'tablet', 'opera mini', 'iemobile'
|
||||||
|
]
|
||||||
|
|
||||||
|
return mobileKeywords.some(keyword => userAgent.includes(keyword))
|
||||||
}
|
}
|
||||||
|
|
||||||
const mobileKeywords = [
|
if (typeof window !== 'undefined') {
|
||||||
'android', 'iphone', 'ipad', 'ipod', 'blackberry', 'windows phone',
|
isClient.value = true
|
||||||
'mobile', 'tablet', 'opera mini', 'iemobile'
|
const updateWidth = () => {
|
||||||
]
|
width.value = window.innerWidth
|
||||||
|
}
|
||||||
|
updateWidth()
|
||||||
|
window.addEventListener('resize', updateWidth)
|
||||||
|
onUnmounted(() => {
|
||||||
|
window.removeEventListener('resize', updateWidth)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return mobileKeywords.some(keyword => userAgent.includes(keyword))
|
return computed(() => {
|
||||||
}
|
if (isClient.value) {
|
||||||
|
return width.value > 0 ? width.value <= 768 : isMobileUserAgent()
|
||||||
|
}
|
||||||
|
|
||||||
// 客户端初始化
|
return isMobileUserAgent()
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
isClient.value = true
|
|
||||||
width.value = window.innerWidth
|
|
||||||
window.addEventListener('resize', () => {
|
|
||||||
width.value = window.innerWidth
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 服务端和客户端的移动设备检测
|
|
||||||
export const isMobile = computed(() => {
|
|
||||||
if (isClient.value) {
|
|
||||||
// 客户端:优先使用窗口宽度,如果窗口宽度不可用则使用用户代理
|
|
||||||
return width.value > 0 ? width.value <= 768 : isMobileUserAgent()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 服务端:使用请求头中的用户代理字符串
|
|
||||||
return isMobileUserAgent()
|
|
||||||
})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user