185 lines
4.1 KiB
JavaScript
185 lines
4.1 KiB
JavaScript
|
|
// pages/login/login.js
|
||
|
|
const app = getApp();
|
||
|
|
|
||
|
|
Page({
|
||
|
|
data: {
|
||
|
|
isLoading: false,
|
||
|
|
redirectUrl: ''
|
||
|
|
},
|
||
|
|
|
||
|
|
onLoad(options) {
|
||
|
|
// 获取重定向URL
|
||
|
|
if (options.redirect) {
|
||
|
|
this.setData({
|
||
|
|
redirectUrl: decodeURIComponent(options.redirect)
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 如果已经登录,直接跳转
|
||
|
|
if (app.isLoggedIn()) {
|
||
|
|
this.redirectToTarget();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 处理登录
|
||
|
|
*/
|
||
|
|
handleLogin() {
|
||
|
|
if (this.data.isLoading) return;
|
||
|
|
|
||
|
|
this.setData({ isLoading: true });
|
||
|
|
|
||
|
|
// 执行登录(必须在点击事件的同步上下文中调用)
|
||
|
|
app.login()
|
||
|
|
.then(result => {
|
||
|
|
console.log('登录成功', result);
|
||
|
|
|
||
|
|
wx.showToast({
|
||
|
|
title: '登录成功',
|
||
|
|
icon: 'success',
|
||
|
|
duration: 1500
|
||
|
|
});
|
||
|
|
|
||
|
|
// 延迟跳转,让用户看到成功提示
|
||
|
|
setTimeout(() => {
|
||
|
|
// 询问是否开启消息通知
|
||
|
|
this.askForNotification();
|
||
|
|
|
||
|
|
// 跳转到目标页面
|
||
|
|
this.redirectToTarget();
|
||
|
|
}, 1500);
|
||
|
|
})
|
||
|
|
.catch(error => {
|
||
|
|
console.error('登录失败', error);
|
||
|
|
|
||
|
|
let errorMsg = '登录失败,请重试';
|
||
|
|
if (error.errMsg) {
|
||
|
|
if (error.errMsg.includes('auth deny') || error.errMsg.includes('deny')) {
|
||
|
|
errorMsg = '您拒绝了授权,无法登录';
|
||
|
|
} else if (error.errMsg.includes('cancel')) {
|
||
|
|
errorMsg = '您取消了授权';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
wx.showModal({
|
||
|
|
title: '登录失败',
|
||
|
|
content: errorMsg,
|
||
|
|
showCancel: false
|
||
|
|
});
|
||
|
|
|
||
|
|
this.setData({ isLoading: false });
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 跳过登录
|
||
|
|
*/
|
||
|
|
handleSkipLogin() {
|
||
|
|
wx.showModal({
|
||
|
|
title: '提示',
|
||
|
|
content: '未登录将无法使用完整功能,确定要跳过吗?',
|
||
|
|
confirmText: '确定跳过',
|
||
|
|
cancelText: '去登录',
|
||
|
|
success: res => {
|
||
|
|
if (res.confirm) {
|
||
|
|
// 执行静默登录
|
||
|
|
this.silentLogin();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 静默登录
|
||
|
|
*/
|
||
|
|
async silentLogin() {
|
||
|
|
try {
|
||
|
|
await app.silentLogin();
|
||
|
|
this.redirectToTarget();
|
||
|
|
} catch (error) {
|
||
|
|
console.error('静默登录失败', error);
|
||
|
|
this.redirectToTarget();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 跳转到目标页面
|
||
|
|
*/
|
||
|
|
redirectToTarget() {
|
||
|
|
const url = this.data.redirectUrl || '/pages/index/index';
|
||
|
|
|
||
|
|
// 判断是否是 TabBar 页面
|
||
|
|
const tabBarPages = [
|
||
|
|
'/pages/index/index',
|
||
|
|
'/pages/device/list/list',
|
||
|
|
'/pages/statistics/index/index',
|
||
|
|
'/pages/warning/list/list',
|
||
|
|
'/pages/profile/index/index'
|
||
|
|
];
|
||
|
|
|
||
|
|
if (tabBarPages.includes(url)) {
|
||
|
|
wx.switchTab({ url });
|
||
|
|
} else {
|
||
|
|
wx.redirectTo({ url });
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 询问是否开启消息通知
|
||
|
|
*/
|
||
|
|
askForNotification() {
|
||
|
|
wx.showModal({
|
||
|
|
title: '开启消息通知',
|
||
|
|
content: '开启后可以及时接收设备检验预警提醒',
|
||
|
|
confirmText: '立即开启',
|
||
|
|
cancelText: '暂不开启',
|
||
|
|
success: res => {
|
||
|
|
if (res.confirm) {
|
||
|
|
this.subscribeMessages();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 订阅消息
|
||
|
|
*/
|
||
|
|
async subscribeMessages() {
|
||
|
|
try {
|
||
|
|
await app.subscribeAllMessages();
|
||
|
|
wx.showToast({
|
||
|
|
title: '订阅成功',
|
||
|
|
icon: 'success'
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('订阅失败', error);
|
||
|
|
// 订阅失败不影响使用,不显示错误提示
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 显示用户协议
|
||
|
|
*/
|
||
|
|
showAgreement() {
|
||
|
|
wx.showModal({
|
||
|
|
title: '用户协议',
|
||
|
|
content: '这里是用户协议的内容...\n\n1. 用户需遵守相关法律法规\n2. 不得利用本系统从事违法活动\n3. 保护个人信息安全\n4. 合理使用系统资源',
|
||
|
|
showCancel: false,
|
||
|
|
confirmText: '我知道了'
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 显示隐私政策
|
||
|
|
*/
|
||
|
|
showPrivacy() {
|
||
|
|
wx.showModal({
|
||
|
|
title: '隐私政策',
|
||
|
|
content: '这里是隐私政策的内容...\n\n1. 我们会收集您的基本信息\n2. 信息仅用于提供服务\n3. 不会向第三方泄露\n4. 您可以随时删除个人信息',
|
||
|
|
showCancel: false,
|
||
|
|
confirmText: '我知道了'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|