微信小程序向本地保存

快来打我* 2021-09-19 16:16 498阅读 0赞

image

提示框:

wx.showToast(OBJECT)

显示消息提示框

  1. wx.saveImageToPhotosAlbum({
  2. filePath : "./test.png", //这个只是测试路径,没有效果
  3. success(res) {
  4. console.log("success");
  5. },
  6. fail : function(res) {
  7. console.log(res);
  8. }
  9. })
  10. wx.showToast({
  11. title: '成功',
  12. icon: 'success',
  13. duration: 2000
  14. })
  15. wx.hideToast()
  16. 隐藏消息提示框
  17. wx.showToast({
  18. title: '加载中',
  19. icon: 'loading',
  20. duration: 10000
  21. })
  22. setTimeout(function(){
  23. wx.hideToast()
  24. },2000)
  25. wx.showModal(OBJECT)
  26. 显示模态弹窗
  27. wx.showModal({
  28. title: '提示',
  29. content: '这是一个模态弹窗',
  30. success: function(res) {
  31. if (res.confirm) {
  32. console.log('用户点击确定')
  33. }
  34. }
  35. })
  36. wx.showActionSheet({
  37. itemList: ['A', 'B', 'C'],
  38. success: function(res) {
  39. if (!res.cancel) {
  40. console.log(res.tapIndex)
  41. }
  42. }
  43. })

wx.setNavigationBarTitle(OBJECT)

动态设置当前页面的标题。

  1. wx.setNavigationBarTitle({
  2. title: '当前页面'
  3. })
  4. wx.getSetting({
  5. success(res) {
  6. console.log(res)
  7. if (!res.authSetting['scope.writePhotosAlbum']) {
  8. wx.openSetting({
  9. success(res) {
  10. //拒绝授权后重新提示授权,并授权成功
  11. }
  12. })
  13. }else{
  14. wx.saveImageToPhotosAlbum({
  15. filePath: 'imgs/home-icons/2019011709194.png',
  16. success(result) {
  17. //已授权过可直接执行保存图片
  18. }
  19. })
  20. }
  21. }
  22. })

image

  1. wx.downloadFile({
  2. url: 'https://example.com/audio/123', //仅为示例,并非真实的资源
  3. success: function (res) {
  4. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  5. if (res.statusCode === 200) {
  6. wx.saveImageToPhotosAlbum({
  7. filePath: res.tempFilePath,
  8. success(res) {
  9. wx.showToast({
  10. title: '保存图片成功!',
  11. })
  12. },
  13. fail(res) {
  14. wx.showToast({
  15. title: '保存图片失败!',
  16. })
  17. }
  18. })
  19. }
  20. }
  21. })
  22. wx.downloadFile({
  23. url: 'https://example.com/audio/123', //仅为示例,并非真实的资源
  24. success: function (res) {
  25. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  26. if (res.statusCode === 200) {
  27. wx.saveImageToPhotosAlbum({
  28. filePath: res.tempFilePath,
  29. success(res) {
  30. wx.showToast({
  31. title: '保存图片成功!',
  32. })
  33. },
  34. fail(res) {
  35. wx.showToast({
  36. title: '保存图片失败!',
  37. })
  38. }
  39. })
  40. }
  41. }
  42. })

请点赞!因为你的鼓励是我写作的最大动力!

官方微信公众号

吹逼交流群:711613774

吹逼交流群

发表评论

表情:
评论列表 (有 0 条评论,498人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序使用本地存储

    在使用微信小程序的时候当获取接口的数据的时候,接口传输过来的数据很大,而每一次回到这个页面又再一次的查询获取数据,这难免会拖垮小程序的速度。这个时候就需要使用本地存储。 变量