car_front/pages/info/info.js
2025-07-14 00:16:37 +08:00

339 lines
7.6 KiB
JavaScript

const request = require('../../utils/request');
Page({
data: null,
onLoad(options) {
// 获取车辆ID
if (options.id) {
this.setData({
carId: options.id
});
}
// 加载车辆详情
this.loadCarDetail();
},
onShow() {
// 页面显示时刷新数据
this.refreshData();
},
onShareAppMessage() {
// 分享配置
return {
title: this.data.carDetail.title,
desc: `${this.data.carDetail.desc} - 仅售${this.data.carDetail.price}`,
path: `/pages/car-detail/car-detail?id=${this.data.carId}`,
imageUrl: this.data.carDetail.cover_image[0]
};
},
onShareTimeline() {
// 分享到朋友圈
return {
title: `${this.data.carDetail.title} - 仅售${this.data.carDetail.price}`,
imageUrl: this.data.carDetail.cover_image[0]
};
},
// 初始化数据
initializeData() {
const { carDetail } = this.data;
// 计算优惠金额
const discountAmount = (carDetail.re_price - carDetail.price).toFixed(1);
// 转换属性为数组格式
const attributesList = Object.entries(carDetail.attributes).map(([key, value]) => ({
key,
value
}));
this.setData({
discountAmount,
attributesList
});
},
// 加载车辆详情
async loadCarDetail() {
if (!this.data.carId) return;
this.setData({ loading: true });
try {
// 模拟API调用
const carDetail = await this.fetchCarDetail(this.data.carId);
console.log(carDetail)
if (carDetail) {
// 计算优惠金额
const discountAmount = (carDetail.re_price - carDetail.price).toFixed(1);
// 转换属性为数组格式
const attributesList = Object.entries(carDetail.attr).map(([key, value]) => ({
key,
value
}));
this.setData({
carDetail,
discountAmount,
attributesList
});
}
} catch (error) {
console.error('加载车辆详情失败:', error);
wx.showToast({
title: '加载失败',
icon: 'none'
});
} finally {
this.setData({ loading: false });
}
},
async fetchCarDetail(id) {
try {
const raw = await request({
url: '/admin/wechat/api/carInfo?id=' + id,
method: 'GET'
});
return raw;
} catch (err) {
console.error('加载品牌失败', err);
}
},
// 刷新数据
refreshData() {
// 可以在这里添加数据刷新逻辑
console.log('页面数据刷新');
},
// 返回上一页
goBack() {
// 检查是否有上一页
const pages = getCurrentPages();
if (pages.length > 1) {
wx.navigateBack({
delta: 1
});
} else {
// 如果没有上一页,跳转到首页
wx.redirectTo({
url: '/pages/index/index'
});
}
},
// 分享车辆
shareVehicle() {
const { carDetail } = this.data;
// 显示分享菜单
wx.showActionSheet({
itemList: ['分享给朋友', '分享到朋友圈', '复制链接'],
success: (res) => {
switch(res.tapIndex) {
case 0:
// 分享给朋友 - 由微信原生分享处理
break;
case 1:
// 分享到朋友圈
this.shareToTimeline();
break;
case 2:
// 复制链接
this.copyLink();
break;
}
}
});
},
// 分享到朋友圈
shareToTimeline() {
// 由于小程序限制,这里只能提示用户
wx.showModal({
title: '分享提示',
content: '请点击右上角菜单选择"分享到朋友圈"',
showCancel: false
});
},
// 复制链接
copyLink() {
const shareUrl = `https://yourapp.com/car-detail?id=${this.data.carId}`;
wx.setClipboardData({
data: shareUrl,
success: () => {
wx.showToast({
title: '链接已复制',
icon: 'success'
});
},
fail: () => {
wx.showToast({
title: '复制失败',
icon: 'none'
});
}
});
},
// 拨打电话
makePhoneCall(e) {
const phone = e.currentTarget.dataset.phone || this.data.contactPhone;
wx.makePhoneCall({
phoneNumber: phone,
success: () => {
console.log('拨打电话成功');
},
fail: (err) => {
console.error('拨打电话失败:', err);
wx.showToast({
title: '拨打失败',
icon: 'none'
});
}
});
},
// 打开微信咨询
openWechat() {
// 方案1: 复制微信号
const wechatId = 'car_consultant_001';
wx.showModal({
title: '微信咨询',
content: `客服微信号:${wechatId}\n点击确定复制微信号`,
success: (res) => {
if (res.confirm) {
wx.setClipboardData({
data: wechatId,
success: () => {
wx.showToast({
title: '微信号已复制',
icon: 'success'
});
}
});
}
}
});
},
// 图片预览
previewImage(e) {
const current = e.currentTarget.dataset.src;
const urls = this.data.carDetail.cover_image;
wx.previewImage({
current,
urls
});
},
// 收藏车辆
toggleFavorite() {
// 这里应该调用收藏API
wx.showToast({
title: '收藏成功',
icon: 'success'
});
},
// 查看更多图片
viewMoreImages() {
wx.navigateTo({
url: `/pages/car-images/car-images?id=${this.data.carId}`
});
},
// 查看检测报告
viewInspectionReport() {
const { carDetail } = this.data;
if (carDetail.attributes['检测状态'] === '已检测') {
wx.navigateTo({
url: `/pages/inspection-report/inspection-report?id=${this.data.carId}`
});
} else {
wx.showToast({
title: '暂无检测报告',
icon: 'none'
});
}
},
// 计算器
openCalculator() {
wx.navigateTo({
url: `/pages/loan-calculator/loan-calculator?price=${this.data.carDetail.price}`
});
},
// 预约看车
bookViewing() {
wx.navigateTo({
url: `/pages/book-viewing/book-viewing?id=${this.data.carId}`
});
},
// 举报车辆
reportVehicle() {
wx.showActionSheet({
itemList: ['价格有误', '车况描述不符', '联系方式错误', '其他问题'],
success: (res) => {
const reasons = ['价格有误', '车况描述不符', '联系方式错误', '其他问题'];
const reason = reasons[res.tapIndex];
wx.showModal({
title: '举报确认',
content: `您要举报的原因:${reason}`,
success: (modalRes) => {
if (modalRes.confirm) {
// 提交举报
this.submitReport(reason);
}
}
});
}
});
},
// 提交举报
submitReport(reason) {
// 这里应该调用举报API
wx.showToast({
title: '举报成功',
icon: 'success'
});
},
// 页面下拉刷新
onPullDownRefresh() {
this.loadCarDetail().then(() => {
wx.stopPullDownRefresh();
});
},
// 页面触底加载
onReachBottom() {
// 如果有相关推荐车辆,可以在这里加载
console.log('触底加载更多');
},
// 用户点击右上角分享
onShareAppMessage() {
return {
title: this.data.carDetail.title,
desc: `${this.data.carDetail.desc} - 仅售${this.data.carDetail.price}`,
path: `/pages/car-detail/car-detail?id=${this.data.carId}`,
imageUrl: this.data.carDetail.cover_image[0]
};
}
});