代码示例
<template>
<scroll-view class="org-container" scroll-y enhanced :show-scrollbar="false">
<view class="com-container">
<up-navbar :placeholder="true" :autoBack="true" title="课程表"></up-navbar>
<view class="" style="color: #777;margin-top: 36rpx;text-align: center;">
</view>
<image src="https://xs-face.oss-cn-shanghai.aliyuncs.com/202507/1950904723374788609_875364.jpg" mode="aspectFill"
style="width: 100%;height: 700rpx;"></image>
<view class="btn" v-if="identity == 1">
<up-button type="primary" size="small" text="上传课程表" @click="chooseImage"></up-button>
</view>
</view>
</scroll-view>
</template>
<script setup>
import {
ref,
reactive,
getCurrentInstance
} from 'vue';
import {
onShow,
onLoad,
} from '@dcloudio/uni-app'
import httpUtil from '../../common/httpUtil.js'
const {
proxy
} = getCurrentInstance()
let identity = uni.getStorageSync('identity')
function chooseImage() {
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
let url = res.tempFilePaths[0]
console.log(url);
uploadImage(url)
},
fail: (err) => {
console.error('选择图片失败', err);
}
});
}
function uploadImage(url) {
uni.showLoading({
title: '上传中...'
});
uni.uploadFile({
url: 'http://localhost:8997/file/upload',
filePath: url,
name: 'file',
header: {
'Content-Type': 'multipart/form-data',
'token': uni.getStorageSync('token')
},
formData: {},
success: (uploadRes) => {
uni.hideLoading();
if (uploadRes.statusCode === 200) {
const data = JSON.parse(uploadRes.data);
console.log('上传成功', data);
if (data.code == 200) {
dealUpload(data.data)
}
} else {
uni.showToast({
title: '上传失败',
icon: 'none'
});
}
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: '上传失败',
icon: 'none'
});
console.error('上传失败', err);
}
});
}
function dealUpload(url) {
httpUtil('/tbTimetable/add', 'POST', {
"classId": uni.getStorageSync('classId'),
"img": url,
}, (res) => {
console.log(res);
if (res.code == 200) {
uni.showToast({
title: '上传成功',
icon: 'success'
});
} else {
uni.showToast({
title: res.msg,
icon: 'none'
});
}
})
}
</script>
<style lang="scss">
.btn {
margin-top: 28rpx;
}
</style>