123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892 |
- import {
- myCanvas
- } from '../canvasWrite_smoothing.js'
- const penDataHandle = require('../../tqlsdk/penDataHandle');
- const app = getApp()
- const tqlSDK = app.globalData.tqlSDK
- import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog"
- const event = require("../../tqlsdk/event")
- import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast"
- // import {
- // allData,
- // feiData,
- // myData
- // } from './data'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- // 科目
- subject: "",
- homeworkId: "",
- studentId: "",
- test: "",
- fullPage: 0,
- canvasHeight: 0,
- canvasWidth: 0,
- imgs: ['../images/page1.png', '../images/page2.png'],
- myCanvas: null,
- connnetFlag: false,
- showConnectPen: false, //显示已连接弹窗
- blueToothOn: false, //蓝牙开启
- deviceId: null,
- deviceName: null,
- deviceMac: null,
- basePic: [{
- name: 'penWidth',
- icon: './pics/penWidth.png',
- iconClick: "./pics/penWidthChosen.png",
- text: "线宽"
- }, {
- name: 'color',
- icon: "./pics/color.png",
- iconClick: "./pics/colorChosen.png",
- text: "颜色"
- }, {
- name: "clear",
- icon: "./pics/clear.png",
- iconClick: './pics/clearChosen.png',
- text: "清除"
- }, {
- name: "more",
- icon: "./pics/more.png",
- iconClick: "./pics/moreChosen.png",
- text: "更多"
- }],
- activeTab: '', //选中的tab
- penWidthImg: [{
- img: './penWidth/penWidth1.png'
- }, {
- img: './penWidth/penWidth2.png'
- }, {
- img: './penWidth/penWidth3.png'
- }, {
- img: './penWidth/penWidth4.png'
- }, {
- img: './penWidth/penWidth5.png'
- }],
- // 笔颜色数组
- penColors: [{
- color: '#151515'
- }, {
- color: '#FF4E4E'
- }, {
- color: '#1A81F9'
- }, {
- color: '#53D769'
- }, {
- color: '#FED02F'
- }, {
- color: '#FF8F21'
- }],
- chosenIndex: 0,
- optionImg: [{
- img: './optionPic/penRecognition.png',
- text: "笔迹识别"
- }, {
- img: './optionPic/offlineData.png',
- text: '离线数据'
- }, {
- img: './optionPic/penInfo.png',
- text: '笔参数'
- }, {
- img: './optionPic/otaUpdate.png',
- text: "OTA升级"
- }],
- offlineImg: [{
- img: './optionPic/getOffline.png',
- text: '获取数据'
- }, {
- img: './optionPic/stopGet.png',
- text: "停止获取"
- }, {
- img: './optionPic/clearOffline.png',
- text: "清除数据"
- }, {
- img: './optionPic/offlineNum.png',
- text: '离线数据量'
- }],
- // 打开离线数据选项
- checkOffline: false,
- // 笔迹宽度
- penWidthIndex: 0,
- currentPage: 0,
- offlineDataNum: null,
- // 获取离线数据弹窗按钮
- buttonText: "暂停",
- showDialog: false,
- offlineProcess: 0,
- // 控制离线数据dialog展示变量
- showOfflineDialog: false,
- // 暂停继续离线数据flag
- continueFlag: true,
- localFileList: null,
- showFileListFlag: false,
- identifyAnswer: "", //识别结果
- showIdentify: false, //展示识别结果
- penBattery: null, //笔电量
- offFlag: false, //用户主动断开flag
- activeTabBar: 0, //激活栏
- codeList: [], //点读码列表
- // codeList: Array.from(new Array(5000).keys()).map(i => ({
- // num: i,
- // code: i,
- // })), //点读码列表
- templateCodeList: [],
- templateCodeListPage: 1,
- invalidCodeNum: 0, //无效码数量
- isIphoneX: false
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- // console.log(options)
- this.setData({
- studentId: options.studentId,
- homeworkId: options.homeworkId,
- subject: options.subject
- })
- let modelmes = wx.getStorageSync('modelmes');
- let isIphoneX = app.globalData.isIphoneX;
- this.setData({
- isIphoneX: isIphoneX
- })
- // 页面重加载时将所有的监听事件移除一遍在监听 防止相同事件多次重复监听
- wx.getSystemInfo({
- success: (result) => {
- if (isIphoneX) {
- this.setData({
- canvasHeight: result.windowHeight - 140 - 34,
- canvasWidth: result.windowWidth,
- fullPage: result.windowHeight
- })
- } else {
- this.setData({
- canvasHeight: result.windowHeight - 140,
- canvasWidth: result.windowWidth,
- fullPage: result.windowHeight
- })
- }
- let canvas = new myCanvas('#myCanvas', this.data.canvasWidth, this.data.isIphoneX ? this.data.canvasHeight - 140 - 33 : this.data.canvasHeight - 140, this.data.imgs)
- // 初始化canvas
- this.setData({
- myCanvas: canvas
- })
- },
- })
- // tqlSDK.fs.writeFile({
- // filePath: `${wx.env.USER_DATA_PATH}/testData.txt`,
- // encoding: "utf-8",
- // success: () => {
- // console.log('writeFileSuccess');
- // },
- // fail: () => {
- // console.log('writeFileFail');
- // }
- // })
- // event.remove('blueToothConnect')
- // 获取 是否偏移 和 码点类型12*12 / 16*16
- event.on('isOffset', this, (data) => {
- this.data.myCanvas.setCodePointType(data.codeType)
- switch (data.codeType) {
- case 12:
- this.data.myCanvas.setActualSize(182, 256)
- break;
- case 16:
- this.data.myCanvas.setActualSize(179, 253)
- break;
- default:
- this.data.myCanvas.setActualSize(176, 250)
- break;
- }
- })
- event.on('blueToothConnect', this, () => {
- this.setData({
- deviceMac: app.globalData.deviceMac,
- deviceName: app.globalData.deviceName,
- deviceId: app.globalData.deviceId,
- connnetFlag: true
- })
- console.log('index.js', '连接成功!')
- // 获取笔的电量
- // setTimeout(() => {
- // this.startGetOfflineData()
- // this.getPenBattery()
- // }, 1000)
- })
- // 监听获取数据
- event.on('AddressDataChanged', this, data => {
- // tqlSDK.fs.appendFileSync(`${wx.env.USER_DATA_PATH}/testData.txt`, JSON.stringify(data)+',', 'utf-8')
- // console.log('index.js',data);
- if (this.data.activeTabBar === 1) {
- this.setData({
- activeTabBar: 0
- })
- }
- if (data) {
- this.data.myCanvas.usbData(data)
- }
- })
- // 监听笔的笔锋开关
- event.on('strokeSwitch', this, data => {
- this.data.myCanvas.changeStroke(data)
- })
- // 监听笔的返回值
- let timer = null
- event.on('penData', this, (data) => {
- switch (data.cmd) {
- // 获取离线数据量
- case "getOfflineDataNum":
- this.setData({
- offlineDataNum: data.data
- })
- setTimeout(() => {
- Dialog.alert({
- title: "离线数据量",
- message: `${this.data.offlineDataNum * 10}bytes`
- }).then()
- }, 2000)
- break
- // 离线数据进程
- case "offLineProgress":
- break
- // 停止获取离线数据
- case "offLineProgressStop":
- break
- // 确认离线数据全部接收完毕
- case "offLineDataConfirm":
- break
- // 删除离线数据
- case "offLineDataDelete":
- Toast.success('删除成功')
- break
- //获取笔的电量
- case "getPenBattery":
- this.setData({
- penBattery: data.data.penBattery
- })
- break
- // 在线点读码
- case "getOID4CodeValue":
- // 离线点读码
- case "offlineOID4codeValue": {
- // 更新tabber选中项
- if (this.data.activeTabBar !== 1) {
- this.setData({
- activeTabBar: 1
- })
- }
- const cmds = ['getOID4CodeValue', 'offlineOID4codeValue']
- // 在线 or 离线
- const isOffline = !cmds.indexOf(data.cmd)
- console.log(data)
- const item = {
- code: data.data,
- isOffline,
- num: this.data.codeList.flat().length + 1
- }
- this.data.codeList.push(item)
- if (this.data.codeList.length % 5 === 0) {
- this.setData({
- templateCodeList: this.data.codeList.slice(-50)
- })
- }
- clearTimeout(timer)
- timer = setTimeout(() => {
- this.setData({
- templateCodeList: this.data.codeList.slice(-50)
- })
- }, 500);
- break
- }
- case "invalidCode":
- Toast.fail('无效码')
- break
- default:
- break
- }
- })
- // 监听离线数据获取量
- event.on('offlineGross', this, data => {
- this.setData({
- offlineProcess: Math.floor(data / this.data.offlineDataNum * 100)
- })
- })
- // 监听离线数据接收完毕
- event.on('offlineDataFinish', this, data => {
- console.log('offlineDataFinish')
- if (data) {
- this.data.myCanvas.offlineFlag = false
- this.setData({
- showOfflineDialog: false
- })
- }
- })
- // 监听切页数据
- event.on('changePage', this, data => {
- this.setData({
- currentPage: data
- })
- })
- // 监听笔关机断开连接事件
- event.on('unConnect', this, data => {
- if (this.data.offFlag) {
- this.setData({
- offFlag: false
- })
- return
- }
- this.setData({
- connnetFlag: false,
- deviceId: null,
- deviceMac: null,
- deviceName: null
- })
- // 已连接deviceId
- tqlSDK.connectDeviceId = null
- // 已连接serviceid
- tqlSDK.connectServiceId = null
- // 已连接characteristicId
- tqlSDK.connectCharacteristicId = null
- event.emit('pageUnconnect', true)
- Dialog.alert({
- title: "提示",
- message: "蓝牙断开",
- }).then()
- })
- },
- scrollViewBottom() {
- this.data.templateCodeListPage++
- this.setData({
- templateCodeList: this.data.codeList.slice(-50 * this.data.templateCodeListPage).reverse()
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- setTimeout(() => {
- this.initBluetooth()
- // this.data.myCanvas.changePage(0)
- }, 100)
- },
- //连接笔或者查看已连接
- goConnect() {
- if (!this.data.blueToothOn) {
- Dialog.alert({
- title: "提示",
- message: "请先开启蓝牙"
- })
- return
- }
- if (this.data.showOfflineDialog) {
- return
- }
- if (this.data.blueToothOn && !this.data.connnetFlag) {
- wx.navigateTo({
- url: '../search/search',
- })
- return
- }
- this.setData({
- showConnectPen: true,
- })
- },
- saveCanvasImage(e) {
- console.log(this.data.myCanvas)
- this.data.myCanvas.saveCanvasImage(this.data.homeworkId, this.data.studentId, "student", this.data.subject);
- },
- gotoTeacher() {
- wx.navigateTo({
- url: `../teacher/teacher?flag=${false}`,
- })
- },
- // 初始化蓝牙
- initBluetooth() {
- tqlSDK.initBlueTooth(() => {
- Dialog.alert({
- title: '提示',
- message: '蓝牙开启成功'
- })
- this.setData({
- blueToothOn: true
- })
- }, (e) => {
- Dialog.alert({
- title: '提示',
- message: e.msg
- })
- tqlSDK.listenBlueState(() => {
- tqlSDK.initBlueTooth(() => {
- Dialog.alert({
- title: '提示',
- message: '蓝牙开启成功'
- })
- this.setData({
- blueToothOn: true
- })
- })
- })
- })
- },
- // 隐藏已连接笔
- hidePen() {
- this.setData({
- showConnectPen: false
- })
- },
- //切换笔
- changePen() {
- if (this.data.showOfflineDialog) {
- return
- }
- this.setData({
- offFlag: true
- })
- tqlSDK.unConnectBlueTooth(this.data.deviceId, () => {
- this.setData({
- deviceName: '',
- deviceMac: '',
- deviceId: null,
- showConnectPen: false,
- connnetFlag: false,
- })
- wx.navigateTo({
- url: '../search/search',
- })
- }, () => {
- })
- },
- // 继续获取离线数据
- continueGetOfflineData() {
- tqlSDK.continueGetOfflineData()
- },
- // 开始获取离线数据
- startGetOfflineData() {
- tqlSDK.startGetOfflineData(() => {
- }, () => {
- }, () => {
- Dialog.alert({
- title: '错误',
- message: '电量低于10%\/n无法获取离线数据',
- confirmButtonText: '知道了'
- }).then(() => {
- })
- })
- },
- // 获取离线数据量
- getOfflineNum() {
- tqlSDK.getPenOfflineDataSize()
- },
- // 停止获取离线数据
- stopGetOfflineData() {
- tqlSDK.stopGetOfflineData()
- },
- // 暂停获得离线数据
- pauseOfflineData() {
- tqlSDK.pauseGetOfflineData()
- },
- // 确认离线数据接收完毕
- confirmOfflineDataDone() {
- tqlSDK.confirmOfflineDataDone()
- },
- // 清除离线数据
- deleteOfflineData() {
- tqlSDK.deleteOfflineData()
- },
- // 切换tab
- changeTab(e) {
- if (!this.data.blueToothOn) {
- Dialog.alert({
- title: "提示",
- message: "请先打开蓝牙"
- })
- return
- }
- if (!this.data.connnetFlag) {
- Dialog.alert({
- confirmButtonText: '去连接',
- title: "智能笔",
- message: "请先连接智能笔",
- showCancelButton: true
- }).then(() => {
- wx.navigateTo({
- url: '../search/search',
- })
- }).catch()
- return
- }
- switch (e.detail) {
- case "clear":
- if (this.data.showOfflineDialog) {
- return
- }
- this.setData({
- activeTab: e.detail
- })
- Dialog.confirm({
- title: "警告",
- message: "此操作将清除所有书写数据"
- }).then(
- () => {
- Toast.loading({
- forbidClick: true,
- message: '正在清除'
- })
- if (this.data.activeTabBar === 0) {
- this.data.myCanvas.clear()
- } else {
- this.setData({
- codeList: []
- })
- }
- setTimeout(() => {
- Toast.success('清除成功')
- this.setData({
- activeTab: ""
- })
- }, 2000)
- }
- ).catch()
- break
- case "penWidth":
- case "color":
- case "more":
- if (this.data.activeTab === e.detail) {
- this.setData({
- activeTab: ''
- })
- return
- }
- this.setData({
- activeTab: e.detail
- })
- break
- default:
- break
- }
- },
- // 选择笔的宽度
- chosenPenwidth(e) {
- let penWidth = Number(e.currentTarget.dataset.index)
- if (penWidth === this.data.penWidthIndex) {
- setTimeout(() => {
- this.setData({
- activeTab: ''
- })
- }, 500)
- return
- }
- this.data.myCanvas.changeWidth(penWidth === 0 ? 0.5 : penWidth === 1 ? 1 : penWidth === 2 ? 1.5 : penWidth === 3 ? 2.5 : 3)
- this.setData({
- penWidthIndex: penWidth
- })
- setTimeout(() => {
- this.setData({
- activeTab: ''
- })
- }, 500)
- },
- // 选择笔的颜色
- changeColor(e) {
- let index = Number(e.currentTarget.dataset.index)
- this.data.myCanvas.changeColor(this.data.penColors[index].color)
- this.setData({
- chosenIndex: index
- })
- setTimeout(() => {
- this.setData({
- activeTab: ""
- })
- }, 500)
- },
- // 隐藏离线的侧边栏
- hideOffline() {
- this.setData({
- checkOffline: false
- })
- },
- // 更多的选项
- tapOption(e) {
- switch (e.currentTarget.dataset.index) {
- // 笔迹识别
- case 0:
- this.setData({
- activeTab: ''
- })
- this.penCloud()
- break
- case 1:
- this.setData({
- checkOffline: true
- })
- break
- case 2:
- this.setData({
- activeTab: ''
- })
- wx.navigateTo({
- url: `../penInfo/penInfo?flag=${this.data.myCanvas.strokeFlag}`,
- })
- break
- // Ota升级
- case 3:
- this.OTAupdate()
- break;
- // 读取txt
- case 4:
- console.log(wx.env.USER_DATA_PATH)
- // 读取本地USER_DATA_PATH下面的txt文档列表
- tqlSDK.fs.readdir({
- dirPath: wx.env.USER_DATA_PATH,
- success: (res) => {
- console.log(res);
- let fileList = res.files.filter((item) => {
- if (item.indexOf('.txt') !== -1 || item.indexOf('.TXT') !== -1) {
- return item
- }
- }).map(item => {
- return {
- name: item
- }
- })
- if (fileList.length === 0) {
- Toast.fail('指定目录下没有txt文件');
- return;
- }
- this.setData({
- activeTab: "",
- showFileListFlag: true,
- localFileList: fileList
- })
- },
- fail: (res) => {
- }
- })
- break;
- case 5:
- wx.navigateTo({
- url: '../about/about',
- });
- break;
- }
- },
- // 离线数据操作
- offlineDataActivity(e) {
- let index = Number(e.currentTarget.dataset.index)
- switch (index) {
- // 开始获取离线数据
- case 0:
- Dialog.alert({
- title: "警告",
- message: '获取离线中请勿插USB充电',
- confirmButtonText: '知道了,继续获取'
- }).then(() => {
- if (this.data.showOfflineDialog) {
- return
- }
- this.data.myCanvas.offlineFlag = true
- tqlSDK.getOfflineDataFlag = true
- this.setData({
- activeTab: '',
- showOfflineDialog: true,
- continueFlag: true
- })
- this.startGetOfflineData()
- })
- break;
- // 停止获取
- case 1:
- this.setData({
- activeTab: '',
- continueFlag: true,
- showOfflineDialog: false,
- offlineProcess: 0
- })
- this.data.myCanvas.offlineFlag = false
- tqlSDK.getOfflineDataFlag = false
- this.stopGetOfflineData()
- break;
- // 清除离线数据
- case 2:
- if (this.data.showOfflineDialog) {
- return
- }
- Dialog.confirm({
- title: '警告',
- message: '此操作将删除所有离线数据'
- }).then(() => {
- this.setData({
- activeTab: ''
- })
- this.deleteOfflineData()
- }).catch()
- break
- // 离线数据量
- case 3:
- if (this.data.showOfflineDialog) {
- return
- }
- this.setData({
- activeTab: ''
- })
- this.getOfflineNum()
- break
- default:
- break
- }
- },
- // 暂停继续下载离线数据
- processButton() {
- this.data.continueFlag ? this.pauseOfflineData() : this.continueGetOfflineData()
- this.setData({
- continueFlag: !this.data.continueFlag
- })
- },
- // ota升级
- OTAupdate() {
- tqlSDK.otaUpdate()
- },
- // 给蓝牙设备发送升级文件
- sendFile(ArrayBuffer) {
- wx.writeBLECharacteristicValue({
- characteristicId: tqlSDK.connectCharacteristicId,
- deviceId: tqlSDK.connectServiceId,
- serviceId: tqlSDK.connectDeviceId,
- value: ArrayBuffer,
- success: () => {
- console.log('success')
- },
- fail: () => {
- }
- })
- },
- // 读取本地文件
- readLocalFile(e) {
- // console.log(e)
- this.setData({
- showFileListFlag: false
- })
- // 将canvaswirte和sdk中的读取本地文件flag改为true
- const toast = Toast.loading({
- duration: 0,
- message: "正在读取本地文件",
- forbidClick: true
- })
- setTimeout(() => {
- tqlSDK.readLocalTxtFlag = true
- this.data.myCanvas.readLocalFileFlag = true
- let fileData = tqlSDK.fs.readFileSync(`${wx.env.USER_DATA_PATH}/${e.detail.name}`, 'utf-8')
- let singal = fileData.split(/[\s\n]/)
- toast.clear()
- let myArray = singal.map((currentValue, index) => {
- if (index !== singal.length - 1) {
- return parseInt(currentValue, 16)
- }
- })
- // console.log(myArray);
- let dataLength = Math.floor(myArray.length / 10)
- for (let i = 0; i < dataLength; i++) {
- penDataHandle.splitData(myArray.slice(i * 10, (i + 1) * 10))
- }
- // 读取完后将flag的值复原
- tqlSDK.readLocalTxtFlag = false
- this.data.myCanvas.readLocalFileFlag = false
- }, 200)
- },
- //隐藏文件栏
- hideFileList() {
- this.setData({
- showFileListFlag: false
- })
- },
- // 手写识别xxx
- penCloud() {
- let xPoint = []
- let yPoint = []
- let penData = []
- if (!this.data.myCanvas.mapData.get(this.data.myCanvas.lastPage)) {
- Toast.fail('请先书写');
- return
- }
- const loading = Toast.loading({
- message: '正在识别,请稍等',
- duration: 0
- })
- this.data.myCanvas.mapData.get(this.data.myCanvas.lastPage).forEach(item => {
- xPoint.push(item.xPoint)
- yPoint.push(item.yPoint)
- if (item.dotType === 'PEN_UP') {
- penData.push({
- x: xPoint.join(','),
- y: yPoint.join(",")
- })
- xPoint = []
- yPoint = []
- }
- })
- console.log(penData);
- wx.request({
- url: 'https://hwrapi.pencloud.cn/script',
- method: "POST",
- data: {
- viewSizeHeight: this.data.myCanvas.canvasHeight * 2, //画布高度
- viewSizeWidth: this.data.myCanvas.canvasWidth * 2, //画布宽度
- // applicationKey: 'a55a66c9-ae8f-482e-bb90-c2771390d388',
- applicationKey: '306d01c5-46d8-495a-9b72-edd9e0196c27',
- macAddress: tqlSDK.connectDeviceId.split(':').join(''),
- penData,
- scriptType: 'Text',
- languages: "zh_CN"
- },
- success: (res) => {
- console.log(res);
- if (res.data.code !== 0) {
- Toast.fail("识别失败")
- return
- }
- this.setData({
- showIdentify: true,
- identifyAnswer: res.data.data
- })
- },
- fail: (res) => {
- console.log(res);
- },
- complete: () => {
- loading.clear()
- }
- })
- },
- hideSheet() {
- this.setData({
- showIdentify: false
- })
- },
- // 获取笔的电量
- getPenBattery() {
- tqlSDK.getPenBattery()
- },
- // tab栏切换
- onChange(event) {
- // console.log(event);
- this.setData({
- activeTabBar: event.detail.index
- })
- }
- })
|