HomeLease/pages/index/index.vue

497 lines
14 KiB
Vue
Raw Normal View History

2025-08-12 15:38:25 +08:00
<template>
<view class="home-container">
<!-- 头部信息 -->
2025-08-14 09:24:37 +08:00
2025-08-26 17:14:01 +08:00
<custom-nav-bar
background-color="#FFDECB"
title="湖北幺娘子厨燃能源科技有限公司"
2025-08-26 17:14:01 +08:00
title-align="left"
></custom-nav-bar>
2025-08-12 15:38:25 +08:00
<!-- 公告栏 -->
2025-08-13 11:05:40 +08:00
<announcement-bar
:announcement-icon="commonEnum.ANNOUNCEMENT_ICON"
:announcement-text="announcementText"
@announcement-click="onAnnouncementClick"
/>
2025-08-12 15:38:25 +08:00
<!-- 轮播图 -->
2025-08-13 11:05:40 +08:00
<banner-swiper
:autoplay="autoplay"
:banner-list="bannerList"
:duration="duration"
:indicator-dots="indicatorDots"
:interval="interval"
@change="onBannerChange"
@banner-click="onBannerClick"
/>
2025-08-12 15:38:25 +08:00
<!-- 设备列表 -->
2025-08-13 11:05:40 +08:00
<equipment-list
:equipment-list="equipmentList"
:title="equipmentTitle"
@renew="onRenew"
@equipment-click="onEquipmentClick"
/>
2025-08-12 15:38:25 +08:00
2025-08-21 18:12:21 +08:00
<!-- 续费弹窗 -->
<renew-modal
:device="selectedDevice"
:loading="packageLoading"
:package-list="packageList"
:visible="showRenewModal"
@close="closeRenewModal"
@select-package="onSelectPackage"
@confirm-renew="onConfirmRenew"
/>
2025-08-13 11:43:35 +08:00
<!-- 底部导航已由系统tabBar处理 -->
2025-08-20 13:57:52 +08:00
</view>
2025-08-12 15:38:25 +08:00
</template>
<script>
2025-08-13 11:10:19 +08:00
import commonEnum from '../../enum/commonEnum'
import AnnouncementBar from '../../components/announcement-bar/announcement-bar.vue'
import BannerSwiper from '../../components/banner-swiper/banner-swiper.vue'
import EquipmentList from '../../components/equipment-list/equipment-list.vue'
2025-08-21 18:12:21 +08:00
import RenewModal from '../../components/renew-modal/renew-modal.vue'
import { getNewAnnouncement } from '../../api/article/article.js'
2025-08-18 17:58:18 +08:00
import { getBannerList } from '../../api/banner/banner.js'
2025-08-19 08:55:34 +08:00
import { getDeviceList } from '../../api/device/device.js'
2025-09-13 17:47:26 +08:00
import {
renewDevice,
getPeriodPackages,
getPeriodPackagesByTypeId,
getPeriodPackagesByDevId,
} from '../../api/lease/lease.js'
2025-09-12 15:17:47 +08:00
import { getIsRealName } from '../../api'
2025-09-15 10:46:50 +08:00
import { refreshPayResult } from '../../api/pay/pay'
2025-08-13 11:10:19 +08:00
2025-08-20 13:57:52 +08:00
export default {
2025-08-13 11:10:19 +08:00
components: {
AnnouncementBar,
BannerSwiper,
EquipmentList,
2025-08-21 18:12:21 +08:00
RenewModal,
2025-08-13 11:10:19 +08:00
},
2025-08-20 13:57:52 +08:00
data() {
return {
2025-09-13 17:47:26 +08:00
hasExecuted: uni.getStorageSync('hasExecutedGoToRealName') || false,
2025-08-13 11:10:19 +08:00
// 基础配置
2025-09-12 15:17:47 +08:00
isRealName: false,
2025-08-13 11:10:19 +08:00
indicatorDots: true,
autoplay: true,
interval: 2000,
duration: 500,
commonEnum: commonEnum,
// 页面数据
companyName: '福鼎创特物联科技有限公司',
announcementText: '暂无更多公告! 暂无更多公告! 暂无更多公告!',
currentAnnouncement: null, // 当前公告数据
2025-08-13 11:10:19 +08:00
equipmentTitle: '我的租赁设备',
navItems: ['首页', '申请租赁', '个人中心'],
activeNavIndex: 0,
// 轮播图数据
currentBannerIndex: 0,
2025-08-18 17:58:18 +08:00
bannerList: [],
2025-08-13 11:10:19 +08:00
// 设备列表数据
2025-08-19 08:55:34 +08:00
equipmentList: [],
2025-08-21 18:12:21 +08:00
// 续费相关数据
showRenewModal: false,
selectedDevice: null,
packageList: [],
packageLoading: false,
selectedPackage: null,
2025-09-13 17:47:26 +08:00
renewData: {},
2025-09-15 10:46:50 +08:00
//订单id
no: '',
2025-08-20 13:57:52 +08:00
}
},
2025-08-13 11:10:19 +08:00
// 生命周期钩子
2025-08-20 13:57:52 +08:00
onLoad() {
this.fetchAnnouncement()
2025-08-18 17:58:18 +08:00
this.fetchBannerList()
2025-08-19 08:55:34 +08:00
this.fetchDeviceList()
},
2025-08-22 17:52:50 +08:00
onShow() {
this.fetchAnnouncement()
this.fetchBannerList()
this.fetchDeviceList()
this.onceTipRealName()
2025-08-22 17:52:50 +08:00
},
2025-08-13 11:10:19 +08:00
methods: {
async onceTipRealName() {
console.log('onceTipRealName-hasExecuted:', this.hasExecuted)
const storageInfo = uni.getStorageInfoSync()
console.log('当前存储Keys:', storageInfo.keys)
let token = uni.getStorageSync('token')
console.log('onceTipRealName-token:', token)
if (token && !this.hasExecuted) {
await this.goToRealName()
this.hasExecuted = true
uni.setStorageSync('hasExecutedGoToRealName', true)
}
},
2025-09-12 15:17:47 +08:00
async goToRealName() {
const res = await getIsRealName()
if (!res.data) {
uni.showModal({
title: '提示',
content: '需要租赁,请先实名',
showCancel: true,
confirmText: '去实名',
cancelText: '暂不',
success: function (res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/realNameAuthentication/realNameAuthentication',
})
}
},
})
}
},
// 获取最新公告
async fetchAnnouncement() {
try {
const response = await getNewAnnouncement()
if (response.code === 200 && response.data) {
this.currentAnnouncement = response.data
// 更新公告文本去除HTML标签
const content = response.data.content || ''
const plainText = content.replace(/<[^>]*>/g, '')
this.announcementText = response.data.title || plainText || '暂无更多公告!'
}
} catch (error) {
console.error('获取公告失败:', error)
this.announcementText = '暂无更多公告!'
}
},
2025-08-18 17:58:18 +08:00
// 获取轮播图列表
async fetchBannerList() {
try {
const response = await getBannerList()
if (response.code === 200 && response.data && Array.isArray(response.data)) {
// 按orderNum排序确保轮播图顺序正确
const sortedBanners = response.data
.filter(banner => banner.state === '1') // 只显示启用的轮播图
.sort((a, b) => parseInt(a.orderNum) - parseInt(b.orderNum))
.map(banner => ({
id: banner.id,
image: banner.imgUrl,
link: banner.link,
2025-08-20 13:57:52 +08:00
name: `轮播图${banner.id}`,
2025-08-18 17:58:18 +08:00
}))
2025-08-20 13:57:52 +08:00
2025-08-18 17:58:18 +08:00
this.bannerList = sortedBanners
}
} catch (error) {
console.error('获取轮播图失败:', error)
// 如果获取失败,使用默认轮播图
this.bannerList = [
{
id: 'default1',
image: commonEnum.TEMP1,
2025-08-20 13:57:52 +08:00
name: '默认轮播图1',
2025-08-18 17:58:18 +08:00
},
{
2025-08-20 13:57:52 +08:00
id: 'default2',
2025-08-18 17:58:18 +08:00
image: commonEnum.TEMP2,
2025-08-20 13:57:52 +08:00
name: '默认轮播图2',
},
2025-08-18 17:58:18 +08:00
]
}
},
2025-08-19 08:55:34 +08:00
// 获取设备列表
async fetchDeviceList() {
try {
const token = uni.getStorageSync('token')
if (!token) {
console.log('暂无登录,不请求设备列表')
return
}
2025-08-19 08:55:34 +08:00
const response = await getDeviceList()
2025-08-19 08:55:34 +08:00
if (response.code === 200 && response.data && Array.isArray(response.data)) {
// 转换设备数据格式
const devices = response.data.map(device => {
// 根据operationState判断设备状态
let status = 'normal'
if (device.operationState === '1') {
2025-08-19 08:58:59 +08:00
status = 'available' // 可租用
2025-08-19 08:55:34 +08:00
} else if (device.operationState === '2') {
2025-08-19 08:58:59 +08:00
status = 'rented' // 已出租
2025-08-19 08:55:34 +08:00
} else if (device.operationState === '3') {
status = 'maintenance' // 维护中
2025-08-19 08:58:59 +08:00
} else if (device.operationState === '4') {
status = 'scrapped' // 报废
2025-08-19 08:55:34 +08:00
}
// 根据onlineState判断在线状态
const isOnline = device.onlineState === '1'
2025-08-20 13:57:52 +08:00
2025-08-19 08:55:34 +08:00
return {
id: device.id,
name: device.newTypeName || device.typeName || '未知设备',
status: status,
startTime: device.leaseTime || '',
endTime: device.expirationTime || '',
image: device.img || commonEnum.TEMP2,
mac: device.mac,
sn: device.sn,
isOnline: isOnline,
powerStatus: device.powerStatus,
2025-08-20 13:57:52 +08:00
iotExpireTime: device.iotExpireTime,
2025-08-21 18:12:21 +08:00
// 保留原始数据用于API调用
2025-09-08 11:55:10 +08:00
suitIds: device.suitIds || device.devicesuitIds,
2025-08-21 18:12:21 +08:00
originalData: device,
2025-08-19 08:55:34 +08:00
}
})
2025-08-20 13:57:52 +08:00
2025-08-19 08:55:34 +08:00
this.equipmentList = devices
}
} catch (error) {
console.error('获取设备列表失败:', error)
}
},
2025-08-13 11:10:19 +08:00
// 头部点击事件
onLocationClick() {
uni.showToast({
title: '选择位置',
icon: 'none',
})
2025-08-12 15:38:25 +08:00
},
2025-08-13 11:10:19 +08:00
// 公告栏点击事件
onAnnouncementClick() {
if (this.currentAnnouncement) {
// 显示公告详情
uni.navigateTo({
2025-08-21 18:12:21 +08:00
url: '/pages/announcementList/announcementList',
})
} else {
uni.showToast({
title: '暂无公告',
icon: 'none',
})
}
2025-08-13 11:10:19 +08:00
},
// 轮播图变化事件
onBannerChange(index) {
this.currentBannerIndex = index
},
// 轮播图点击事件
onBannerClick({ item, index }) {
2025-08-18 17:58:18 +08:00
if (item.link) {
// 如果有链接,跳转到对应页面
uni.navigateTo({
2025-08-20 13:57:52 +08:00
url: item.link,
2025-08-18 17:58:18 +08:00
})
2025-08-20 13:57:52 +08:00
}
2025-08-13 11:10:19 +08:00
},
// 设备点击事件
onEquipmentClick(equipment) {
2025-08-19 08:55:34 +08:00
// 跳转到设备详情页面,传递设备信息
2025-08-13 11:10:19 +08:00
},
// 续费事件
2025-08-21 18:12:21 +08:00
async onRenew(equipment) {
try {
this.selectedDevice = equipment
this.packageLoading = true
// 根据设备ID查询套餐
2025-09-13 17:47:26 +08:00
console.log('设备信息originalData', this.selectedDevice.originalData)
await this.fetchPackageList(this.selectedDevice.originalData.id)
console.log('设备id', this.selectedDevice.originalData.id)
2025-08-21 18:12:21 +08:00
this.showRenewModal = true
} catch (error) {
console.error('打开续费弹窗失败:', error)
uni.showToast({
title: '打开续费弹窗失败',
icon: 'error',
})
}
},
// 获取套餐列表
async fetchPackageList(deviceId) {
try {
2025-09-08 11:55:10 +08:00
console.log('设备类型ID:', deviceId)
2025-08-22 10:13:31 +08:00
2025-09-13 17:47:26 +08:00
const response = await getPeriodPackagesByDevId(deviceId)
2025-08-21 18:12:21 +08:00
if (response.code === 200) {
this.packageList = response.data || []
console.log('套餐列表:', this.packageList)
} else {
throw new Error(response.message || '获取套餐列表失败')
}
} catch (error) {
console.error('获取套餐列表失败:', error)
uni.showToast({
title: error.message || '获取套餐列表失败',
icon: 'error',
})
} finally {
this.packageLoading = false
}
},
// 关闭续费弹窗
closeRenewModal() {
this.showRenewModal = false
this.selectedDevice = null
this.selectedPackage = null
this.packageList = []
},
// 选择套餐
onSelectPackage(combo) {
this.selectedPackage = combo
console.log('选择的套餐:', combo)
},
// 确认续费
async onConfirmRenew() {
if (!this.selectedPackage) {
uni.showToast({
title: '请选择套餐',
icon: 'none',
})
return
}
try {
2025-09-13 17:47:26 +08:00
if (this.selectedPackage.id && this.selectedPackage.id.endsWith('a')) {
this.renewData = {
payAmount: this.packageList.lastSuitAmount,
lastSuitDay: this.packageList.lastSuitDay,
lastSuitId: this.packageList.lastSuitId,
lastSuitName: this.packageList.lastSuitName,
// suitId: this.selectedPackage.id,
isLastSuit: true,
appId: '1', // 应用ID根据实际情况调整
// payAmount: this.selectedPackage.price || this.selectedPackage.amount,
channelId: '3', // 渠道ID根据实际情况调整
devId: this.selectedDevice.id,
}
console.log('ID以a结尾')
} else {
this.renewData = {
suitId: this.selectedPackage.id,
appId: '1', // 应用ID根据实际情况调整
payAmount: this.selectedPackage.price || this.selectedPackage.amount,
channelId: '3', // 渠道ID根据实际情况调整
devId: this.selectedDevice.id,
}
console.log('ID不以a结尾')
2025-08-21 18:12:21 +08:00
}
2025-09-13 17:47:26 +08:00
console.log('续费数据:', this.renewData)
2025-09-12 09:05:57 +08:00
uni.showLoading({
2025-09-13 17:47:26 +08:00
title: '生成续费订单中',
2025-09-12 09:05:57 +08:00
mask: 'true',
})
2025-09-13 17:47:26 +08:00
const response = await renewDevice(this.renewData)
2025-09-12 09:05:57 +08:00
uni.hideLoading()
2025-08-21 18:12:21 +08:00
if (response.code === 200) {
2025-08-29 12:00:06 +08:00
const data = response.data
2025-09-15 10:46:50 +08:00
this.no = data.pay.no
console.log('data.pay.no:', this.no)
2025-08-29 12:00:06 +08:00
const wxpPayParams = data.payParams
2025-09-15 10:46:50 +08:00
console.log('续费response:', data)
2025-08-29 12:00:06 +08:00
const payParams = {
package: wxpPayParams.packageVal, // 后端返回的字段名
timeStamp: wxpPayParams.timeStamp,
nonceStr: wxpPayParams.nonceStr,
signType: wxpPayParams.signType,
paySign: wxpPayParams.paySign,
}
console.log('支付后端回调数据', payParams)
wx.requestPayment({
...payParams,
2025-09-15 10:46:50 +08:00
success: async function (res) {
2025-08-29 12:00:06 +08:00
uni.showToast({
title: '支付成功',
icon: 'success',
2025-08-29 14:50:22 +08:00
duration: 3000,
2025-08-29 12:00:06 +08:00
})
2025-09-15 10:46:50 +08:00
await refreshPayResult(this.no)
2025-08-29 14:50:22 +08:00
setTimeout(() => {
uni.navigateTo({
url: `/pages/myOrder/orderDetail?id=${data.pay.bstId}`,
})
}, 3000)
2025-08-29 12:00:06 +08:00
},
fail: function (res) {
uni.showToast({
title: '支付失败',
icon: 'fail',
})
},
complete: function (res) {
console.log('微信支付调用结束')
},
2025-08-21 18:12:21 +08:00
})
// 关闭弹窗
this.closeRenewModal()
// 刷新设备列表
this.fetchDeviceList()
} else {
throw new Error(response.message || '续费失败')
}
} catch (error) {
uni.hideLoading()
console.error('续费失败:', error)
uni.showToast({
title: error.message || '续费失败',
icon: 'error',
})
}
2025-08-12 15:38:25 +08:00
},
2025-08-13 11:05:40 +08:00
2025-08-13 11:10:19 +08:00
// 底部导航点击事件
onNavClick(index) {
this.activeNavIndex = index
const navItems = ['首页', '申请租赁', '个人中心']
uni.showToast({
title: `切换到${navItems[index]}`,
icon: 'none',
})
2025-08-13 09:58:47 +08:00
},
2025-08-13 11:10:19 +08:00
},
2025-08-20 13:57:52 +08:00
}
2025-08-12 15:38:25 +08:00
</script>
2025-08-13 11:05:40 +08:00
<style lang="scss" scoped>
2025-08-13 11:10:19 +08:00
.home-container {
2025-08-22 18:40:51 +08:00
background: linear-gradient(to bottom, #ffddca 0px, #ffddca 450rpx, #fff 450rpx, #ffffff 100%);
2025-08-13 11:10:19 +08:00
padding-bottom: 120rpx; /* 为底部导航留出空间 */
max-width: 750rpx;
margin: 0 auto;
z-index: -2;
2025-08-20 13:57:52 +08:00
}
2025-08-12 15:38:25 +08:00
</style>