index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. import {
  2. myCanvas
  3. } from '../canvasWrite_smoothing.js'
  4. const penDataHandle = require('../../tqlsdk/penDataHandle');
  5. const app = getApp()
  6. const tqlSDK = app.globalData.tqlSDK
  7. import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog"
  8. const event = require("../../tqlsdk/event")
  9. import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast"
  10. // import {
  11. // allData,
  12. // feiData,
  13. // myData
  14. // } from './data'
  15. Page({
  16. /**
  17. * 页面的初始数据
  18. */
  19. data: {
  20. // 科目
  21. subject: "",
  22. homeworkId: "",
  23. studentId: "",
  24. test: "",
  25. fullPage: 0,
  26. canvasHeight: 0,
  27. canvasWidth: 0,
  28. imgs: ['../images/page1.png', '../images/page2.png'],
  29. myCanvas: null,
  30. connnetFlag: false,
  31. showConnectPen: false, //显示已连接弹窗
  32. blueToothOn: false, //蓝牙开启
  33. deviceId: null,
  34. deviceName: null,
  35. deviceMac: null,
  36. basePic: [{
  37. name: 'penWidth',
  38. icon: './pics/penWidth.png',
  39. iconClick: "./pics/penWidthChosen.png",
  40. text: "线宽"
  41. }, {
  42. name: 'color',
  43. icon: "./pics/color.png",
  44. iconClick: "./pics/colorChosen.png",
  45. text: "颜色"
  46. }, {
  47. name: "clear",
  48. icon: "./pics/clear.png",
  49. iconClick: './pics/clearChosen.png',
  50. text: "清除"
  51. }, {
  52. name: "more",
  53. icon: "./pics/more.png",
  54. iconClick: "./pics/moreChosen.png",
  55. text: "更多"
  56. }],
  57. activeTab: '', //选中的tab
  58. penWidthImg: [{
  59. img: './penWidth/penWidth1.png'
  60. }, {
  61. img: './penWidth/penWidth2.png'
  62. }, {
  63. img: './penWidth/penWidth3.png'
  64. }, {
  65. img: './penWidth/penWidth4.png'
  66. }, {
  67. img: './penWidth/penWidth5.png'
  68. }],
  69. // 笔颜色数组
  70. penColors: [{
  71. color: '#151515'
  72. }, {
  73. color: '#FF4E4E'
  74. }, {
  75. color: '#1A81F9'
  76. }, {
  77. color: '#53D769'
  78. }, {
  79. color: '#FED02F'
  80. }, {
  81. color: '#FF8F21'
  82. }],
  83. chosenIndex: 0,
  84. optionImg: [{
  85. img: './optionPic/penRecognition.png',
  86. text: "笔迹识别"
  87. }, {
  88. img: './optionPic/offlineData.png',
  89. text: '离线数据'
  90. }, {
  91. img: './optionPic/penInfo.png',
  92. text: '笔参数'
  93. }, {
  94. img: './optionPic/otaUpdate.png',
  95. text: "OTA升级"
  96. }],
  97. offlineImg: [{
  98. img: './optionPic/getOffline.png',
  99. text: '获取数据'
  100. }, {
  101. img: './optionPic/stopGet.png',
  102. text: "停止获取"
  103. }, {
  104. img: './optionPic/clearOffline.png',
  105. text: "清除数据"
  106. }, {
  107. img: './optionPic/offlineNum.png',
  108. text: '离线数据量'
  109. }],
  110. // 打开离线数据选项
  111. checkOffline: false,
  112. // 笔迹宽度
  113. penWidthIndex: 0,
  114. currentPage: 0,
  115. offlineDataNum: null,
  116. // 获取离线数据弹窗按钮
  117. buttonText: "暂停",
  118. showDialog: false,
  119. offlineProcess: 0,
  120. // 控制离线数据dialog展示变量
  121. showOfflineDialog: false,
  122. // 暂停继续离线数据flag
  123. continueFlag: true,
  124. localFileList: null,
  125. showFileListFlag: false,
  126. identifyAnswer: "", //识别结果
  127. showIdentify: false, //展示识别结果
  128. penBattery: null, //笔电量
  129. offFlag: false, //用户主动断开flag
  130. activeTabBar: 0, //激活栏
  131. codeList: [], //点读码列表
  132. // codeList: Array.from(new Array(5000).keys()).map(i => ({
  133. // num: i,
  134. // code: i,
  135. // })), //点读码列表
  136. templateCodeList: [],
  137. templateCodeListPage: 1,
  138. invalidCodeNum: 0, //无效码数量
  139. isIphoneX: false
  140. },
  141. /**
  142. * 生命周期函数--监听页面加载
  143. */
  144. onLoad: function (options) {
  145. // console.log(options)
  146. this.setData({
  147. studentId: options.studentId,
  148. homeworkId: options.homeworkId,
  149. subject: options.subject
  150. })
  151. let modelmes = wx.getStorageSync('modelmes');
  152. let isIphoneX = app.globalData.isIphoneX;
  153. this.setData({
  154. isIphoneX: isIphoneX
  155. })
  156. // 页面重加载时将所有的监听事件移除一遍在监听 防止相同事件多次重复监听
  157. wx.getSystemInfo({
  158. success: (result) => {
  159. if (isIphoneX) {
  160. this.setData({
  161. canvasHeight: result.windowHeight - 140 - 34,
  162. canvasWidth: result.windowWidth,
  163. fullPage: result.windowHeight
  164. })
  165. } else {
  166. this.setData({
  167. canvasHeight: result.windowHeight - 140,
  168. canvasWidth: result.windowWidth,
  169. fullPage: result.windowHeight
  170. })
  171. }
  172. let canvas = new myCanvas('#myCanvas', this.data.canvasWidth, this.data.isIphoneX ? this.data.canvasHeight - 140 - 33 : this.data.canvasHeight - 140, this.data.imgs)
  173. // 初始化canvas
  174. this.setData({
  175. myCanvas: canvas
  176. })
  177. },
  178. })
  179. // tqlSDK.fs.writeFile({
  180. // filePath: `${wx.env.USER_DATA_PATH}/testData.txt`,
  181. // encoding: "utf-8",
  182. // success: () => {
  183. // console.log('writeFileSuccess');
  184. // },
  185. // fail: () => {
  186. // console.log('writeFileFail');
  187. // }
  188. // })
  189. // event.remove('blueToothConnect')
  190. // 获取 是否偏移 和 码点类型12*12 / 16*16
  191. event.on('isOffset', this, (data) => {
  192. this.data.myCanvas.setCodePointType(data.codeType)
  193. switch (data.codeType) {
  194. case 12:
  195. this.data.myCanvas.setActualSize(182, 256)
  196. break;
  197. case 16:
  198. this.data.myCanvas.setActualSize(179, 253)
  199. break;
  200. default:
  201. this.data.myCanvas.setActualSize(176, 250)
  202. break;
  203. }
  204. })
  205. event.on('blueToothConnect', this, () => {
  206. this.setData({
  207. deviceMac: app.globalData.deviceMac,
  208. deviceName: app.globalData.deviceName,
  209. deviceId: app.globalData.deviceId,
  210. connnetFlag: true
  211. })
  212. console.log('index.js', '连接成功!')
  213. // 获取笔的电量
  214. // setTimeout(() => {
  215. // this.startGetOfflineData()
  216. // this.getPenBattery()
  217. // }, 1000)
  218. })
  219. // 监听获取数据
  220. event.on('AddressDataChanged', this, data => {
  221. // tqlSDK.fs.appendFileSync(`${wx.env.USER_DATA_PATH}/testData.txt`, JSON.stringify(data)+',', 'utf-8')
  222. // console.log('index.js',data);
  223. if (this.data.activeTabBar === 1) {
  224. this.setData({
  225. activeTabBar: 0
  226. })
  227. }
  228. if (data) {
  229. this.data.myCanvas.usbData(data)
  230. }
  231. })
  232. // 监听笔的笔锋开关
  233. event.on('strokeSwitch', this, data => {
  234. this.data.myCanvas.changeStroke(data)
  235. })
  236. // 监听笔的返回值
  237. let timer = null
  238. event.on('penData', this, (data) => {
  239. switch (data.cmd) {
  240. // 获取离线数据量
  241. case "getOfflineDataNum":
  242. this.setData({
  243. offlineDataNum: data.data
  244. })
  245. setTimeout(() => {
  246. Dialog.alert({
  247. title: "离线数据量",
  248. message: `${this.data.offlineDataNum * 10}bytes`
  249. }).then()
  250. }, 2000)
  251. break
  252. // 离线数据进程
  253. case "offLineProgress":
  254. break
  255. // 停止获取离线数据
  256. case "offLineProgressStop":
  257. break
  258. // 确认离线数据全部接收完毕
  259. case "offLineDataConfirm":
  260. break
  261. // 删除离线数据
  262. case "offLineDataDelete":
  263. Toast.success('删除成功')
  264. break
  265. //获取笔的电量
  266. case "getPenBattery":
  267. this.setData({
  268. penBattery: data.data.penBattery
  269. })
  270. break
  271. // 在线点读码
  272. case "getOID4CodeValue":
  273. // 离线点读码
  274. case "offlineOID4codeValue": {
  275. // 更新tabber选中项
  276. if (this.data.activeTabBar !== 1) {
  277. this.setData({
  278. activeTabBar: 1
  279. })
  280. }
  281. const cmds = ['getOID4CodeValue', 'offlineOID4codeValue']
  282. // 在线 or 离线
  283. const isOffline = !cmds.indexOf(data.cmd)
  284. console.log(data)
  285. const item = {
  286. code: data.data,
  287. isOffline,
  288. num: this.data.codeList.flat().length + 1
  289. }
  290. this.data.codeList.push(item)
  291. if (this.data.codeList.length % 5 === 0) {
  292. this.setData({
  293. templateCodeList: this.data.codeList.slice(-50)
  294. })
  295. }
  296. clearTimeout(timer)
  297. timer = setTimeout(() => {
  298. this.setData({
  299. templateCodeList: this.data.codeList.slice(-50)
  300. })
  301. }, 500);
  302. break
  303. }
  304. case "invalidCode":
  305. Toast.fail('无效码')
  306. break
  307. default:
  308. break
  309. }
  310. })
  311. // 监听离线数据获取量
  312. event.on('offlineGross', this, data => {
  313. this.setData({
  314. offlineProcess: Math.floor(data / this.data.offlineDataNum * 100)
  315. })
  316. })
  317. // 监听离线数据接收完毕
  318. event.on('offlineDataFinish', this, data => {
  319. console.log('offlineDataFinish')
  320. if (data) {
  321. this.data.myCanvas.offlineFlag = false
  322. this.setData({
  323. showOfflineDialog: false
  324. })
  325. }
  326. })
  327. // 监听切页数据
  328. event.on('changePage', this, data => {
  329. this.setData({
  330. currentPage: data
  331. })
  332. })
  333. // 监听笔关机断开连接事件
  334. event.on('unConnect', this, data => {
  335. if (this.data.offFlag) {
  336. this.setData({
  337. offFlag: false
  338. })
  339. return
  340. }
  341. this.setData({
  342. connnetFlag: false,
  343. deviceId: null,
  344. deviceMac: null,
  345. deviceName: null
  346. })
  347. // 已连接deviceId
  348. tqlSDK.connectDeviceId = null
  349. // 已连接serviceid
  350. tqlSDK.connectServiceId = null
  351. // 已连接characteristicId
  352. tqlSDK.connectCharacteristicId = null
  353. event.emit('pageUnconnect', true)
  354. Dialog.alert({
  355. title: "提示",
  356. message: "蓝牙断开",
  357. }).then()
  358. })
  359. },
  360. scrollViewBottom() {
  361. this.data.templateCodeListPage++
  362. this.setData({
  363. templateCodeList: this.data.codeList.slice(-50 * this.data.templateCodeListPage).reverse()
  364. })
  365. },
  366. /**
  367. * 生命周期函数--监听页面初次渲染完成
  368. */
  369. onReady: function () {
  370. setTimeout(() => {
  371. this.initBluetooth()
  372. // this.data.myCanvas.changePage(0)
  373. }, 100)
  374. },
  375. //连接笔或者查看已连接
  376. goConnect() {
  377. if (!this.data.blueToothOn) {
  378. Dialog.alert({
  379. title: "提示",
  380. message: "请先开启蓝牙"
  381. })
  382. return
  383. }
  384. if (this.data.showOfflineDialog) {
  385. return
  386. }
  387. if (this.data.blueToothOn && !this.data.connnetFlag) {
  388. wx.navigateTo({
  389. url: '../search/search',
  390. })
  391. return
  392. }
  393. this.setData({
  394. showConnectPen: true,
  395. })
  396. },
  397. saveCanvasImage(e) {
  398. console.log(this.data.myCanvas)
  399. this.data.myCanvas.saveCanvasImage(this.data.homeworkId, this.data.studentId, "student", this.data.subject);
  400. },
  401. gotoTeacher() {
  402. wx.navigateTo({
  403. url: `../teacher/teacher?flag=${false}`,
  404. })
  405. },
  406. // 初始化蓝牙
  407. initBluetooth() {
  408. tqlSDK.initBlueTooth(() => {
  409. Dialog.alert({
  410. title: '提示',
  411. message: '蓝牙开启成功'
  412. })
  413. this.setData({
  414. blueToothOn: true
  415. })
  416. }, (e) => {
  417. Dialog.alert({
  418. title: '提示',
  419. message: e.msg
  420. })
  421. tqlSDK.listenBlueState(() => {
  422. tqlSDK.initBlueTooth(() => {
  423. Dialog.alert({
  424. title: '提示',
  425. message: '蓝牙开启成功'
  426. })
  427. this.setData({
  428. blueToothOn: true
  429. })
  430. })
  431. })
  432. })
  433. },
  434. // 隐藏已连接笔
  435. hidePen() {
  436. this.setData({
  437. showConnectPen: false
  438. })
  439. },
  440. //切换笔
  441. changePen() {
  442. if (this.data.showOfflineDialog) {
  443. return
  444. }
  445. this.setData({
  446. offFlag: true
  447. })
  448. tqlSDK.unConnectBlueTooth(this.data.deviceId, () => {
  449. this.setData({
  450. deviceName: '',
  451. deviceMac: '',
  452. deviceId: null,
  453. showConnectPen: false,
  454. connnetFlag: false,
  455. })
  456. wx.navigateTo({
  457. url: '../search/search',
  458. })
  459. }, () => {
  460. })
  461. },
  462. // 继续获取离线数据
  463. continueGetOfflineData() {
  464. tqlSDK.continueGetOfflineData()
  465. },
  466. // 开始获取离线数据
  467. startGetOfflineData() {
  468. tqlSDK.startGetOfflineData(() => {
  469. }, () => {
  470. }, () => {
  471. Dialog.alert({
  472. title: '错误',
  473. message: '电量低于10%\/n无法获取离线数据',
  474. confirmButtonText: '知道了'
  475. }).then(() => {
  476. })
  477. })
  478. },
  479. // 获取离线数据量
  480. getOfflineNum() {
  481. tqlSDK.getPenOfflineDataSize()
  482. },
  483. // 停止获取离线数据
  484. stopGetOfflineData() {
  485. tqlSDK.stopGetOfflineData()
  486. },
  487. // 暂停获得离线数据
  488. pauseOfflineData() {
  489. tqlSDK.pauseGetOfflineData()
  490. },
  491. // 确认离线数据接收完毕
  492. confirmOfflineDataDone() {
  493. tqlSDK.confirmOfflineDataDone()
  494. },
  495. // 清除离线数据
  496. deleteOfflineData() {
  497. tqlSDK.deleteOfflineData()
  498. },
  499. // 切换tab
  500. changeTab(e) {
  501. if (!this.data.blueToothOn) {
  502. Dialog.alert({
  503. title: "提示",
  504. message: "请先打开蓝牙"
  505. })
  506. return
  507. }
  508. if (!this.data.connnetFlag) {
  509. Dialog.alert({
  510. confirmButtonText: '去连接',
  511. title: "智能笔",
  512. message: "请先连接智能笔",
  513. showCancelButton: true
  514. }).then(() => {
  515. wx.navigateTo({
  516. url: '../search/search',
  517. })
  518. }).catch()
  519. return
  520. }
  521. switch (e.detail) {
  522. case "clear":
  523. if (this.data.showOfflineDialog) {
  524. return
  525. }
  526. this.setData({
  527. activeTab: e.detail
  528. })
  529. Dialog.confirm({
  530. title: "警告",
  531. message: "此操作将清除所有书写数据"
  532. }).then(
  533. () => {
  534. Toast.loading({
  535. forbidClick: true,
  536. message: '正在清除'
  537. })
  538. if (this.data.activeTabBar === 0) {
  539. this.data.myCanvas.clear()
  540. } else {
  541. this.setData({
  542. codeList: []
  543. })
  544. }
  545. setTimeout(() => {
  546. Toast.success('清除成功')
  547. this.setData({
  548. activeTab: ""
  549. })
  550. }, 2000)
  551. }
  552. ).catch()
  553. break
  554. case "penWidth":
  555. case "color":
  556. case "more":
  557. if (this.data.activeTab === e.detail) {
  558. this.setData({
  559. activeTab: ''
  560. })
  561. return
  562. }
  563. this.setData({
  564. activeTab: e.detail
  565. })
  566. break
  567. default:
  568. break
  569. }
  570. },
  571. // 选择笔的宽度
  572. chosenPenwidth(e) {
  573. let penWidth = Number(e.currentTarget.dataset.index)
  574. if (penWidth === this.data.penWidthIndex) {
  575. setTimeout(() => {
  576. this.setData({
  577. activeTab: ''
  578. })
  579. }, 500)
  580. return
  581. }
  582. this.data.myCanvas.changeWidth(penWidth === 0 ? 0.5 : penWidth === 1 ? 1 : penWidth === 2 ? 1.5 : penWidth === 3 ? 2.5 : 3)
  583. this.setData({
  584. penWidthIndex: penWidth
  585. })
  586. setTimeout(() => {
  587. this.setData({
  588. activeTab: ''
  589. })
  590. }, 500)
  591. },
  592. // 选择笔的颜色
  593. changeColor(e) {
  594. let index = Number(e.currentTarget.dataset.index)
  595. this.data.myCanvas.changeColor(this.data.penColors[index].color)
  596. this.setData({
  597. chosenIndex: index
  598. })
  599. setTimeout(() => {
  600. this.setData({
  601. activeTab: ""
  602. })
  603. }, 500)
  604. },
  605. // 隐藏离线的侧边栏
  606. hideOffline() {
  607. this.setData({
  608. checkOffline: false
  609. })
  610. },
  611. // 更多的选项
  612. tapOption(e) {
  613. switch (e.currentTarget.dataset.index) {
  614. // 笔迹识别
  615. case 0:
  616. this.setData({
  617. activeTab: ''
  618. })
  619. this.penCloud()
  620. break
  621. case 1:
  622. this.setData({
  623. checkOffline: true
  624. })
  625. break
  626. case 2:
  627. this.setData({
  628. activeTab: ''
  629. })
  630. wx.navigateTo({
  631. url: `../penInfo/penInfo?flag=${this.data.myCanvas.strokeFlag}`,
  632. })
  633. break
  634. // Ota升级
  635. case 3:
  636. this.OTAupdate()
  637. break;
  638. // 读取txt
  639. case 4:
  640. console.log(wx.env.USER_DATA_PATH)
  641. // 读取本地USER_DATA_PATH下面的txt文档列表
  642. tqlSDK.fs.readdir({
  643. dirPath: wx.env.USER_DATA_PATH,
  644. success: (res) => {
  645. console.log(res);
  646. let fileList = res.files.filter((item) => {
  647. if (item.indexOf('.txt') !== -1 || item.indexOf('.TXT') !== -1) {
  648. return item
  649. }
  650. }).map(item => {
  651. return {
  652. name: item
  653. }
  654. })
  655. if (fileList.length === 0) {
  656. Toast.fail('指定目录下没有txt文件');
  657. return;
  658. }
  659. this.setData({
  660. activeTab: "",
  661. showFileListFlag: true,
  662. localFileList: fileList
  663. })
  664. },
  665. fail: (res) => {
  666. }
  667. })
  668. break;
  669. case 5:
  670. wx.navigateTo({
  671. url: '../about/about',
  672. });
  673. break;
  674. }
  675. },
  676. // 离线数据操作
  677. offlineDataActivity(e) {
  678. let index = Number(e.currentTarget.dataset.index)
  679. switch (index) {
  680. // 开始获取离线数据
  681. case 0:
  682. Dialog.alert({
  683. title: "警告",
  684. message: '获取离线中请勿插USB充电',
  685. confirmButtonText: '知道了,继续获取'
  686. }).then(() => {
  687. if (this.data.showOfflineDialog) {
  688. return
  689. }
  690. this.data.myCanvas.offlineFlag = true
  691. tqlSDK.getOfflineDataFlag = true
  692. this.setData({
  693. activeTab: '',
  694. showOfflineDialog: true,
  695. continueFlag: true
  696. })
  697. this.startGetOfflineData()
  698. })
  699. break;
  700. // 停止获取
  701. case 1:
  702. this.setData({
  703. activeTab: '',
  704. continueFlag: true,
  705. showOfflineDialog: false,
  706. offlineProcess: 0
  707. })
  708. this.data.myCanvas.offlineFlag = false
  709. tqlSDK.getOfflineDataFlag = false
  710. this.stopGetOfflineData()
  711. break;
  712. // 清除离线数据
  713. case 2:
  714. if (this.data.showOfflineDialog) {
  715. return
  716. }
  717. Dialog.confirm({
  718. title: '警告',
  719. message: '此操作将删除所有离线数据'
  720. }).then(() => {
  721. this.setData({
  722. activeTab: ''
  723. })
  724. this.deleteOfflineData()
  725. }).catch()
  726. break
  727. // 离线数据量
  728. case 3:
  729. if (this.data.showOfflineDialog) {
  730. return
  731. }
  732. this.setData({
  733. activeTab: ''
  734. })
  735. this.getOfflineNum()
  736. break
  737. default:
  738. break
  739. }
  740. },
  741. // 暂停继续下载离线数据
  742. processButton() {
  743. this.data.continueFlag ? this.pauseOfflineData() : this.continueGetOfflineData()
  744. this.setData({
  745. continueFlag: !this.data.continueFlag
  746. })
  747. },
  748. // ota升级
  749. OTAupdate() {
  750. tqlSDK.otaUpdate()
  751. },
  752. // 给蓝牙设备发送升级文件
  753. sendFile(ArrayBuffer) {
  754. wx.writeBLECharacteristicValue({
  755. characteristicId: tqlSDK.connectCharacteristicId,
  756. deviceId: tqlSDK.connectServiceId,
  757. serviceId: tqlSDK.connectDeviceId,
  758. value: ArrayBuffer,
  759. success: () => {
  760. console.log('success')
  761. },
  762. fail: () => {
  763. }
  764. })
  765. },
  766. // 读取本地文件
  767. readLocalFile(e) {
  768. // console.log(e)
  769. this.setData({
  770. showFileListFlag: false
  771. })
  772. // 将canvaswirte和sdk中的读取本地文件flag改为true
  773. const toast = Toast.loading({
  774. duration: 0,
  775. message: "正在读取本地文件",
  776. forbidClick: true
  777. })
  778. setTimeout(() => {
  779. tqlSDK.readLocalTxtFlag = true
  780. this.data.myCanvas.readLocalFileFlag = true
  781. let fileData = tqlSDK.fs.readFileSync(`${wx.env.USER_DATA_PATH}/${e.detail.name}`, 'utf-8')
  782. let singal = fileData.split(/[\s\n]/)
  783. toast.clear()
  784. let myArray = singal.map((currentValue, index) => {
  785. if (index !== singal.length - 1) {
  786. return parseInt(currentValue, 16)
  787. }
  788. })
  789. // console.log(myArray);
  790. let dataLength = Math.floor(myArray.length / 10)
  791. for (let i = 0; i < dataLength; i++) {
  792. penDataHandle.splitData(myArray.slice(i * 10, (i + 1) * 10))
  793. }
  794. // 读取完后将flag的值复原
  795. tqlSDK.readLocalTxtFlag = false
  796. this.data.myCanvas.readLocalFileFlag = false
  797. }, 200)
  798. },
  799. //隐藏文件栏
  800. hideFileList() {
  801. this.setData({
  802. showFileListFlag: false
  803. })
  804. },
  805. // 手写识别xxx
  806. penCloud() {
  807. let xPoint = []
  808. let yPoint = []
  809. let penData = []
  810. if (!this.data.myCanvas.mapData.get(this.data.myCanvas.lastPage)) {
  811. Toast.fail('请先书写');
  812. return
  813. }
  814. const loading = Toast.loading({
  815. message: '正在识别,请稍等',
  816. duration: 0
  817. })
  818. this.data.myCanvas.mapData.get(this.data.myCanvas.lastPage).forEach(item => {
  819. xPoint.push(item.xPoint)
  820. yPoint.push(item.yPoint)
  821. if (item.dotType === 'PEN_UP') {
  822. penData.push({
  823. x: xPoint.join(','),
  824. y: yPoint.join(",")
  825. })
  826. xPoint = []
  827. yPoint = []
  828. }
  829. })
  830. console.log(penData);
  831. wx.request({
  832. url: 'https://hwrapi.pencloud.cn/script',
  833. method: "POST",
  834. data: {
  835. viewSizeHeight: this.data.myCanvas.canvasHeight * 2, //画布高度
  836. viewSizeWidth: this.data.myCanvas.canvasWidth * 2, //画布宽度
  837. // applicationKey: 'a55a66c9-ae8f-482e-bb90-c2771390d388',
  838. applicationKey: '306d01c5-46d8-495a-9b72-edd9e0196c27',
  839. macAddress: tqlSDK.connectDeviceId.split(':').join(''),
  840. penData,
  841. scriptType: 'Text',
  842. languages: "zh_CN"
  843. },
  844. success: (res) => {
  845. console.log(res);
  846. if (res.data.code !== 0) {
  847. Toast.fail("识别失败")
  848. return
  849. }
  850. this.setData({
  851. showIdentify: true,
  852. identifyAnswer: res.data.data
  853. })
  854. },
  855. fail: (res) => {
  856. console.log(res);
  857. },
  858. complete: () => {
  859. loading.clear()
  860. }
  861. })
  862. },
  863. hideSheet() {
  864. this.setData({
  865. showIdentify: false
  866. })
  867. },
  868. // 获取笔的电量
  869. getPenBattery() {
  870. tqlSDK.getPenBattery()
  871. },
  872. // tab栏切换
  873. onChange(event) {
  874. // console.log(event);
  875. this.setData({
  876. activeTabBar: event.detail.index
  877. })
  878. }
  879. })