小程序登录/授权/个人信息/敏感信息/缓存/音效

1.登录授权

在需要查询是否登录的地方,我这里是在首页的onready中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
onReady: function () {
  //是否授权
  wx.getSetting({
    success: res => {
      if (res.authSetting['scope.userInfo']) {
        //已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
        console.log("已经授权");
        wx.getUserInfo({
          success: function (res){
            app.globalData.userInfo = res.userInfo
          }
        })
      } else {
        console.log("未授权跳转login");
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }
    }
  })
},

 //我这里没有将个人信息存入缓存中,也可以存入缓存然后通过缓存中是否有用户信息来判断是否授权

Login界面中有一个登录按钮

1
<button  class='login-btn' open-type='getUserInfo' lang='zh_CN' bindgetuserinfo='onGotUserInfo' hover-class='btn-hover' >微信登录</button>

点击方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
onGotUserInfo: function (e) {
  console.log('errMsg:'+e.detail.errMsg)
  console.log(+e.detail.userInfo)
  console.log('rawData:'+e.detail.rawData)
  if(e.detail.userInfo){
    console.log('点击了授权')
    app.globalData.userInfo = e.detail.userInfo
    wx.login({
      success: function(res){
        console.log(res)
        //返回临时凭证code 传入后台获取openid和session_key
        wx.request({
          url: 'testurl',
          data:{
            code:res.code
          },
          method:'POST',
          header:{
            'content-type': 'application/json' // 默认值
          },
          success: function(res){
            console.log(res)
          }
 
        })
      }
    })
  }else{
    console.log('拒绝了授权')
  }
  wx.navigateBack({
     
  })
},

 2.音效的使用

直接用innerAudioContext播放URL会有延迟,体验很差,可以将资源存入缓存中,通过PATH来播放

globaldata中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
home_btn:'',
next_question:'',
wrong_1:'',
wrong_2: '',
right_1:'',
right_2:'',
unit_pass_1:'',
unit_pass_2:'',
unit_pass_3:'',
unit_fail:'',
game_pass_1:'',
game_pass_2:'',
game_pass_3:'',
game_fail:'',

 

onLaunch中:
1
2
self.saveAudioFile('home_btn')
self.saveAudioFile('next_question')

saveAuidoFile方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
saveAudioFile:function(fileName){
    var self = this;
    //先判断缓存中是否存在
    wx.getStorage({
      key: fileName,
      success: function(res) {
        //存在的话直接赋值给globaldata
        self.globalData[fileName] = res.data
      },
      fail: function(res){
        //不存在,将提示音存入本地缓存
        wx.downloadFile({
          url: self.globalData.audioHeader + fileName + '.mp3',
          success(res) {
            wx.saveFile({
              tempFilePath: res.tempFilePath,
              success(res) {
                wx.setStorage({
                  key: fileName,
                  data: res.savedFilePath,
                })
                //赋值给globaldata
                self.globalData[fileName] = res.savedFilePath
              },
              fail(res) {
                console.log(res)
              }
            })
          }
        })
      }
    })
  },

 播放:

1
2
3
4
pointAudioPlay: function (name){
  innerAudioContext.src = app.globalData.home_btn
  innerAudioContext.play()
},

 很方便效果也很好!

 

 

posted @   小师傅啊小师傅  阅读(674)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2018-05-31 求数组的平均值、最大值、最小值、总和
点击右上角即可分享
微信分享提示