- 云开发批量上传图片,上传完图片再上传数据库 [即抄即用,拎包入住]
大家好,又是我拎包哥,今天我们来实现在云开发中批量上传图片。 经过Stephen哥的指正,我改用了Promise.all的方法来达到目的。 Promise.all的作用就是等待所包含的promise函数结束后再执行下一步逻辑,非常方便好用!const db = wx.cloud.database() const test = db.collection('test') Page({ onLoad() { this.imgList = [] wx.chooseImage({ success: (res) => { this.TFP = res.tempFilePaths } }) }, btn() { let promiseMethod = new Array(this.TFP.length) for (let i = 0; i < this.TFP.length; i++) { promiseMethod[i] = wx.cloud.uploadFile({ cloudPath: 'img' + i + '.png', filePath: this.TFP[i] }).then(res => { this.imgList.push(res.fileID) }) } Promise.all([...promiseMethod]).then(() => { test.add({ data: { imgList: this.imgList } }) }) } }) --------------------------------------我是分割线-------------------------------------- async await 要点: ctrl c + ctrl v这里用了await阻塞在wx.cloud.uploadFile前面,避免还没上传完图片就往数据库插入数组。减少了then里的代码,美观逼格高。嘻嘻嘻。await wx.cloud.uploadFile不能放在wx.chooseImage里,如果可以的话,请告诉我怎么做,谢谢!欢迎交流,指出错误,我立刻修改么么哒。 标准版 const db = wx.cloud.database() const test = db.collection('test') Page({ onLoad() { this.imgList = [] wx.chooseImage({ success: (res) => { this.TFP = res.tempFilePaths } }) }, async btn() { this.imgList = [] console.log(this.TFP) for (let i = 0; i < this.TFP.length; i++) { await wx.cloud.uploadFile({ cloudPath: 'img' + i + '.png', filePath: this.TFP[i] }).then(res => { this.imgList.push(res.fileID) }) } test.add({ data: { imgList: this.imgList } }) } }) 新手最爱一锅炖版(不推荐) 为什么不推荐呢,因为选择图片并不意味着要上传图片,用户还没进行最终的确定操作(不过可以用来了解async await)。 onLoad() { this.imgList = [] wx.chooseImage({ success: async res => { this.TFP = res.tempFilePaths for (let i = 0; i < this.TFP.length; i++) { await wx.cloud.uploadFile({ cloudPath: 'img' + i + '.png', filePath: this.TFP[i] }).then(res => { this.imgList.push(res.fileID) }) } test.add({ data: { imgList: this.imgList } }) } }) } [图片] ==========================end==========================
2020-05-17 - createAnimation菜单下拉动画 [即抄即用,拎包入住]
短评createAnimation 垃圾:1.为什么不能是%??还要计算下拉ctr的高度。 2.rpx不彻底,和px要慢慢换算,开发者你写出来自己有没有用? 优秀:比@keyframes能省出50%的代码。 注:上下拉都有动画。 [图片] <view bindtap="mClick" class="mClick">下拉</view><view class="ctr" animation='{{ani}}'> <view class="item">内容1</view> <view class="item">内容2</view> <view class="item">内容3</view></view> .mClick{ position: fixed; width: 100vw; height: 50px; background-color: yellow; z-index: 1; } .ctr{ position: fixed; width: 100vw; top: -100px; } .item{ width: 100vw; height: 50px; background-color: red; } Page({ data: { dur: 300, ty: 150 }, onLoad() { var dur = this.dur this.ani = wx.createAnimation({ duration: dur, timingFunction: 'ease' }) this.setData({ ani: this.ani.export() }) }, mClick() { var ani = this.ani var ty = this.data.ty ani.translateY(ty).step() var altTy = (ty == 150) ? 0 : 150 this.setData({ dur: 300, ty: altTy, ani: ani.export() }) } }) end
2021-12-06 - 地址选择解决方案 [即抄即用,拎包入住]
操作:ctrl c + ctrl v 资源:所需的JS和图片资源在云盘 (提取码:o4mk)里,直接粘贴在demo2文件夹就可以了 思路:选择自己附近的地址或搜索想要的地址 欢迎热烈交流啊哈哈哈23333,经常在线的 ps. 记得申请个腾讯地图qqmap key啊 os. 代码有点小多,ctrl c + ctrl v要勤快点 [图片] demo [代码]<view bindtap="jump"> <view wx:if='{{!address}}'>请选择地址(必选)</view> <view wx:else>{{address}}</view> </view> ------------------------------------------------------ jump(){ wx.navigateTo({ url: '/pages/demo2/demo2', }) } [代码] demo2 [代码]<view class="searchCtr"> <view class="searchIcon"> <icon type='search' /> </view> <input bindinput="getAdr" placeholder='请输入地址' placeholder-class="searchPH" class="search" /> </view> <map class="map" longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}" hidden='{{keyword}}' /> <view class="adrTxtBox" style="margin-top:{{keyword?100:650}}rpx"> <view wx:for="{{adrList}}" wx:key="index"> <view bindtap="adrConfirm" data-index="{{index}}" class="adrBox"> <view class="imgNtitle"> <image src="minilandmark.png" class="minilandmark" /> <view class="title">{{item.title}}</view> </view> <view class="adrDetail">{{item.address}}</view> </view> </view> </view> ------------------------------------------------------------------------------------------ var QQMapWX = require('/qqmap-wx-jssdk.js') var qqmapsdk; Page({ onLoad() { var that = this qqmapsdk = new QQMapWX({ key: '你自己的qqMap key' }) wx.getLocation({ type: 'gcj02', success(res) { const lat = res.latitude const lon = res.longitude const markers = [{ latitude: lat, longitude: lon, iconPath: 'landmark.png', width: 40, height: 40, label: { content: '你现在位置', bgColor: '#ffffff', display: 'always', borderRadius: 3, borderWidth: 1, anchorY:-30, anchorX:15 } }] qqmapsdk.reverseGeocoder({ location: { latitude: lat, longitude: lon }, get_poi: 1, success(res) { var poiList = res.result.pois var adrList = [] for (var i in poiList) { adrList.push({ title: poiList[i].title, address: poiList[i].address, latitude: poiList[i].location.lat, longitude: poiList[i].location.lng }) } that.setData({ latitude: lat, longitude: lon, markers: markers, adrList: adrList }) } }) } }) }, getAdr(e) { var that = this; var keyword = e.detail.value this.setData({ keyword: keyword }) qqmapsdk.getSuggestion({ keyword: e.detail.value, success: function (res) { var poiList = res.data var adrList = []; for (var i = 0; i < res.data.length; i++) { adrList.push({ title: poiList[i].title, address: poiList[i].address, latitude: poiList[i].location.lat, longitude: poiList[i].location.lng }); } that.setData({ adrList: adrList }); } }); }, adrConfirm(e) { let pages = getCurrentPages() let prePage = pages[pages.length - 2] let index = e.currentTarget.dataset.index let adrList = this.data.adrList let address = adrList[index].title let latitude = adrList[index].latitude let longitude = adrList[index].longitude prePage.setData({ address: address, latitude: latitude, longitude: longitude }) wx.navigateBack({ delta: 1 }) } }) ------------------------------------------------------------------------------------------------ .searchCtr { position: fixed; display: flex; justify-content: center; align-items: center; top: 0; height: 100rpx; width: 100%; background-color: #fec22c; z-index: 1; } .searchIcon { display: flex; align-items: center; justify-content: center; background-color: white; width: 10%; height: 70rpx; border-radius: 60rpx 0 0 60rpx; } .search { background-color: white; width: 80%; height: 70rpx; border-radius: 0 60rpx 60rpx 0; } .searchPH { font-size: 27rpx; } .map { position: fixed; top: 100rpx; height: 550rpx; width: 100%; z-index: 1; } .adrTxtBox { width: 100%; } .adrBox { display: flex; flex-direction: column; justify-content: center; margin-left: 2.5%; height: 150rpx; width: 95%; border-bottom: 2rpx solid rgba(133, 133, 133, 0.2); } .imgNtitle { display: flex; flex-direction: row; align-items: center; height: 50rpx; } .minilandmark { width: 40rpx; height: 40rpx; } .title { margin-left: 20rpx; } .adrDetail { margin-top: 20rpx; margin-left: 60rpx; font-size: 24rpx; color: #666; } [代码]
2020-05-21