Files
property-resident-side/property-uniapp-project/pageSubPack/activity-list/live-add.vue

404 lines
11 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="add-status-page">
<!-- 主体内容 -->
<scroll-view class="content" scroll-y>
<!-- 实况描述输入框 -->
<view class="form-item">
<text class="label">实况描述</text>
<textarea
class="textarea"
placeholder="请输入活动实况内容,也可打卡签到"
v-model="statusDesc"
maxlength="500"
@input="handleTextInput"
></textarea>
<text class="count">{{ currentLength }}/500</text>
</view>
<!-- 活动照片上传 -->
<view class="form-item">
<text class="label">活动照片</text>
<view class="upload-area">
<!-- 已上传图片 -->
<view class="uploaded-img" v-for="(img, index) in imgList" :key="index">
<image :src="img" mode="aspectFill"></image>
<text class="del-img" @click="deleteImg(index)">×</text>
</view>
<!-- 上传按钮最多3张 -->
<view class="upload-btn" @click="chooseImg" v-if="imgList.length < 3">
<text class="plus-icon">+</text>
<text class="tips">最多3张图片</text>
</view>
</view>
</view>
<!-- 签到地点 -->
<view class="form-item">
<text class="label">签到地点</text>
<view class="location-area">
<text class="location-text">{{ location }}</text>
<view class="re-locate" @click="reLocate">
<uni-icons type="refreshempty" size="14" color="#2196f3" class="re-icon"></uni-icons>
重新定位
</view>
</view>
</view>
</scroll-view>
<!-- 底部提交按钮 -->
<view class="submit-btn-area">
<button class="submit-btn" @click="submitStatus">提交</button>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { addActivityLive, signinActivityLive, uploadImage } from '../api/api.js';
// 活动ID
const activityId = ref('');
// 实况描述内容
const statusDesc = ref('');
// 输入字符长度
const currentLength = ref(0);
// 图片列表(本地临时路径)
const imgList = ref([]);
// 定位信息
const location = ref('山东省日照市东港区学院路');
// 经纬度
const longitude = ref('');
const latitude = ref('');
// 接收活动ID
onLoad((option) => {
activityId.value = option?.id || '';
});
// 监听输入框字数变化
const handleTextInput = (e) => {
currentLength.value = e.detail.value.length;
};
// 选择图片
const chooseImg = () => {
uni.chooseImage({
count: 3 - imgList.value.length,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
imgList.value = [...imgList.value, ...res.tempFilePaths];
},
fail: (err) => {
uni.showToast({ title: '选择图片失败', icon: 'none' });
console.error('选择图片失败:', err);
}
});
};
// 删除图片
const deleteImg = (index) => {
imgList.value.splice(index, 1);
};
// 重新定位
// 定位并获取具体地点名称
const reLocate = () => {
uni.showLoading({ title: '定位中...' });
// 第一步:获取经纬度
uni.getLocation({
type: 'gcj02', // 微信地图专用坐标系,必须用这个
success: (locRes) => {
// 第二步:通过经纬度逆解析为具体地址
uni.request({
url: 'https://apis.map.qq.com/ws/geocoder/v1/', // 腾讯地图逆地理编码接口(免费)
data: {
location: `${locRes.latitude},${locRes.longitude}`, // 纬度,经度
key: 'XCNBZ-TJ46B-BCJUZ-JBMIM-LZWRJ-SBBWW', // 替换成你自己的Key下面教你怎么获取
get_poi: 0 // 0=只返回基础地址1=返回周边POI
},
success: (geoRes) => {
uni.hideLoading();
if (geoRes.data.status === 0) {
// 解析成功,获取具体地址
const address = geoRes.data.result.address; // 完整地址北京市海淀区中关村南大街5号
const formattedAddress = geoRes.data.result.formatted_addresses.recommend; // 推荐地址(更简洁)
const province = geoRes.data.result.address_component.province; // 省
const city = geoRes.data.result.address_component.city; // 市
const district = geoRes.data.result.address_component.district; // 区
const street = geoRes.data.result.address_component.street; // 街道
// 保存数据(按需选择)
longitude.value = locRes.longitude;
latitude.value = locRes.latitude;
location.value = address; // 把location改为存储具体地址而非经纬度
// 也可以自定义格式location.value = `${city}${district}${street}`;
// 提示定位成功并显示地址
uni.showToast({ title: `定位成功:${formattedAddress}`, icon: 'success', duration: 2000 });
console.log('完整地址:', address);
console.log('省市区:', province, city, district);
} else {
// 解析地址失败,降级显示经纬度
location.value = `${locRes.longitude},${locRes.latitude}`;
uni.showToast({ title: '定位成功,但无法解析地址', icon: 'none' });
}
},
fail: (geoErr) => {
uni.hideLoading();
// 接口请求失败,降级显示经纬度
location.value = `${locRes.longitude},${locRes.latitude}`;
uni.showToast({ title: '定位成功,地址解析失败', icon: 'none' });
console.error('地址解析失败:', geoErr);
}
});
},
fail: (locErr) => {
uni.hideLoading();
uni.showToast({ title: '定位失败,请检查权限', icon: 'none' });
console.error('定位失败:', locErr);
}
});
};
// const reLocate = () => {
// // 调用微信原生选择位置接口(自带地址解析,无调用上限)
// uni.chooseLocation({
// success: (res) => {
// // res.address = 具体地址XX市XX区XX街道XX小区
// // res.name = 地点名称XX小区
// longitude.value = res.longitude;
// latitude.value = res.latitude;
// location.value = res.address; // 显示具体地址
// uni.showToast({ title: `定位成功:${res.name}`, icon: 'success' });
// },
// fail: (err) => {
// // 处理权限拒绝/定位失败
// if (err.errMsg.includes('auth deny')) {
// uni.showModal({
// title: '需要定位权限',
// content: '请允许获取位置信息以显示具体地点',
// confirmText: '去设置',
// success: (modalRes) => {
// if (modalRes.confirm) {
// uni.openSetting({
// success: (settingRes) => {
// // 用户开启权限后重新定位
// if (settingRes.authSetting['scope.userLocation']) {
// reLocate();
// }
// }
// });
// }
// }
// });
// } else {
// uni.hideLoading();
// uni.showToast({ title: '定位失败,请重试', icon: 'none' });
// console.error('定位失败:', err);
// }
// }
// });
// };
// 上传所有图片
const uploadAllImages = async () => {
if (imgList.value.length === 0) return [];
const uploadPromises = imgList.value.map(img => uploadImage(img));
const results = await Promise.all(uploadPromises);
return results.map(res => res.data?.url || res.data);
};
// 提交实况
const submitStatus = async () => {
if (!statusDesc.value.trim()) {
uni.showToast({ title: '请输入实况描述', icon: 'none' });
return;
}
if (!activityId.value) {
uni.showToast({ title: '活动ID缺失', icon: 'none' });
return;
}
uni.showLoading({ title: '提交中...', mask: true });
try {
// 先上传图片
const imageUrls = await uploadAllImages();
// 提交实况
const liveData = {
content: statusDesc.value.trim(),
images: imageUrls,
location: location.value,
longitude: longitude.value,
latitude: latitude.value
};
await addActivityLive(activityId.value, liveData);
// 同时签到
await signinActivityLive(activityId.value, {
location: location.value,
longitude: longitude.value,
latitude: latitude.value
});
uni.hideLoading();
uni.showToast({ title: '添加成功', icon: 'success' });
uni.$emit('refreshLiveList');
setTimeout(() => {
uni.navigateBack();
}, 1500);
} catch (err) {
uni.hideLoading();
uni.showToast({ title: err || '提交失败', icon: 'none' });
}
};
</script>
<style scoped>
/* 页面整体样式 */
.add-status-page {
width: 100%;
height: 100vh;
background-color: #f5f5f5;
display: flex;
flex-direction: column;
}
/* 主体内容 */
.content {
flex: 1;
padding: 15px 0px;
}
/* 表单项通用样式 */
.form-item {
background-color: #fff;
padding: 15px;
border-radius: 8px;
margin-bottom: 15px;
}
.label {
font-size: 16px;
font-weight: 500;
margin-bottom: 10px;
display: block;
}
/* 输入框样式 */
.textarea {
width: 100%;
min-height: 120px;
padding: 10px;
border: 1px solid #eee;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
}
.count {
text-align: right;
font-size: 12px;
color: #999;
margin-top: 5px;
display: block;
}
/* 图片上传区域 */
.upload-area {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.uploaded-img {
width: 80px;
height: 80px;
position: relative;
}
.uploaded-img image {
width: 100%;
height: 100%;
border-radius: 4px;
}
.del-img {
position: absolute;
top: -5px;
right: -5px;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
background-color: #ff4444;
color: #fff;
border-radius: 50%;
font-size: 16px;
}
.upload-btn {
width: 80px;
height: 80px;
border: 1px dashed #ddd;
border-radius: 4px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.plus-icon {
font-size: 24px;
color: #999;
margin-bottom: 5px;
}
.tips {
font-size: 12px;
color: #999;
}
/* 定位区域 */
.location-area {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 0;
}
.location-text {
font-size: 14px;
color: #333;
}
.re-locate {
font-size: 14px;
color: #007aff;
display: flex;
align-items: center;
}
.re-icon{
color: #007aff;
}
/* .re-locate::before {
content: '';
width: 16px;
height: 16px;
background: url(/static/logo.png) no-repeat center;
background-size: 100%;
margin-right: 5px;
} */
/* 提交按钮 */
.submit-btn-area {
padding: 15px;
}
.submit-btn {
width: 100%;
height: 48px;
line-height: 48px;
background-color: #007aff;
color: #fff;
border: none;
border-radius: 8px;
font-size: 16px;
}
</style>