一、数据库准备
方法一、(LeanCloud的基本使用)
1.注册登录LeanCloud
注册网址:leancloud
2.新建LeanCloud服务应用
方法二、使用微信云开发存储图片
1. 创建云开发小程序
2. 将原有的小程序代码,迁移到云开发的miniprograme-3目录中
3. 在小程序云开发面板中,创建云环境
4. 使用wx.chooseImage提取本地图片资源路径
wx.chooseImage({
count: 1,
success:(res)=>{
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
// 此处可以写,上传图片的逻辑代码
}
})
5. 使用wx.cloud.uploadFile方法,将本地图片资源上传至微信云存储
wx.cloud.init({ //初始化wx.cloud方法,使得该方法能够对应到指定的云环境
env:'自己的云环境ID' //云环境ID
})
var imgname = Math.random().toString(36).substring(2) + Date.now() //生成随机字符串,作为图片名称
wx.cloud.uploadFile({ //【2】调用云方法,上传图片至微信云
cloudPath: imgname, // 上传至云端的路径
filePath:temp, // 小程序临时文件路径
success: (res) => {
// 返回文件 ID
console.log(res.fileID) //得到在线图片路径地址
this.setData({
imgurl:res.fileID //将在线路径,设置给data进行预览展示
})
},
fail: console.error
})