增加线上配置分享功能+用户后台登录,不影响首页渲染
This commit is contained in:
@@ -4,11 +4,19 @@ const { request } = require('./utils/api')
|
||||
|
||||
App({
|
||||
onLaunch() {
|
||||
// 自动静默登录:wx.login 获取 code,再请求后端接口换取 openid
|
||||
// 优先从本地缓存恢复登录态,避免阻塞页面
|
||||
const cached = wx.getStorageSync('userInfo')
|
||||
if (cached && cached.openid) {
|
||||
this.globalData.userInfo = cached
|
||||
this.globalData.isLoggedIn = true
|
||||
}
|
||||
// 静默登录获取最新 session,不阻塞页面
|
||||
this.silentLogin()
|
||||
},
|
||||
|
||||
silentLogin() {
|
||||
this._loginPromise = new Promise((resolve) => {
|
||||
this._loginResolve = resolve
|
||||
wx.login({
|
||||
success: (loginRes) => {
|
||||
if (loginRes.code) {
|
||||
@@ -23,6 +31,19 @@ App({
|
||||
this.handleLoginFail()
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 返回登录完成的 Promise,供页面 await 使用
|
||||
* @param {boolean} useCache - 是否在已有缓存时立即 resolve
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
waitLogin(useCache = false) {
|
||||
if (useCache && this.globalData.isLoggedIn) {
|
||||
return Promise.resolve(this.globalData.userInfo)
|
||||
}
|
||||
return this._loginPromise || Promise.resolve(this.globalData.userInfo || null)
|
||||
},
|
||||
|
||||
async loginWithCode(code) {
|
||||
@@ -39,22 +60,25 @@ App({
|
||||
}
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = true
|
||||
this.globalData.loginFailed = false
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(userInfo)
|
||||
}
|
||||
if (this._loginResolve) this._loginResolve(userInfo)
|
||||
} catch (err) {
|
||||
console.error('后端登录失败', err)
|
||||
// 如果有缓存登录态,登录失败不立即清除,让页面继续使用缓存
|
||||
if (!this.globalData.isLoggedIn) {
|
||||
this.handleLoginFail()
|
||||
} else {
|
||||
this.globalData.loginFailed = true
|
||||
if (this._loginResolve) this._loginResolve(this.globalData.userInfo)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
handleLoginFail() {
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.loginFailed = true
|
||||
if (this.loginReadyCallback) {
|
||||
this.loginReadyCallback(null)
|
||||
}
|
||||
if (this._loginResolve) this._loginResolve(null)
|
||||
},
|
||||
|
||||
globalData: {
|
||||
|
||||
@@ -34,21 +34,16 @@ Page({
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
if (app.globalData.isLoggedIn) {
|
||||
this.initPage()
|
||||
} else if (app.globalData.loginFailed) {
|
||||
this.showLoginTipAndGoBack()
|
||||
} else {
|
||||
// 登录异步未完成,等待回调
|
||||
app.loginReadyCallback = (userInfo) => {
|
||||
app.loginReadyCallback = null
|
||||
this._awaitLoginAndInit()
|
||||
},
|
||||
|
||||
async _awaitLoginAndInit() {
|
||||
const userInfo = await app.waitLogin(true)
|
||||
if (userInfo) {
|
||||
this.initPage()
|
||||
} else {
|
||||
this.showLoginTipAndGoBack()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
initPage() {
|
||||
@@ -292,5 +287,18 @@ Page({
|
||||
console.error('提交预约失败', err)
|
||||
wx.showToast({ title: '提交失败,请重试', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
onShareAppMessage(res) {
|
||||
return {
|
||||
title: '访客预约',
|
||||
path: '/pages/index/index'
|
||||
}
|
||||
},
|
||||
|
||||
onShareTimeline() {
|
||||
return {
|
||||
title: '访客预约'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
+26
-25
@@ -11,45 +11,33 @@ Page({
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
// 首页不阻塞,直接渲染;有缓存登录态时立即加载数据
|
||||
if (app.globalData.isLoggedIn) {
|
||||
this.onLoginReady()
|
||||
} else if (app.globalData.loginFailed) {
|
||||
this.onLoginFailed()
|
||||
} else {
|
||||
app.loginReadyCallback = (userInfo) => {
|
||||
if (userInfo) {
|
||||
this.onLoginReady()
|
||||
} else {
|
||||
this.onLoginFailed()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
if (app.globalData.isLoggedIn) {
|
||||
this.setData({ isLoggedIn: true })
|
||||
this.loadLatestRecord()
|
||||
}
|
||||
},
|
||||
|
||||
onLoginReady() {
|
||||
async onShow() {
|
||||
// 每次显示时等待登录完成,再加载最新数据
|
||||
const userInfo = await app.waitLogin(true)
|
||||
if (userInfo) {
|
||||
this.setData({ isLoggedIn: true, loginFailed: false })
|
||||
this.loadLatestRecord()
|
||||
},
|
||||
|
||||
onLoginFailed() {
|
||||
} else {
|
||||
this.setData({ isLoggedIn: false, loginFailed: true })
|
||||
}
|
||||
},
|
||||
|
||||
onRetry() {
|
||||
async onRetry() {
|
||||
this.setData({ loginFailed: false })
|
||||
app.silentLogin()
|
||||
app.loginReadyCallback = (userInfo) => {
|
||||
const userInfo = await app.waitLogin()
|
||||
if (userInfo) {
|
||||
this.onLoginReady()
|
||||
this.setData({ isLoggedIn: true, loginFailed: false })
|
||||
this.loadLatestRecord()
|
||||
} else {
|
||||
this.onLoginFailed()
|
||||
}
|
||||
this.setData({ loginFailed: true })
|
||||
}
|
||||
},
|
||||
|
||||
@@ -85,5 +73,18 @@ Page({
|
||||
|
||||
showQrcode(e) {
|
||||
this.selectComponent('#qrcodeModal').show(e.currentTarget.dataset.id)
|
||||
},
|
||||
|
||||
onShareAppMessage(res) {
|
||||
return {
|
||||
title: '访客预约',
|
||||
path: '/pages/index/index'
|
||||
}
|
||||
},
|
||||
|
||||
onShareTimeline() {
|
||||
return {
|
||||
title: '访客预约'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
+4
-11
@@ -1,16 +1,9 @@
|
||||
<!--index.wxml-->
|
||||
<view class="page">
|
||||
<!-- loading 遮罩 -->
|
||||
<view class="loading-mask" wx:if="{{!isLoggedIn && !loginFailed}}">
|
||||
<view class="loading-spinner"></view>
|
||||
<text class="loading-text">正在获取身份信息...</text>
|
||||
</view>
|
||||
|
||||
<!-- 登录失败 -->
|
||||
<view class="loading-mask" wx:if="{{loginFailed}}">
|
||||
<text class="fail-icon">⚠️</text>
|
||||
<text class="fail-text">网络异常,请重试</text>
|
||||
<view class="retry-btn" bindtap="onRetry">重新加载</view>
|
||||
<!-- 登录失败提示条(不遮挡页面) -->
|
||||
<view class="login-fail-bar" wx:if="{{loginFailed && !isLoggedIn}}">
|
||||
<text>网络异常,无法获取身份信息</text>
|
||||
<text class="retry-link" bindtap="onRetry">重试</text>
|
||||
</view>
|
||||
|
||||
<view class="header">
|
||||
|
||||
+12
-55
@@ -21,67 +21,24 @@ page {
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
/* loading 遮罩 */
|
||||
.loading-mask {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: #f0f5fa;
|
||||
/* 登录失败提示条(不遮挡页面) */
|
||||
.login-fail-bar {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 999;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
width: 64rpx;
|
||||
height: 64rpx;
|
||||
border: 6rpx solid #dbeafe;
|
||||
border-top: 6rpx solid #5b9bd5;
|
||||
border-radius: 50%;
|
||||
animation: spin 0.8s linear infinite;
|
||||
margin-bottom: 24rpx;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.loading-text {
|
||||
font-size: 28rpx;
|
||||
color: #7f8fa6;
|
||||
letter-spacing: 2rpx;
|
||||
}
|
||||
|
||||
/* 登录失败 */
|
||||
.fail-icon {
|
||||
font-size: 80rpx;
|
||||
justify-content: space-between;
|
||||
background: #fff3e0;
|
||||
color: #e6a23c;
|
||||
font-size: 24rpx;
|
||||
padding: 16rpx 24rpx;
|
||||
border-radius: 12rpx;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.fail-text {
|
||||
font-size: 28rpx;
|
||||
color: #7f8fa6;
|
||||
margin-bottom: 32rpx;
|
||||
}
|
||||
|
||||
.retry-btn {
|
||||
font-size: 28rpx;
|
||||
color: #fff;
|
||||
background: linear-gradient(135deg, #5b9bd5, #4a8bc2);
|
||||
padding: 16rpx 56rpx;
|
||||
border-radius: 40rpx;
|
||||
.login-fail-bar .retry-link {
|
||||
color: #5b9bd5;
|
||||
font-weight: 600;
|
||||
letter-spacing: 2rpx;
|
||||
box-shadow: 0 4rpx 16rpx rgba(91, 155, 213, 0.3);
|
||||
}
|
||||
|
||||
.retry-btn:active {
|
||||
opacity: 0.85;
|
||||
flex-shrink: 0;
|
||||
margin-left: 20rpx;
|
||||
}
|
||||
|
||||
.header-title {
|
||||
|
||||
+26
-26
@@ -13,19 +13,16 @@ Page({
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
if (app.globalData.isLoggedIn) {
|
||||
this.onLoginReady()
|
||||
} else if (app.globalData.loginFailed) {
|
||||
this.onLoginFailed()
|
||||
} else {
|
||||
app.loginReadyCallback = (userInfo) => {
|
||||
app.loginReadyCallback = null
|
||||
this._awaitLoginAndLoad()
|
||||
},
|
||||
|
||||
async _awaitLoginAndLoad() {
|
||||
const userInfo = await app.waitLogin(true)
|
||||
if (userInfo) {
|
||||
this.onLoginReady()
|
||||
this.setData({ isLoggedIn: true, loginFailed: false })
|
||||
this.loadRecords()
|
||||
} else {
|
||||
this.onLoginFailed()
|
||||
}
|
||||
}
|
||||
this.setData({ isLoggedIn: false, loginFailed: true, loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
@@ -36,25 +33,15 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
onLoginReady() {
|
||||
this.setData({ isLoggedIn: true, loginFailed: false })
|
||||
this.loadRecords()
|
||||
},
|
||||
|
||||
onLoginFailed() {
|
||||
this.setData({ isLoggedIn: false, loginFailed: true, loading: false })
|
||||
},
|
||||
|
||||
onRetry() {
|
||||
async onRetry() {
|
||||
this.setData({ loginFailed: false, loading: true })
|
||||
app.silentLogin()
|
||||
app.loginReadyCallback = (userInfo) => {
|
||||
app.loginReadyCallback = null
|
||||
const userInfo = await app.waitLogin()
|
||||
if (userInfo) {
|
||||
this.onLoginReady()
|
||||
this.setData({ isLoggedIn: true, loginFailed: false })
|
||||
this.loadRecords()
|
||||
} else {
|
||||
this.onLoginFailed()
|
||||
}
|
||||
this.setData({ loginFailed: true })
|
||||
}
|
||||
},
|
||||
|
||||
@@ -122,5 +109,18 @@ Page({
|
||||
|
||||
showQrcode(e) {
|
||||
this.selectComponent('#qrcodeModal').show(e.currentTarget.dataset.id)
|
||||
},
|
||||
|
||||
onShareAppMessage(res) {
|
||||
return {
|
||||
title: '访客预约',
|
||||
path: '/pages/index/index'
|
||||
}
|
||||
},
|
||||
|
||||
onShareTimeline() {
|
||||
return {
|
||||
title: '访客预约'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -94,5 +94,18 @@ Page({
|
||||
this.setData({ verifying: false })
|
||||
wx.showToast({ title: err.message || '核销失败,请稍后重试', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
onShareAppMessage(res) {
|
||||
return {
|
||||
title: '访客预约',
|
||||
path: '/pages/index/index'
|
||||
}
|
||||
},
|
||||
|
||||
onShareTimeline() {
|
||||
return {
|
||||
title: '访客预约'
|
||||
}
|
||||
}
|
||||
})
|
||||
+1
-1
@@ -5,7 +5,7 @@ const ENV_CONFIG = {
|
||||
release: 'https://smartguest.bmser.com:8091',
|
||||
trial: 'https://smartguest.bmser.com:8091',
|
||||
// develop: 'https://192.168.123.76:8080'
|
||||
develop: 'https://10.50.13.191:8091'
|
||||
develop: 'https://10.50.13.185:8091'
|
||||
}
|
||||
|
||||
// 自动判断当前运行环境
|
||||
|
||||
Reference in New Issue
Block a user