解决微信小程序首页空白问题。
This commit is contained in:
@@ -170,9 +170,12 @@
|
||||
},
|
||||
created() {
|
||||
// #ifdef VUE3
|
||||
let getbinddata = getApp().$vm.$.appContext.config.globalProperties.binddata
|
||||
if (!getbinddata) {
|
||||
getApp().$vm.$.appContext.config.globalProperties.binddata = function(name, value, formName) {
|
||||
try {
|
||||
const app = getApp({ allowDefault: true });
|
||||
if (app && app.$vm && app.$vm.$ && app.$vm.$.appContext && app.$vm.$.appContext.config && app.$vm.$.appContext.config.globalProperties) {
|
||||
let getbinddata = app.$vm.$.appContext.config.globalProperties.binddata
|
||||
if (!getbinddata) {
|
||||
app.$vm.$.appContext.config.globalProperties.binddata = function(name, value, formName) {
|
||||
if (formName) {
|
||||
this.$refs[formName].setValue(name, value);
|
||||
} else {
|
||||
@@ -191,6 +194,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 静默失败
|
||||
}
|
||||
// #endif
|
||||
|
||||
// 子组件实例数组
|
||||
|
||||
@@ -480,9 +480,11 @@
|
||||
console.log({params,type});
|
||||
//toLowerCase
|
||||
let action = 'loginBy' + type.trim().replace(type[0], type[0].toUpperCase())
|
||||
|
||||
const uniIdCo = uniCloud.importObject("uni-id-co",{
|
||||
customUI:true
|
||||
})
|
||||
|
||||
uniIdCo[action](params).then(result => {
|
||||
uni.showToast({
|
||||
title: '登录成功',
|
||||
@@ -492,7 +494,65 @@
|
||||
// #ifdef H5
|
||||
result.loginType = type
|
||||
// #endif
|
||||
mutations.loginSuccess(result)
|
||||
|
||||
// 特殊处理微信登录:由于uni-id-pages的结果格式与我们自定义的后端API不同,需要手动设置用户信息
|
||||
if (type === 'weixin') {
|
||||
try {
|
||||
// 获取用户信息(从uni-id-pages的用户数据)
|
||||
const db = uniCloud.database();
|
||||
const userCollection = db.collection('uni-id-users');
|
||||
|
||||
// 根据uid获取完整用户信息
|
||||
userCollection.doc(result.uid).field('username,nickname,avatar_file,mobile,email').get().then(userRes => {
|
||||
const userData = userRes.result.data[0] || {};
|
||||
|
||||
// 设置用户信息到store
|
||||
const userInfoToSet = {
|
||||
username: userData.username || userData.nickname || '微信用户',
|
||||
nickname: userData.nickname,
|
||||
avatar_file: userData.avatar_file,
|
||||
mobile: userData.mobile,
|
||||
email: userData.email
|
||||
};
|
||||
|
||||
mutations.setUserInfo(userInfoToSet, { cover: true });
|
||||
|
||||
// 保存 uni-id token
|
||||
if (result.newToken && result.newToken.token) {
|
||||
uni.setStorageSync('uni_id_token', result.newToken.token);
|
||||
if (result.newToken.tokenExpired) {
|
||||
uni.setStorageSync('uni_id_token_expired', result.newToken.tokenExpired);
|
||||
}
|
||||
}
|
||||
|
||||
// 微信登录成功后跳转到首页
|
||||
setTimeout(() => {
|
||||
uni.switchTab({
|
||||
url: '/pages/furniture_reception/furniture_reception'
|
||||
})
|
||||
}, 500);
|
||||
}).catch(err => {
|
||||
console.error('获取用户信息失败:', err);
|
||||
// 如果获取失败,至少设置基本的登录状态
|
||||
mutations.setUserInfo({
|
||||
username: '微信用户',
|
||||
nickname: '微信用户'
|
||||
}, { cover: true });
|
||||
|
||||
// 即使获取用户信息失败,也要跳转
|
||||
setTimeout(() => {
|
||||
uni.switchTab({
|
||||
url: '/pages/furniture_reception/furniture_reception'
|
||||
})
|
||||
}, 500);
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('设置微信登录用户信息失败:', e);
|
||||
}
|
||||
} else {
|
||||
// 其他登录方式使用原有的处理逻辑
|
||||
mutations.loginSuccess(result);
|
||||
}
|
||||
})
|
||||
.catch(e=>{
|
||||
uni.showModal({
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
|
||||
<script>
|
||||
import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
|
||||
import { mutations } from '@/common/store.js'
|
||||
import { store, mutations } from '@/common/store.js'
|
||||
import { getApiUrl } from '@/common/config.js'
|
||||
export default {
|
||||
mixins: [mixin],
|
||||
@@ -184,6 +184,7 @@
|
||||
email: loginData.email,
|
||||
avatar_file: loginData.avatar ? { url: loginData.avatar } : undefined
|
||||
}, { cover: true })
|
||||
console.log('[Login] Store updated successfully, hasLogin:', store.hasLogin, 'userInfo:', store.userInfo)
|
||||
} catch (e) {
|
||||
console.error('更新本地用户信息失败:', e)
|
||||
}
|
||||
@@ -196,7 +197,7 @@
|
||||
|
||||
// 更新全局角色信息
|
||||
try {
|
||||
const app = getApp();
|
||||
const app = getApp({ allowDefault: true });
|
||||
if (app && app.setGlobalRoleInfo) {
|
||||
app.setGlobalRoleInfo();
|
||||
}
|
||||
@@ -205,11 +206,32 @@
|
||||
}
|
||||
|
||||
// 登录成功后跳转到首页(接待页面)
|
||||
// 在微信小程序中增加延迟,确保store更新完全后再跳转
|
||||
// #ifdef MP-WEIXIN
|
||||
setTimeout(() => {
|
||||
console.log('[Login] Preparing to navigate after login, hasLogin:', store.hasLogin)
|
||||
uni.switchTab({
|
||||
url: '/pages/furniture_reception/furniture_reception',
|
||||
success: () => {
|
||||
console.log('[Login] Navigation successful, checking login status after navigation')
|
||||
// 延迟检查,确保页面跳转完成后再检查状态
|
||||
setTimeout(() => {
|
||||
console.log('[Login] Final check - hasLogin:', store.hasLogin, 'backend-token exists:', !!uni.getStorageSync('backend-token'))
|
||||
}, 500)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('[Login] Navigation failed:', err)
|
||||
}
|
||||
})
|
||||
}, 500)
|
||||
// #endif
|
||||
// #ifndef MP-WEIXIN
|
||||
setTimeout(() => {
|
||||
uni.switchTab({
|
||||
url: '/pages/furniture_reception/furniture_reception'
|
||||
})
|
||||
}, 300)
|
||||
// #endif
|
||||
} catch (err) {
|
||||
console.error('调用登录接口失败:', err)
|
||||
uni.showToast({
|
||||
|
||||
Reference in New Issue
Block a user