diff --git a/pages/login/login.vue b/pages/login/login.vue index 9565d49..ae7234c 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -153,19 +153,15 @@ export default { console.error('获取配置失败:', err); } }, - async submit() { // 去除首尾空格 - this.nickName = this.nickName.trim() this.queryvalue = this.queryvalue.trim() - + if (!this.queryvalue || !this.rdpasswd) { uni.showToast({ title: '请输入读者证号和密码', icon: 'none' }); return; } - uni.showLoading({ title: '绑定中...' }); - try { const params = { ...this.screenConfig, @@ -173,13 +169,7 @@ export default { queryvalue: this.queryvalue, rdpasswd: this.rdpasswd, }; - // 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 res = await FetchReaderList(params); let result = {}; try { @@ -189,7 +179,6 @@ export default { uni.showToast({ title: '数据解析失败', icon: 'none' }); return; } - // 统一提取错误信息 let errMsg = ''; if (result.messagelist?.length) { @@ -200,71 +189,113 @@ export default { 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"); + 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, } - // 调用绑定接口 - const bindRes = await FetchBindReadCard(bindParams); - console.log('绑定结果', bindRes); - - if(bindRes.code === 200 ){ + + // ========== 绑定接口独立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(); - uni.showToast({ - title: bindRes.data || '读者证绑定成功', - icon: 'success' + let errTip = typeof bindErr === 'string' ? bindErr : '绑定请求异常,请稍后重试'; + uni.showModal({ + title: '系统异常', + content: errTip, + showCancel: false }); - const data = { - libcode: getApp().globalData.libcode, - openId: openId - } - FetchFindAllReaderBindByOpenId(data).then(res => { - // console.log('获取读者证列表',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); - }) - - // 绑定成功后返回上一页 - setTimeout(() => { - uni.navigateBack(); - }, 1500); - }else{ - // 绑定失败提示 - uni.hideLoading() - uni.showToast({ - title: bindResult.message || '绑定失败,请重试', - icon: 'none' - }) } + // ====================================================================== + } else { uni.hideLoading(); - uni.showToast({ - title: errMsg || '读者证或密码错误', - icon: 'none' + uni.showModal({ + title: '认证失败', + content: errMsg || '读者证或密码错误', + showCancel: false }); } } catch (err) { console.error('绑定流程异常:', err) uni.hideLoading(); - uni.showToast({ title: err || '绑定失败', icon: 'none' }); + 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 + }); } }, + /** * 跳转到在线办证页面 */ diff --git a/utils/request.js b/utils/request.js index 69e3698..411ff18 100644 --- a/utils/request.js +++ b/utils/request.js @@ -27,10 +27,8 @@ function request({ url, data, method = 'GET' }) { if (store.state.user.token) { header.Authorization = store.state.user.token; } - let finalUrl = BASE_URL + url let finalData = data - // GET 请求:手动拼装query,数组展开多同名参数 if (method.toUpperCase() === 'GET' && data && typeof data === 'object') { const query = buildQuery(data) @@ -39,24 +37,14 @@ function request({ url, data, method = 'GET' }) { } finalData = undefined // GET 清空data,防止uni.request重复处理 } - uni.request({ url: finalUrl, data: finalData, method: method.toUpperCase(), header, success: ({ data }) => { - if (data.code === 200) { - resolve(data); - } else { - uni.showToast({ - title: data.message || '请求失败', - icon: 'none', - mask: true, - duration: 3000 - }); - reject(data.message); - } + // ✅ 全部直接resolve,业务层自己判断code,不再reject业务错误 + resolve(data); }, fail: (error) => { uni.showToast({ @@ -72,4 +60,4 @@ function request({ url, data, method = 'GET' }) { }); } -export default request; \ No newline at end of file +export default request;