test.vue 9.6 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<template>
	<view class="content">


		<button @tap="testStartWifi" style="width: 100%;">初始化wifi模块</button>
		<button @tap="testGetWifiList" style="width: 100%;">获取当前wifi列表</button>
		<button @tap="testOffGetWifiList" style="width: 100%;">移除wifi列表监听</button>
		<button @tap="testGetConnnectWifi" style="width: 100%;">获取当前连接的wifi</button>
		<button @tap="testConnnectWifi" style="width: 100%;">链接wifi</button>
		<button @tap="testStopWifi" style="width: 100%;">关闭wifi模块</button>

		<button @tap="onGetWifiList2_assert0" style="width: 100%;">onGetWifiList2_assert0</button>

		<button @tap="testScreenShotListen">开启截屏监听</button>
		<button @tap="testScreenShotOff">关闭截屏监听</button>
		<button @tap="testSetUserCaptureScreen">{{setUserCaptureScreenText}}</button>

		<button @tap="testGetBatteryInfo">获取电池电量</button>
		<button @tap="testGetBatteryInfoSync">同步获取电池电量</button>
		<button @tap="testonMemoryWarning">开启内存不足告警监听</button>
		<button @tap="testoffMemoryWarning">关闭内存不足告警监听</button>
		<button @tap="getLocationTest" style="width: 100%;">获取定位</button>
23 24
		<button type="default" @click="handleInstallApk">安装apk</button>
    <button type="default" @click="handleShowNotificationProgress">显示通知栏下载进度</button>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
25 26 27 28 29 30 31

	</view>
</template>

<script>
	import {
		installApk
32 33 34 35 36 37 38
	} from "@/uni_modules/uni-installApk"


	let pre = 0
	let speed = 1
	let preTime = 0
	let isBegin = false
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

	export default {
		data() {
			return {
				memListener: null,
				setUserCaptureScreenFlag: false,
				setUserCaptureScreenText: '禁止截屏',
				permissionGranted: false,
				id: 0
			}
		},
		onLoad() {

		},
		methods: {
			onMemoryWarning: function(res) {
				console.log(res);
			},
			fn: function(res) {
				console.log(res)
			},
			getLocationTest() {
				console.log(" ------- getLocationTest: ");
				uni.getLocation({
					type: 'gcj02 ',
					success(res) {
						console.log(" success ", res);
					},
					fail(res) {
						console.log(" fail ", res);
					}
				})
			},
			onGetWifiList2_assert0() {
				console.log(" ------- onGetWifiList2_assert0: ", this.id);
				const fn = res => console.log('onGetWifiList res', res)
				uni.startWifi({
					success() {
						uni.onGetWifiList(fn)
						uni.getWifiList({
							success() {
								console.log('getWifiList success');
								uni.offGetWifiList(fn)
								uni.stopWifi({
									success() {},
									fail(e) {
										console.log("stopWifi fail: ", e);
									}
								})
							}
						})
					}
				})
				this.id++
			},

			testConnnectWifi() {

				uni.startWifi({
					success: (res) => {
						console.log("success: " + JSON.stringify(res));
						// uni.connectWifi({
						// 	maunal:false,
						// 	SSID:"Xiaomi_20D0",
						// 	password:"BBB111",
						// 	complete:(res)=>{
						// 		console.log(res);
						// 	}
						// });
					},
					fail: (res) => {
						console.log("fail: " + JSON.stringify(res));
					},
					complete: (res) => {
						console.log("complete: " + JSON.stringify(res));
					}
				})



			},
			testGetConnnectWifi() {
				uni.getConnectedWifi({
					partialInfo: false,
					complete: (res) => {
						console.log(res);
						if (res.errCode == 0) {
							uni.showToast({
								icon: 'none',
								title: res.wifi.SSID
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: res.errMsg
							})
						}

					}
				});
			},
			testStartWifi() {
				uni.startWifi({
					success: (res) => {
						console.log("success: " + JSON.stringify(res));
						// wifi 开启成功后,注册wifi链接状态监听和wifi列表获取监听
						uni.onGetWifiList(function(res) {
							console.log("onGetWifiList");
							console.log(res);
						});
						uni.onWifiConnected(function(res) {
							console.log("onWifiConnected");
							console.log(res);
						});
						uni.onWifiConnectedWithPartialInfo(function(res) {
							console.log("onWifiConnectedWithPartialInfo");
							console.log(res);
						});

					},
					fail: (res) => {
						console.log("fail: " + JSON.stringify(res));
					},
					complete: (res) => {
						console.log("complete: " + JSON.stringify(res));
					}
				})
			},
			testStopWifi() {
				uni.offWifiConnected()
				uni.offWifiConnectedWithPartialInfo()

				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));
					}
				})

			},
			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));
					}
				})

			},

			testOffGetWifiList() {
				uni.offGetWifiList()
			},



			testonMemoryWarning() {
				uni.onMemoryWarning(this.onMemoryWarning)
				uni.showToast({
					icon: 'none',
					title: '已监听,注意控制台输出'
				})
			},
			testoffMemoryWarning() {
				uni.offMemoryWarning(this.onMemoryWarning)
				uni.showToast({
					icon: 'none',
					title: '监听已移除'
				})
			},
			testScreenShotListen() {
				var that = this;
				uni.onUserCaptureScreen(function(res) {
					console.log(res);
					uni.showToast({
						icon: "none",
						title: '捕获截屏事件'
					})
					that.screenImage = res.path
				});

				if (uni.getSystemInfoSync().platform != "android" || that.permissionGranted) {
					// 除android 之外的平台,直接提示监听已开启
					uni.showToast({
						icon: "none",
						title: '截屏监听已开启'
					})
				}
			},
			testScreenShotOff() {
				uni.offUserCaptureScreen(function(res) {
					console.log(res);
				});
				// 提示已经开始监听,注意观察
				uni.showToast({
					icon: "none",
					title: '截屏监听已关闭'
				})
			},
			testGetBatteryInfo() {
				uni.getBatteryInfo({
					success(res) {
						console.log(res);
						uni.showToast({
							title: "当前电量:" + res.level + '%',
							icon: 'none'
						});
					}
				})
			},

			testGetBatteryInfoSync() {
				let ret = uni.getBatteryInfoSync()
				console.log(ret)
			},

			testSetUserCaptureScreen() {
				let flag = this.setUserCaptureScreenFlag;
				uni.setUserCaptureScreen({
					enable: flag,
					success: (res) => {
						console.log("setUserCaptureScreen enable: " + flag + " success: " + JSON.stringify(
							res));
					},
					fail: (res) => {
						console.log("setUserCaptureScreen enable: " + flag + " fail: " + JSON.stringify(res));
					},
					complete: (res) => {
						console.log("setUserCaptureScreen enable: " + flag + " complete: " + JSON.stringify(
							res));
					}
				});
				uni.showToast({
					icon: "none",
					title: this.setUserCaptureScreenText
				});
				this.setUserCaptureScreenFlag = !this.setUserCaptureScreenFlag;
				if (this.setUserCaptureScreenFlag) {
					this.setUserCaptureScreenText = '允许截屏';
				} else {
					this.setUserCaptureScreenText = '禁止截屏';
				}
			},
			handleInstallApk() {
				installApk({
294 295
					// filePath: "/sdcard/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/ddd.apk",
					filePath:"/static/test.apk",
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
296 297 298 299
					complete(res) {
						console.log(res);
					}
				})
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
			},
      handleShowNotificationProgress(){
        const task = uni.downloadFile({
        	url: "http://192.168.213.108:8080/test.apk",
        	success(e) {
        		console.log("success111 :", e);
        		uni.finishNotificationProgress({
        			title: "安装升级包",
        			content: "下载完成。",
        			callback: () => {
        				uni.installApk({
        					filePath: e.tempFilePath,
        					complete(res) {
        						console.log(res);
        					}
        				})
        			}
        		})
        	},
        	fail(e) {
        		console.log("fail : ", e);
        	}
        });

        task.onProgressUpdate((res) => {
        	const sd = this.calculateSpeed(res.totalBytesWritten)
        	const remian = ((res.totalBytesExpectedToWrite - res.totalBytesWritten) / sd).toFixed(0)
        	const remianStr = sd != 1 ? "剩余时间 " + remian + "" : "正在计算"

        	uni.createNotificationProgress({
        		title: "正在下载升级包",
        		content: remianStr,
        		progress: res.progress
        	})

        	if (res.progress == 100) {
        		pre = 0
        		speed = 1
        		preTime = Date.now()
        		isBegin = false
        	}
        })
      },
      calculateSpeed(current) {
      	//简略的计算下载速度
      	if (!isBegin) {
      		preTime = Date.now()
      		isBegin = true
      		return speed
      	}
      	const currentTime = Date.now()
      	if (currentTime - preTime > 1000) {
      		speed = current - pre
      		pre = current
      		preTime = currentTime
      	}
      	return speed
      }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
		}
	}
</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;
	}
388
</style>