唐人同学 發表於 2021-8-4 15:52:00

uni-app 使用API中的uni.chooseImage 上传照片以及uni.previewImage图片预览(身份证照片为例)

<p>在uni-app项目中通常需要用户上传照片,如下图所示:<br>
<img src="https://img2020.cnblogs.com/blog/2348167/202108/2348167-20210804154016262-920754676.png"><img src="https://img2020.cnblogs.com/blog/2348167/202108/2348167-20210804154749669-377364367.png"></p>
<p>其中部分参数为本项目中请求接口时所需要的,可以根据需求自行修改对应的参数等,代码如下:</p>
<pre><code>&lt;template&gt;
        &lt;div class="container"&gt;
                &lt;div class="user-info"&gt;
                        &lt;div class="id-info"&gt;身份证照片&lt;/div&gt;
                        &lt;div class="tips"&gt;*请上传本人的身份证照片(单张照片限制1M以内)&lt;/div&gt;
                        &lt;div&gt;
                                &lt;img class="img-info" :src="card_front ? card_front : img_url+'/id-f.png'" @click="upLondImg('cardFront',card_front)"&gt;
                        &lt;/div&gt;
                        &lt;div&gt;
                                &lt;img class="img-info" :src="card_back ? card_back : img_url+'/id-z.png'" @click="upLondImg('cardBack',card_back)"&gt;
                        &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="user-info item"&gt;
                        &lt;div class="id-info"&gt;个人证件照&lt;/div&gt;
                        &lt;div class="tips"&gt;*请上传近期白底或蓝底免冠正面证件照&lt;/div&gt;
                        &lt;div&gt;
                                &lt;img class="upload" :src="stu_card ? stu_card : img_url+'/jh.png'" @click="upLondImg('personalPhoto',stu_card)"&gt;
                        &lt;/div&gt;
                &lt;/div&gt;
                &lt;button class="submit-btn" @click="subBtn" v-if="stu_info.label != 2"&gt;提交&lt;/button&gt;
        &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
        export default {
                data() {
                        return {
                                img_url: this.$http.img_url(),
                                img: '',
                                Path: {},
                                stu_card: '',
                                card_back: '',
                                card_front: '',
                                stu_info: null
                        }
                },
                onLoad(option) {
                        this.stu_info = JSON.parse(option.photo)
                        this.card_front = this.stu_info.card_front
                        this.card_back = this.stu_info.card_back
                        this.stu_card = this.stu_info.stu_card
                },
                methods: {
                        subBtn() {
                                if (!this.card_front) {
                                        uni.showToast({
                                                title: '请上传身份证正面照',
                                                icon: 'none'
                                        })
                                        return
                                }
                                if (!this.card_back) {
                                        uni.showToast({
                                                title: '请上传身份证反面照',
                                                icon: 'none'
                                        })
                                        return
                                }
                                if (!this.stu_card) {
                                        uni.showToast({
                                                title: '请上传个人证件照',
                                                icon: 'none'
                                        })
                                        return
                                }
                                this.$http.post('personal/identity', {
                                        stu_card: this.stu_card,
                                        card_back: this.card_back,
                                        card_front: this.card_front
                                }).then(res =&gt; {
                                        if (res.code == 200) {
                                                uni.showToast({
                                                        title: '提交成功',
                                                        success: () =&gt; {
                                                                setTimeout(() =&gt; {
                                                                        uni.navigateBack({
                                                                                delta: 1
                                                                        });
                                                                }, 1000);
                                                        }
                                                })
                                        }
                                })
                        },
                        upLondImg(path,src) {
                                if (this.stu_info.label == 2) {
                                       uni.previewImage({
                                                urls:
                                        })
                                        return
                                }
                               
                                this.$http.uniApi({
                                        events: uni.chooseImage
                                }, {
                                        count: 1,
                                        sizeType: ['original', 'compressed']
                                }).then(res =&gt; {
                                        this.$http.upLoad({
                                                img: res.tempFilePaths,
                                                path: path
                                        }).then(res =&gt; {
                                                if (path == 'cardFront') {
                                                        this.card_front = JSON.parse(res).data.url
                                                } else if (path == 'cardBack') {
                                                        this.card_back = JSON.parse(res).data.url
                                                } else {
                                                        this.stu_card = JSON.parse(res).data.url
                                                }
                                        })
                                })

                        }
                }
        }
&lt;/script&gt;
</code></pre>
<p>通过以上的设置就可以完整的实现身份证照片上传以及图片的预览</p><br><br>
来源:https://www.cnblogs.com/jimyking/p/15099277.html
頁: [1]
查看完整版本: uni-app 使用API中的uni.chooseImage 上传照片以及uni.previewImage图片预览(身份证照片为例)