判断小程序是在什么环境下打开的

判断小程序是微信打开还是企业微信打开

wx.getSystemInfo(Object object)

微信官方文档

  • 以 Promise 风格 调用:支持
  • 小程序插件:支持,需要小程序基础库版本不低于 1.9.6
  • 微信 Windows 版:支持
  • 微信 Mac 版:支持

获取系统信息。由于历史原因,wx.getSystemInfo 是异步的调用格式,但是是同步返回,需要异步获取系统信息请使用 wx.getSystemInfoAsync。

企业微信官方文档

获取系统信息。
小程序可以在微信和企业微信中调用此接口,但是在企业微信中调用此接口时,会额外返回一个 environment 字段(微信中不返回),如此字段值为wxwork,则表示当前小程序运行在企业微信环境中。注意:只有运行在企业微信环境中,才能调用wx.qy的接口。

属性类型默认值必填说明
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

回调函数部分常用属性

wx.getSystemInfo({
  success (res) {
    console.log(res.model); // 设备型号
    console.log(res.pixelRatio); // 设备像素比
    console.log(res.windowWidth); // 可使用窗口宽度,单位px
    console.log(res.windowHeight); // 可使用窗口高度,单位px
    console.log(res.language); // 微信设置的语言
    console.log(res.version); // 微信的版本号
    console.log(res.platform); // 客户端平台
    console.log(res.environment); // *重点:小程序当前运行的环境,微信小程序调用时无该属性
  },
  fail (res) {
    console.log(res)
  }
})

示例

var that = this
wx.getSystemInfo({
  success(res) {
    if (res.environment == 'wxwork') {
      console.log('企业微信打开');
      wx.qy.login({
        success: data => {
          that.getInfo(data.code)
        }
      });
    } else {
      console.log('微信打开');
      wx.login({
        success: data => {
          that.getInfo(data.code)
        }
      });
    }
  },
  fail(res) {
    console.log(res);
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值