gx
queenwuli
2021-01-15 4b80c98ef5fda8d6358778f2efe8bb35cb20ccf9
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
/**
 * 拍照,从相册选择图片
 * */
 import httpUtils from './httpUtils.js';
 import toast from './toast.js';
 const ImageUploadUtils = {
    show(callback){
        this.callback = callback;
        uni.showActionSheet({
            itemList: ['拍照', '从相册选择'],
            success:  (res) => {
                if(res.tapIndex == 0){
                    this.takingPicture()
                }else if(res.tapIndex == 1){
                    this.pickPicture()
                }
            }
        });
    },
    // 拍照
    takingPicture(){
        uni.chooseImage({
            count: 1,
            sourceType: ['camera'],
            success: (data) => {
                this.pathToBase64(data.tempFilePaths[0])
            }
        })
    },
    // 从相册选择
    pickPicture(){
        uni.chooseImage({
            count: 1,
            sourceType: ['album'],
            success: (data) => {
                this.pathToBase64(data.tempFilePaths[0])
            }
        })
    },
    pathToBase64(path){
        //#ifdef APP-PLUS
        plus.io.resolveLocalFileSystemURL(path, (entry) => {
            entry.file((file) => {
                var fileReader = new plus.io.FileReader();
                fileReader.readAsDataURL( file );
                fileReader.onloadend = (evt) =>  {
                    this.upload(evt.target.result)
                }
            })
        })
        //#endif  
    },
    upload(file){
        httpUtils.request('/api/common/uploadPhotoBase64', {
            base64: 'data:image/jpeg;base64,'+ file
        }, 'POST').then((res) => {
            if(res.status == 200){
                this.callback && this.callback(res.mapInfo.file)
            }else{
                toast.info(res.info)
            }
        })
    }
 }
 
 export default ImageUploadUtils