弹框上传图片

在这里插入图片描述
在这里插入图片描述

	 <view class='title red'>
    提示:可绑定非车主本人银行卡
  </view>
  <!-- 车主身份证 -->
  <view class='Xbox'>
    <view class='xian'></view>
    <view class="note-title">车主身份证</view>
  </view>
  <view class='id-card-example'>
    <view class='upload-item' bindtap='tan' data-type="idCardFace">
      <block wx:if="{{ idCardFace=='' }}">
        <image src='/image/touxiang.png' mode="widthFix" />
      </block>
      <block wx:else>
        <image src='{{idCardFace}}' mode="scaleToFill" />
      </block>
      <text class='ziliao-txt'>点击上传带头像一面</text>
    </view>
    <view class='upload-item' bindtap='tan' data-type="idCardBack">

      <block wx:if="{{ idCardBack=='' }}">
        <image src='/image/guohui.png' mode="widthFix" />
      </block>
      <block wx:else>
        <image src='{{idCardBack}}' mode="scaleToFill" />
      </block>
      <text class='ziliao-txt'>点击上传带国徽一面</text>
    </view>
  </view>
  <!-- 用到了一个自定义弹框组件 -->
  <middle_tan id="easyModal" bindgetSon="getSon">	
  <view class='yuanjiao'>
    <image src='{{cc}}' mode='widthFix' />	//弹框出来的图片 根据点击的正反面弹出正确图片示例
    
    <view class='upload' bindtap='chooseImage'>确定</view>
    <view class='w60'>
      <image bindtap='hid' src='http://etc-app.oss-cn-beijing.aliyuncs.com/image_201907261527404661.png' mode='widthFix' />
    </view>
  </view>
</middle_tan>
/* upload */
.id-card-example {
 display: flex;
 justify-content: space-between;
 flex-wrap: wrap;
  padding-left: 48rpx; 
padding-right: 48rpx; 
box-sizing: border-box;
}
.upload-item {
  width: 307rpx;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}
.upload-item image {
  width:100%; 
  margin-bottom: 24rpx;
  height:200rpx;
  display: block;
}
.ziliao-txt {
  font-size: 24rpx;
	font-weight: normal;
	font-stretch: normal;
	/* letter-spacing: 1rpx; */
	color: #000000;
	opacity: 0.4;
}
.mt {
  margin-top: 36rpx; 
}

image {
  width: 100%;
  display: block;

}
.w60 {
  width:60rpx;
  height: 60rpx;
  position: absolute;
  top: -77rpx;
  right: 0;
}
/* ?/? */
.Xbox {
  overflow: hidden;
  margin-top: 36rpx;
  margin-bottom: 35rpx;
}
.xian {
  width: 4rpx;
  height: 34rpx;
  background-color:#15bf5d; 
  margin-left: 24rpx;
  margin-right: 32rpx;
  float: left;
  margin-top:11rpx;

}
.note-title {
  display: inline-block;
  font-size: 36rpx;
	color: #000000;
	opacity: 0.7;
  font-family: PingFang-SC-Bold;
}
/* tankuang */
.yuanjiao {
  width: 654rpx;
	height: 864rpx;
	background-color: #ffffff;
	box-shadow: 0px 5rpx 16rpx 0px 
		rgba(197, 197, 197, 0.6);
	border-radius: 20rpx;
  padding-top: 38rpx;
  position: relative;
}
.yuanjiao> image {
    width: 654rpx;
    height: 603rpx;
    display: block;

}
.upload {
  width: 606rpx;
	height: 90rpx;
	background-image: linear-gradient(90deg, 
		#15bf69 0%, 
		#15bf5d 100%), 
	linear-gradient(
		#8d30ff, 
		#8d30ff);
	background-blend-mode: normal, 
		normal;
	box-shadow: 0px 5rpx 10rpx 0px 
		rgba(21, 191, 93, 0.6);
	border-radius: 45rpx;
  margin: 0 auto;
  	font-size: 42rpx;
	font-weight: normal;
	font-stretch: normal;
	line-height: 90rpx;
	letter-spacing: 2rpx;
	color: #ffffff;
  text-align: center;
  margin-top: 34rpx;
}
let tan;			//最外部声明
	data:{
		 idCardFace: '',
	   	 idCardFaceId: '',
	  	 idCardBack: '',
	     idCardBackId: '',
   		 cc: '',
	},
	onLoad: function(query) {
		tan = this.selectComponent("#easyModal");
	},
	getSon(e){
		console.log('子组件传给父组件值'+e)
	}
	hid() {
    tan.hide()
  },
   tan(e) {
    let type = e.currentTarget.dataset.type
    switch (type) {
      case 'idCardFace':
        this.setData({
          cc: 'http://etc-app.oss-cn-beijing.aliyuncs.com/image_201905201752398374.png'
        })
        tan.show()
        break;
      case 'idCardBack':
        this.setData({
          cc: 'http://etc-app.oss-cn-beijing.aliyuncs.com/image_201907261524206047.jpg'
        })
        tan.show()
        break;
    },
     chooseImage() {
    let that = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        tan.hide()
        wx.showLoading({
          title: '上传中',
          mask: true
        })
        wx.uploadFile({
          url: getApp().globalData.serverHost + "api/upload",
          filePath: tempFilePaths[0],
          name: 'image',
          success(res) {
            wx.hideLoading()
            console.log(res)
            const data = JSON.parse(res.data)
            console.log(data)
            //do something
            switch (that.data.cc) {
              case 'http://etc-app.oss-cn-beijing.aliyuncs.com/image_201905201752398374.png':
                that.setData({
                  idCardFace: data.data.url,
                  idCardFaceId: data.data.id
                })
                break;
              case 'http://etc-app.oss-cn-beijing.aliyuncs.com/image_201907261524206047.jpg':
                that.setData({
                  idCardBack: data.data.url,
                  idCardBackId: data.data.id
                })
                break;
            }
          },
          fail(res1) {
            wx.hideLoading()
            wx.showModal({
              title: "提示",
              content: '上传失败:' + res1.errorMessage
            });
          }
        })
      }
    })
  },
  },
	自定义组件middle_tan
	<view bindtap='close' class='container' hidden='{{isShow}}'>
    <slot data-id="slo">
    </slot>
</view>

	.container {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 11;
    /* background-color:rgba(116,116,116,.7); */
    background-color:rgba(0,0,0,.6);
    display: flex;
    justify-content: center;
    align-items: center;
}
	// component/middle_tan/middle.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    show() {
      this.setData({
        isShow: false
      })
    },
    hide() {
      this.setData({
        isShow: true
      })
    },
    close(e){
      console.log(e)
      if(e.target.dataset.id !='slo') {
        this.setData({
          isShow:true
        })
        this.triggerEvent('getSon', false)
      }
    }
  }
})
要实现用thinkPHP接收element上传的图片,需要在前端使用Element-ui的上传组件,同时在后端用thinkPHP接收上传的图片并进行处理。 首先,在前端的表单弹框中加入上传组件,代码如下: ``` <template> <el-dialog title="上传图片" :visible.sync="dialogVisible"> <el-upload action="your-upload-url" :headers="{ 'Authorization': 'Bearer ' + token }" :on-success="handleSuccess" :on-error="handleError" :before-upload="beforeUpload" :file-list="fileList" list-type="picture" :data="{ 'type': 'image' }" > <el-button slot="trigger" size="small" type="primary">选择图片</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过5MB</div> </el-upload> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="submitForm">确 定</el-button> </div> </el-dialog> </template> <script> export default { data() { return { dialogVisible: false, fileList: [], token: localStorage.getItem('token') }; }, methods: { handleSuccess(response, file, fileList) { console.log(response); this.fileList = fileList; this.$message.success('上传成功'); }, handleError(error, file, fileList) { console.log(error); this.$message.error('上传失败'); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt5M = file.size / 1024 / 1024 < 5; if (!isJPG) { this.$message.error('上传图片只能是 JPG/PNG 格式!'); } if (!isLt5M) { this.$message.error('上传图片大小不能超过 5MB!'); } return isJPG && isLt5M; }, submitForm() { // 提交表单 } } }; </script> ``` 其中,`:action` 属性指定上传的地址,`:headers` 属性设置请求头,`:on-success` 和 `:on-error` 属性分别设置上传成功和失败的回调函数,`:before-upload` 属性设置上传前的校验规则,`:file-list` 属性绑定已上传的图片列表,`:data` 属性设置额外的参数。 接下来,在后端用thinkPHP接收上传的图片并进行处理。首先需要在控制器中创建一个上传图片的方法: ``` public function uploadImage() { $file = request()->file('file'); if (!$file) { return json(['code' => -1, 'msg' => '请选择上传的文件']); } $info = $file->validate(['size' => 512000, 'ext' => 'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads'); if ($info) { $url = '/uploads/' . $info->getSaveName(); return json(['code' => 0, 'msg' => '上传成功', 'url' => $url]); } else { return json(['code' => -1, 'msg' => '上传失败:' . $file->getError()]); } } ``` 该方法首先获取上传的文件对象,然后进行校验,最后将文件移动到指定的目录,并返回上传成功后的图片地址。 最后,在前端的上传组件中设置上传成功后的回调函数,将上传成功后返回的图片地址保存到表单中: ``` handleSuccess(response, file, fileList) { console.log(response); this.fileList = fileList; this.$message.success('上传成功'); // 保存图片地址到表单中 let imageUrl = response.url; this.form.image = imageUrl; }, ``` 这样就可以实现用thinkPHP接收element上传的图片了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值