小程序开发过程中的技巧

1、当前位置信息的获取,app.json中添加

    "permission": {
    
    
        "scope.userLocation": {
    
    
            "desc": "你的位置信息将用于小程序位置接口的效果展示"
        }
    },
   "requiredPrivateInfos": [
        "chooseLocation",
        "choosePoi",
        "chooseAddress"
    ],

2、小程序开发中使用sass语法,project.config.json

 "setting": {
    
    
     "useCompilerPlugins": [
        "sass"
     ],
 }

3、使用分包处理,app.js中添加

"subPackages"[
{
    
    
	“root”:‘’,
	“pages”:[]
}]

4、小程序页面适配iPhone和安卓的安全区域,在app.js中onLaunch使用

    //  获取设备信息
    wx.getSystemInfo({
    
    
      success: res => {
    
    
        let {
    
     screenHeight, safeArea: {
    
     bottom } } = res
        // 苹果的安全区域
        this.globalData.safeArea = screenHeight - bottom
        // 底部高度
        this.globalData.bottom_spacing = (20 + (screenHeight - bottom)) + 'rpx'
        //自定义tabbar 时,底部安全高度, 68为tabbar的高度
        this.globalData.tabbar = 68 + (screenHeight - bottom)
        //页面提交按钮fixed底部,页面留的空白高度
        this.globalData.page_btn = (148 + (screenHeight - bottom)) + 'rpx'
      }
    })

5、强制更新新的版本,在app.js中onLaunch使用

  autoUpdate:function(){
    
    
    var self=this
    // 获取⼩程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
    
    
      const updateManager = wx.getUpdateManager()
     //1. 检查⼩程序是否有新版本发布
      updateManager.onCheckForUpdate(function (res) {
    
    
      // 请求完新版本信息的回调
        if (res.hasUpdate) {
    
    
        //2. ⼩程序有新版本,则静默下载新版本,做好更新准备
          updateManager.onUpdateReady(function () {
    
    
            wx.showModal({
    
    
              title: '更新提⽰',
              content: '新版本已经准备好,是否重启应⽤?',
              success: function (res) {
    
    
                if (res.confirm) {
    
    
                //3. 新的版本已经下载好,调⽤ applyUpdate 应⽤新版本并重启
                  updateManager.applyUpdate()
                } else if (res.cancel) {
    
    
                  //如果需要强制更新,则给出⼆次弹窗,如果不需要,则这⾥的代码都可以删掉了
                  wx.showModal({
    
    
                    title: '温馨提⽰~',
                    content: '本次版本更新涉及到新的功能添加,旧版本⽆法正常访问的哦~',
                    success: function (res) {
    
    
                    //  self.autoUpdate()
                    //  return;
                    //第⼆次提⽰后,强制更新
                      if (res.confirm) {
    
    
                        // 新的版本已经下载好,调⽤ applyUpdate 应⽤新版本并重启
                        updateManager.applyUpdate()
                      } else if (res.cancel) {
    
    
                        //重新回到版本更新提⽰
                        self.autoUpdate()
                      }
                    }
                  })
                }
              }
            })
          })
        updateManager.onUpdateFailed(function () {
    
    
          // 新的版本下载失败
          wx.showModal({
    
    
            title: '已经有新版本了哟~',
            content: '新版本已经上线啦~,请您删除当前⼩程序,重新搜索打开哟~',
          })
        })
        }
      })
    } else {
    
    
     // 如果希望⽤户在最新版本的客户端上体验您的⼩程序,可以这样⼦提⽰
     wx.showModal({
    
    
      title: '提⽰',
      content: '当前微信版本过低,⽆法使⽤该功能,请升级到最新微信版本后重试。'
     })
    }
  },

消息订阅弹框

  },
  openSend() {
    
    
    var _that = this;
    wx.getSetting({
    
    
      withSubscriptions: true,
      success(res) {
    
    
          var itemSettings = res.subscriptionsSetting.itemSettings;
          console.log("订阅情况",itemSettings);
          if (itemSettings){
    
    
            if (itemSettings['toQw1CWLNqV7vt6j34R6DfQxPcNgzojgihLuULH-SgA']== 'accept') {
    
    
                return
            }
          }
        wx.showModal({
    
    
          title: '',
          content: '活动开始提醒',
          confirmText: "同意",
          cancelText: "拒绝",
          success: function (res) {
    
    
            if (res.confirm) {
    
    
              //调用订阅消息
              console.log('用户点击确定');
 
              //调用订阅
              _that.requestSubscribe();
            } else if (res.cancel) {
    
    
              ///显示第二个弹说明一下
              wx.showModal({
    
    
                title: '',
                content: '拒绝后您将无法收到活动开始提醒',
                confirmText: "知道了",
                showCancel: false,
                success: function (res) {
    
    
                  ///点击知道了的后续操作 
                  ///如跳转首页面 
                }
              });
            }
          }
        });
      }
    })
  },
 
  //发起消息订阅
  requestSubscribe() {
    
    
    wx.requestSubscribeMessage({
    
    
      tmplIds: [''],
      success: (res) => {
    
    
        console.log("订阅消息 成功 " + res);
        console.log(res);
      },
      fail: (errCode, errMessage) => {
    
    
        console.log("订阅消息 失败 " + errCode + " message " + errMessage);
        console.log(errCode);
      },
      complete: (errMsg) => {
    
    
        console.log("订阅消息 完成 " + errMsg);
        console.log(errMsg);
      }
    });
  },

猜你喜欢

转载自blog.csdn.net/weixin_45474673/article/details/132907457