|
|
<template> <view style="background-color: #fff; height: calc(100vh);"> <view class="top-bar"> <image class="top-bar-bg" src="@/static/images/mingqi-beij@2x.png" mode="aspectFill"></image> <view class="library-info"> <image class="avatar" :src="libraryLogo" mode="aspectFill"></image> <view class="library-name">{{ fondsName }}</view> </view> </view>
<view class="form-box"> <view class="item"> <uni-icons class="form-icon" custom-prefix="iconfont" type="icon-duzhezheng" size="24"></uni-icons> <view class="uni-input-wrapper"> <input class="input" placeholder="请输入读者证号" v-model="queryvalue" @input="handleInput('queryvalue')" /> <uni-icons class="clear-icon" v-if="clearIconStatus.queryvalue" @click="clearInput('queryvalue')" type="close" size="20" ></uni-icons> </view> </view> <view class="item"> <uni-icons class="form-icon" type="locked" size="24"></uni-icons> <input class="input" placeholder="请输入密码" :password="!showPwd" v-model="rdpasswd" /> <uni-icons class="form-right-icon" :type="showPwd ? 'eye' : 'eye-slash'" size="20" @click="togglePwd"></uni-icons> </view> <button class="login-btn" type="primary" @click="submit">绑定</button> </view>
<view class="tips"> 温馨提示:<br /> 1、密码默认为 <text style="color:#e74c3c">身份证后6位</text>,如果身份证号最后一位为“X”,“X”需要大写; </view>
<button class="register-btn" type="primary" @click="goRegister">在线办证</button> </view></template>
<script>// 引入绑定接口
import { FetchInitScreenSetting, FetchReaderList, FetchBindReadCard,FetchFindAllReaderBindByOpenId } from '@/api/user';import config from '@/utils/config';import { STORAGE_KEYS } from '@/utils/storage';
export default { data() { return { fondsName: getApp().globalData.fondsName || '', queryvalue: '', rdpasswd: '', libraryLogo: null, showPwd: false, screenConfig: {}, avatarUrl: '', nickName: '', // 清空按钮状态(支持多输入框)
clearIconStatus: { nickName: false, queryvalue: false } }; },
onLoad() { this.getScreenSetting(); },
methods: { // 获取微信头像 + 上传到服务器
onChooseAvatar(e) { // console.log('获取微信头像', e)
// 临时文件路径
const tempFilePath = e.detail.avatarUrl;
// 开始上传
uni.uploadFile({ url: config.baseUrl +'/api/fileRelevant/uploadWxAvatarImg', filePath: tempFilePath, name: 'file', header: { "Content-Type": "multipart/form-data" }, success: (res) => { // console.log("上传成功原始返回:", res);
let realAvatar = ''; try { const data = JSON.parse(res.data); // console.log('解析成功:', data)
realAvatar = data.data || data.url || ''; } catch (e) { realAvatar = res.data; } if (realAvatar) { // console.log("永久头像URL:", realAvatar);
// this.avatarUrl = config.baseUrl + '/api/fileRelevant/getImg?imgType=5&imgId=' + realAvatar
this.avatarUrl = `${config.baseUrl}/files/${realAvatar}`; } else { uni.showToast({ title: '头像上传失败', icon: 'none' }); } }, fail: (err) => { // console.log("上传失败", err);
uni.showToast({ title: '头像上传失败', icon: 'none' }); } }); }, // 统一监听输入(自动控制清空按钮显示隐藏)
handleInput(field) { this.clearIconStatus[field] = this[field].trim().length > 0 }, // 统一清空输入框内容
clearInput(field) { this[field] = '' this.clearIconStatus[field] = false },
// 切换密码显隐
togglePwd() { this.showPwd = !this.showPwd; },
async getScreenSetting() { try { const res = await FetchInitScreenSetting({ libcode: getApp().globalData.libcode }); const data = res.data; this.screenConfig = { thirdUrl: data.open_lib_http?.context || '', thirdAppid: data.open_lib_appId?.context || '', thirdSecret: data.open_lib_secret?.context || '', sm4Key: data.sm4_key?.context || '' };
if(data.library_logo && data.library_logo.context){ this.libraryLogo = config.baseUrl + '/files/' + data.library_logo.context; }else{ this.libraryLogo = null; } } catch (err) { console.error('获取配置失败:', err); } }, async submit() { // 去除首尾空格
this.queryvalue = this.queryvalue.trim()
if (!this.queryvalue || !this.rdpasswd) { uni.showToast({ title: '请输入读者证号和密码', icon: 'none' }); return; } uni.showLoading({ title: '绑定中...' }); try { const params = { ...this.screenConfig, selecttype: 'rdid', queryvalue: this.queryvalue, rdpasswd: this.rdpasswd, };
const res = await FetchReaderList(params); let result = {}; try { result = JSON.parse(res.data); } catch (e) { uni.hideLoading(); uni.showToast({ title: '数据解析失败', icon: 'none' }); return; } // 统一提取错误信息
let errMsg = ''; if (result.messagelist?.length) { const item = result.messagelist[0]; errMsg = item.message || Object.values(item)[0] || '认证失败'; } if (result.messageList?.length) { const item = result.messageList[0]; errMsg = item.message || Object.values(item)[0] || '认证失败'; } // 420703GD0000748
// {"code":200,"message":"操作成功","data":"{\"messagelist\":[{\"code\":\"R00138\",\"message\":\"未找到符合条件的读者!\"}],\"success\":false}","timestamp":1778154499544}
// {"code":200,"message":"操作成功","data":"{\"success\":true,\"pagedata\":[{\"rdClusterCode\":null,\"rdlib\":\"GD\",\"rdid\":\"420105198509200438\",\"rdcfstate\":1}]}","timestamp":1778154647854}
// {"code":200,"message":"操作成功","data":"{\"messageList\":[{\"R00131\":\"认证失败,系统存在该读者,密码不匹配!\"}],\"success\":true,\"pagedata\":\"\"}","timestamp":1778155520501}
// 成功判断
const realSuccess = result.success === true && result.pagedata?.length > 0; if (realSuccess) { // 开始绑定
const openId = uni.getStorageSync("wx_login_code"); if (!openId) { uni.hideLoading(); uni.showModal({ title: '绑定提示', content: '未获取到微信登录信息,请重新登录', showCancel: false }) return; } const bindParams = { openid: openId, bindValue: this.queryvalue, bindType: 'rdid', libcode: getApp().globalData.libcode, }
// ========== 绑定接口独立try‑catch,业务错误不会跑到外层大catch ==========
try { const bindRes = await FetchBindReadCard(bindParams); if (bindRes.code === 200) { uni.hideLoading(); uni.showModal({ title: '绑定成功', content: bindRes.data || '读者证绑定成功', showCancel: false, success: (modalRes) => { if (modalRes.confirm) { const data = { libcode: getApp().globalData.libcode, openId: openId } FetchFindAllReaderBindByOpenId(data).then(res => { if (res.code === 200 && res.data.length > 0) { uni.setStorageSync(STORAGE_KEYS.READER_CARD_LIST, res.data); } else { uni.setStorageSync(STORAGE_KEYS.READER_CARD_LIST, []); } }) .catch(err => { console.error('获取读者证列表失败:', err); }) uni.navigateBack(); } } }); } else { // 业务错误:已被绑定、密码不匹配等业务返回错误
uni.hideLoading() const failMsg = bindRes.message || '绑定失败,请重试'; uni.showModal({ title: '绑定失败', content: failMsg, showCancel: false }) } } catch (bindErr) { // 这里才是绑定接口真正的请求异常(http错误、网络异常)
uni.hideLoading(); let errTip = typeof bindErr === 'string' ? bindErr : '绑定请求异常,请稍后重试'; uni.showModal({ title: '系统异常', content: errTip, showCancel: false }); } // ======================================================================
} else { uni.hideLoading(); uni.showModal({ title: '认证失败', content: errMsg || '读者证或密码错误', showCancel: false }); } } catch (err) { console.error('绑定流程异常:', err) uni.hideLoading(); let errTip = '' if (typeof err === 'string') { errTip = err } else if (err && err.message) { errTip = err.message } else { errTip = '绑定失败,请稍后重试' } uni.showModal({ title: '系统异常', content: errTip, showCancel: false }); } },
/** * 跳转到在线办证页面 */ goRegister() { uni.navigateTo({ url: '/subpkg/pages/register/register' }); } }};</script>
<style lang="scss" scoped>.user-info-section { display: flex; flex-direction: column; align-items: center; padding: 30px 0 0 0;
.avatar-btn { background: transparent; display: flex; flex-direction: column; align-items: center; &::after{ border: none !important; }
.avatar-img { width: 60px; height: 60px; border-radius: 50%; }
.tip-text { font-size: 12px; color: #999; } }}
.form-box { padding: 20px 15px;
.item { width: 100%; min-height: 44px; border-radius: 22px; background-color: #f7f7f7; display: flex; align-items: center; margin-bottom: 15px; .input { flex: 1; padding: 10px; font-size: 14px; } }
.uni-input-wrapper{ flex: 1; display: flex; align-items: center; .input { flex: 1; padding: 10px; font-size: 14px; } .clear-icon{ padding: 0 12px; color: #ccc; } }}
.login-btn { margin-top: 10px; background-color: #01a4fe !important; border-radius: 22px; font-size: 16px; height: 44px; line-height: 44px;}
.tips { margin: 30px 20px; font-size: 12px; color: #333; line-height: 20px;}
.form-icon { ::v-deep .uni-icons { margin-left: 10px; color: #01a4fe !important; }}
.form-right-icon { ::v-deep .uni-icons { margin-right: 10px; }}</style>
|