1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
| <template>
| <view></view>
| </template>
|
| <script>
| export default {
| data() {
| return {
| /*需要返回的图片*/
| imageList:[]
| };
| },
| props: {
| num: {
| type: Number,
| default: 9
| },
| file_type: {
| type: String,
| default: "image"
| },
| // 是否保留原图
| is_original: {
| type: Boolean,
| default: false
| }
| },
| onLoad() {},
| mounted() {
| this.chooseFileFunc();
| },
| methods: {
| chooseFileFunc() {
| if (this.file_type == 'image') {
| this.chooseImageFunc();
| } else if (this.file_type == 'video') {
| this.chooseVideoFunc();
| }
| },
| /*打开相机或者相册,选择图片*/
| chooseImageFunc() {
| let self=this;
| uni.chooseImage({
| count: self.num || 9, //默认9
| sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
| sourceType: ['album','camera'], //从相册选择
| success: function(res) {
| self.uploadFile(res.tempFilePaths);
| },
| fail:function(res){
| self.$emit('getImgs',null);
| },
| complete:function(res){
|
| }
| });
| },
|
| /*打开相机或者相册,选择视频*/
| chooseVideoFunc() {
| let self=this;
| uni.chooseVideo({
| sourceType: ['album','camera'], //从相册选择
| success: function(res) {
| self.uploadFile([res.tempFilePath]);
| },
| fail:function(res){
| self.$emit('getImgs',null);
| },
| complete:function(res){
|
| }
| });
| },
|
| /*上传图片*/
| uploadFile: function(tempList) {
| let self = this;
| let i = 0;
| let img_length = tempList.length;
| let params = {
| token: uni.getStorageSync('token'),
| app_id: self.getAppId(),
| file_type: self.file_type,
| is_original: self.is_original
| };
| uni.showLoading({
| title:'上传中'
| })
| tempList.forEach(function(filePath, fileKey) {
| uni.uploadFile({
| url: self.websiteUrl + '/index.php?s=/api/file.upload/image',
| filePath: filePath,
| name: 'iFile',
| formData: params,
| success: function(res) {
| let result = typeof res.data === 'object' ? res.data : JSON.parse(res.data);
| if (result.code === 1) {
| self.imageList.beforeFilePath = filePath;
| self.imageList.push(result.data);
| }
| },
| complete: function() {
| i++;
| if (img_length === i) {
| uni.hideLoading()
| // 所有文件上传完成
| self.$emit('getImgs',self.imageList);
| }
| }
| });
| });
| }
| }
| };
| </script>
|
| <style></style>
|
|