2 lines
15 KiB
JavaScript

if("undefined"==typeof Promise||Promise.prototype.finally||(Promise.prototype.finally=function(e){const t=this.constructor;return this.then((a=>t.resolve(e()).then((()=>a))),(a=>t.resolve(e()).then((()=>{throw a}))))}),"undefined"!=typeof uni&&uni&&uni.requireGlobal){const e=uni.requireGlobal();ArrayBuffer=e.ArrayBuffer,Int8Array=e.Int8Array,Uint8Array=e.Uint8Array,Uint8ClampedArray=e.Uint8ClampedArray,Int16Array=e.Int16Array,Uint16Array=e.Uint16Array,Int32Array=e.Int32Array,Uint32Array=e.Uint32Array,Float32Array=e.Float32Array,Float64Array=e.Float64Array,BigInt64Array=e.BigInt64Array,BigUint64Array=e.BigUint64Array}uni.restoreGlobal&&uni.restoreGlobal(Vue,weex,plus,setTimeout,clearTimeout,setInterval,clearInterval),function(e){"use strict";function t(e,t,...a){uni.__log__?uni.__log__(e,t,...a):console[e].apply(console,[...a,t])}const a=(e,t)=>{const a=e.__vccOpts||e;for(const[n,i]of t)a[n]=i;return a};const n=a({data:()=>({foundDevices:[],isScanning:!1,bluetoothEnabled:!1,connectedDeviceId:""}),onLoad(){this.initBluetooth()},onUnload(){this.stopScan(),uni.closeBluetoothAdapter(),uni.offBluetoothDeviceFound(this.onDeviceFound)},methods:{initBluetooth(){uni.openBluetoothAdapter({success:()=>{this.bluetoothEnabled=!0},fail:e=>{this.bluetoothEnabled=!1,uni.showModal({title:"蓝牙开启失败",content:"请检查设备蓝牙是否开启",showCancel:!1}),t("error","at pages/index/index.vue:88","蓝牙初始化失败:",e)}})},toggleScan(){this.bluetoothEnabled?this.isScanning?this.stopScan():this.startScan():this.initBluetooth()},startScan(){this.isScanning=!0,this.foundDevices=[],uni.startBluetoothDevicesDiscovery({services:[],allowDuplicatesKey:!1,success:()=>{uni.showToast({title:"开始扫描",icon:"none"}),uni.onBluetoothDeviceFound(this.onDeviceFound)},fail:e=>{this.isScanning=!1,uni.showToast({title:"扫描失败",icon:"none"}),t("error","at pages/index/index.vue:124","扫描失败:",e)}}),setTimeout((()=>{this.isScanning&&this.stopScan()}),5e3)},stopScan(){this.isScanning&&uni.stopBluetoothDevicesDiscovery({success:()=>{this.isScanning=!1,0!=this.foundDevices.length?uni.showToast({title:`扫描完成,发现${this.foundDevices.length}台设备`,icon:"none"}):uni.showToast({title:"暂未扫描到任何设备",icon:"none"})},fail:e=>{t("error","at pages/index/index.vue:154","停止扫描失败:",e)}})},onDeviceFound(e){(e.devices||[]).forEach((e=>{if(!e.deviceId)return;if(this.foundDevices.some((t=>t.deviceId===e.deviceId)))return;var t=!1;const a=new Uint8Array(e.advertisData).slice(2,6);"dzbj"==String.fromCharCode(...a)&&(t=!0),this.foundDevices.push({is_bj:t,name:e.name||"未知设备",deviceId:e.deviceId,rssi:e.RSSI,connected:e.deviceId===this.connectedDeviceId})}))},getRssiWidth(e){let t=(e- -100)/70;return t=Math.max(0,Math.min(1,t)),100*t+"%"},connectDevice(e){var t=this;this.stopScan(),uni.showLoading({title:"连接中..."}),uni.createBLEConnection({deviceId:e.deviceId,success(a){t.foundDevices=t.foundDevices.map((t=>({...t,connected:t.deviceId===e.deviceId}))),uni.hideLoading(),uni.showToast({title:`已连接${e.name}`,icon:"none"}),uni.navigateTo({url:"../connect/connect?deviceId="+e.deviceId,fail:t=>{uni.showToast({title:"连接失败,请稍后重试",icon:"error"}),uni.closeBLEConnection({deviceId:e.deviceId,success(){console("断开连接成功")}})}})},fail(){uni.hideLoading(),uni.showToast({title:"连接失败",icon:"error"})}})}}},[["render",function(t,a,n,i,c,o){return e.openBlock(),e.createElementBlock("view",{class:"container"},[e.createElementVNode("view",{class:"title-bar"},[e.createElementVNode("text",{class:"title"},"连接设备"),e.createElementVNode("button",{class:"scan-btn",disabled:c.isScanning,onClick:a[0]||(a[0]=(...e)=>o.toggleScan&&o.toggleScan(...e))},e.toDisplayString(c.isScanning?"停止扫描":"开始扫描"),9,["disabled"])]),c.isScanning?(e.openBlock(),e.createElementBlock("view",{key:0,class:"status-tip"},[e.createElementVNode("text",null,"正在扫描设备..."),e.createElementVNode("view",{class:"loading"})])):0===c.foundDevices.length?(e.openBlock(),e.createElementBlock("view",{key:1,class:"status-tip"},[e.createElementVNode("text",null,'未发现设备,请点击"开始扫描"')])):e.createCommentVNode("",!0),e.createElementVNode("view",{class:"device-list"},[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(c.foundDevices,((t,a)=>(e.openBlock(),e.createElementBlock("view",{class:"device-item",key:t.deviceId,onClick:e=>o.connectDevice(t)},[e.createElementVNode("view",{class:"device-name"},[e.createElementVNode("text",null,e.toDisplayString(t.name||"未知设备"),1),t.is_bj?(e.openBlock(),e.createElementBlock("view",{key:0,class:"is_bj"},"吧唧")):e.createCommentVNode("",!0),e.createElementVNode("text",{class:"device-id"},e.toDisplayString(t.deviceId),1)]),e.createElementVNode("view",{class:"device-rssi"},[e.createElementVNode("text",null,"信号: "+e.toDisplayString(t.rssi)+" dBm",1),e.createElementVNode("view",{class:"rssi-bar",style:e.normalizeStyle({width:o.getRssiWidth(t.rssi)})},null,4)]),t.connected?(e.openBlock(),e.createElementBlock("view",{key:0,class:"device-status"},[e.createElementVNode("text",{class:"connected"},"已连接")])):e.createCommentVNode("",!0)],8,["onClick"])))),128))])])}],["__scopeId","data-v-a57057ce"]]);const i=a({data:()=>({uploadedImages:[],currentImageUrl:"",currentImageIndex:-1,imageDir:"",isConnected:!0,isScanning:!1,batteryLevel:0,temperature:0,humidity:0,faceStatus:0,deviceId:"",imageServiceuuid:"",imageWriteuuid:"",dataTimer:null}),computed:{faceStatusText(){switch(this.faceStatus){case 0:return"正常";case 1:return"更新中";case 2:return"异常";default:return"未知"}},batteryColor(){return this.batteryLevel>70?"#52c41a":this.batteryLevel>30?"#faad14":"#ff4d4f"}},onLoad(e){this.deviceId=e.deviceId,this.initImageDir(),this.loadSavedImages(),this.setBleMtu()},onUnload(){this.stopDataSimulation(),this.isConnected&&this.disconnectDevice()},methods:{writeBleImage(){},getBleService(){var e=this;uni.getBLEDeviceServices({deviceId:e.deviceId,success(t){e.imageServiceuuid=t.services[2].uuid,e.getBleChar()},fail(){t("log","at pages/connect/connect.vue:224","获取服务Id失败")}})},getBleChar(){var e=this;uni.getBLEDeviceCharacteristics({deviceId:e.deviceId,serviceId:e.imageServiceuuid,success(t){e.imageWriteuuid=t.characteristics[0].uuid},fail(){t("log","at pages/connect/connect.vue:237","获取特征Id失败")}})},setBleMtu(){var e=this;uni.setBLEMTU({deviceId:e.deviceId,mtu:512,success(){e.isConnected=!0,t("log","at pages/connect/connect.vue:249","MTU设置成功"),e.getBleService()},fail(){t("log","at pages/connect/connect.vue:253","MTU设置失败")}})},initImageDir(){const e=plus.io.convertLocalFileSystemURL("_doc/");this.imageDir=e+"watch_faces/",plus.io.resolveLocalFileSystemURL(this.imageDir,(()=>{}),(()=>{plus.io.resolveLocalFileSystemURL("_doc/",(e=>{e.getDirectory("watch_faces",{create:!0},(()=>{t("log","at pages/connect/connect.vue:268","目录创建成功")}))}))}))},loadSavedImages(){plus.io.resolveLocalFileSystemURL(this.imageDir,(e=>{e.createReader().readEntries((e=>{const t=[];e.forEach((e=>{e.isFile&&this.isImageFile(e.name)&&t.push({name:e.name,url:e.toLocalURL()})})),this.uploadedImages=t,t.length>0&&(this.currentImageUrl=t[0].url,this.currentImageIndex=0)}))}))},isImageFile(e){const t=e.toLowerCase().split(".").pop();return["jpg","jpeg","png","gif","bmp"].includes(t)},chooseImage(){uni.chooseImage({count:1,sizeType:["compressed"],sourceType:["album","camera"],success:e=>{t("log","at pages/connect/connect.vue:329",e);const a=e.tempFilePaths[0];uni.getFileSystemManager().readFile({filePath:a,encoding:"binary",success:e=>{this.imageBuffer=e.data,this.log="图片读取成功,大小:"+this.imageBuffer.byteLength+"字节"}}),this.saveImageToLocal(a)},fail:e=>{t("error","at pages/connect/connect.vue:343","选择图片失败:",e),uni.showToast({title:"选择图片失败",icon:"none"})}})},saveImageToLocal(e){const a=`face_${Date.now()}.png`;this.imageDir,plus.io.resolveLocalFileSystemURL(e,(e=>{plus.io.resolveLocalFileSystemURL(this.imageDir,(n=>{e.copyTo(n,a,(e=>{const t=e.toLocalURL();this.uploadedImages.push({name:a,url:t}),this.currentImageIndex=this.uploadedImages.length-1,this.currentImageUrl=t,uni.showToast({title:"图片保存成功",icon:"success"})}),(e=>{t("error","at pages/connect/connect.vue:368","保存图片失败:",e),uni.showToast({title:"保存图片失败",icon:"none"})}))}))}))},selectImage(e){this.currentImageIndex=e,this.currentImageUrl=this.uploadedImages[e].url},handleCarouselChange(e){const t=e.detail.current;this.currentImageIndex=t,this.currentImageUrl=this.uploadedImages[t].url},setAsCurrentFace(){this.faceStatus=1,setTimeout((()=>{this.faceStatus=0,uni.showToast({title:"表盘已更新",icon:"success"}),uni.setStorageSync("current_watch_face",this.currentImageUrl)}),1500)},toggleConnection(){this.isConnected?this.disconnectDevice():this.connectDevice()},connectDevice(){var e=this;this.isScanning=!0,uni.showToast({title:"正在连接设备...",icon:"none"}),uni.createBLEConnection({deviceId:e.deviceId,success(){e.isScanning=!1,e.isConnected=!0,uni.showToast({title:"设备连接成功",icon:"success"}),e.setBleMtu(),e.startDataSimulation()}})},disconnectDevice(){var e=this;uni.closeBLEConnection({deviceId:e.deviceId,success(){e.isConnected=!1,e.statusPotColor="red",uni.showToast({title:"已断开连接",icon:"none"}),e.stopDataSimulation()},fail(a){1e4==a&&uni.openBluetoothAdapter({success(){e.isConnected=!1},fail(){t("log","at pages/connect/connect.vue:464","初始化失败")}})}})},startDataSimulation(){this.dataTimer&&clearInterval(this.dataTimer),this.batteryLevel=Math.floor(30*Math.random())+70,this.temperature=Math.floor(10*Math.random())+20,this.humidity=Math.floor(30*Math.random())+40,this.dataTimer=setInterval((()=>{this.batteryLevel>1&&(this.batteryLevel=Math.max(1,this.batteryLevel-2*Math.random())),this.temperature=Math.max(15,Math.min(35,this.temperature+(2*Math.random()-1))),this.humidity=Math.max(30,Math.min(80,this.humidity+(4*Math.random()-2)))}),5e3)},stopDataSimulation(){this.dataTimer&&(clearInterval(this.dataTimer),this.dataTimer=null)}}},[["render",function(t,a,n,i,c,o){const s=e.resolveComponent("uni-icons");return e.openBlock(),e.createElementBlock("view",{class:"container"},[e.createElementVNode("view",{class:"nav-bar"},[e.createElementVNode("text",{class:"nav-title"},"表盘管理器")]),e.createElementVNode("view",{class:"content"},[e.createElementVNode("view",{class:"preview-container"},[e.createElementVNode("view",{class:"preview-frame"},[c.currentImageUrl?(e.openBlock(),e.createElementBlock("image",{key:0,src:c.currentImageUrl,class:"preview-image",mode:"aspectFill"},null,8,["src"])):(e.openBlock(),e.createElementBlock("view",{key:1,class:"empty-preview"},[e.createVNode(s,{type:"image",size:"60",color:"#ccc"}),e.createElementVNode("text",null,"请选择表盘图片")]))]),c.currentImageUrl?(e.openBlock(),e.createElementBlock("button",{key:0,class:"set-btn",onClick:a[0]||(a[0]=(...e)=>o.setAsCurrentFace&&o.setAsCurrentFace(...e))}," 设置为当前表盘 ")):e.createCommentVNode("",!0),e.createElementVNode("button",{class:"upload-btn",onClick:a[1]||(a[1]=(...e)=>o.chooseImage&&o.chooseImage(...e))},[e.createVNode(s,{type:"camera",size:"24",color:"#fff"}),e.createElementVNode("text",null,"上传图片")])]),e.createElementVNode("view",{class:"carousel-section"},[e.createElementVNode("text",{class:"section-title"},"已上传表盘"),e.createElementVNode("view",{class:"carousel-container"},[e.createElementVNode("swiper",{class:"card-swiper",circular:"","previous-margin":"200rpx","next-margin":"200rpx",onChange:a[2]||(a[2]=(...e)=>o.handleCarouselChange&&o.handleCarouselChange(...e))},[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(c.uploadedImages,((t,a)=>(e.openBlock(),e.createElementBlock("swiper-item",{key:a},[e.createElementVNode("view",{class:"card-item",onClick:e=>o.selectImage(a)},[e.createElementVNode("view",{class:"card-frame"},[e.createElementVNode("image",{src:t.url,class:"card-image",mode:"aspectFill"},null,8,["src"]),c.currentImageIndex===a?(e.openBlock(),e.createElementBlock("view",{key:0,class:"card-overlay"},[e.createVNode(s,{type:"checkmark",size:"30",color:"#007aff"})])):e.createCommentVNode("",!0)])],8,["onClick"])])))),128))],32),0===c.uploadedImages.length?(e.openBlock(),e.createElementBlock("view",{key:0,class:"carousel-hint"},[e.createElementVNode("text",null,"暂无上传图片,请点击上方上传按钮添加")])):e.createCommentVNode("",!0)])]),e.createElementVNode("view",{class:"data-section"},[e.createElementVNode("text",{class:"section-title"},"设备状态"),e.createElementVNode("view",{class:"data-grid"},[e.createElementVNode("view",{class:"data-card"},[e.createElementVNode("view",{class:"data-icon battery-icon"},[e.createVNode(s,{type:"battery",size:"36",color:o.batteryColor},null,8,["color"])]),e.createElementVNode("view",{class:"data-info"},[e.createElementVNode("text",{class:"data-value"},e.toDisplayString(c.batteryLevel)+"%",1),e.createElementVNode("text",{class:"data-label"},"电量")])]),e.createElementVNode("view",{class:"data-card"},[e.createElementVNode("view",{class:"data-icon temp-icon"},[e.createVNode(s,{type:"thermometer",size:"36",color:"#ff7a45"})]),e.createElementVNode("view",{class:"data-info"},[e.createElementVNode("text",{class:"data-value"},e.toDisplayString(c.temperature)+"°C",1),e.createElementVNode("text",{class:"data-label"},"温度")])]),e.createElementVNode("view",{class:"data-card"},[e.createElementVNode("view",{class:"data-icon humidity-icon"},[e.createVNode(s,{type:"water",size:"36",color:"#40a9ff"})]),e.createElementVNode("view",{class:"data-info"},[e.createElementVNode("text",{class:"data-value"},e.toDisplayString(c.humidity)+"%",1),e.createElementVNode("text",{class:"data-label"},"湿度")])]),e.createElementVNode("view",{class:"data-card"},[e.createElementVNode("view",{class:"data-icon status-icon"},[e.createVNode(s,{type:"watch",size:"36",color:"#52c41a"})]),e.createElementVNode("view",{class:"data-info"},[e.createElementVNode("text",{class:"data-value"},e.toDisplayString(o.faceStatusText),1),e.createElementVNode("text",{class:"data-label"},"表盘状态")])])]),e.createElementVNode("view",{class:"connection-status"},[e.createVNode(s,{type:"bluetooth",size:"24",color:c.isConnected?"#52c41a":"#ff4d4f",animation:c.isScanning?"spin":""},null,8,["color","animation"]),e.createElementVNode("view",{class:"status-pot",style:e.normalizeStyle({backgroundColor:c.isConnected?"Green":"red"})},null,4),e.createElementVNode("text",{class:"status-text"},e.toDisplayString(c.isConnected?"已连接设备":c.isScanning?"正在搜索设备...":"未连接设备"),1),e.createElementVNode("button",{class:"connect-btn",onClick:a[3]||(a[3]=(...e)=>o.toggleConnection&&o.toggleConnection(...e)),disabled:c.isScanning},e.toDisplayString(c.isConnected?"断开连接":"连接设备"),9,["disabled"])])])])])}],["__scopeId","data-v-6b60e2e0"]]);__definePage("pages/index/index",n),__definePage("pages/connect/connect",i);const c={onLaunch:function(){t("log","at App.vue:7","App Launch")},onShow:function(){t("log","at App.vue:10","App Show")},onHide:function(){t("log","at App.vue:13","App Hide")},onExit:function(){t("log","at App.vue:34","App Exit")}};const{app:o,Vuex:s,Pinia:l}={app:e.createVueApp(c)};uni.Vuex=s,uni.Pinia=l,o.provide("__globalStyles",__uniConfig.styles),o._component.mpType="app",o._component.render=()=>{},o.mount("#app")}(Vue);