修改cid无效、登录缓存问题,正在对接支付功能

This commit is contained in:
zhouyanli
2026-07-14 14:35:54 +08:00
parent 3124f95cb3
commit 173f1c4585
10 changed files with 391 additions and 121 deletions

View File

@@ -1,6 +1,10 @@
<script setup>
import { onLaunch,onShow,onHide } from '@dcloudio/uni-app'
let cidRetryTimer = null
let cidRetryCount = 0
const MAX_RETRY = 5 // 最多重试 5 次
// 应用启动时执行
onLaunch(() => {
console.log('App 启动')
@@ -12,29 +16,90 @@
// 只有 App 平台才获取 CID
getPushCID()
// #endif
// 自动登录:检查本地是否已有 token有则直接跳转首页
const token = uni.getStorageSync('token')
const hasShowGuide = uni.getStorageSync('hasShowGuide')
if (token) {
// 已有登录凭证,直接进入首页
console.log('检测到已登录,跳转首页')
uni.reLaunch({ url: '/pages/index/index' })
} else if (hasShowGuide) {
// 已看过引导页但没有 token直接跳转登录页
console.log('已看过引导页,跳转登录页')
uni.reLaunch({ url: '/pageSubPack/loginSub/pwd-login' })
}
// 否则停留在引导页navigation
})
onShow(() =>{
console.log('App Show')
// #ifdef APP-PLUS
// 每次回到前台时检查 CID 是否有效,没有则重新获取
const savedCid = uni.getStorageSync('push_cid')
if (!savedCid) {
console.log('App Show 检测到 CID 为空,重新获取')
cidRetryCount = 0
getPushCID()
}
// #endif
})
onHide(() =>{
console.log('App Hide')
})
// 获取 uniPush 2.0 CID
// 获取 uniPush 2.0 CID(带重试机制,解决离线安装首次启动 CID 为空的问题)
function getPushCID() {
// #ifdef APP-PLUS
// 先尝试 plus.push.getClientInfo 获取(兼容老版本)
try {
const clientInfo = plus.push.getClientInfo()
if (clientInfo && clientInfo.clientid) {
console.log('推送 CIDplus.push=', clientInfo.clientid)
uni.setStorageSync('push_cid', clientInfo.clientid)
return
}
} catch (e) {
console.log('plus.push.getClientInfo 失败,改用 uni.getPushClientId')
}
uni.getPushClientId({
success: (res) => {
console.log('推送初始化成功')
console.log('CID =', res.cid)
// 存起来备用
if (res.cid) {
console.log('推送初始化成功, CID =', res.cid)
uni.setStorageSync('push_cid', res.cid)
cidRetryCount = 0
if (cidRetryTimer) {
clearTimeout(cidRetryTimer)
cidRetryTimer = null
}
} else {
// CID 返回为空(常见于离线安装首次启动)
console.warn('获取到 CID 为空,将重试')
scheduleRetry()
}
},
fail: (err) => {
console.error('获取推送 CID 失败', err)
scheduleRetry()
}
})
// #endif
}
// 定时重试获取 CID间隔递增2s → 4s → 8s → 16s → 32s
function scheduleRetry() {
if (cidRetryCount >= MAX_RETRY) {
console.error('CID 获取重试已达上限(' + MAX_RETRY + '次),放弃重试')
return
}
cidRetryCount++
const delay = Math.pow(2, cidRetryCount) * 1000
console.log('将在 ' + delay / 1000 + 's 后第 ' + cidRetryCount + ' 次重试获取 CID')
if (cidRetryTimer) clearTimeout(cidRetryTimer)
cidRetryTimer = setTimeout(() => {
getPushCID()
}, delay)
}
</script>

View File

@@ -3,7 +3,7 @@
"appid" : "__UNI__29C3D60",
"description" : "智慧社区居民端面向小区业主使用,支持注册、验证码、账号密码多种登录方式,提供密码找回功能,账号使用安全便捷。支持多小区切换,首页设有轮播 banner、社区公告集成远程开门、生活缴费、在线报修、访客邀请、社区活动常用服务。全服务板块统一涵盖门禁开门、线上缴费、报修申报、访客预约、投诉反馈、问卷调研、活动报名、社区公示、一键联系物业功能。个人中心可管理房屋、住户、车位、车辆信息支持更换手机号、修改密码、消息通知设置、版本更新及账号退出一站式满足业主居家生活、社区服务、房产车辆管理需求实现社区生活数字化、便捷化。",
"versionName" : "1.0.1",
"versionCode" : 109,
"versionCode" : 111,
"transformPx" : false,
"uniCloud" : {
"provider" : "aliyun",
@@ -172,6 +172,7 @@
"setting" : {
"urlCheck" : false
},
"navigateToMiniProgramAppIdList" : [ "00004951" ],
"requiredPrivateInfos" : [ "chooseLocation", "getLocation" ],
"permission" : {
"scope.camera" : {

View File

@@ -8,10 +8,10 @@ if (process.env.NODE_ENV === 'development') {
export default function request(url, data = {}, method = "GET") {
return new Promise((resolve, reject) => {
uni.showLoading({
title: '请求中...',
mask: true
});
// uni.showLoading({
// title: '请求中...',
// mask: true
// });
uni.request({
url: baseUrl + url,
@@ -23,7 +23,7 @@ export default function request(url, data = {}, method = "GET") {
'content-type': 'application/json'
},
success: (res) => {
uni.hideLoading();
// uni.hideLoading();
// HTTP 失败
if (res.statusCode !== 200) {
@@ -54,7 +54,7 @@ export default function request(url, data = {}, method = "GET") {
}
},
fail: (err) => {
uni.hideLoading();
// uni.hideLoading();
reject({ msg: '网络异常', code: -1, raw: err });
}
});

View File

@@ -205,7 +205,7 @@ const doLogin = () => {
setTimeout(() =>{
uni.switchTab({
url: '/pages/index/index'
},1000);
},2000);
})
}else{
uni.showToast({ title: res.msg, icon: 'error' });

View File

@@ -37,7 +37,7 @@
<view class="line">
<view class="row">
<text class="label">车位状态</text>
<text class="value">{{ item.parkingStatus }}</text>
<text class="value">{{ getParkingStatus(item.parkingStatus) }}</text>
</view>
</view>
<view class="line">
@@ -94,6 +94,18 @@
const pageNum = ref(1)
const pageSize = ref(10)
// 车位状态映射
const getParkingStatus =(status) =>{
const statusMap = {
'0':'闲置',
'1':'自用',
'2':'租赁',
'3':'借出',
'4':'其他'
}
return statusMap[status]
}
// ==================== 获取列表数据 ====================
const getList = async () => {
// 防重复请求

View File

@@ -70,12 +70,12 @@
title: '注销账号',
type: 'arrow'
},
{
title: '升级版本',
type: 'text',
value: '当前版本 1.0.0',
valueClass: 'item-version'
}
// {
// title: '升级版本',
// type: 'text',
// value: '当前版本 1.0.0',
// valueClass: 'item-version'
// }
])
// 格式化字节大小
@@ -165,8 +165,8 @@
const checkVersionUpdate = async () => {
uni.showLoading({ title: '检查中...', mask: true })
// #ifdef APP-PLUS
// const channel = plus.runtime.channel; // 云打包自动写入,自定义包为空
// const channel = 'xioami'
const channel = plus.runtime.channel; // 云打包自动写入,自定义包为空
// const channel = 'xiaomi'
// console.log('channel',channel)
// #endif
@@ -174,8 +174,7 @@
const systemInfo = uni.getSystemInfoSync()
const platform = systemInfo.platform === 'android' ? 'android':'ios'
const appChannel = 'resident'
const channel = 'xiaomi'
// const channel = plus.runtime.channel;
// const channel = 'xiaomi'
// 1. 查询最新版本
const versionRes = await getAppVersion(platform, channel, appChannel)
@@ -299,6 +298,10 @@
});
authLogout().then(res => {
if (res.code === 200) {
// 清除本地登录凭证
uni.removeStorageSync('token');
uni.removeStorageSync('userId');
uni.removeStorageSync('currentVillageId');
uni.hideLoading();
uni.showToast({
title: '退出成功',
@@ -306,22 +309,30 @@
});
setTimeout(() => {
uni.hideLoading();
uni.redirectTo({
uni.reLaunch({
url: '/pageSubPack/loginSub/pwd-login'
});
}, 1500);
} else {
uni.showToast({
title: `${res.msg}`,
icon: 'error'
// 即使服务端返回失败,也清除本地数据并跳转登录页
uni.removeStorageSync('token');
uni.removeStorageSync('userId');
uni.removeStorageSync('currentVillageId');
uni.hideLoading();
uni.reLaunch({
url: '/pageSubPack/loginSub/pwd-login'
});
}
}).catch(err => {
console.log(err);
uni.showToast({
title: err.msg,
icon: 'error'
// 网络异常时也清除本地数据并跳转登录页
uni.removeStorageSync('token');
uni.removeStorageSync('userId');
uni.removeStorageSync('currentVillageId');
uni.hideLoading();
uni.reLaunch({
url: '/pageSubPack/loginSub/pwd-login'
});
})

View File

@@ -135,9 +135,14 @@
// 检查是否有从日照银行小程序返回的支付结果
if (options && options.referrerInfo && options.referrerInfo.extraData) {
const extraData = options.referrerInfo.extraData
if (extraData.payResult === 'success') {
currentStep.value = 2
if (extraData.retCode === 'SUCCESS') {
sendPaySuccessPush()
uni.redirectTo({
url: '/pageSubPack/payment-list/addPaymentSuccess?type=pay'
})
return
} else if (extraData.retCode === 'FAIL') {
uni.showToast({ title: extraData.retMsg || '支付失败', icon: 'none' })
return
}
}
@@ -145,7 +150,9 @@
const paySuccess = uni.getStorageSync('paySuccess')
if (paySuccess) {
uni.removeStorageSync('paySuccess')
currentStep.value = 2
uni.redirectTo({
url: '/pageSubPack/payment-list/addPaymentSuccess?type=pay'
})
}
})
@@ -191,14 +198,14 @@
const mappedDeviceType = deviceTypeMap[paymentItem.type] || paymentItem.deviceType || ''
const res = await getBankMiniProgramParams({
acct: uni.getStorageSync('userId'), // 用户userId
orderType: '20', // 水电费等手输金额
orderAmount: String(Math.round(Number(payNum.value) * 100)), // 金额,单位:分
acct: uni.getStorageSync('userId'),
orderType: '20',
orderAmount: String(Math.round(Number(payNum.value) * 100)),
deviceCode: paymentItem.deviceCode || '',
deviceType: mappedDeviceType, // 1-电表2-水表
billId: paymentItem.billId || '', // 账单明细ID
deviceType: mappedDeviceType,
billId: paymentItem.billId || '',
residentUserId: uni.getStorageSync('userId'),
goodName: paymentItem.name || '' // 商品名称
goodName: paymentItem.name || ''
})
uni.hideLoading()
@@ -207,24 +214,81 @@
uni.showToast({ title: res.msg || '获取支付参数失败', icon: 'none' })
return
}
console.log('resres返回数据',res)
console.log('resres',res.data)
console.log('resres返回数据', res)
console.log('resres', res.data)
const { appId, path, extraData } = res.data
console.log('navigateToMiniProgram参数:', { appId, path, extraData })
// 校验必要参数
if (!appId) {
console.error('缺少appId无法拉起支付小程序', res.data)
uni.showToast({ title: '获取支付参数失败缺少appId', icon: 'none' })
return
}
// 解析 extraData后端返回可能是字符串或对象
// ' let extraDataObj = extraData
// if (typeof extraData === 'string') {
// try {
// extraDataObj = JSON.parse(extraData)
// } catch (e) {
// console.error('extraData JSON解析失败使用空对象', e)
// extraDataObj = {}
// }
// }
// if (!extraDataObj || typeof extraDataObj !== 'object') {
// extraDataObj = {}
// }'
// #ifdef APP-PLUS
// 原生App通过微信SDK拉起小程序
plus.share.getServices((services) => {
const weixin = services.find(s => s.id === 'weixin')
if (!weixin) {
uni.showToast({ title: '未检测到微信客户端', icon: 'none' })
return
}
if (!weixin.launchMiniProgram) {
uni.showToast({ title: '当前微信版本太低,不支持拉起小程序', icon: 'none' })
return
}
weixin.launchMiniProgram({
id: appId,
type: 1, // 0=正式版 1=测试版 2=预览版
path: path || '',
extraData: extraData
}, (res) => {
if (res.retCode === 'SUCCESS') {
console.log('成功拉起日照银行小程序App', res.retCode)
} else {
console.log('拉起日照银行小程序返回失败App======)', res.retMsg)
}
console.log('日照银行小程序回调结果App', JSON.stringify(res))
}, (err) => {
console.error('拉起日照银行小程序失败App', JSON.stringify(err))
uni.showToast({ title: '拉起支付小程序失败', icon: 'none' })
})
})
// #endif
// #ifdef MP-WEIXIN
// 微信小程序直接navigateToMiniProgram
uni.navigateToMiniProgram({
appId: appId,
path: path || '',
extraData: extraData || {},
envVersion: 'release',
extraData: extraData,
envVersion: 'develop',
success: () => {
console.log('成功拉起日照银行小程序')
console.log('成功拉起日照银行小程序(微信)')
},
fail: (err) => {
console.error('拉起日照银行小程序失败', err)
uni.showToast({ title: err.msg ||'拉起支付小程序失败', icon: 'none' })
console.error('拉起日照银行小程序失败(微信)', JSON.stringify(err))
uni.showToast({ title: err.errMsg || err.msg || '拉起支付小程序失败', icon: 'none' })
}
})
// #endif
} catch (err) {
uni.hideLoading()
console.error('获取支付参数异常', err)
@@ -232,7 +296,7 @@
}
}
// 缴费成功推送通知
// 缴费成功推送通知
const sendPaySuccessPush = () => {
const userId = uni.getStorageSync('userId')
if (userId) {

View File

@@ -151,13 +151,13 @@
// iconClass: "gas"
// },
{
name: "水",
name: "水",
type: "water",
icon: "https://wuyeadmin.bugtc.com/images/propertyImgs/water.png",
iconClass: "water"
},
{
name: "电",
name: "电",
type: "electric",
icon: "https://wuyeadmin.bugtc.com/images/propertyImgs/electric.png",
iconClass: "electric"
@@ -213,7 +213,7 @@
onReady(() => {})
onLoad(() => {
postOpenId()
// postOpenId()
uni.hideTabBar({
animation: false // 关闭动画,更稳定
});
@@ -269,7 +269,7 @@
uni.redirectTo({
url: '/pageSubPack/payment-list/selectPaymentEntity'
})
} else if (type.name == '水' || type.name == '电') {
} else if (type.name == '水' || type.name == '电') {
uni.redirectTo({
url: '/pageSubPack/payment-list/addHydropower'
})

View File

@@ -83,6 +83,9 @@
import {
getPushSingle
} from '/pageSubPack/api/api.js'
import {
getBankMiniProgramParams
} from '/pageSubPack/api/apiSub.js'
// 响应式数据
const outstandingPayment = ref(true)
@@ -106,11 +109,30 @@
})
// 从支付收银台返回时检查支付结果
onShow(() => {
onShow((options) => {
// 检查是否有从日照银行小程序返回的支付结果
if (options && options.referrerInfo && options.referrerInfo.extraData) {
const extraData = options.referrerInfo.extraData
if (extraData.retCode === 'SUCCESS') {
currentStep.value = 2
sendPaySuccessPush()
uni.redirectTo({
url: '/pageSubPack/payment-list/addPaymentSuccess?type=pay'
})
return
} else if (extraData.retCode === 'FAIL') {
uni.showToast({ title: extraData.retMsg || '支付失败', icon: 'none' })
return
}
}
const paySuccess = uni.getStorageSync('paySuccess')
if (paySuccess) {
uni.removeStorageSync('paySuccess')
currentStep.value = 2
uni.redirectTo({
url: '/pageSubPack/payment-list/addPaymentSuccess?type=pay'
})
// currentStep.value = 2
}
})
@@ -130,21 +152,110 @@
});
}
// 立即缴费 — 跳转到支付收银台选择支付方式
const handlePay = () => {
// 优先使用用户输入金额,否则使用页面展示的应缴金额
const amount = payAmount.value
uni.setStorageSync('payData', {
amount: String(amount),
deviceCode: paymentItem.deviceCode,
type: paymentItem.type,
name: paymentItem.name,
fullAddress: paymentItem.fullAddress
// 立即缴费 — 通过微信拉起日照银行小程序进行支付
const handlePay = async () => {
try {
uni.showLoading({ title: '加载中...' })
// 设备类型映射物业费→3车位费→4垃圾处理费→5
const deviceTypeMap = { '物业费': '3', '车位费': '4', '垃圾处理费': '5' }
const mappedDeviceType = deviceTypeMap[switchStatus(paymentItem.type)] || ''
const res = await getBankMiniProgramParams({
acct: uni.getStorageSync('userId'),
orderType: '20',
orderAmount: String(Math.round(Number(payAmount.value) * 100)),
deviceCode: paymentItem.deviceCode || '',
deviceType: mappedDeviceType,
billId: paymentItem.billId || '',
residentUserId: uni.getStorageSync('userId'),
goodName: paymentItem.name || ''
})
// uni.navigateTo({ url: '/pageSubPack/payment-list/yinlianPayTn' })
uni.hideLoading()
if (res.code !== 200 || !res.data) {
uni.showToast({ title: res.msg || '获取支付参数失败', icon: 'none' })
return
}
console.log('resres返回数据', res)
console.log('resres', res.data)
const { appId, path, extraData } = res.data
console.log('navigateToMiniProgram参数:', { appId, path, extraData })
// 校验必要参数
if (!appId) {
console.error('缺少appId无法拉起支付小程序', res.data)
uni.showToast({ title: '获取支付参数失败缺少appId', icon: 'none' })
return
}
// 缴费成功推送通知
// 解析 extraData后端返回可能是字符串或对象
let extraDataObj = extraData
if (typeof extraData === 'string') {
try {
extraDataObj = JSON.parse(extraData)
} catch (e) {
console.error('extraData JSON解析失败使用空对象', e)
extraDataObj = {}
}
}
if (!extraDataObj || typeof extraDataObj !== 'object') {
extraDataObj = {}
}
// #ifdef APP-PLUS
// 原生App通过微信SDK拉起小程序
plus.share.getServices((services) => {
const weixin = services.find(s => s.id === 'weixin')
if (!weixin) {
uni.showToast({ title: '未检测到微信客户端', icon: 'none' })
return
}
if (!weixin.launchMiniProgram) {
uni.showToast({ title: '当前微信版本太低,不支持拉起小程序', icon: 'none' })
return
}
weixin.launchMiniProgram({
id: appId,
type: 0, // 0=正式版 1=测试版 2=预览版
path: path || '',
extraData: extraDataObj
}, () => {
console.log('成功拉起日照银行小程序App')
}, (err) => {
console.error('拉起日照银行小程序失败App', JSON.stringify(err))
uni.showToast({ title: '拉起支付小程序失败', icon: 'none' })
})
})
// #endif
// #ifdef MP-WEIXIN
// 微信小程序直接navigateToMiniProgram
uni.navigateToMiniProgram({
appId: appId,
path: path || '',
extraData: extraDataObj,
envVersion: 'develop',
success: () => {
console.log('成功拉起日照银行小程序(微信)')
},
fail: (err) => {
console.error('拉起日照银行小程序失败(微信)', JSON.stringify(err))
uni.showToast({ title: err.errMsg || err.msg || '拉起支付小程序失败', icon: 'none' })
}
})
// #endif
} catch (err) {
uni.hideLoading()
console.error('获取支付参数异常', err)
uni.showToast({ title: err.msg || '请求异常,请重试', icon: 'none' })
}
}
// 缴费成功推送通知
const sendPaySuccessPush = () => {
const userId = uni.getStorageSync('userId')
if (userId) {

View File

@@ -95,14 +95,20 @@ const handleBtnClick = () => {
// 防重复跳转:已触发过跳转则直接返回
if (isNavigating.value) return;
isNavigating.value = true;
// 立即体验,跳转登录页
// 1. 记录引导已完成
// 记录引导已完成
uni.setStorageSync('hasShowGuide', true);
// 2. 跳转登录页
// 检查是否已有登录 token有则直接进首页否则跳转登录页
const token = uni.getStorageSync('token');
if (token) {
uni.switchTab({
url: '/pages/index/index'
})
} else {
uni.redirectTo({
url: "/pageSubPack/loginSub/pwd-login"
})
}
}
};
</script>