fix:修复setNavigationBarTitle调用后导致背景颜色被重置问题

master
黄伟杰 3 days ago
parent a97d9223b6
commit e632a6dd8f

@ -2,17 +2,19 @@
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import { initializeLocale, translateLiteral } from '@/locales' import { initializeLocale, translateLiteral } from '@/locales'
import useUserStore from '@/store/modules/user' import useUserStore from '@/store/modules/user'
import { applyNavigationBarTheme } from '@/utils/navigationBar'
let wrapped = false let wrapped = false
function wrapUniTextApi() { function wrapUniTextApi() {
if (wrapped) return if (wrapped) return
wrapped = true wrapped = true
const rawShowToast = uni.showToast const rawShowToast = uni.showToast
const rawShowModal = uni.showModal const rawShowModal = uni.showModal
const rawShowLoading = uni.showLoading const rawShowLoading = uni.showLoading
const rawSetNavigationBarTitle = uni.setNavigationBarTitle const rawSetNavigationBarTitle = uni.setNavigationBarTitle
const rawSetNavigationBarColor = uni.setNavigationBarColor
uni.showToast = (options = {}) => { uni.showToast = (options = {}) => {
const next = { ...options } const next = { ...options }
if (typeof next.title === 'string') { if (typeof next.title === 'string') {
@ -49,7 +51,17 @@ function wrapUniTextApi() {
next.title = translateLiteral(next.title) next.title = translateLiteral(next.title)
} }
const result = rawSetNavigationBarTitle(next) const result = rawSetNavigationBarTitle(next)
applyNavigationBarTheme() setTimeout(() => {
const pages = getCurrentPages()
if (pages && pages.length > 0) {
const route = pages[pages.length - 1].route || ''
if (route === 'pages/login') {
rawSetNavigationBarColor({ frontColor: '#000000', backgroundColor: '#ffffff' })
} else {
rawSetNavigationBarColor({ frontColor: '#ffffff', backgroundColor: '#22486e' })
}
}
}, 150)
return result return result
} }
} }
@ -58,7 +70,6 @@ export default {
onLaunch: function () { onLaunch: function () {
initializeLocale() initializeLocale()
wrapUniTextApi() wrapUniTextApi()
applyNavigationBarTheme()
if (getToken()) { if (getToken()) {
useUserStore().getInfo().catch(() => {}) useUserStore().getInfo().catch(() => {})
} }
@ -66,7 +77,6 @@ export default {
onShow: function () { onShow: function () {
initializeLocale() initializeLocale()
wrapUniTextApi() wrapUniTextApi()
applyNavigationBarTheme()
}, },
onHide: function () { onHide: function () {
} }

@ -1,8 +1,8 @@
// 应用全局配置 // 应用全局配置
const config = { const config = {
// baseUrl: 'http://47.106.185.127:48080',127.0.0.1 // baseUrl: 'http://47.106.185.127:48080',127.0.0.1
baseUrl: 'https://besure.ngsk.tech:7001', // baseUrl: 'https://besure.ngsk.tech:7001',
// baseUrl: 'http://192.168.5.167:48081', baseUrl: 'http://192.168.5.106:48081',
// baseUrl: '', // baseUrl: '',
// 应用信息 // 应用信息
appInfo: { appInfo: {

@ -11,9 +11,6 @@ import { translateLiteral } from '@/locales'
import { useDict } from '@/utils/dict' import { useDict } from '@/utils/dict'
import { parseTime, resetForm, addDateRange, handleTree, selectDictLabel, selectDictLabels } from '@/utils/ruoyi' import { parseTime, resetForm, addDateRange, handleTree, selectDictLabel, selectDictLabels } from '@/utils/ruoyi'
import { applyNavigationBarTheme } from '@/utils/navigationBar'
export function createApp() { export function createApp() {
const app = createSSRApp(App) const app = createSSRApp(App)
@ -33,12 +30,6 @@ export function createApp() {
app.config.globalProperties.selectDictLabels = selectDictLabels app.config.globalProperties.selectDictLabels = selectDictLabels
app.config.globalProperties.$tl = translateLiteral app.config.globalProperties.$tl = translateLiteral
app.mixin({
onShow() {
applyNavigationBarTheme()
}
})
return { return {
app app
} }

@ -11,14 +11,17 @@
"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue" "^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue"
} }
}, },
"globalStyle": {
"navigationBarTextStyle": "white",
"navigationBarTitleText": "BESURE",
"navigationBarBackgroundColor": "#22486e"
},
"pages": [ "pages": [
{ {
"path": "pages/index", "path": "pages/index",
"style": { "style": {
"navigationBarTitleText": "首页", "navigationBarTitleText": "首页",
"enablePullDownRefresh": false, "enablePullDownRefresh": false
"navigationBarBackgroundColor": "#22486e",
"navigationBarTextStyle": "white"
} }
}, },
{ {
@ -33,43 +36,32 @@
"path": "pages/work", "path": "pages/work",
"style": { "style": {
"navigationBarTitleText": "管理", "navigationBarTitleText": "管理",
"enablePullDownRefresh": false, "enablePullDownRefresh": false
"navigationBarBackgroundColor": "#22486e",
"navigationBarTextStyle": "white"
} }
}, },
{ {
"path": "pages/mine", "path": "pages/mine",
"style": { "style": {
"navigationBarTitleText": "个人中心", "navigationBarTitleText": "个人中心"
"navigationBarBackgroundColor": "#22486e",
"navigationBarTextStyle": "white"
} }
}, },
{ {
"path": "pages/common/webview/index", "path": "pages/common/webview/index",
"style": { "style": {
"navigationBarTitleText": "浏览网页", "navigationBarTitleText": "浏览网页"
"navigationBarBackgroundColor": "#22486e",
"navigationBarTextStyle": "white"
} }
}, },
{ {
"path": "pages/common/textview/index", "path": "pages/common/textview/index",
"style": { "style": {
"navigationBarTitleText": "浏览文本", "navigationBarTitleText": "浏览文本"
"navigationBarBackgroundColor": "#22486e",
"navigationBarTextStyle": "white"
} }
}, },
{ {
"path": "pages/report", "path": "pages/report",
"style": { "style": {
"navigationBarTitleText": "报表", "navigationBarTitleText": "报表",
"enablePullDownRefresh": false, "enablePullDownRefresh": false
"navigationBarBackgroundColor": "#22486e",
"navigationBarTextStyle": "white"
} }
} }
], ],
@ -88,8 +80,7 @@
"style": { "style": {
"navigationBarTitleText": "代报工" "navigationBarTitleText": "代报工"
} }
} },
,
{ {
"path": "planProgress", "path": "planProgress",
"style": { "style": {
@ -649,10 +640,5 @@
"text": "我的" "text": "我的"
} }
] ]
},
"globalStyle": {
"navigationBarTextStyle": "white",
"navigationBarTitleText": "BESURE",
"navigationBarBackgroundColor": "#22486e"
} }
} }

@ -19,7 +19,6 @@
<script setup> <script setup>
import { onMounted, onUnmounted, ref } from 'vue' import { onMounted, onUnmounted, ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { onLocaleChange, offLocaleChange, setNavigationTitle } from '@/locales' import { onLocaleChange, offLocaleChange, setNavigationTitle } from '@/locales'
import BannerSection from '@/components/dashboard/BannerSection.vue' import BannerSection from '@/components/dashboard/BannerSection.vue'
import NavSection from '@/components/dashboard/NavSection.vue' import NavSection from '@/components/dashboard/NavSection.vue'
@ -49,9 +48,6 @@ function goTop() {
} }
const updatePageTitle = () => setNavigationTitle('nav.home') const updatePageTitle = () => setNavigationTitle('nav.home')
onShow(() => {
updatePageTitle()
})
onLocaleChange(updatePageTitle) onLocaleChange(updatePageTitle)
onUnmounted(() => { onUnmounted(() => {
offLocaleChange(updatePageTitle) offLocaleChange(updatePageTitle)

@ -84,7 +84,6 @@
<script setup> <script setup>
import { onUnmounted, ref } from "vue"; import { onUnmounted, ref } from "vue";
import { onShow } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import useUserStore from '@/store/modules/user' import useUserStore from '@/store/modules/user'
import { onLocaleChange, offLocaleChange, setNavigationTitle } from '@/locales' import { onLocaleChange, offLocaleChange, setNavigationTitle } from '@/locales'
@ -101,9 +100,6 @@ uni.$on('refresh', () => {
}) })
const updatePageTitle = () => setNavigationTitle('nav.mine') const updatePageTitle = () => setNavigationTitle('nav.mine')
onShow(() => {
updatePageTitle()
})
onLocaleChange(updatePageTitle) onLocaleChange(updatePageTitle)
function handleToInfo() { function handleToInfo() {

@ -1,48 +0,0 @@
const DEFAULT_NAV_BACKGROUND = '#22486e'
const DEFAULT_NAV_FRONT = '#ffffff'
const LOGIN_NAV_BACKGROUND = '#ffffff'
const LOGIN_NAV_FRONT = '#000000'
function getCurrentRoute() {
try {
const pages = getCurrentPages()
if (!pages || pages.length === 0) return ''
return pages[pages.length - 1]?.route || ''
} catch (error) {
return ''
}
}
function getNavigationTheme(route = '') {
const currentRoute = route || getCurrentRoute()
if (currentRoute === 'pages/login') {
return {
frontColor: LOGIN_NAV_FRONT,
backgroundColor: LOGIN_NAV_BACKGROUND
}
}
return {
frontColor: DEFAULT_NAV_FRONT,
backgroundColor: DEFAULT_NAV_BACKGROUND
}
}
export function applyNavigationBarTheme(route = '') {
const theme = getNavigationTheme(route)
try {
uni.setNavigationBarColor({
frontColor: theme.frontColor,
backgroundColor: theme.backgroundColor,
animation: {
duration: 0,
timingFunc: 'linear'
}
})
} catch (error) {
}
}
export function getDefaultNavigationBackground() {
return DEFAULT_NAV_BACKGROUND
}
Loading…
Cancel
Save