Administrator
2021-06-17 a813edb983b7b099ac58090b28115b1fd4b142a0
头像查看/上传优化
1 files modified
125 ■■■■ changed files
pages/myself/myself.vue 125 ●●●● patch | view | raw | blame | history
pages/myself/myself.vue
@@ -175,10 +175,10 @@
                var that = this;
                uni.showActionSheet({
                    // itemList按钮的文字接受的是数组
                    itemList: ["查看头像","更换头像"],
                    itemList: ["查看头像","拍摄","从相册选择"],
                    success(e){
                        var index = e.tapIndex;
                        if(index === 0){
                        if(index == 0){
                            // 用户点击了预览当前图片
                            // 可以自己实现当前头像链接的读取
                            let url  = that.useimg;
@@ -189,66 +189,73 @@
                                urls: arr
                            })
                        }
                        if(index === 1){
                            // 用户点击了从图库上传
                            uni.chooseImage({
                                count: 1, //可选择数量,默认9
                                sizeType: ['compressed','original'], //上传压缩图,原图
                                sourceType: ['album','camera'], //从相册选择或从使用相机
                                success: async (res) => {
                                    if(res.tempFiles[0].size > 10 * 1024 * 1000){  //上传图片大小限制
                                        uni.showToast({
                                            title: "照片大小不能10MB",
                                            icon: "none"
                                        })
                                        return
                                    }
                                    var tempFilePath = res.tempFilePaths;
                                    try{
                                        uni.showLoading({
                                            title:"上传中...",
                                            mask:true
                                        })
                                        // 文件上传
                                        uni.uploadFile({
                                            // url: 'https://web.byisf.com/api/blade-jfpts/depl/put-depl', //仅为示例,非真实的接口地址
                                            url: 'http://web.byisf.com:82/blade-jfpts/depl/put-depl',
                                            filePath: tempFilePath[0],
                                            name: 'file',
                                            formData: {
                                                'user': 'test'  // 上传附带参数
                                            },
                                            success: (data) => {
                                                that.useimg = JSON.parse(data.data).data;
                                                //修改用户头像信息
                                                uni.request({
                                                    url:"http://s16s652780.51mypc.cn/api/blade-user/updateUserInfo",
                                                    method:"POST",
                                                    data:{
                                                        id: that.$store.state.puserID,
                                                        avatar:that.useimg
                                                    },
                                                    success:(res)=> {
                                                        if(res.data.code==200){
                                                            uni.showToast({
                                                                title: '上传成功!',
                                                                duration: 2000
                                                            });
                                                        }
                                                    }
                                                })
                                            }
                                        });
                                    }catch(e){
                                        console.log(e);
                                    }finally{
                                        uni.hideLoading();
                                    }
                                }
                            })
                        if(index == 1 || index==2 ){
                            //拍照或从图库上传
                            that.uploadPictrue(index);
                        }
                    }
                });
            },
            //图片上传
            uploadPictrue(e){
                var that = this;
                var sourceTypeArr = [];
                e==1 ? sourceTypeArr.push("camera") : sourceTypeArr.push("album")
                uni.chooseImage({
                    count: 1, //可选择数量,默认9
                    sizeType: ['compressed','original'], //上传压缩图,原图
                    sourceType: sourceTypeArr, //从相册选择
                    success: async (res) => {
                        if(res.tempFiles[0].size > 10 * 1024 * 1000){  //上传图片大小限制
                            uni.showToast({
                                title: "照片大小不能10MB",
                                icon: "none"
                            })
                            return
                        }
                        var tempFilePath = res.tempFilePaths;
                        try{
                            uni.showLoading({
                                title:"上传中...",
                                mask:true
                            })
                            // 文件上传
                            uni.uploadFile({
                                // url: 'https://web.byisf.com/api/blade-jfpts/depl/put-depl', //仅为示例,非真实的接口地址
                                url: 'http://web.byisf.com:82/blade-jfpts/depl/put-depl',
                                filePath: tempFilePath[0],
                                name: 'file',
                                formData: {
                                    'user': 'test'  // 上传附带参数
                                },
                                success: (data) => {
                                    that.useimg = JSON.parse(data.data).data;
                                    //修改用户头像信息
                                    uni.request({
                                        url:"http://s16s652780.51mypc.cn/api/blade-user/updateUserInfo",
                                        method:"POST",
                                        data:{
                                            id: that.$store.state.puserID,
                                            avatar:that.useimg
                                        },
                                        success:(res)=> {
                                            if(res.data.code==200){
                                                uni.showToast({
                                                    title: '上传成功!',
                                                    duration: 2000
                                                });
                                            }
                                        }
                                    })
                                }
                            });
                        }catch(e){
                            console.log(e);
                        }finally{
                            uni.hideLoading();
                        }
                    }
                })
            }
        },
    }