codecamp

百度智能小程序 获取运行环境信息同步接口

swan.getEnvInfoSync

Tip:基础库 3.140.1 之前,无法判断接口是否调用失败;基础库 3.140.1 及以后,接口调用失败时会返回一个标准的Error对象,可通过instanceof来判断接口是否调用失败。

解释:获取运行环境信息同步接口

方法参数

同步返回参数说明

参数 类型 说明
appKey String 智能小程序 App Key
appName String 智能小程序名称
lastAppURL String 智能小程序最近一次打开的调起协议
sdkVersion String 基础库版本
scheme String 调起协议的协议头
env String 智能小程序的版本(基础库 3.90.12 开始支持)。有效值:development,trial,production 。
development 代表开发版本;
trial 代表体验版本;
production 代表线上版本。

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <view class="card-area">
        <view class="list-area border-bottom" s-for="item in infoList">
        <view class="result-item">
            <text class="result-item-key-6">{{item.chineseName}}</text>
            <text class="result-item-value">{{item.value}}</text>
        </view>
        </view>
        <view class="button-group">
            <button bind:tap="getEnvInfoSync" type="primary" hover-stop-propagation="true">获取运行环境信息</button>
        </view>
    </view>
</view>
Page({
    data: {
        infoList: [{
            engName: 'appKey',
            chineseName: 'App Key',
            value: ''
        }, {
            engName: 'appName',
            chineseName: '小程序名称',
            value: ''
        }, {
            engName: 'lastAppURL',
            chineseName: '调起协议',
            value: ''
        }, {
            engName: 'sdkVersion',
            chineseName: '基础库版本',
            value: ''
        }, {
            engName: 'scheme',
            chineseName: '协议头',
            value: ''
        }, {
            engName: 'env',
            chineseName: '小程序版本',
            value: ''
        }
    ]
    },
    getEnvInfoSync() {
        let res = swan.getEnvInfoSync();
        // 基础库 3.140.1 之前,无法判断接口是否调用失败
        // 基础库 3.140.1 及以后,通过 instanceof 来判断接口是否调用失败
        if (!(res instanceof Error)) {
            this.updateInfoList(res);
            console.log('getEnvInfoSync success', res);
        }
        else {
            console.log('getEnvInfoSync fail', res.message);
        }
    },
    updateInfoList(res) {
        let infoList = this.data.infoList;
        for (let item of infoList) {
            if (res[item.engName] === '') {
                item.value = '暂无';
            }
            else {
                item.value = res[item.engName];
            }
        }
        this.setData('infoList', infoList);
    }
});

Bug & Tip

  • Tip:基础库 3.140.1 之前,无法判断接口是否调用失败;基础库 3.140.1 及以后,接口调用失败时会返回一个标准的Error对象,可通过instanceof来判断接口是否调用失败。


百度智能小程序 获取系统信息同步接口
百度智能小程序 获取网络类型
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }