353 lines
8.3 KiB
Vue
353 lines
8.3 KiB
Vue
<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" class="upload-img"></image>
|
||
<image src="http://47.104.199.163:9090/images/imgs/delete.png" class="upload-delete" @click="deleteImage(index)"></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, uploadFile } 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 deleteImage = (index) => {
|
||
imgList.value.splice(index, 1);
|
||
};
|
||
|
||
// 重新定位
|
||
// 定位并获取具体地点名称
|
||
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 => uploadFile(img));
|
||
const results = await Promise.all(uploadPromises);
|
||
return results.map(res => res.url);
|
||
};
|
||
|
||
// 提交实况
|
||
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 .upload-img{
|
||
width: 100%;
|
||
height: 100%;
|
||
border-radius: 4px;
|
||
}
|
||
.uploaded-img .upload-delete{
|
||
position: absolute;
|
||
top: -5px;
|
||
right: -5px;
|
||
width: 32rpx;
|
||
height: 32rpx;
|
||
border-radius: 3rpx;
|
||
padding: 2px;
|
||
}
|
||
.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; */
|
||
background-color: #F3F3F3;
|
||
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> |