Files
2023-08-04 13:14:48 +08:00

510 lines
12 KiB
Vue
Raw Permalink 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="container" :style="appThemeStyle">
<u--form :model="form" ref="uForm" :rules="rules">
<!-- 表单组件 -->
<view class="form-wrapper">
<u-form-item prop="content">
<u--textarea v-model="form.content" placeholder="这一刻的想法" :autoHeight="true" :maxlength="-1"
:border="false" :confirmType="null"></u--textarea>
</u-form-item>
<u-form-item prop="files">
<view class="upload-box">
<button @click="previewMdeia(item, index)" v-for="(item, index) in fileList1" :key="index" class="btn-upload u-reset-button">
<image class="upload-img" :src="item.thumb" mode="aspectFit"></image>
</button>
<button v-show="fileList1.length < 9 && (fileList1.length == 0 || fileList1[0].type!= 'video')" class="btn-upload u-reset-button" @click="onChooseMedia">
<u-icon name="plus"></u-icon>
</button>
</view>
</u-form-item>
<view class="a-space">
<!-- <u-icon name="photo" :size="32" style="margin-right: 50rpx;"></u-icon>
<u-icon name="play-right" :size="32"></u-icon> -->
</view>
<u-cell-group>
<!-- <u-cell icon="map" :title="form.address" :isLink="true" @click="onArea"></u-cell> -->
<u-cell icon="chat" :title="form.topic" :isLink="true" @click="onTopic"></u-cell>
<!-- <u-cell icon="bell" :title="form.toUserNames" :isLink="true" @click="onFollowers"></u-cell> -->
</u-cell-group>
</view>
</u--form>
<!-- 操作按钮 -->
<view class="footer">
<view class="btn-wrapper">
<u-button text="发布动态" color="linear-gradient(to bottom, #010002, #681752)" :disabled="disabled"
shape="circle" @click="handleSubmit()"></u-button>
</view>
</view>
<u-picker :show="showTopic" :columns="dict.topic" @confirm="confirmTopic"
@cancel="showTopic = false"></u-picker>
<u-picker :show="showFollowers" :columns="followersUsers" keyName="label" @confirm="confirmFollowers"
@cancel="showFollowers = false"></u-picker>
<!-- 地址选择器 -->
<liu-customize-sel ref="area" @change="chooseSuccess"> </liu-customize-sel>
</view>
</template>
<script>
import store from '@/store'
import platform from '@/core/platform'
import AvatarImage from '@/components/avatar-image'
import * as ArticleApi from '@/api/article.js'
import * as DictApi from '@/api/dict.js'
import * as UserFollowApi from '@/api/user-follow.js'
import * as UploadApi from '@/api/upload'
export default {
components: {
AvatarImage
},
data() {
return {
dict: null,
// 按钮禁用
disabled: false,
isRefresh: false,
// 最大上传图片数量
maxCount: 9,
// 临时图片 (用于上传)
tempFile: null,
// 表单数据
form: {
userInfo: null,
accept: 'image',
address: '添加地点',
topic: '参与话题',
toUsers: [],
toUserNames: '提醒谁看'
},
platform,
fileList1: [],
fileType: '',
// 验证规则
rules: {
'userInfo.name': {
type: 'string',
required: true,
message: '请填写姓名',
trigger: ['blur', 'change']
},
},
showTopic: false,
showFollowers: false,
followersUsers: []
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad() {
this.getDict()
this.getFollowersUsers()
},
onUnload(){
const { isRefresh } = this
const eventChannel = this.getOpenerEventChannel();
eventChannel.emit('reload', {isRefresh});
},
// 必须要在onReady生命周期因为onLoad生命周期组件可能尚未创建完毕
onReady() {
this.$refs.uForm.setRules(this.rules)
// 微信小程序需要用此写法
// this.$refs.datetimePicker.setFormatter(this.formatter)
},
methods: {
onChooseMedia() {
const app = this
if(this.fileList1.length == 0){
uni.showActionSheet({
itemList: ["上传视频", "上传照片"],
success({tapIndex}) {
if(tapIndex == 0){
uni.chooseMedia({
mediaType: ['video'],
maxDuration: 15,
count: 1,
success(media) {
console.log('media: ',media);
const {tempFilePath, thumbTempFilePath, size,width,height} = media.tempFiles[0]
if(size > 31457280){
uni.showToast({
title: "上传单个视频最大为30m",
icon: 'none'
})
return
}
uni.showLoading({
title: '正在上传',
mask: true
})
// 上传封面和视频
Promise.all([
UploadApi.uploadFile({
filePath: thumbTempFilePath,
fileType: 'image',
name: 'file'
}),
UploadApi.uploadFile({
filePath: tempFilePath,
fileType: 'video',
name: 'file'
})
]).then(([res1,res2])=>{
app.fileList1.push({
thumb: res1.data.url,
tempFilePath,
url: res2.data.url,
type: 'video',
width,
height
})
}).finally(()=>{
uni.hideLoading()
})
}
})
}else {
app.chooseImage()
}
}
})
}else {
app.chooseImage()
}
},
async chooseImage() {
const app = this
uni.chooseImage({
count: 9 - this.fileList1.length,
success({tempFiles}) {
app.uploadImage(tempFiles)
}
})
},
async uploadImage(tempFiles) {
uni.showLoading({
title: '正在上传',
mask: true
})
for (let i = 0; i < tempFiles.length; i++) {
console.log("result: ",result);
const result = await this.uploadFilePromise(tempFiles[i].path)
this.fileList1.push({
thumb: result.url,
url: result.url,
type: 'image'
})
}
uni.hideLoading()
},
previewMdeia(item , index){
if(item.type == 'image'){
const urls = this.fileList1.map(d => d.url)
console.log('urls: ',urls);
uni.previewImage({
urls,
current: index
})
}else {
uni.previewMedia({
sources: [{
url: item.url,
type: "video",
poster: item.thumb
}]
})
}
},
getDict() {
DictApi.listDictionary().then(res => {
this.dict = res.data;
})
},
getFollowersUsers(){
const app = this
UserFollowApi.listFollow().then(res => {
const arr = res.data.map(d => {
return {
value: d.shopId,
label: d.shopNickname
}
})
app.followersUsers = [arr]
})
},
// 选择微信地址
// #ifdef MP-WEIXIN
chooseAddress() {
const {
form,
$refs
} = this
uni.chooseAddress({
success(res) {
const names = $refs.sRegion.getOptionItemByNames(res)
form.name = res.userName
form.phone = res.telNumber
form.detail = res.detailInfo
form.region = names.length > 0 ? names : []
}
})
},
// #endif
// 确认修改
async handleSubmit() {
const app = this
if (app.disabled === true) return
this.$refs.uForm.validate().then(() => {
if(app.fileList1.length == 0){
return app.$toast('请上传图片或视频')
}
app.form.platform = platform
app.form.files = JSON.stringify(app.fileList1)
app.form.categoryId = 0
app.form.toUsers = JSON.stringify(app.form.toUsers)
app.disabled = true
ArticleApi.addArticle(app.form).then(result => {
app.$toast('发布成功')
app.isRefresh = true
setTimeout(function() {
uni.navigateBack({
isRefresh: true
})
}, 2000)
})
}).catch(errors => {
// uni.$u.toast('校验失败')
})
},
// 删除图片
deletePic(event) {
this[`fileList${event.name}`].splice(event.index, 1)
},
// 新增图片
async afterRead(event) {
// 上传的文件类型不是图片则限制只能传一个
if(event.name == '2'){
this.fileType = 'video'
this.maxCount = 1
this.form.accept = 'video'
}
if(event.name == '1'){
this.fileType = 'image'
}
// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
let lists = [].concat(event.file)
let fileListLen = this[`fileList${event.name}`].length
lists.map((item) => {
this[`fileList${event.name}`].push({
...item,
status: 'uploading',
message: '上传中'
})
})
for (let i = 0; i < lists.length; i++) {
console.log("result: ",result);
const result = await this.uploadFilePromise(lists[i].url)
let item = this[`fileList${event.name}`][fileListLen]
this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
status: 'success',
message: '',
url: result.url,
thumb: result.thumbUrl
}))
fileListLen++
}
},
uploadFilePromise(url) {
return new Promise((resolve, reject) => {
UploadApi.uploadFile({
filePath: url,
fileType: 'image',
name: 'file'
}).then(res => {
resolve(res.data)
})
})
},
onTopic(){
this.showTopic = true
},
confirmTopic(e){
this.form.topic = e.value[0]
this.showTopic = false
},
onArea() {
// this.$refs.area.open()
uni.chooseLocation({
latitude: 22.817072,
longitude: 108.369102,
success: function (res) {
console.log('位置名称:' + res.name);
console.log('详细地址:' + res.address);
console.log('纬度:' + res.latitude);
console.log('经度:' + res.longitude);
},
complete(res) {
console.log(res)
}
});
},
//地址选择成功
chooseSuccess(e) {
const data = e.value
this.form.address = `${data[0].label} ${data[1].label} ${data[2].label}`
},
onFollowers(){
this.showFollowers = true
},
confirmFollowers(e){
this.form.toUserNames = '提醒谁看'
this.toUsers = []
this.form.toUserNames = e.value[0].label
this.form.toUsers.push(e.value[0].value)
this.showFollowers = false
}
}
}
</script>
<style>
page {
background: #ffffff;
}
</style>
<style lang="scss" scoped>
.container {}
.page-title {
width: 94%;
margin: 0 auto;
padding-top: 40rpx;
font-size: 28rpx;
color: rgba(69, 90, 100, 0.6);
}
.interest {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
.u-page__tag-item {
margin-bottom: 10rpx;
white-space: nowrap;
}
}
.form-wrapper {
margin: 20rpx auto 20rpx auto;
padding: 0 40rpx;
width: 700rpx;
border-radius: 16rpx;
background: #fff;
.a-space {
min-height: 50rpx;
height: auto;
display: flex;
// padding: 30rpx 0;
}
.video{
margin-top: 20rpx;
width: 150rpx;
display: flex;
font-size: 26rpx;
color: #999999;
}
}
/* 底部操作栏 */
.footer {
margin-top: 50rpx;
padding-bottom: 50rpx;
.btn-wrapper {
width: 300rpx;
margin: auto;
// display: flex;
// align-items: center;
padding: 0 20rpx;
}
.btn-item {
flex: 1;
font-size: 28rpx;
height: 86rpx;
color: #fff;
border-radius: 50rpx;
display: flex;
justify-content: center;
align-items: center;
}
.btn-item-wechat {
background: #0ba90b;
margin-bottom: 26rpx;
}
.btn-item-main {
background: linear-gradient(to right, $main-bg, $main-bg2);
color: $main-text;
// 禁用按钮
&.disabled {
opacity: 0.6;
}
}
}
.upload-box{
display: flex;
// justify-content: space-between;
flex-wrap: wrap;
&::after {
content: '';
width: 150rpx;
display: block;
}
.btn-upload{
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 150rpx;
height: 170rpx;
// background: rgba(15, 0, 32, 0.5);
border: 2rpx solid #e8e8e8;
border-radius: 10rpx;
// overflow: hidden;
margin: 0 20rpx 0 0;
position: relative;
.btn-del{
height: 20rpx;
}
.btn-upload-plus{
width: 72rpx;
height: 72rpx;
}
.upload-img{
width: 150rpx;
height: 150rpx;
}
}
}
</style>