| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <template>
- <view class="gzms-wrap">
- <view class="photo_btn" @click="chooseImage">拍照</view>
- <view class="photo_list">
- <PreviewPhoto type="add" @imagedelete="imagedelete" :imageList="imageList" />
- </view>
- </view>
- </template>
- <script>
- import PreviewPhoto from '@/pages/maintenance/check/components/PreviewPhoto.vue'
- export default {
- components: {
- PreviewPhoto
- },
- props: {
- imageUrl: {
- type: Array,
- default: () => []
- }
- },
- watch: {
- imageUrl: {
- handler(newVal) {
- let arr = JSON.parse(JSON.stringify(newVal));
- this.imageList = arr;
- },
- deep: true,
- immediate: true
- }
- },
- data() {
- return {
- imageList: [],
- }
- },
- methods: {
- imagedelete(index) {
- this.imageList.splice(index, 1);
- },
- getImageData() {
- return this.imageList || [];
- },
- // *** 新增拍照
- chooseImage() {
- const _this = this
- if (_this.imageList.length >= 9) {
- _this.$refs.uToast.show({
- type: "warning",
- message: "最多上传9张照片",
- })
- return
- }
- uni.chooseImage({
- count: 9, //默认9
- sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
- sourceType: ['camera'], //从相册选择
- success: function(res) {
- uni.showLoading({
- title: '上传中'
- })
- _this.uploadFile(res.tempFilePaths).then(res => {
- res.forEach(item => {
- let fileNames = item.storePath.split('/')
- let url = _this.apiUrl +
- '/main/file/getFile?objectName=' + item.storePath +
- '&fullfilename=' + fileNames[fileNames.length -
- 1]
- let images = _this.imageList;
- // 不能存储超过 9 张
- if (images.length < 9) {
- _this.imageList.push(url);
- }
- })
- uni.hideLoading();
- }).catch((error) => {
- console.log(error, 'errorerrorerror')
- uni.hideLoading();
- })
- }
- });
- },
- uploadFile(list) {
- let PromiseAll = []
- const apiUrl = this.apiUrl
- const token = uni.getStorageSync("token"); //取存本地的token
- list.forEach(item => {
- PromiseAll.push(
- new Promise((resolve, reject) => {
- uni.uploadFile({
- url: apiUrl + '/main/file/upload',
- filePath: item,
- name: 'multiPartFile',
- header: {
- authorization: token
- },
- success: (uploadFileRes) => {
- if (uploadFileRes.statusCode == 500) {
- uni.hideLoading();
- return;
- }
- let data = JSON.parse(uploadFileRes.data)
- resolve(data.data)
- },
- fail: (err) => {
- console.log(err, '123456');
- // 上传请求本身失败(如网络问题等),直接 reject
- uni.hideLoading();
- reject(err)
- }
- });
- }),
- )
- })
- return Promise.all(PromiseAll)
- },
- }
- }
- </script>
- <style scoped lang="scss">
- .photo_btn {
- background-color: #157a2c;
- width: 140rpx;
- height: 60rpx;
- line-height: 60rpx;
- font-size: 24rpx;
- color: #fff;
- text-align: center;
- border-radius: 8rpx;
- margin-bottom: 12rpx;
- display: inline-block;
- }
- </style>
|