修复bug

This commit is contained in:
2026-05-20 14:57:38 +08:00
parent 16c91facac
commit 0df38bd544
2 changed files with 319 additions and 352 deletions

View File

@@ -1,36 +1,25 @@
<template>
<view class="login-container">
<!-- 顶部状态栏 -->
<!-- <view class="status-bar">
<u-icon name="arrow-left" size="40" color="#333" @tap="goBack" class="nav-left"></u-icon>
<u-icon :name="btn.icon" :color="btn.color || '#333'" :size="btn.size || 40"></u-icon>
</view> -->
<customNavbar opacity="0" :show-home="true" :show-back="false" :is-transparent="true" />
<!-- 主体内容区 -->
<view class="content">
<!-- 标题区域 -->
<view class="title">登录</view>
<!-- 登录插图区域 -->
<view class="illustration">
<image :src="staticHost + '/public/static/login/bg.png'" mode="aspectFit" />
</view>
<!-- 登录按钮 -->
<button class="login-btn" open-type="getPhoneNumber" @getphonenumber="onGetPhone">
手机号快捷登录
</button>
<!-- 用户类型选择 -->
<view class="user-type" shape="square">
<view class="user-type">
<u-radio-group v-model="loginType" active-color="#EA414A" shape="square">
<u-radio name="0">个人</u-radio>
<u-radio name="1">企业</u-radio>
</u-radio-group>
</view>
<!-- 协议勾选 -->
<view class="agreement">
<u-checkbox v-model="agreeProtocol" shape="square" active-color="#EA414A">
<text class="agreement-text">
@@ -45,259 +34,198 @@
</view>
</template>
<script>
export default {
data() {
return {
designWidth: 750,
screenWidth: 0,
scale: 1,
loginType: "0",
agreeProtocol: false,
menuRect: {}
}
},
onLoad() {
wx.getWindowInfo({
success: (res) => {
this.screenWidth = res.windowWidth
this.scale = this.screenWidth / this.designWidth
}
})
const menuRect = wx.getMenuButtonBoundingClientRect()
this.menuRect = menuRect
},
onUnload() {
agreeProtocol: false
};
},
computed: {
// title 样式
titleStyle() {
const s = this.scale;
return {
width: `${93 * s}rpx`,
height: `${45 * s}rpx`,
fontSize: `${48 * s}rpx`,
lineHeight: `${40 * s}rpx`,
fontWeight: 500,
color: '#222222',
fontFamily: 'Noto Sans S Chinese',
textAlign: 'center'
}
},
navStyle() {
const rect = this.menuRect
if (!rect.top) return {}
return {
paddingTop: rect.top + 'px',
height: rect.height + 'px',
lineHeight: rect.height + 'px'
}
},
staticHost() {
return this.$config.staticUrl
},
headerStyle() {
const s = this.scale;
return {
marginTop: `${214 * s}rpx`,
flex: 1,
display: 'flex',
flexDirection: 'column',
alignItems: 'center', // 水平居中
justifyContent: 'flex-start' // 从顶部开始
}
},
// cover 样式
coverStyle() {
const s = this.scale;
return {
width: `${428 * s}rpx`,
height: `${608 * s}rpx`
}
return this.$config.staticUrl;
}
},
methods: {
/** 微信手机号授权 */
onGetPhone(e) {
const {
code
} = e.detail;
if (!code) {
this.$mytip.toast("授权失败");
// 微信获取手机号(修复版)
async onGetPhone(e) {
// 用户拒绝
if (e.detail.errMsg !== "getPhoneNumber:ok") {
this.$mytip.toast("已取消授权");
return;
}
this.doLogin(code);
},
goBack() {
uni.switchTab({
url: '/pages/index/index'
})
},
/** 执行登录逻辑 */
async doLogin(phoneGetCode) {
// 必须勾选协议
if (!this.agreeProtocol) {
this.$mytip.toast('请先阅读并同意用户协议隐私政策')
return
this.$mytip.toast("请先同意用户协议隐私政策");
return;
}
uni.showLoading({
title: "登录中...",
mask: true
});
try {
// 微信登录
// 1. 先拿微信登录 code
const loginRes = await new Promise((resolve, reject) => {
uni.login({
provider: 'weixin',
provider: "weixin",
success: resolve,
fail: reject
});
});
// 调用后端登录接口
const authRes = await this.$u.post(`/login/weChatLogin`, {
phoneGetCode: phoneGetCode,
// 2. 传给后端登录
await this.doLogin({
loginCode: loginRes.code,
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
loginType: this.loginType
});
// 保存token
let token = authRes.data;
this.$u.vuex('vuex_token', token);
} catch (err) {
this.$mytip.toast("授权失败,请重试");
}
},
// 登录接口(修复:用户信息 + 全局刷新)
async doLogin(data) {
uni.showLoading({ title: "登录中...", mask: true });
try {
// 1. 登录拿 token
const authRes = await this.$u.post("/login/weChatLogin", data);
const token = authRes.data;
if (!token) {
uni.hideLoading();
this.getUserOtherInfo(this.loginType, token)
// 跳转首页
this.$mytip.toast("登录失败");
return;
}
// 2. 保存 token全局
this.$u.vuex("vuex_token", token);
// 3. 获取并保存用户信息(修复:必须 await 等它完成)
await this.getUserOtherInfo(this.loginType, token);
// 4. 登录成功 → 强制刷新全局状态 + 跳首页
uni.hideLoading();
this.$mytip.toast("登录成功");
// 🔥 关键:强制刷新页面,解决用户信息不显示
setTimeout(() => {
uni.switchTab({
url: '/pages/index/index'
url: "/pages/index/index",
success() {
const page = getCurrentPages().pop();
if (page) page.onLoad();
}
});
}, 500);
} catch (err) {
uni.hideLoading();
this.$mytip.toast("登录失败");
console.error('Login failed:', err);
this.$mytip.toast("登录失败,请稍后重试");
}
},
getUserOtherInfo(loginType, token) {
let url = `/login/userInfo`;
this.$u.get(url, {}, {
'WT': token,
'USERTYPE': loginType
}).then(obj => {
if (obj.flag) {
uni.setStorageSync('userInfo', {
userType: obj.data.userType,
oaAuth: obj.data.oaAuth,
cusNo: obj.data.cusNo,
userName: obj.data.userName,
openId: obj.data.openId,
subscribe: obj.data.subscribeMsg
})
}
});
},
/** 触发手机号选择(子组件 → 父组件) */
showPhoneSelector(phoneList, openid) {
this.$emit("choosePhone", {
phoneList,
openid
});
},
/** 查看协议 */
goPrivacy(type) {
console.log(type)
const url = type === 'user' ?
'/pages-biz/privacy/userAgreement' :
'/pages-biz/privacy/privacyPolicy';
uni.navigateTo({
url
// 获取用户信息(修复:必须返回 Promise
getUserOtherInfo(loginType, token) {
return new Promise((resolve, reject) => {
this.$u.get("/login/userInfo", {}, {
"WT": token,
"USERTYPE": loginType
}).then(obj => {
if (obj?.flag) {
// 🔥 保存用户信息到缓存
uni.setStorageSync('userInfo', obj.data);
// 🔥 同时存入 vuex 全局状态(首页就能实时刷新)
this.$u.vuex('vuex_userInfo', obj.data);
}
resolve();
}).catch(() => {
resolve();
});
});
},
// 返回
goBack() {
uni.navigateBack({ delta: 1 });
},
// 协议
goPrivacy(type) {
const url = type === "user"
? "/pages-biz/privacy/userAgreement"
: "/pages-biz/privacy/privacyPolicy";
uni.navigateTo({ url });
}
}
}
};
</script>
<style lang="scss" scoped>
.login-container {
height: 100vh;
background-color: #fff;
overflow: hidden;
.status-bar {
display: flex;
flex-direction: row;
margin-top: 11.56%;
height: var(--status-bar-height);
}
.nav-left {
width: 80rpx;
display: flex;
padding-left: 5%;
align-items: center;
background: #fff;
padding-top: var(--status-bar-height);
}
.content {
display: flex;
flex-direction: column;
align-items: center;
height: calc(100vh - var(--status-bar-height));
margin-top: 12.8%;
padding: 0 40rpx;
margin-top: 200rpx;
}
.title {
height: 45rpx;
font-family: Noto Sans S Chinese;
font-weight: 500;
font-size: 48rpx;
color: #222222;
line-height: 40rpx;
font-weight: 500;
color: #222;
margin-bottom: 60rpx;
}
.illustration {
width: 100%;
height: 44.6%;
padding: 0 21.47%;
margin-bottom: 6%;
}
height: 400rpx;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 60rpx;
.illustration image {
width: 100%;
image {
width: 80%;
height: 100%;
}
}
.login-btn {
width: 78.6%;
height: 7.12%;
width: 100%;
height: 88rpx;
background: linear-gradient(90deg, #FF2F31 0%, #FF9379 100%);
border-radius: 49rpx;
font-size: 36rpx;
color: #ffffff;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 40rpx;
border: none;
}
.user-type {
display: flex;
justify-content: center;
gap: 40rpx;
margin-bottom: 20rpx;
margin-bottom: 40rpx;
}
.agreement {
margin-top: 6.24%;
padding: 0 40rpx;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.agreement-text {
@@ -308,24 +236,4 @@
.link {
color: #334254;
}
}
/* 响应式适配 */
@media (max-width: 320rpx) {
.title {
font-size: 42rpx;
margin-bottom: 100rpx;
}
.illustration {
height: 360rpx;
}
.login-btn {
height: 80rpx;
font-size: 30rpx;
}
}
</style>

View File

@@ -1,176 +1,235 @@
<template>
<view class="u-m-20">
<u-navbar :is-back="true" title="账号实名信息" :border-bottom="false"></u-navbar>
<view>
<view class="mt-30">
<u-cell-group>
<u-cell-item :title="getTitle" :arrow="false" hover-class="none" @click="updateName">
{{(cardNo == null ? orgNo : cardNo) || ''}}
<u-cell-item :title="getTitle" :arrow="true" hover-class="none" @click="showModel = true">
<text class="value-text">{{ showValue || '未设置' }}</text>
</u-cell-item>
</u-cell-group>
</view>
<u-modal
v-model="showModel"
@confirm="confirmAuthCode"
ref="uModal"
:async-close="true"
:title="'设置' + getTitle"
>
<view class="slot-content">
<u-input
<!-- ======================
完全自定义弹窗扔掉u-modal
样式100%可控 永不转圈 超美观
========================= -->
<view class="modal-mask" v-if="showModel" @click="showModel = false"></view>
<view class="modal-content" v-if="showModel">
<view class="modal-header">
<text class="title">设置{{ getTitle }}</text>
</view>
<view class="modal-body">
<input
v-model="authCode"
type="text"
:border="false"
:placeholder="'请输入' + getTitle"
class="modal-input"
/>
</view>
</u-modal>
<view class="modal-footer">
<view class="btn cancel" @click="closeModal">取消</view>
<view class="btn confirm" :class="{loading: submitting}" @click="doSubmit">
<text v-if="!submitting">确定</text>
<text v-else class="loading-text">提交中...</text>
</view>
</view>
</view>
</view>
</template>
<script>
import config from "@/common/config.js"
import { rsaEncrypt } from "@/common/utils/ras.js"
export default {
data() {
return {
user: {
userType: null,
userType: '',
oaAuth: null,
cusNo: null,
userName: null,
openId: null
},
pic: null,
authCode: '',
showModel: false,
submitting: false,
cardNo: null,
orgNo: null
}
},
onLoad(options) {
// 页面加载时同步最新用户信息
onLoad() {
this.getUserProfile();
},
methods: {
updateName() {
this.showModel = true;
closeModal() {
this.showModel = false
this.authCode = ''
},
// 身份证校验
checkIdCard(code) {
return /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/.test(code);
},
// 统一社会信用代码校验
checkCreditCode(code) {
return /^[0-9A-Z]{18}$/.test(code);
},
confirmAuthCode() {
// 修复:统一使用 loginType 变量
const loginType = this.user.userType;
async doSubmit() {
const userType = String(this.user.userType)
const input = this.authCode.trim()
if (!this.authCode) {
this.showModel = false;
return this.$mytip.toast(loginType === '0' ? '请输入身份证号' : '请输入组织社会信用代码')
if (!input) {
this.$mytip.toast(userType === '0' ? '请输入身份证号' : '请输入企业信用代码')
return
}
if (userType === '0' && !this.checkIdCard(input)) {
uni.showToast({ title: '身份证格式不正确', icon: 'none' })
return
}
if (userType === '1' && !this.checkCreditCode(input)) {
uni.showToast({ title: '社会信用代码格式错误', icon: 'none' })
return
}
// 校验身份证
if (loginType === '0' && !this.checkIdCard(this.authCode)) {
uni.showToast({ title: '身份证号格式不正确', icon: 'none' });
return;
}
this.submitting = true
try {
const token = this.$getToken()
const res = await this.$u.post('/login/updateVerifyCode', {
userType,
code: userType === '0' ? rsaEncrypt(input) : input
}, { WT: token })
// 校验信用代码
if (loginType === '1' && !this.checkCreditCode(this.authCode)) {
uni.showToast({ title: '社会信用代码格式不正确', icon: 'none' });
return;
}
const token = this.$getToken();
const url = "/login/updateVerifyCode";
const encryptCode = rsaEncrypt(this.authCode);
this.$u.post(url, {
userType: loginType,
code: loginType === '0' ? encryptCode : this.authCode
}, {
'WT': token
}).then(obj => {
if (obj.flag) {
// 赋值展示
if (loginType === '0') {
this.cardNo = this.applyMask(this.authCode);
if (res.flag) {
this.$mytip.toast('设置成功')
this.closeModal()
await this.getUserProfile()
setTimeout(() => uni.switchTab({ url: '/pages/center/center' }), 1500)
} else {
this.orgNo = this.authCode;
this.$mytip.toast(res.msg || '设置失败')
}
this.showModel = false;
this.$mytip.toast('修改成功');
// 关键:先更新用户信息,成功后再跳转
this.getUserProfile().then(() => {
setTimeout(() => {
uni.switchTab({ url: '/pages/center/center' });
}, 1000);
});
} else {
this.$mytip.toast('修改失败');
} catch (err) {
this.$mytip.toast('提交失败')
} finally {
this.submitting = false
}
});
},
// 身份证脱敏修复
applyMask(idCard) {
if (!idCard || idCard.length !== 18) return idCard;
// 前6位 + 8个* + 后4位
return idCard.replace(/^(\d{6})\d{8}(\d{4})$/, '$1********$2');
if (!idCard || idCard.length !== 18) return idCard
return idCard.replace(/^(\d{6})\d{8}(\d{4})$/, '$1********$2')
},
updateAvatar() {
this.$u.route('/pages-biz/profile/avatar')
},
// 关键修复:获取用户信息并同步到当前页面 data
async getUserProfile() {
try {
const token = this.$getToken();
const url = `/login/userInfo`;
const obj = await this.$u.get(url, {}, { 'WT': token });
const userInfo = {
userType: obj.data.userType,
oaAuth: obj.data.oaAuth,
cusNo: obj.data.cusNo,
userName: obj.data.userName,
openId: obj.data.openId,
subscribe: obj.data.subscribeMsg
};
// 同步缓存 + 当前页面数据
uni.setStorageSync('userInfo', userInfo);
this.user = userInfo;
// 同步展示实名信息
if (userInfo.userType === '0') {
this.cardNo = obj.data.cardNo ? this.applyMask(obj.data.cardNo) : null;
this.orgNo = null;
const token = this.$getToken()
const obj = await this.$u.get('/login/userInfo', {}, { WT: token })
if (!obj.data) return
const user = obj.data
this.user = {
userType: String(user.userType),
oaAuth: user.oaAuth,
cusNo: user.cusNo,
userName: user.userName,
openId: user.openId
}
if (this.user.userType === '0') {
this.cardNo = user.cardNo ? this.applyMask(user.cardNo) : null
this.orgNo = null
} else {
this.orgNo = obj.data.orgNo || null;
this.cardNo = null;
}
return userInfo;
} catch (e) {
console.error('获取用户信息失败', e);
this.orgNo = user.orgNo || null
this.cardNo = null
}
uni.setStorageSync('userInfo', this.user)
} catch (e) {}
}
},
computed: {
getTitle() {
return this.user.userType === '0' ? '身份证号' : '企业社会信用代码';
return this.user.userType === '0' ? '身份证号' : '企业社会信用代码'
},
showValue() {
return this.user.userType === '0' ? this.cardNo : this.orgNo
}
}
}
</script>
<style lang="scss" scoped>
.slot-content {
padding: 40rpx;
/* 页面样式 */
.mt-30 {
margin-top: 30rpx;
}
.value-text {
font-size: 28rpx;
color: #666;
}
/* ======================
自定义弹窗样式(超美观)
========================= */
.modal-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 999;
}
.modal-content {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 82%;
background: #fff;
border-radius: 20rpx;
z-index: 1000;
overflow: hidden;
box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.15);
}
.modal-input {
width: 100%;
height: 80rpx;
font-size: 32rpx;
border-bottom: 2rpx solid #EEEEEE;
padding: 0 10rpx;
box-sizing: border-box;
color: #333;
}
.modal-header {
padding: 40rpx 30rpx 20rpx;
text-align: center;
font-weight: bold;
.title {
font-size: 34rpx;
color: #222;
}
}
.modal-body {
padding: 20rpx 40rpx 40rpx;
}
.modal-footer {
display: flex;
border-top: 1rpx solid #f0f0f0;
height: 96rpx;
.btn {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
font-size: 30rpx;
&.cancel {
color: #666;
background: #f8f8f8;
}
&.confirm {
color: #fff;
background: #EA414A;
&.loading {
opacity: 0.8;
}
}
}
}
</style>