index.vue 4.7 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6
<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
7
		<button @tap="testScreenShotListen">开启截屏监听</button>
8 9 10 11
		<button @tap="testScreenShotOff">关闭截屏监听</button>
		<button @tap="testGetBatteryInfo">获取电池电量</button>
		<button @tap="testonMemoryWarning">开启内存不足告警监听</button>
		<button @tap="testoffMemoryWarning">关闭内存不足告警监听</button>
12
		
打打卡夫卡's avatar
打打卡夫卡 已提交
13
		<button @tap="testStartWifi">初始化wifi模块</button>
杜庆泉's avatar
杜庆泉 已提交
14 15
		<button @tap="testGetWifiList">获取当前wifi列表</button>
		<button @tap="testGetConnnectWifi">获取当前连接的wifi</button>
杜庆泉's avatar
杜庆泉 已提交
16
		<button @tap="testConnnectWifi">链接wifi</button>
打打卡夫卡's avatar
打打卡夫卡 已提交
17
		<button @tap="testStopWifi">关闭wifi模块</button>
DCloud-yyl's avatar
DCloud-yyl 已提交
18 19 20 21
	</view>
</template>

<script>
22

DCloud-yyl's avatar
DCloud-yyl 已提交
23 24 25
	export default {
		data() {
			return {
杜庆泉's avatar
杜庆泉 已提交
26 27
				title: 'Hello',
				memListener:null,
DCloud-yyl's avatar
DCloud-yyl 已提交
28 29 30 31 32 33
			}
		},
		onLoad() {

		},
		methods: {
杜庆泉's avatar
杜庆泉 已提交
34 35 36
			onMemoryWarning:function(res){
				console.log(res);
			},
杜庆泉's avatar
杜庆泉 已提交
37
			testConnnectWifi(){
杜庆泉's avatar
杜庆泉 已提交
38 39 40 41 42 43 44 45 46

				uni.connectWifi({
					partialInfo:false,
					SSID:"Xiaomi_20D0",
					password:"BBBB",
					complete:(res)=>{
						console.log(res);
					}
				});
杜庆泉's avatar
杜庆泉 已提交
47
			},
杜庆泉's avatar
杜庆泉 已提交
48
			testGetConnnectWifi(){
49
				uni.getConnectedWifi({
杜庆泉's avatar
杜庆泉 已提交
50 51 52
					partialInfo:false,
					complete:(res)=>{
						console.log(res);
杜庆泉's avatar
杜庆泉 已提交
53 54 55 56
						uni.showToast({
							icon:'none',
							title:res.wifi.SSID
						})
杜庆泉's avatar
杜庆泉 已提交
57
					}
杜庆泉's avatar
杜庆泉 已提交
58 59 60 61 62 63
				});
			},
			testStartWifi(){
				uni.startWifi({
					success:(res)=> {
						console.log("success: " + JSON.stringify(res));
64
						// wifi 开启成功后,注册wifi链接状态监听和wifi列表获取监听
杜庆泉's avatar
杜庆泉 已提交
65
						uni.onGetWifiList(function(res){
66
							console.log("onGetWifiList");
杜庆泉's avatar
杜庆泉 已提交
67 68
							console.log(res);
						});
杜庆泉's avatar
杜庆泉 已提交
69
						uni.onWifiConnected(function(res){
70 71
							console.log("onWifiConnected");
							console.log(res);
杜庆泉's avatar
杜庆泉 已提交
72 73
						});
						
杜庆泉's avatar
杜庆泉 已提交
74 75 76 77
					},fail:(res)=>{
						console.log("fail: " + JSON.stringify(res));
					},complete:(res)=>{
						console.log("complete: " + JSON.stringify(res));
78 79 80
					}
				})
			},
杜庆泉's avatar
杜庆泉 已提交
81 82 83 84 85 86 87 88 89
			testStopWifi() {
				uni.stopWifi({
					success:(res)=> {
						console.log("success: " + JSON.stringify(res));
					},fail:(res)=>{
						console.log("fail: " + JSON.stringify(res));
					},complete:(res)=>{
						console.log("complete: " + JSON.stringify(res));
					}
90
				})
杜庆泉's avatar
杜庆泉 已提交
91
				
92
			},
杜庆泉's avatar
杜庆泉 已提交
93 94 95 96 97 98 99 100 101
			testGetWifiList() {
				uni.getWifiList({
					success:(res)=> {
						console.log("success: " + JSON.stringify(res));
					},fail:(res)=>{
						console.log("fail: " + JSON.stringify(res));
					},complete:(res)=>{
						console.log("complete: " + JSON.stringify(res));
					}
102
				})
杜庆泉's avatar
杜庆泉 已提交
103 104 105 106
				
			},
			testonMemoryWarning() {
				uni.onMemoryWarning(this.onMemoryWarning)
107 108 109 110
				uni.showToast({
					icon:'none',
					title:'已监听,注意控制台输出'
				})
杜庆泉's avatar
杜庆泉 已提交
111 112
			},
			testoffMemoryWarning(){
113 114 115 116 117
				uni.offMemoryWarning(this.onMemoryWarning)
				uni.showToast({
					icon:'none',
					title:'监听已移除'
				})
118
			},
119 120 121 122 123 124 125
			testScreenShotListen() {
				var that = this;
				uni.onUserCaptureScreen(function(res) {
						console.log(res);
						
						if (uni.getSystemInfoSync().platform == "android") {
							// 除android 之外的平台,不需要判断返回状态码
杜庆泉's avatar
杜庆泉 已提交
126
							if(res.errCode == -1){
127 128
								// 启动失败
								return ;
杜庆泉's avatar
杜庆泉 已提交
129
							}else if(res.errCode == 0){
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
								uni.showToast({
									icon:"none",
									title:'截屏监听已开启'
								})
							}else {
								uni.showToast({
									icon:"none",
									title:'捕获截屏事件'
								})
								that.screenImage = res.image
							}
						}else{
							// 除android 之外的平台,不需要判断返回状态码
							uni.showToast({
								icon:"none",
								title:'捕获截屏事件'
							})
						}
						
					});
					
					if (uni.getSystemInfoSync().platform != "android") {
						// 除android 之外的平台,直接提示监听已开启
						uni.showToast({
							icon:"none",
							title:'截屏监听已开启'
						})
					}
			},
			testScreenShotOff() {
				uni.offUserCaptureScreen(function(res) {
						console.log(res);
				});
				// 提示已经开始监听,注意观察
				uni.showToast({
					icon:"none",
					title:'截屏监听已关闭'
				})
			},
			testGetBatteryInfo() {
				uni.getBatteryInfo({
					success(res) {
						uni.showToast({
							title: "当前电量:" + res.level + '%',
							icon: 'none'
						});
					}
				})
			},
DCloud-yyl's avatar
DCloud-yyl 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>