uniapp develops small programs - how to determine whether the small program is opened on the mobile phone or PC

Official description

https://developers.weixin.qq.com/miniprogram/dev/devtools/pc-dev.html

How to determine if a mini program is a PC platform?

Through getSystemInfo official interface (platform is windows)
Through UA (PC UA includes MiniProgramEnv/Windows)

app.vue

<script>
export default {
      
      
  onLaunch: function () {
      
      
    console.warn(
      "当前组件仅支持 uni_modules 目录结构 ,请升级 HBuilderX 到 3.1.0 版本以上!"
    );
    console.log("App Launch", uni.getSystemInfoSync().platform);
    uni.setStorageSync("platform", uni.getSystemInfoSync().platform);
    uni.setStorageSync(
      "windowWidth",
      wx.getStorageSync("devices").windowWidth 
    );
    uni.setStorageSync(
      "windowHeight",
      wx.getStorageSync("devices").windowHeight 
    );
  },
  onLoad() {
      
      },
  onShow: function () {
      
      
  },
  onHide: function () {
      
      
  },
};
</script>

<style lang="scss">
/*每个页面公共css */
@import "@/uni_modules/uni-scss/index.scss";
// 设置整个项目的背景色
page {
      
      
  background-color: #f5f5f5;
}
</style>

Determine whether it is a function on the PC side (if the width is enough, it will also be regarded as the PC side)

/** 是否是pc端 */
function isPC() {
    
    
  if (["windows", "mac"].some((v) => v === wx.getStorageSync("platform"))) {
    
    
    return true;
  }
  if (Number(wx.getStorageSync("windowWidth")) > 1000) {
    
    
    return true;
  }
  return false;
}

At the same time, it is worth noting that the platform attribute of the WeChat developer tool is devtools, which can only be tested in the trial version platformProperties

Guess you like

Origin blog.csdn.net/qq_42611074/article/details/134553756