| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <template>
- <view class="imgUpload">
- <uni-file-picker
- v-model="fileList"
- :limit='limit'
- :countLength='1'
- :auto-upload="false"
- fileMediatype="image"
- @select="select"
- @delete="deleteFn"
- mode="grid"
- />
- </view>
- </template>
-
- <script>
- /*
- * imgUpload 上传
- */
- import { uploadImage } from '../api/upload.js'
- import { getToken } from '@/utils/auth'
- // import store from '@/store/index.js'
- // let { state:{ login } } = store
- let intervalTime = null
- const app = getApp()
- export default {
- name:"imgUpload",
- data(){
- return {
- fileList:[]
- }
- },
- props:{
- value:{
- type:Array,
- default:() => []
- },
- disabled:{
- type:Boolean,
- default:() => false
- },
- limit:{
- type:Number,
- default:1
- }
- },
- watch:{
- value:{
- immediate:true,
- handler(val){
- if(val.length){
- this.initData()
- }
- }
- }
- },
- // created(){
- // // this.initData()
- // },
- methods:{
- // 初始化数据
- initData(){
- intervalTime = setInterval(() => {
- if(this.fileList.length != 0){
- clearInterval(intervalTime)
- return;
- }
- let arr = [...this.value]
- // TODO
- if(arr.length != 0){
- let imgTitem = arr.map(img => {
- return {
- "name":img,
- "extname":'image',
- "url":this.imgFilter(img),
- "currentUrl":img,
- }
- })
- this.fileList = [...this.fileList,...imgTitem]
- }
- },500)
- },
- // 过滤图片
- imgFilter(url){
- // console.log(url,"url")
- let str = url.split('/slm')[1]
- let currentUrl = app.globalData.requestUrl + str
- return currentUrl
- },
- // 文件上传
- select(e){
- // loading
- uni.showLoading({
- title: '上传中'
- });
- let { tempFilePaths } = e
- // console.log(tempFilePaths[0])
- uni.uploadFile({
- url:getApp().globalData.requestUrl + uploadImage,
- filePath: tempFilePaths[0],
- header:{
- Authorization:getToken() || '',
- },
- name: 'file',
- // TODO
- success:(res) => {
- let { data } = res
- uni.showToast({
- title:this.$t('requst.succes'),
- icon:'none',
- duration:1000
- })
- let imgpath = JSON.parse(data).data
- let obj = {
- "name":imgpath,
- "extname":'image',
- "url":this.imgFilter(imgpath),
- "currentUrl":imgpath,
- }
-
- this.fileList = [...this.fileList,obj]
- // let valueArr = [...this.value,imgpath]
- this.$emit('update:value',this.fileList);
- uni.hideLoading();
- },
- fail:(err) => {
- uni.showToast({
- title:this.$t('requst.fail'),
- icon:'none',
- duration:1000
- })
- this.fileList.pop()
- this.$emit('update:value',this.fileList)
- uni.hideLoading()
- }
- })
- },
- // 文件删除
- deleteFn(e){
- let { tempFilePath,tempFile:{ currentUrl } } = e
- let imgArr = [...this.value]
- this.fileList = this.fileList.filter(item => {
- return item.url != tempFilePath
- })
- imgArr = imgArr.filter(item => {
- return currentUrl.indexOf(item) == -1
- })
- this.$emit('update:value',imgArr)
- }
- }
- }
- </script>
-
- <style scoped lang="scss">
- .dataCharts{
- width: 100%;
- height:100%;
- }
- </style>
|