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

1.登录授权

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

  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界面中有一个登录按钮

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

点击方法:

  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中:

    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中:

    self.saveAudioFile(\'home_btn\')
    self.saveAudioFile(\'next_question\')

saveAuidoFile方法:

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)
              }
            })
          }
        })
      }
    })
  },

播放:

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

很方便效果也很好!