微信小程序> 微信小程序常用API-微信小程序入驻收费吗-小程序付费推广

微信小程序常用API-微信小程序入驻收费吗-小程序付费推广

浏览量:2176 时间: 来源:七块oooo
1.保存图片1.

API:wx.getImageInfo():获取图片路径;wx.saveImageToPhotosAlbum()保存图片关键点:通过模态框去引导用户进行授权操作:wx.showModal(),wx.openSetting()

//保存图片saveImg(){letcurrentthis.data.imageurlletthatthisconsole.log("saveImgBtn",current)wx.showLoading({title:'保存中'})wx.getImageInfo({src:current,success:function(res){wx.saveImageToPhotosAlbum({filePath:res.path,success:function(res){wx.hideLoading();wx.showToast({title:'保存成功',icon:'success'},2000)},fail:function(res){wx.hideLoading()if(res.errMsg"saveImageToPhotosAlbum:fail:authdenied"||res.errMsg"saveImageToPhotosAlbum:failauthdeny"||res.errMsg"saveImageToPhotosAlbum:failauthorizenoresponse"){//这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用that.imageRrrorAuth()}else{wx.showToast({title:'保存失败',icon:'none'})}}})},fail:function(error){console.log("roo",error)}})},//授权失败提示授权操作imageRrrorAuth(){wx.showModal({title:'提示',content:'需要您授权保存相册',showCancel:false,success:modalSuccess{wx.openSetting({success(settingdata){console.log("settingdata",settingdata)if(settingdata.authSetting['scope.writePhotosAlbum']){wx.showModal({title:'提示',content:'获取权限成功,再次保存图片即可',showCancel:false,})}else{wx.showModal({title:'提示',content:'获取权限失败,将无法保存到相册',showCancel:false,})}},fail(failData){console.log("failData",failData)},complete(finishData){console.log("finishData",finishData)}})}})},2.上传图片2.

先使用wx.chooseImage()选择图片,然后wx.uploadFile()将filePath图片的路径POST到服务器。tips:限制最多三张上传

viewclass'addImage'viewclass'commentImage'wx:for"{{imgList}}"wx:key"{{index}}"imagebindtap'previewImage'data-index"{{index}}"data-url'{{imageHost+item}}'src"{{imageHost+item}}"/imageimagedata-index"{{index}}"bindtap"deleteImage"class'close'style"width:50rpx;height:50rpx;"src'/images/closed.png'/image/viewimagewx:if"{{imgList.length3}}"src"/images/addImageIcon.png"bindtap"chooseimage"/image/viewchooseImage:function(e){letthatthis;letuserIdwx.getStorageSync("id");letimgListthat.data.imgListletimgCount3-imgList.length;//为了确定已经还能上传几张,设置最多三张上传wx.chooseImage({count:imgCount,//默认9,最多上传9sizeType:['original','compressed'],//可以指定是原图还是压缩图,默认二者都有sourceType:['album','camera'],//可以指定来源是相册还是相机,默认二者都有success:function(res){//返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片lettempFilePathsres.tempFilePaths//console.log("tempFilePaths",JSON.stringify(tempFilePaths))for(leti0;itempFilePaths.length;i++){lettempFilePathtempFilePaths[i]+''that.uploadImg(tempFilePath)}}})},uploadImg(tempFilePath){letimgListthis.data.imgListlettokenwx.getStorageSync("token");letthatthis;wx.uploadFile({url:API.api.userMemos,method:'POST',header:{"Content-Type":"application/json",'Authorization':token},filePath:tempFilePath,name:'file',//文件对应的参数名字(key)success:function(res){varfPath[];fPath.push(JSON.parse(res.data).filePath);console.log("图片上传完成:"+res.data);if(res.statusCode500){wx.showToast({title:'图片大小不超过5M',icon:'none'})}else{imgList.push(fPath[0])that.setData({imgList})console.log("imgList2",imgList)}}})},//预览图片previewImage(e){console.log("previewImage:::::"+e.currentTarget.dataset.url)leturle.currentTarget.dataset.url;letimgListthis.data.imgList;letimageHostthis.data.imageHost;leturls[];for(leti0;iimgList.length;i++){urls.push(imageHost+""+imgList[i])}console.log("urls:::::"+JSON.stringify(urls))wx.previewImage({current:url,//当前显示图片的http链接urls:urls//需要预览的图片http链接列表})},//删除图片deleteImage(e){console.log(JSON.stringify(e));letthatthis;letindexe.currentTarget.dataset.index;letimgListthat.data.imgList;imgListimgList.splice(index,1);that.setData({imgList:imgList})},3.用户授权获取用户信息buttonclass'btn'open-type"getUserInfo"bindgetuserinfo'authorize'/buttonauthorize(){if(e.detail.errMsg'getUserInfo:failauthdeny'){//弹提示框,重新授权wx.showModal({title:'授权失败',content:'请重新授权',showCancel:false,success:function(e){}})}else{//授权成功进行的操作console.log(JSON.parse(e.detail.rawData).avatarUrl);console.log(JSON.parse(e.detail.rawData).nickName);}}4.获取formId,用于发送模板消息formbindsubmit'getFormId'report-submit'true'buttonform-type'submit'确认/button/formgetFormId(e){letformIde.detail.formIdconsole.log("formId",formId)},5.小程序版本更新updataApp:function(){//版本更新if(wx.canIUse('getUpdateManager')){constupdateManagerwx.getUpdateManager()updateManager.onCheckForUpdate(function(res){if(res.hasUpdate){//请求完新版本信息的回调updateManager.onUpdateReady(function(){wx.showModal({title:'更新提示',content:'新版本已经准备好,是否重启应用?',success:function(res){if(res.confirm){//新的版本已经下载好,调用applyUpdate应用新版本并重启updateManager.applyUpdate()}}})})updateManager.onUpdateFailed(function(){wx.showModal({//新的版本下载失败title:'已经有新版本了哟~',content:'新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',})})}})}else{wx.showModal({//如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示title:'提示',content:'当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'})}},

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎