优化,完善代码

This commit is contained in:
2026-05-14 14:42:51 +08:00
parent 79a21ff0a5
commit 16c91facac
26 changed files with 1792 additions and 1322 deletions

View File

@@ -2,16 +2,6 @@
<view class="u-m-20">
<u-navbar :is-back="true" title="账号实名信息" :border-bottom="false"></u-navbar>
<view>
<!-- <u-cell-group>
<u-cell-item title="头像" :arrow="false" hover-class="none" @click="updateAvatar">
<u-avatar :src="pic" size="100"></u-avatar>
</u-cell-item>
</u-cell-group> -->
<!-- <u-cell-group>
<u-cell-item title="昵称" :arrow="false" hover-class="none" @click="updateName">
{{vuex_user.user.nickName}}
</u-cell-item>
</u-cell-group> -->
<u-cell-group>
<u-cell-item :title="getTitle" :arrow="false" hover-class="none" @click="updateName">
{{(cardNo == null ? orgNo : cardNo) || ''}}
@@ -19,29 +9,28 @@
</u-cell-group>
</view>
<u-modal v-model="showModel" @confirm="confirmAuthCode" ref="uModal" :async-close="true"
:title="'设置' + getTitle">
<u-modal
v-model="showModel"
@confirm="confirmAuthCode"
ref="uModal"
:async-close="true"
:title="'设置' + getTitle"
>
<view class="slot-content">
<u-input v-model="authCode" type="text" :border="false" :placeholder="'请输入' + getTitle" />
<u-input
v-model="authCode"
type="text"
:border="false"
:placeholder="'请输入' + getTitle"
/>
</view>
</u-modal>
<!-- <view class="u-m-t-20">
<u-button type="primary" @click="subProfile">提交</u-button>
</view> -->
<!-- 如果是微信登录小程序则获取用户的昵称与头像 -->
<!-- #ifdef MP-WEIXIN -->
<!-- <u-button type="default">使用微信头像与昵称</u-button> -->
<!-- #endif -->
</view>
</template>
<script>
import config from "@/common/config.js" // 全局配置文件
import {
rsaEncrypt
} from "@/common/utils/ras.js"
import config from "@/common/config.js"
import { rsaEncrypt } from "@/common/utils/ras.js"
export default {
data() {
return {
@@ -60,49 +49,46 @@
}
},
onLoad(options) {
let userVo = uni.getStorageSync('userInfo');
this.user = userVo;
// 页面加载时同步最新用户信息
this.getUserProfile();
},
methods: {
updateName() {
this.showModel = true;
},
// 简单身份证校验18 位)
// 身份证校验
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
);
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);
},
// 统一社会信用代码校验18 位)
// 统一社会信用代码校验
checkCreditCode(code) {
return /^[0-9A-Z]{18}$/.test(code);
},
confirmAuthCode() {
let loginType = this.user.userType
// 修复:统一使用 loginType 变量
const loginType = this.user.userType;
if (!this.authCode) {
this.showModel = false;
return this.$mytip.toast(loginType === '0' ? '请输入身份证号' : '请输入组织社会信用代码')
}
// 身份证校验
if (this.loginType === '0' && !this.checkIdCard(this.authCode)) {
uni.showToast({
title: '身份证号格式不正确',
icon: 'none'
});
// 校验身份证
if (loginType === '0' && !this.checkIdCard(this.authCode)) {
uni.showToast({ title: '身份证号格式不正确', icon: 'none' });
return;
}
// 企业社会信用代码校验
if (this.loginType === '1' && !this.checkCreditCode(this.authCode)) {
uni.showToast({
title: '社会信用代码格式不正确',
icon: 'none'
});
// 校验信用代码
if (loginType === '1' && !this.checkCreditCode(this.authCode)) {
uni.showToast({ title: '社会信用代码格式不正确', icon: 'none' });
return;
}
let token = this.$getToken()
let url = "/login/updateVerifyCode";
let encryptCode = rsaEncrypt(this.authCode);
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
@@ -110,64 +96,74 @@
'WT': token
}).then(obj => {
if (obj.flag) {
// 赋值展示
if (loginType === '0') {
this.cardNo = encryptCode
this.cardNo = this.applyMask(this.authCode)
this.cardNo = this.applyMask(this.authCode);
} else {
this.orgNo = this.authCode
this.orgNo = this.authCode;
}
this.showModel = false;
this.$mytip.toast('修改成功');
this.getUserProfile();
uni.switchTab({
url: '/pages/center/center'
})
// 关键:先更新用户信息,成功后再跳转
this.getUserProfile().then(() => {
setTimeout(() => {
uni.switchTab({ url: '/pages/center/center' });
}, 1000);
});
} else {
this.$mytip.toast('修改失败');
}
});
},
// 身份证脱敏修复
applyMask(idCard) {
if (idCard.length !== 18) return idCard; // 非18位直接返回
return idCard.replace(/(\d{6})(\d{8})(\d{4})/, '$1********$2****');
if (!idCard || idCard.length !== 18) return idCard;
// 前6位 + 8个* + 后4位
return idCard.replace(/^(\d{6})\d{8}(\d{4})$/, '$1********$2');
},
updateAvatar() {
this.$u.route('/pages-biz/profile/avatar')
},
subProfile() {
// 1.更新vuex中的用户信息
this.$mytip.toast('修改成功')
// 2.页面跳转
},
getUserProfile() {
// 如果是微信登录小程序,则获取用户的昵称与头像
// #ifdef MP-WEIXIN
// 此处执行微信才执行
// #endif
uni.removeStorageSync('userInfo');
let token = this.$getToken();
let url = `/login/userInfo`;
this.$u.get(url, {}, {
'WT': token
}).then(obj => {
uni.setStorageSync('userInfo', {
// 关键修复:获取用户信息并同步到当前页面 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;
} else {
this.orgNo = obj.data.orgNo || null;
this.cardNo = null;
}
return userInfo;
} catch (e) {
console.error('获取用户信息失败', e);
}
}
},
computed: {
getTitle() {
return this.user.userType === '0' ?
'身份证号' :
'企业社会信用代码'
return this.user.userType === '0' ? '身份证号' : '企业社会信用代码';
}
}
}

View File

@@ -5,11 +5,11 @@
<view class="setting-content">
<u-cell-group>
<u-cell-item title="账号实名信息" @click="profile"></u-cell-item>
<u-cell-item title="订阅系统消息" :arrow="false">
<!-- <u-cell-item title="订阅系统消息" :arrow="false">
<u-switch v-model="longSubscribe" @change="toggleLongSubscribe" />
</u-cell-item>
</u-cell-item> -->
</u-cell-group>
</view>
@@ -53,32 +53,30 @@
// 微信小程序专用
// #ifdef MP-WEIXIN
if (this.longSubscribe) {
const tmplIds = ['ZVl9bkFv8Nzha2n_6wO36IBqe0H5VwJBvV-7OkVT5jo'] // 在小程序后台配置的长期订阅模板
wx.requestSubscribeMessage({
tmplIds,
success: (res) => {
// 用户同意才生效
console.log('长期订阅授权结果', res)
if (Object.values(res).some((v) => v === 'accept')) {
uni.showToast({
title: '长期订阅开启成功',
icon: 'success'
})
// 可以把状态保存到后端
this.saveLongSubscribeStatus(true)
} else {
uni.showToast({
title: '未授权订阅',
icon: 'none'
})
this.longSubscribe = false
}
},
fail: (err) => {
console.error(err)
this.longSubscribe = false
},
})
// const tmplIds = ['9QlNxNONJBICzw3Vcetqbf9yv4lI9q9cR_px8ujlOu8']
// wx.requestSubscribeMessage({
// tmplIds,
// success: (res) => {
// if (Object.values(res).some((v) => v === 'accept')) {
// uni.showToast({
// title: '长期订阅开启成功',
// icon: 'success'
// })
// this.saveLongSubscribeStatus(true)
// } else {
// uni.showToast({
// title: '未授权订阅',
// icon: 'none'
// })
// this.longSubscribe = false
// }
// },
// fail: (err) => {
// console.error(err)
// this.longSubscribe = false
// },
// })
this.saveLongSubscribeStatus(true)
} else {
// 取消订阅
uni.showToast({