微信小程序的笔记记录

小程序已经做了两个项目了,踩了不少坑,自己把重要的部分做个记录。
以下是app.js的代码,其中对于日期列表的生成,是为项目中展示的旅游日历定制的;还有版本更新提示,用户登录等

const util = require('utils/util.js')   //引入其他js
//app.js
App({
  mainUrl: "https://xxx/",      // ajax请求地址
  onLaunch: function () {
    //小程序版本更新提示
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      // console.log(res.hasUpdate)
    })
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    xcxToken: null,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Cookie': ''
    },
    dateList: null
  },
  toLogin: function () {
    var that = this;
    var code = "";
    wx.login({
      success(res) {
        console.log("wxLogin:", res);
        if (res.code) {
          code = res.code;
          var url = that.mainUrl + "login/getWechatSessionKey";
          wx.request({
            url: url,
            data: {
              code: code
            },
            method: 'GET',
            success: function (res) {
              console.log(res);
              if (res.data.success) {
                wx.getUserInfo({
                  lang: "zh_CN",
                  success: function (res) {
                    that.globalData.userInfo = res.userInfo
                    var encryptedData = res.encryptedData;
                    var iv = res.iv;
                    wx.request({
                      url: that.mainUrl + "login/decodeUserInfo",
                      data: {
                        encryptedData: encryptedData,
                        iv: iv
                      },
                      method: 'GET',
                      success: function (res) {
                        console.log(res);
                        if (res.data.success && res.data.obj) {
                          that.globalData.xcxToken = res.data.obj.xcxToken
                          wx.setStorage({
                            key: 'xcxToken',
                            data: res.data.obj.xcxToken,
                            success: function (res) {
                              console.log(res)
                            }
                          })
                        }
                      }
                    })
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  toastModel: function (tips, fun) {
    wx.showModal({
      title: '提示',
      content: tips,
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          if (fun)
            fun()
        }
      }
    })
  },
  goBack:function(){
    //获取页面栈
    var pages = getCurrentPages()
    if (pages.length > 1) {
      //返回上一个页面
      wx.navigateBack({
        delta: pages.length - 2
      })
    }
  },
  //按数组某个属性排序,使用方法:arr.sort(app.compareArr('property'))
  compareArr: function(property){
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      return value1 - value2;
    }
  },
  //按数组的数值排序,使用方法:arr.sort(sortNumber)
  sortNumber: function (a, b) {
    return a - b
  },
  //初始化从当前日期起,m个月的日期列表
  getDateMonth: function (m) {
    var dateList = []
    var currentDate = new Date()                        //当前日期 
    var currentYear = currentDate.getFullYear()         //当前年
    var currentMonth = currentDate.getMonth() + 1       //当前月
    var currentDay = currentDate.getDate()              //当前日 
    var days = util.mGetDate(currentYear, currentMonth) //当前年月的天数
    var lastDays = days - currentDay                    //当月还剩余的天数
    console.log(currentYear + "年" + currentMonth + "月有" + days + '天,还剩' + lastDays + '天')
    for (var i = currentDay; i <= days; i++) {
      var newDate = new Date(currentYear, currentMonth - 1, i)
      var obj = {
        "year": currentYear,
        "month": currentMonth,
        "day": i,
        "week": util.formatWeek(newDate.getDay()),
        "price": "0"
      }
      dateList.push(obj)
    }
    if (m && m > 1) {
      // currentMonth = currentMonth - 1
      for (var n = 0; n < m; n++) {
        currentMonth ++
        if (currentMonth == 12) {
          currentYear = currentYear + 1
          currentMonth = 1
        }
        days = util.mGetDate(currentYear, currentMonth)
        if (n == m - 1) {
          days = currentDay - 1
        }
        for (var j = 1; j <= days; j++) {
          var newDate = new Date(currentYear, currentMonth-1, j)
          var obj = {
            "year": currentYear,
            "month": currentMonth,
            "day": j,
            "week": util.formatWeek(newDate.getDay()),
            "price": "0"
          }
          dateList.push(obj)
        }
      }
    }else{
      currentMonth = currentMonth + 1
      for (var j = 1; j < currentDay; j++) {
        if (currentMonth == 12) {
          currentYear = currentYear + 1
          currentMonth = 1
        }
        var newDate = new Date(currentYear, currentMonth-1, j)
        var obj = {
          "year": currentYear,
          "month": currentMonth,
          "day": j,
          "week": util.formatWeek(newDate.getDay()),
          "price": "0"
        }
        dateList.push(obj)
      }
    }
    this.globalData.dateList = dateList
  }
})
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值