优化登录逻辑,提交登录请求的节流

This commit is contained in:
WindowBird 2025-09-05 09:09:25 +08:00
parent 33352da106
commit 802bb504d0
3 changed files with 379 additions and 398 deletions

View File

@ -25,6 +25,7 @@ import PageScrollMixin from "./mixins/page-scroll-mixin.js";
Vue.use("tab-bar", tabbar); Vue.use("tab-bar", tabbar);
Vue.use(uView); Vue.use(uView);
uni.$u.config.unit = "rpx"; uni.$u.config.unit = "rpx";
Vue.use(cookies); Vue.use(cookies);
Vue.config.productionTip = false; Vue.config.productionTip = false;

View File

@ -1,47 +1,49 @@
<template> <template>
<view class="page"> <view class="page">
<u-navbar <u-navbar
:background="bgc"
:border-bottom="false"
:is-back="false" :is-back="false"
:title="titleTxt" :title="titleTxt"
:border-bottom="false" height="36"
:background="bgc"
title-color="#000" title-color="#000"
title-size="36" title-size="36"
height="36"
> >
</u-navbar> </u-navbar>
<view class="imgbox"> <view class="imgbox">
<image <image
src="https://api.ccttiot.com/e3227491d46d2d7ac1c1c592e44f48bc-1754467412954.jpg"
mode="" mode=""
src="https://api.ccttiot.com/e3227491d46d2d7ac1c1c592e44f48bc-1754467412954.jpg"
></image> ></image>
</view> </view>
<button class="button" @click="getPhoneNumber">授权登录</button> <button class="button" @click="goToLogin">授权登录</button>
<view class="tip"> <view class="tip">
<view class="checkbox-container" @click="toggleAgreement"> <view class="checkbox-container" @click="toggleAgreement">
<view class="checkbox" :class="{ checked: hasAgreed }"> <view :class="{ checked: hasAgreed }" class="checkbox">
<text v-if="hasAgreed" class="checkmark"></text> <text v-if="hasAgreed" class="checkmark"></text>
</view> </view>
</view> </view>
<text>我已同意并阅读</text> <text>我已同意并阅读</text>
<text class="link-text" @click="showServiceTerms">服务条款</text> <text class="link-text" @click="showServiceTerms">服务条款</text>
<text class="link-text" @click="showPrivacyPolicy">法律条款及隐私政策</text> <text class="link-text" @click="showPrivacyPolicy"
>法律条款及隐私政策
</text>
</view> </view>
<!-- 服务条款弹窗 --> <!-- 服务条款弹窗 -->
<u-popup <u-popup
v-model="showServiceTermsPopup" v-model="showServiceTermsPopup"
border-radius="20rpx"
height="800rpx"
mode="center" mode="center"
width="600rpx" width="600rpx"
height="800rpx"
border-radius="20rpx"
> >
<view class="popup-content"> <view class="popup-content">
<view class="popup-header"> <view class="popup-header">
<text class="popup-title">服务条款</text> <text class="popup-title">服务条款</text>
<text class="popup-close" @click="closeServiceTermsPopup">×</text> <text class="popup-close" @click="closeServiceTermsPopup">×</text>
</view> </view>
<scroll-view class="popup-body" scroll-y="true" :scroll-top="scrollTop"> <scroll-view :scroll-top="scrollTop" class="popup-body" scroll-y="true">
<view class="terms-content"> <view class="terms-content">
<rich-text :nodes="serviceTermsContent"></rich-text> <rich-text :nodes="serviceTermsContent"></rich-text>
</view> </view>
@ -56,17 +58,17 @@
<!-- 隐私政策弹窗 --> <!-- 隐私政策弹窗 -->
<u-popup <u-popup
v-model="showPrivacyPolicyPopup" v-model="showPrivacyPolicyPopup"
border-radius="20rpx"
height="800rpx"
mode="center" mode="center"
width="600rpx" width="600rpx"
height="800rpx"
border-radius="20rpx"
> >
<view class="popup-content"> <view class="popup-content">
<view class="popup-header"> <view class="popup-header">
<text class="popup-title">法律条款及隐私政策</text> <text class="popup-title">法律条款及隐私政策</text>
<text class="popup-close" @click="closePrivacyPolicyPopup">×</text> <text class="popup-close" @click="closePrivacyPolicyPopup">×</text>
</view> </view>
<scroll-view class="popup-body" scroll-y="true" :scroll-top="scrollTop"> <scroll-view :scroll-top="scrollTop" class="popup-body" scroll-y="true">
<view class="terms-content"> <view class="terms-content">
<rich-text :nodes="privacyPolicyContent"></rich-text> <rich-text :nodes="privacyPolicyContent"></rich-text>
</view> </view>
@ -81,424 +83,402 @@
</template> </template>
<script> <script>
import { navigateToPage } from '@/utils/router.js' import { navigateToPage } from "@/utils/router.js";
import { wxLogin } from '@/api/auth/auth.js' import { wxLogin } from "@/api/auth/auth.js";
import { forceHideLoading, AutoLoadingManager } from '@/utils/request.js' import { AutoLoadingManager, forceHideLoading } from "@/utils/request.js";
import { getServiceTerms, getPrivacyPolicy } from '@/api/article/article.js' import { getPrivacyPolicy, getServiceTerms } from "@/api/article/article.js";
export default { export default {
data() { data() {
return { return {
bgc: { bgc: {
backgroundColor: '#fff', backgroundColor: "#fff",
}, },
titleTxt: '登录', titleTxt: "登录",
chooseType: true, chooseType: true,
usertype: '', usertype: "",
login: false, login: false,
hasAgreed: false, // hasAgreed: false, //
hasReadServiceTerms: false, // hasReadServiceTerms: false, //
hasReadPrivacyPolicy: false, // hasReadPrivacyPolicy: false, //
showServiceTermsPopup: false, // showServiceTermsPopup: false, //
showPrivacyPolicyPopup: false, // showPrivacyPolicyPopup: false, //
serviceTermsContent: '', // serviceTermsContent: "", //
privacyPolicyContent: '', // privacyPolicyContent: "", //
scrollTop: 0, // scrollTop: 0, //
};
},
onLoad() {
// loading
this.pageLoading = new AutoLoadingManager();
},
onUnload() {
// loading
forceHideLoading();
if (this.pageLoading) {
this.pageLoading.destroy();
}
},
methods: {
userType(num) {
this.chooseType = false;
this.login = true;
if (num === 1) {
this.usertype = "01";
} else {
this.usertype = "00";
} }
}, },
onLoad() { //
// loading toggleAgreement() {
this.pageLoading = new AutoLoadingManager() this.hasAgreed = true;
}, },
onUnload() { //
// loading async showServiceTerms() {
forceHideLoading() try {
if (this.pageLoading) { const res = await getServiceTerms();
this.pageLoading.destroy() if (res.code === 200 && res.data) {
//
let content = res.data.content || "暂无服务条款内容";
// HTML
content = content.replace(/\n/g, "<br/>");
//
content = `<div style="word-wrap: break-word; word-break: break-all; line-height: 1.6; font-size: 28rpx; color: #333;">${content}</div>`;
this.serviceTermsContent = content;
this.showServiceTermsPopup = true;
//
this.scrollTop = 0;
} else {
uni.showToast({
title: "获取服务条款失败",
icon: "none",
});
}
} catch (error) {
console.error("获取服务条款失败:", error);
uni.showToast({
title: "获取服务条款失败",
icon: "none",
});
} }
}, },
methods: { //
userType(num) { async showPrivacyPolicy() {
this.chooseType = false try {
this.login = true const res = await getPrivacyPolicy();
if (num === 1) { if (res.code === 200 && res.data) {
this.usertype = '01' //
} else { let content = res.data.content || "暂无隐私政策内容";
this.usertype = '00' // HTML
} content = content.replace(/\n/g, "<br/>");
}, //
// content = `<div style="word-wrap: break-word; word-break: break-all; line-height: 1.6; font-size: 28rpx; color: #333;">${content}</div>`;
toggleAgreement() { this.privacyPolicyContent = content;
if (this.hasReadServiceTerms && this.hasReadPrivacyPolicy) { this.showPrivacyPolicyPopup = true;
this.hasAgreed = !this.hasAgreed //
this.scrollTop = 0;
} else { } else {
uni.showToast({ uni.showToast({
title: '请先阅读服务条款和隐私政策', title: "获取隐私政策失败",
icon: 'none', icon: "none",
}) });
} }
}, } catch (error) {
// console.error("获取隐私政策失败:", error);
async showServiceTerms() { uni.showToast({
try { title: "获取隐私政策失败",
const res = await getServiceTerms() icon: "none",
if (res.code === 200 && res.data) { });
// }
let content = res.data.content || '暂无服务条款内容' },
// HTML //
content = content.replace(/\n/g, '<br/>') closeServiceTermsPopup() {
// this.showServiceTermsPopup = false;
content = `<div style="word-wrap: break-word; word-break: break-all; line-height: 1.6; font-size: 28rpx; color: #333;">${content}</div>` },
this.serviceTermsContent = content //
this.showServiceTermsPopup = true closePrivacyPolicyPopup() {
// this.showPrivacyPolicyPopup = false;
this.scrollTop = 0 },
} else { //
uni.showToast({ async agreeServiceTerms() {
title: '获取服务条款失败', this.hasReadServiceTerms = true;
icon: 'none', this.closeServiceTermsPopup();
}) this.checkAgreementStatus();
} },
} catch (error) { //
console.error('获取服务条款失败:', error) async agreePrivacyPolicy() {
uni.showToast({ this.hasReadPrivacyPolicy = true;
title: '获取服务条款失败', this.closePrivacyPolicyPopup();
icon: 'none', this.checkAgreementStatus();
}) },
} //
}, checkAgreementStatus() {
// if (this.hasReadServiceTerms && this.hasReadPrivacyPolicy) {
async showPrivacyPolicy() { //
try { this.hasAgreed = true;
const res = await getPrivacyPolicy() }
if (res.code === 200 && res.data) { },
//
let content = res.data.content || '暂无隐私政策内容'
// HTML
content = content.replace(/\n/g, '<br/>')
//
content = `<div style="word-wrap: break-word; word-break: break-all; line-height: 1.6; font-size: 28rpx; color: #333;">${content}</div>`
this.privacyPolicyContent = content
this.showPrivacyPolicyPopup = true
//
this.scrollTop = 0
} else {
uni.showToast({
title: '获取隐私政策失败',
icon: 'none',
})
}
} catch (error) {
console.error('获取隐私政策失败:', error)
uni.showToast({
title: '获取隐私政策失败',
icon: 'none',
})
}
},
//
closeServiceTermsPopup() {
this.showServiceTermsPopup = false
},
//
closePrivacyPolicyPopup() {
this.showPrivacyPolicyPopup = false
},
//
async agreeServiceTerms() {
this.hasReadServiceTerms = true
this.closeServiceTermsPopup()
this.checkAgreementStatus()
// goToLogin() {
if (!this.hasReadPrivacyPolicy) { this.$u.throttle(this.Login, 3000);
setTimeout(() => { },
this.showPrivacyPolicy()
}, 500) // 500ms
}
},
//
async agreePrivacyPolicy() {
this.hasReadPrivacyPolicy = true
this.closePrivacyPolicyPopup()
this.checkAgreementStatus()
// async Login() {
if (!this.hasReadServiceTerms) { if (!this.hasAgreed) {
setTimeout(() => { const confirmed = await this.showLoginConfirmation();
this.showServiceTerms() if (!confirmed) return;
}, 500) // 500ms }
}
},
//
checkAgreementStatus() {
if (this.hasReadServiceTerms && this.hasReadPrivacyPolicy) {
//
this.hasAgreed = true
}
},
// uni.login({
async showUnreadTerms() { success(res) {
// if (res.code) {
if (!this.hasReadServiceTerms) { console.log("登录!", res);
await this.showServiceTerms() let data = {
return loginCode: res.code,
} appId: 1,
};
// // loading
if (!this.hasReadPrivacyPolicy) { if (this.pageLoading) {
await this.showPrivacyPolicy() this.pageLoading.show("登录中...");
return
}
},
getPhoneNumber() {
//
if (!this.hasReadServiceTerms || !this.hasReadPrivacyPolicy) {
uni.showToast({
title: '请先阅读服务条款和隐私政策',
icon: 'none',
duration: 2000,
})
//
this.showUnreadTerms()
return
}
let that = this
uni.login({
success(res) {
if (res.code) {
console.log('登录!', res)
let data = {
loginCode: res.code,
appId: 1,
}
// loading
if (that.pageLoading) {
that.pageLoading.show('登录中...')
}
wxLogin(data)
.then(res => {
// loading
if (that.pageLoading) {
that.pageLoading.hide()
}
forceHideLoading()
if (res.code == 200) {
console.log(res, 'resres')
uni.setStorageSync('token', res.token)
//
uni.showToast({
title: '登录成功',
icon: 'success',
duration: 1500,
})
//
setTimeout(() => {
that.ceshi()
}, 1500)
} else {
//
uni.showToast({
title: res.msg || '登录失败',
icon: 'none',
})
}
})
.catch(error => {
// loading
if (that.pageLoading) {
that.pageLoading.hide()
}
forceHideLoading()
console.error('登录失败:', error)
uni.showToast({
title: '登录失败',
icon: 'none',
})
})
} }
},
fail(err) {
console.error('微信登录失败:', err)
uni.showToast({
title: '微信登录失败',
icon: 'none',
})
},
})
},
async ceshi() {
// loading
if (this.pageLoading) {
this.pageLoading.hide()
}
forceHideLoading()
// 使 wxLogin(data)
navigateToPage('index') .then((res) => {
}, // loading
if (this.pageLoading) {
this.pageLoading.hide();
}
forceHideLoading();
if (res.code == 200) {
console.log(res, "resres");
uni.setStorageSync("token", res.token);
//
uni.showToast({
title: "登录成功",
icon: "success",
duration: 1500,
});
//
setTimeout(() => {
if (this.pageLoading) {
this.pageLoading.hide();
}
forceHideLoading();
// 使
navigateToPage("index");
}, 1500);
} else {
//
uni.showToast({
title: res.msg || "登录失败",
icon: "none",
});
}
})
.catch((error) => {
// loading
if (this.pageLoading) {
this.pageLoading.hide();
}
forceHideLoading();
console.error("登录失败:", error);
uni.showToast({
title: "登录失败",
icon: "none",
});
});
}
},
fail(err) {
console.error("微信登录失败:", err);
uni.showToast({
title: "微信登录失败",
icon: "none",
});
},
});
}, },
}
//
showLoginConfirmation() {
return new Promise((resolve) => {
uni.showModal({
title: "确认登录",
content: "我已阅读并同意服务协议和隐私政策",
confirmText: "确定登录",
cancelText: "取消",
success: (res) => {
this.hasAgreed = res.confirm;
resolve(res.confirm);
},
fail: (err) => {
console.error("弹窗操作失败:", err);
resolve(false);
},
});
});
},
},
};
</script> </script>
<style lang="scss"> <style lang="scss">
page { page {
background: #ffffff; background: #ffffff;
} }
.page {
position: relative; /* 添加相对定位 */
}
.imgbox { .page {
margin: 102rpx auto; position: relative; /* 添加相对定位 */
}
.imgbox {
margin: 102rpx auto;
width: 750rpx;
height: 422rpx;
z-index: 0;
image {
width: 750rpx; width: 750rpx;
height: 422rpx; height: 422rpx;
z-index: 0; }
}
image { .button {
width: 750rpx; margin-top: 200rpx;
height: 422rpx; width: 586rpx;
} height: 90rpx;
} background: #4c97e7;
.button { border-radius: 54rpx 54rpx 54rpx 54rpx;
margin-top: 200rpx; font-weight: 500;
width: 586rpx; font-size: 40rpx;
height: 90rpx; color: #ffffff;
background: #4c97e7; }
border-radius: 54rpx 54rpx 54rpx 54rpx;
font-weight: 500;
font-size: 40rpx;
color: #ffffff;
}
.tip { .tip {
margin-top: 128rpx; margin-top: 128rpx;
display: flex; display: flex;
flex-wrap: nowrap; flex-wrap: nowrap;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
font-weight: 400; font-weight: 400;
font-size: 20rpx; font-size: 20rpx;
color: #979797; color: #979797;
.link-text {
color: #4c97e7;
text-decoration: underline;
}
}
/* 自定义勾选按钮样式 */ .link-text {
.checkbox-container { color: #4c97e7;
display: flex; text-decoration: underline;
align-items: center;
margin-right: 10rpx;
cursor: pointer;
} }
}
.checkbox { /* 自定义勾选按钮样式 */
width: 26rpx; .checkbox-container {
height: 26rpx; display: flex;
border: 2rpx solid #cccccc; align-items: center;
border-radius: 4rpx; margin-right: 10rpx;
display: flex; cursor: pointer;
align-items: center; }
justify-content: center;
background-color: #ffffff;
transition: all 0.3s ease;
}
.checkbox.checked { .checkbox {
background-color: #4c97e7; width: 26rpx;
border-color: #4c97e7; height: 26rpx;
} border: 2rpx solid #cccccc;
border-radius: 4rpx;
display: flex;
align-items: center;
justify-content: center;
background-color: #ffffff;
transition: all 0.3s ease;
}
.checkmark { .checkbox.checked {
color: #ffffff; background-color: #4c97e7;
font-size: 16rpx; border-color: #4c97e7;
font-weight: bold; }
line-height: 1;
}
/* 弹窗样式 */ .checkmark {
.popup-content { color: #ffffff;
background: #fff; font-size: 16rpx;
border-radius: 20rpx; font-weight: bold;
overflow: hidden; line-height: 1;
display: flex; }
flex-direction: column;
height: 100%;
}
.popup-header { /* 弹窗样式 */
padding: 30rpx; .popup-content {
border-bottom: 1rpx solid #eee; background: #fff;
display: flex; border-radius: 20rpx;
justify-content: space-between; overflow: hidden;
align-items: center; display: flex;
background: #f8f8f8; flex-direction: column;
} height: 100%;
}
.popup-title { .popup-header {
font-size: 32rpx; padding: 30rpx;
font-weight: bold; border-bottom: 1rpx solid #eee;
color: #333; display: flex;
} justify-content: space-between;
align-items: center;
background: #f8f8f8;
}
.popup-close { .popup-title {
font-size: 40rpx; font-size: 32rpx;
color: #999; font-weight: bold;
cursor: pointer; color: #333;
padding: 10rpx; }
}
.popup-body { .popup-close {
flex: 1; font-size: 40rpx;
padding: 30rpx; color: #999;
line-height: 1.6; cursor: pointer;
font-size: 28rpx; padding: 10rpx;
color: #333; }
height: 0; /* 确保scroll-view有固定高度 */
}
.terms-content { .popup-body {
word-wrap: break-word; flex: 1;
word-break: break-all; padding: 30rpx;
line-height: 1.6; line-height: 1.6;
font-size: 28rpx; font-size: 28rpx;
color: #333; color: #333;
padding-bottom: 20rpx; /* 底部留白 */ height: 0; /* 确保scroll-view有固定高度 */
padding-right: 30rpx; /* 右边距 */ }
}
.popup-footer { .terms-content {
padding: 30rpx; word-wrap: break-word;
border-top: 1rpx solid #eee; word-break: break-all;
background: #f8f8f8; line-height: 1.6;
} font-size: 28rpx;
color: #333;
padding-bottom: 20rpx; /* 底部留白 */
padding-right: 30rpx; /* 右边距 */
}
.popup-tip { .popup-footer {
display: block; padding: 30rpx;
font-size: 24rpx; border-top: 1rpx solid #eee;
color: #666; background: #f8f8f8;
text-align: center; }
margin-bottom: 20rpx;
line-height: 1.4;
}
.popup-btn { .popup-tip {
width: 100%; display: block;
height: 80rpx; font-size: 24rpx;
background: #4c97e7; color: #666;
color: #fff; text-align: center;
border: none; margin-bottom: 20rpx;
border-radius: 40rpx; line-height: 1.4;
font-size: 30rpx; }
font-weight: 500;
} .popup-btn {
width: 100%;
height: 80rpx;
background: #4c97e7;
color: #fff;
border: none;
border-radius: 40rpx;
font-size: 30rpx;
font-weight: 500;
}
</style> </style>

View File

@ -17,18 +17,18 @@ import {
const ENV_CONFIG = { const ENV_CONFIG = {
develop: { develop: {
// 开发环境 // 开发环境
baseUrl: "http://192.168.2.244:4501", baseUrl: "http://192.168.2.58:4501",
//baseUrl: "https://testlu.chuangtewl.com/prod-api", //baseUrl: "https://testlu.chuangtewl.com/prod-api",
appId: 1, appId: 1,
}, },
trial: { trial: {
// 体验版 // 体验版
baseUrl: "http://192.168.2.244:4501", baseUrl: "http://192.168.2.58:4501",
appId: 1, appId: 1,
}, },
release: { release: {
// 正式版 // 正式版
baseUrl: "http://192.168.2.244:4501", baseUrl: "http://192.168.2.58:4501",
appId: 1, appId: 1,
}, },
}; };