queenwuli
2021-01-19 a3a48da30bdea132d2bfbb47fa2ccf83f1937c45
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
/**
 * 拍照,从相册选择图片
 * */
 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.upload(data.tempFilePaths[0])
                // this.pathToBase64(data.tempFilePaths[0])
            }
        })
    },
    // 从相册选择
    pickPicture(){
        uni.chooseImage({
            count: 1,
            sourceType: ['album'],
            success: (data) => {
                this.upload(data.tempFilePaths[0])
                // 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: file
        // }, 'POST').then((res) => {
        //     console.log(res)
        //     if(res.status == 200){
        //         this.callback && this.callback(res.mapInfo.file)
        //     }else{
        //         toast.info(res.info)
        //     }
        // })
        uni.uploadFile({
            url: httpUtils.baseUrl+'/api/common/uploadImg',
            filePath: file,
            name: 'file',
            success: (res) => {
                let result = JSON.parse(res.data);
                if(result.status == 200){
                    this.callback && this.callback(result.mapInfo.file)
                }else{
                    toast.info(result.info)
                }
            }
        })
    }
 }
 
 export default ImageUploadUtils