123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814 |
- <!-- 蓝牙平台限制 todo https://uniapp.dcloud.io/api/system/bluetooth?id=stopbluetoothdevicesdiscovery -->
- <template>
- <view>
- <uni-nav-bar id="nav-bar" status-bar="true" @clickLeft="onBack()" title="设备升级" color="#000000" fixed="true" :border="false">
- <view slot="left">
- <view class=" flex align-center margin-left">
- <image class="p-left-arrow" src="../../../static/p-left-arrow.png"></image>
- </view>
- </view>
- </uni-nav-bar>
- <view class="card-view padding-top padding-bottom" v-for="(item,index) in devicesList" :key="index" :class="item.bRatio?'hardware-border':''">
- <view class="flex justify-between align-center">
- <image style="margin-left: 30rpx; width: 200rpx;height: 120rpx;" :src="item.icon" mode="aspectFit"></image>
- <view style="width: 250rpx;">
- <view style="font-weight: bold; font-size: 18px; color: #565656; margin: 20rpx 0;">{{item.cname}}</view>
- <view v-if="send_index<=0&&item.currentVersion !== ''">
- <view style="margin: 20rpx 0; font-size: 12px; color: grey;">固件版本号:{{item.currentVersion}}</view>
- <!-- <view style="margin: 20rpx; font-size: 12px;" class="make-text-bPurple">更新版本号:{{item.latestVersion}}</view> -->
- </view>
- <!-- <view class="flex">
- <view style="margin: 20rpx; font-size: 12px; color: grey;">硬件rom:{{item.currentRomVersion}}</view>
- <view style="margin: 20rpx; font-size: 12px;" class="make-text-bPurple">更新版本rom:{{item.latestRomVersion}}</view>
- </view> -->
- <!-- <view class="flex">
- <view style="margin: 20rpx; font-size: 12px; color: grey;">Bytes:{{bin_offset}} 总 {{bin_byteLength}}</view>
- </view> -->
- <view v-if="send_index>0" class="flex justify-between align-center" style="width: 210rpx;">
- <view class="cu-progress round" style=" width: 140rpx; height: 15rpx;">
- <view class="bg-green" :style="[{ width:percentage+'%'}]"></view>
- </view>
- <view class="" style=" font-size: 12px; color: grey;">{{percentage}}%</view>
- <!-- <view>
- <progress :percent="percentage" stroke-width="3" />
- </view> -->
- <!-- ,total {{bin_total}} send:{{send_index}} -->
- <!-- <view style=" font-size: 12px; color: grey;"> {{percentage}}% </view> -->
- </view>
- </view>
- <view v-if = "!bStart" style="width: 180rpx;margin-right: 30rpx;">
- <view v-if="send_index<=0&&item.latestVersion !== ''">
- <view v-if="bCanUpdate&&downLoadOTA!==''" class="text-11px make-text-bPurple " style="text-decoration:underline"
- @click="onSendConnect"> 请更新版本{{item.latestVersion}}
- </view>
- <view v-else class="text-11px text-grey">最新版本{{item.latestVersion}}</view>
- <!-- <button v-else class="text-11px make-bg-bPurple text-white" @click="onCheckDeviceFiles">检查更新</button> -->
- <!-- <button v-if="item.currentVersion !== ''" class="text-11px margin" @click="onSendConnect">开始</button> -->
- <!-- <button v-if="item.currentVersion !== ''" class="text-11px margin" @click="onStop">停止</button> -->
- </view>
- <view v-if="send_index<=0&&item.latestVersion === ''" class="text-26px text-grey text-center"><text class="cuIcon-loading2 cuIconfont-spin"></text></view>
- </view>
- <view v-else style="width: 180rpx;margin-right: 30rpx;">
- <view v-if="send_index<=0&&!sending_file" class="text-11px text-grey">{{writeTip}}</view>
- <view v-else class="text-11px text-grey">{{writeTip}}</view>
- </view>
- </view>
- <!-- <view class="flex">
- <view style="margin: 20rpx; font-size: 12px; color: grey;">发送和接收的次数:send:{{send_index}}</view>
- <view style="margin: 20rpx; font-size: 12px;" class="make-text-bPurple">time:{{send_index/100}}</view>
- </view> -->
- <!-- <scroll-view class="text-box" scroll-y="true">
- <scroll-view scroll-x="true">
- <text selectable="true">{{text}}</text>
- </scroll-view>
- </scroll-view> -->
- </view>
- </view>
- </template>
- <script>
- import config from '@/common/config.js'
- import reqUtil from '@/util/util-js/requstUtil.js';
- import {
- mapState,
- mapMutations
- } from 'vuex';
- export default {
- computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound',
- 'BLEConnectDevice', 'cIndex'
- ]),
- data() {
- return {
- /**
- * OTA config
- */
- UUID_OTA_SERVICE: "f000ffc0-0451-4000-b000-000000000000", //OTA 服务对应的服务的 UUID
- UUID_IDENTFY: "f000ffc1-0451-4000-b000-000000000000", //OTA 版本特征值的UUID,读取版本就是写向这个特征值写入:0x00
- UUID_BLOCK: "f000ffc2-0451-4000-b000-000000000000", //OTA 写bin文件特征值UUID,发送bin文件就是写这个特征值
- /*****/
- boxingList: [{
- cname: "蓝牙手柄",
- ename: "BLEHandle",
- icon: "/static/devicesIcon/handle.png",
- mIcon: "/static/devicesIcon/handle.png",
- bRatio: false,
- limitType: 'noRebound', //app处理蓝牙发送的数据
- deviceType: 'BLEHandle', //指的是外部蓝牙,目前定义为BLEHandle
- deviceName: 'PBox', //连接的设备名称
- limitDis: 100, //
- primaryUUID: 'f000ffc0', //OTA升级主服务id
- currentVersion: '', //当前固件版本
- latestVersion: '', //服务器最新固件版本
- currentRomVersion: '',
- latestRomVersion: '',
- UUID_OTA_SERVICE: "f000ffc0-0451-4000-b000-000000000000", //OTA 服务对应的服务的 UUID
- UUID_IDENTFY: "f000ffc1-0451-4000-b000-000000000000", //OTA 版本特征值的UUID,读取版本就是写向这个特征值写入:0x00
- UUID_BLOCK: "f000ffc2-0451-4000-b000-000000000000", //OTA 写bin文件特征值UUID,发送bin文件就是写这个特征值
- }],
- devicesList: [],
- downLoadOTA: '',
- //文件数据
- bin_buffer: [],
- bin_file_len: 0,
- bin_byteLength: 0,
- bin_total: 0,
- bin_offset: 0,
- //当前发送的下标
- send_index: 0,
- sending_file: false,
- // 当前进度
- percentage: 0,
- //是否可以发送
- bSend: true,
- //是否可以更新
- bCanUpdate: false,
- //是否开始
- bStart: false,
- bListenValueChange: false,
- //测试
- text: '',
- extraLine: [],
- //timeout 对象
- writeHeadTimeout: null,
- backTimeout: null,
- sendFileHeaderTimeout: null,
-
- writeTip:'准备写入数据..',
-
- bBacking:false,
- }
- },
- onLoad(option) {
- uni.$on('callbackCloseBLE', this.updateCallbackCloseBLE);
- this.devicesList = this.boxingList;
- //比如现在BLEHandle 对应的 1,
- //获得游戏列表
- reqUtil
- .requestData(config.URL.GETOTA, {
- bleType: 1, //目前只有一种设备,就是 BLEHandle,todo 后面如果设备多了需要设备多种ota
- })
- .then(
- res => {
- console.log('GETOTA =====', res);
- if (res.code == 0) {
- //设置对应的下载地址
- this.downLoadOTA = res.data.otaDownload;
- //读取需要更新的设备,存储在数据库
- this.onCheckServerFiles();
- this.onCheckDeviceFiles();
-
- uni.showToast({
- title: '获取信息...',
- icon: 'loading',
- duration: 20000
- })
- }
- },
- e => {
- console.log(e);
- }
- );
- },
- onUnload() {
- uni.$off('callbackCloseBLE', this.updateCallbackCloseBLE);
- //如果是获取版本指令timeout
- if (this.writeHeadTimeout) {
- clearTimeout(this.writeHeadTimeout);
- this.writeHeadTimeout = null;
- }
- //回退timeout
- if (this.backTimeout) {
- clearTimeout(this.backTimeout);
- this.backTimeout = null;
- }
- //发送头指令timeout
- if (this.sendFileHeaderTimeout) {
- clearTimeout(this.sendFileHeaderTimeout);
- this.sendFileHeaderTimeout = null;
- }
-
- this.onBack();
- },
- onShow() {
-
- },
- onHide() {},
- methods: {
- ...mapMutations(['initAdapter', 'onCloseBLEConnection',
- 'onGetRSSITransDistance'
- ]),
- updateCallbackCloseBLE() {
- this.onBack();
- },
- onBack() {
- // if(this.bStart){
- // }
- if(this.bBacking)return;
- this.bBacking = true;
- //停止发送数据
- this.onStop();
- console.log('onBack:',this.BLEConnectDevice);
- //关闭当前的蓝牙连接
- this.onCloseBLEConnection({
- getSuccess: () => {
- console.log("close ble");
- this.$store.state.bConnection = false;
- }
- });
- console.log('onBack');
-
- this.$store.state.cIndex = -1;
- this.$store.state.BLEConnectDevice = null;
- this.$store.state.BLEGetServices = null;
- this.$store.state.bOpenBluetooth = false;
- uni.navigateBack({
- delta: 1
- })
- },
- //获取版本
- onCheckDeviceFiles() {
- // console.log('onCheckDeviceFiles:', this.BLEConnectDevice);
- //直接连接对应服务
- this.notifyBLECharacteristicValueChange({
- deviceId: this.BLEConnectDevice.deviceId,
- serviceId: this.UUID_OTA_SERVICE,
- characteristicId: this.UUID_IDENTFY
- });
- },
- //开始写数据
- onSendConnect() {
- uni.showModal({
- title: '更新提示',
- content: '设备更新成功后会自动关闭,是否继续更新?',
- success: (resValue) => {
- if (resValue.confirm) {
- //直接连接对应服务
- this.notifyBLECharacteristicValueChange({
- deviceId: this.BLEConnectDevice.deviceId,
- serviceId: this.UUID_OTA_SERVICE,
- characteristicId: this.UUID_BLOCK
- });
- uni.showToast({
- title: '请求硬件更新数据...',
- icon: 'none',
- duration: 3000,
- mask:true
- })
- //发送头指令timeout
- if (this.sendFileHeaderTimeout) {
- clearTimeout(this.sendFileHeaderTimeout);
- this.sendFileHeaderTimeout = null;
- }
- this.sendFileHeaderTimeout = setTimeout(() => {
- let retryCount = 3;
- this.sendFileHeader(retryCount);
- // this.send_index = -1;
- }, 3000)
- this.bStart = true;
- }
- }
- })
- },
- //启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。
- //注意:必须设备的特征值支持notify或者indicate才可以成功调用,具体参照 characteristic 的 properties 属性
- notifyBLECharacteristicValueChange(context) {
- var _self = this;
- let {
- deviceId,
- serviceId,
- characteristicId
- } = context;
- // 启用notify功能
- uni.notifyBLECharacteristicValueChange({
- state: true,
- deviceId: deviceId,
- serviceId: serviceId,
- characteristicId: characteristicId,
- success: function(res) {
- if (!_self.bListenValueChange) {
- _self.onBLECharacteristicValueChange(); //监听特征值变化
- _self.bListenValueChange = true;
- }
- if (characteristicId == _self.UUID_IDENTFY) {
- //重发3次
- _self.onGetDeviceVersion(context,3);
- }
- },
- fail: function(res) {
- uni.showToast({
- title: 'notify启动失败',
- icon: "none",
- mask: true
- });
- }
- })
- },
-
- onGetDeviceVersion(context,retryCount){
- let _self = this;
- let {
- deviceId,
- serviceId,
- characteristicId
- } = context;
- //如果是获取版本,延迟写一个指令
- if (_self.writeHeadTimeout) {
- clearTimeout(_self.writeHeadTimeout);
- _self.writeHeadTimeout = null;
- }
- _self.writeHeadTimeout = setTimeout(() => {
-
- // 向蓝牙设备发送一个0x00的16进制数据
- let bufferDevice = new ArrayBuffer(1)
- let dataViewDevice = new DataView(bufferDevice)
- dataViewDevice.setUint8(0, 0)
-
- uni.writeBLECharacteristicValue({
- deviceId: deviceId,
- serviceId: serviceId,
- characteristicId: characteristicId,
- value: bufferDevice,
- success:(res) => {
- console.log('获取版本:writeBLECharacteristicValue success', res.errMsg ," == " ,retryCount)
- },
- fail:(fail)=>{
- console.error('获取版本:writeBLECharacteristicValue fail', fail.errMsg ," == " ,retryCount)
-
- if(retryCount>0){
- retryCount--;
- setTimeout(()=>{
- _self.onGetDeviceVersion(context,retryCount);
- },300);
- return;
- }
-
-
- uni.showModal({
- title:'获取固件版本',
- content:'获取不成功,是否重新获取',
- success: (resValue) => {
- if(resValue.confirm){
- _self.onGetDeviceVersion(context,retryCount);
- }
- }
- })
- }
- })
- }, 3000)
- },
- //监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification。
- onBLECharacteristicValueChange() {
- var _self = this;
- // ArrayBuffer转16进制字符串示例
- // console.log("onBLECharacteristicValueChange");
- uni.onBLECharacteristicValueChange(function(res) {
- if (res.characteristicId.toLocaleLowerCase() === _self.UUID_IDENTFY) {
- //如果硬件的版本获取版本
- var resValue = ab2hext(res.value); //16进制字符串
- // console.log("Device 版本:", resValue.substr(0, 4), "Rom 版本号:", resValue.substr(16, 4));
- _self.devicesList[0].currentRomVersion = resValue.substr(16, 4);
- _self.devicesList[0].currentVersion = resValue.substr(0, 4);
- //读取到硬件版本,判断是否需要更新
- if (_self.devicesList[0].currentVersion === _self.devicesList[0].latestVersion && _self.devicesList[0].currentRomVersion ===
- _self.devicesList[0].latestRomVersion) {
- uni.showToast({
- title: '版本一致,无需升级',
- icon: 'none',
- mask: true,
- })
- _self.bCanUpdate = false;
- return;
- }
- uni.showToast({
- title: '获取信息成功',
- icon: 'success',
- mask: true,
- })
- //设置状态
- _self.bCanUpdate = true;
- } else {
- uni.hideToast();
- //成功发送头文件会走一个回调
- let uint8_buf = new DataView(res.value);
- _self.send_index = ((uint8_buf.getUint8(1) << 8) | uint8_buf.getUint8(0));
- console.log("val===>> data:" + ab2hext(res.value) + ",rv index:" + _self.send_index + _self.sending_file)
- if (_self.sending_file == false) {
- _self.sending_file = true;
- // console.log("SendBlePkg");
- _self.writeTip = "正在写入设备...";
- _self.SendBlePkg();
- }
- }
- });
- },
- //下载后读取
- onCheckServerFiles() {
- this.bin_buffer = [];
- this.bin_offset = 0;
- this.bin_total = 0;
- this.reset();
- uni.request({
- url: this.downLoadOTA,
- method: "GET",
- responseType: "arraybuffer",
- success: (res) => {
- //记录二进制数据
- this.bin_buffer = res.data;
- let binData = new Uint8Array(this.bin_buffer, 0, 16); //new Uint8Array(this.bin_buffer);
- this.bin_file_len = (binData[7] << 8) | binData[6]
- this.bin_total = (this.bin_file_len) / 4
- this.bin_byteLength = this.bin_buffer.byteLength;
- function uint8_to_hex_str(data) {
- var v = data.toString(16).slice(-2) + ''
- if (v == '0') {
- v = '00'
- }
- if (v.length == 1) {
- v = '0' + v
- }
- return v
- }
- // console.log("ver2:", uint8_to_hex_str(binData[4]) + uint8_to_hex_str(binData[5]), ",rom:", uint8_to_hex_str(
- // binData[14]) +
- // uint8_to_hex_str(binData[15]));
- //显示信息
- this.devicesList[0].latestRomVersion = uint8_to_hex_str(binData[14]) + uint8_to_hex_str(binData[15]); //str.substr(28, 2);
- this.devicesList[0].latestVersion = uint8_to_hex_str(binData[4]) + uint8_to_hex_str(binData[5]); //str.substr(8, 2);
- },
- fail: (fail) => {
- uni.showToast({
- title: '下载固件失败..',
- icon: 'none'
- });
- }
- });
- },
- //发送文件头信息(配置信息)
- sendFileHeader(retryCount) {
- console.log('---------- sendFileHeader --------------')
- let _self = this
- let buffer = new ArrayBuffer(16)
- let dataView = new DataView(buffer)
- var b = new Uint8Array(_self.bin_buffer, 0, 16);
- for (var i = 0; i < 16; i++) {
- dataView.setUint8(i, b[i])
- }
- console.log('send header:' + ab2hext(buffer))
- // return;
- uni.writeBLECharacteristicValue({
- deviceId: _self.BLEConnectDevice.deviceId,
- serviceId: _self.UUID_OTA_SERVICE.toLocaleUpperCase(),
- characteristicId: _self.UUID_IDENTFY.toLocaleUpperCase(),
- value: buffer,
- success: function(res) {
- // _self.send_index = -1
- // _self.sending_file = false
- uni.showToast({
- title: '准备更新数据...',
- icon: 'none',
- duration: 3000,
- mask:true
- })
- console.log('开始发送文件success', _self.sending_file)
- // _self.SendBlePkg()
- },
- fail: function(err) {
- console.log('read version fail! ' + JSON.stringify(err)+retryCount)
-
- if(retryCount>0){
- retryCount--;
- setTimeout(()=>{
- _self.sendFileHeader(retryCount);
- },300);
- return;
- }
-
- uni.showModal({
- title:'请求更新',
- content:'请求更新失败,是否重新尝试继续。',
- success: (resValue) => {
- if(resValue.confirm){
- _self.sendFileHeader(retryCount);
- }else if(resValue.cancel){
- _self.bStart = false;
- }
- }
- })
- }
- })
- },
- //发送一包数据
- SendBlePkg() {
- var _self = this;
- if (!_self.bSend) return;
- if (_self.send_index == -1) {
- return
- }
- if (_self.send_index >= _self.bin_total) {
- _self.percentage = 100;
- uni.showToast({
- title: "更新完成",
- duration: 2000,
- mask: true
- })
- //回退timeout
- if (this.backTimeout) {
- clearTimeout(this.backTimeout);
- this.backTimeout = null;
- }
- this.backTimeout = setTimeout(() => {
- //更新成功后设备会断开,此时回退出此页面
- uni.hideToast();
- _self.onBack();
- }, 1500)
- return;
- }
- _self.percentage = parseInt(_self.send_index / _self.bin_total * 100);
- //分包长度,16字节有效数据(总数据/16=次数)
- let length = 16;
- //发送每包数据的结构:2个字节的序号 + 16个字节的有效数据
- //设置数据包序号
- let lo = getLow8(_self.send_index);
- let hi = getHeight8(_self.send_index);
- //最要一个18字节的包
- let send_ab = new ArrayBuffer(18);
- let a = new Uint8Array(send_ab);
- //2个字节的序号
- a[0] = lo;
- a[1] = hi;
- if (_self.bin_offset + length > _self.bin_buffer.byteLength) {
- length = _self.bin_buffer.byteLength - _self.bin_offset;
- }
- var b = new Uint8Array(_self.bin_buffer, _self.bin_offset, length);
- a.set(b, 2);
- uni.writeBLECharacteristicValue({
- deviceId: _self.BLEConnectDevice.deviceId,
- serviceId: _self.UUID_OTA_SERVICE, // _deviceId,
- characteristicId: _self.UUID_BLOCK,
- value: send_ab,
- fail: function(res) {
- console.log('write BLEChar fail')
- _self.SendBlePkg();
- },
- success: function(res) {
- _self.send_index++;
- //发送的有效字节数
- _self.bin_offset += length;
- // _self.sending_file = false;
- _self.SendBlePkg();
- // console.log('success')
- },
- })
- },
- reset() {
- //发送字节的下标
- this.send_index = 0;
- //发送的有效字节数
- this.bin_offset = 0;
- this.percentage = 0;
- this.bSend = true;
- },
- onStop() {
- this.bSend = false;
- },
- onReadFile(url) {
- //游戏列表
- // firmwareList: [{
- // name: "24_LYY_3431Q_H2_S3_bk3435_ble_app_oad.bin",
- // downloadUrl: "https://www.yuyekeji.cn/Cocos/bin_version/24_LYY_3431Q_H2_S3_bk3435_ble_app_oad.bin",
- // fileName: "bin_version",
- // suffix: "bin",
- // type: "bin",
- // task: null,
- // progressVal: 0,
- // bFileExists: false,
- // bDownload: false,
- // }],
- // data URIs to buffer
- function dataURL2Buffer(dataURI) {
- var byteStr
- var intArray
- var ab
- var i
- var mimetype
- var parts
- parts = dataURI.split(',')
- parts[1] = parts[1].replace(/\s/g, '')
- // console.log(parts);
- if (~parts[0].indexOf('base64')) {
- byteStr = atob(parts[1])
- } else {
- byteStr = decodeURIComponent(parts[1])
- }
- // // ab = new ArrayBuffer(byteStr.length)
- // intArray = new Uint8Array(byteStr.length)
- // for (i = 0; i < byteStr.length; i++) {
- // intArray[i] = byteStr.charCodeAt(i)
- // }
- // console.log(intArray.length, intArray.byteLength);
- return byteStr;
- }
- let item = this.firmwareList[0];
- console.log("url:" + JSON.stringify(url));
- plus.io.resolveLocalFileSystemURL(url, function(entry) {
- entry.file(function(file) {
- var fileReader = new plus.io.FileReader();
- fileReader.readAsDataURL(file);
- fileReader.onloadend = function(evt) {
- // console.log("_buffer1:" + JSON.stringify(evt.target.result));
- // let _arrayBuffer = dataURL2Buffer(evt.target.result)
- let parts = evt.target.result.split(',')
- let _arrayBuffer = parts[1].replace(/\s/g, '')
- console.log("_buffer1:" + JSON.stringify(_arrayBuffer));
- let _buffer = uni.base64ToArrayBuffer(_arrayBuffer);
- console.log("_buffer2:", JSON.stringify(_buffer));
- console.log("_buffer:", _buffer.length, _buffer.byteLength, _buffer.byteOffset);
- }
- });
- }, function(e) {
- console.log("Resolve file URL failed: " + e.message);
- });
- },
- //下载固件
- downLoadBin() {
- let _self = this;
- let item = this.firmwareList[0];
- let downloadUrl = item.downloadUrl;
- let _filename = '_doc/' + item.name; //+'/'+item.name
- console.log(_filename, downloadUrl);
- item.task = plus.downloader.createDownload(downloadUrl, {
- filename: _filename
- });
- item.task.addEventListener("statechanged", (download, status) => {
- // console.log("status", status);
- //连接服务器后
- // no default
- switch (download.state) {
- case 1:
- console.log('开始下载');
- // item.bDownload = true;
- break;
- case 2:
- console.log('链接到服务器...');
- item.bDownload = true;
- break;
- case 3:
- if (status !== 200) {
- uni.showToast({
- title: 'status=' + status
- })
- return;
- }
- item.progressVal = Math.ceil(download.downloadedSize / download.totalSize * 100);
- // _this.precent = progressVal;
- console.log(item.progressVal);
- break;
- case 4:
- // 下载完成
- console.log("监听下载 success: " + download.getFileName());
- setTimeout(() => {
- item.bDownload = false;
- }, 500)
- break;
- }
- }, false);
- item.task.start();
- },
- add: function(data) {
- // console.log(data)
- if (this.extraLine.length > 500) {
- this.extraLine = [];
- this.text = this.extraLine.join('\n');
- }
- this.extraLine.push(data);
- this.text = this.extraLine.join('\n');
- },
- remove: function(e) {
- if (this.extraLine.length > 0) {
- // this.extraLine.pop();
- this.extraLine = [];
- this.text = this.extraLine.join('\n');
- }
- },
- }
- }
- function ab2hext(buffer) {
- var hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function(bit) {
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join('');
- }
- function getHeight8(data) { //获取高8位
- let height;
- height = (data >> 8);
- return height;
- }
- function getLow8(data) { //获取低8位
- let low;
- low = (data & 0xFF);
- return low;
- }
- </script>
- <style>
- .hardware-border {
- border: 1rpx solid #9898FF;
- box-sizing: border-box;
- }
- .text-box {
- margin: 20rpx;
- display: flex;
- width: 95%;
- min-height: 300rpx;
- max-height: 600rpx;
- background-color: #EEEEEE;
- justify-content: center;
- align-items: center;
- text-align: center;
- font-size: 30upx;
- color: #353535;
- line-height: 1.8;
- border: 1rpx solid #555555;
- overflow-y: hidden;
- }
- </style>
|