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

master
黄伟杰 3 days ago
parent a97d9223b6
commit e632a6dd8f

@ -2,17 +2,19 @@
import { getToken } from '@/utils/auth'
import { initializeLocale, translateLiteral } from '@/locales'
import useUserStore from '@/store/modules/user'
import { applyNavigationBarTheme } from '@/utils/navigationBar'
let wrapped = false
function wrapUniTextApi() {
if (wrapped) return
wrapped = true
const rawShowToast = uni.showToast
const rawShowModal = uni.showModal
const rawShowLoading = uni.showLoading
const rawSetNavigationBarTitle = uni.setNavigationBarTitle
const rawSetNavigationBarColor = uni.setNavigationBarColor
uni.showToast = (options = {}) => {
const next = { ...options }
if (typeof next.title === 'string') {
@ -49,7 +51,17 @@ function wrapUniTextApi() {
next.title = translateLiteral(next.title)
}
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
}
}
@ -58,7 +70,6 @@ export default {
onLaunch: function () {
initializeLocale()
wrapUniTextApi()
applyNavigationBarTheme()
if (getToken()) {
useUserStore().getInfo().catch(() => {})
}
@ -66,7 +77,6 @@ export default {
onShow: function () {
initializeLocale()
wrapUniTextApi()
applyNavigationBarTheme()
},
onHide: function () {
}

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

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

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

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

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