微信小程序扫码功能的使用

  • 效果图添加图片前:

在这里插入图片描述
添加上图片后:
在这里插入图片描述

wxml内容:

<view class="" hover-class="hover-class" catchtap="openChooseImage" style="height: 368rpx;background: #E6EEFE url({{ imgpath ? imgpath + '/bgIcon/addPhoto.png' : '' }}) no-repeat center;background-size: 88rpx 88rpx;">
	<image wx:if="{{imagePath}}" src="{{ imagePath }}" style="max-width: 100%;max-height: 100%;"></image>
</view>
// 打开相册-点击事件
openChooseImage: function() {
	let that = this;
	wx.chooseImage({
		count: 1,
		sizeType: ['original', 'compressed'],
		sourceType: ['album', 'camera'],
		success(res) {
			// tempFilePath可以作为img标签的src属性显示图片
			const tempFilePaths = res.tempFilePaths
			that.setData({
				imagePath: tempFilePaths
			})
		}
	})
}

调用 wx.uploadFile 将图片保存到服务器。 wx.uploadFile:官网地址

wx.uploadFile({
	url: app.severpath+"/api/serveUpdate",
	filePath: this.data.imagePath[0],
	name: "photoFile",
	formData: params,
	// 请求成功
	success: function (res) {
		res.data = JSON.parse(res.data);
		//隐藏加载提示
		wx.hideLoading();
		if(res.statusCode == 200){
			if(res.data.code == 200){
				wx.showModal({
					title: '提示',
					showCancel: false,
					content: '添加成功!'
				});
				// 清空表单数据
				that.clearData();
			}else{
				// 提示错误信息
				wx.showModal({
					title: '提示',
					content: '添加成功!',
					showCancel: false
				});
			}
		}else{
			// 提示错误信息
			wx.showModal({
				title: '提示',
				showCancel: false,
				content: '添加成功!'
			});
		}
	},
	// 请求失败
	fail: function(err){
		wx.hideLoading();//隐藏加载提示
		// 提示错误信息
		wx.showModal({
			title: '提示',
			showCancel: false,
			content: '添加成功!'
		});
	}
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值