subGame.nvue 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. <template>
  2. <view>
  3. <!-- :src="url" src = 'http://192.168.0.112:7456/build/index.html' :src="LocationGameUrl" -->
  4. <!-- @receivedtitle="onReceivedTitle" @pagefinish="onPageFinish" http://192.168.31.223:7456/-->
  5. <view class="web-view">
  6. <web-view class="web-view-child" :src="url" ref="webview" @pagestart="onPageStart"
  7. @onPostMessage="handlePostMessage" @error="onError"></web-view>
  8. </view>
  9. <view class="web-back" @click="navBack">
  10. <image style="width: 40rpx;height: 40rpx;" src="/static/gameCloseW.png"></image>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import reqUtil from "@/util/util-js/requstUtil.js";
  16. import config from "@/common/config.js";
  17. import fruit from "@/components/fruitMachine/fruit.js"
  18. // import AccAndOri from "@/util/util-js/AccAndOri.js"
  19. import puchConfig from "@/util/util-js/puchConfig.js"
  20. import EquipmentAction from "@/util/util-js/EquipmentAction.js"
  21. import ActionJump from "@/util/util-js/action/jump-0.2.js"
  22. var currentWebview;
  23. var orientId = null,
  24. accId = null;
  25. import {
  26. mapState,
  27. mapMutations
  28. } from 'vuex';
  29. export default {
  30. computed: mapState([
  31. 'avatarUrl', 'gender', 'userName', 'city', 'BLEConnectDevice', 'instructionState',
  32. 'currentInstruction',
  33. 'cIndex', 'globalAcc', 'globalOri', 'LocationGameUrl',
  34. 'ConnectBindingDevice',
  35. 'currentModeIndex',
  36. 'bOldDeviceType'
  37. ]),
  38. data() {
  39. return {
  40. url: '',
  41. pagefinish: '',
  42. error: '',
  43. PageStart: false, // 记录网页请求的加载状态,true 加载成功 false 加载失败
  44. gameQuitListener: false, //游戏是否初始化退出监听
  45. xA: 0,
  46. yA: 0,
  47. zA: 0,
  48. oldxA: 0,
  49. oldzA: 0,
  50. bUpdateOnce: false,
  51. bDelayOnce: false,
  52. mass: puchConfig.BOXING_MASS,
  53. //X轴的变化数组值
  54. xAccArray: [],
  55. xMax: 0,
  56. maxTimeoutId: null,
  57. bMaxPause: false,
  58. xMin: 0,
  59. minTimeoutId: null,
  60. bMinPause: false,
  61. bCalculation: false,
  62. calTimeout: null,
  63. //角度比 z/x
  64. angleRatio: 1, //
  65. //当前选择的item
  66. decodeItem: null,
  67. //拳击数据判断对象
  68. EquipmentActionObj: null,
  69. BLENum: 0,
  70. BLEACX: 0,
  71. BLEACYL: 0,
  72. //是否是游戏里面开启的监听,如果是的话,退出游戏界面时候需要关闭相应的监听,如加速计和陀螺仪
  73. bGameOpenListen: false,
  74. //手机陀螺仪和加速计android模块
  75. bMyAttitudeListen: false,
  76. /**
  77. * 跳绳相应数据
  78. */
  79. //跳绳数据判断对象
  80. actionJumpObj: null,
  81. BLEAccIndex: 0
  82. }
  83. },
  84. onLoad() {
  85. let _self = this;
  86. var pages = getCurrentPages();
  87. var page = pages[pages.length - 1];
  88. currentWebview = page.$getAppWebview();
  89. //监听一次调用setOnceGameOption
  90. console.log("子组件初始化");
  91. uni.$once("setOnceGameOption", (option) => {
  92. console.log("setOnceGameOption=", option);
  93. if (option && option.item) {
  94. this.decodeItem = JSON.parse(decodeURIComponent(option.item));
  95. this.url = this.decodeItem.gameWebUrl;
  96. let temp = {
  97. fObjectId: this.decodeItem.gameId,
  98. recentlyType: 0
  99. }
  100. // console.log("game temp:", config.URL.RECENTLYPLAYINGADD);
  101. //添加最近在玩的游戏
  102. reqUtil.requestData(config.URL.RECENTLYPLAYINGADD, temp).then(res => {
  103. console.log('RECENTLYPLAYINGADD =====', res);
  104. if (res.code == 0) {}
  105. },
  106. e => {
  107. console.log(e)
  108. });
  109. }
  110. });
  111. //子窗体onload 后获取父窗体的onload数据
  112. uni.$emit("game-load");
  113. uni.getCurrentSubNVue().addEventListener("hide", function() {
  114. console.log("subNVue子窗体已隐藏!");
  115. //通知游戏,页面退出
  116. _self.sendMessage("onSubHide", {
  117. msg: '退出页面'
  118. });
  119. uni.$off('updateBLEDeviceData', _self.BLECallback);
  120. //json
  121. uni.$off('updateBLEDeviceJson', _self.BLEJsonCallback);
  122. //去掉监听
  123. if (_self.bMyAttitudeListen) {
  124. _self.gStopSimulateBLEUpdate();
  125. }
  126. // console.log(_self.BLEConnectDevice);
  127. //如果连接了蓝牙设备是手柄
  128. if (_self.BLEConnectDevice && _self.BLEConnectDevice.deviceType == "BLEHandle") {
  129. //开启设备回调 3/4关闭
  130. _self.onWriteBLEConnectionValue({
  131. value: "4"
  132. });
  133. //关闭json 回调
  134. _self.onWriteBLEConnectionValue({
  135. value: "6"
  136. });
  137. uni.$off('updateBLEDeviceData', _self.gWatchBLEUpdate);
  138. if (0 === _self.currentModeIndex) {
  139. //拳击柱
  140. _self.uploadUserData_hitCount();
  141. }
  142. } //关闭跳绳模式指令
  143. else if (_self.BLEConnectDevice && _self.BLEConnectDevice.deviceType == "BLERope") {
  144. console.warn("还没设置关闭跳绳模式")
  145. _self.B_CloseRopeSkipping();
  146. uni.$off('updateBLEDeviceData', _self.gWatchBLEUpdate);
  147. }
  148. uni.$emit("game-unload", {
  149. globalAcc: _self.globalAcc,
  150. globalOri: _self.globalOri,
  151. bGameOpenListen: _self.bGameOpenListen
  152. });
  153. });
  154. //*****注释蓝牙操作******
  155. // 监听事件
  156. // uni.$on('callbackCloseBLE', this.callbackCloseBLE);
  157. // uni.$on('callbackBLEState', this.callbackBLEState);
  158. // uni.$on('updateBLEDeviceData', this.BLECallback);
  159. //监听物理返回按钮
  160. plus.key.addEventListener('backbutton', () => {
  161. this.navBack();
  162. }, false);
  163. //设置store状态
  164. _self.$store.state.bGamePlaying = true;
  165. },
  166. onUnload() {
  167. this.$store.state.bGamePlaying = false;
  168. console.log("subNVue子窗体 onUnload!");
  169. //*****注释蓝牙操作******
  170. // uni.$off('callbackCloseBLE', this.callbackCloseBLE);
  171. // uni.$off('callbackBLEState', this.callbackBLEState);
  172. //取消相应的绑定事件
  173. uni.$off('watchAcceleration', this.gWatchAcceleration);
  174. uni.$off("watchAcceleration", this.gWatchBoxingAcc);
  175. uni.$off("watchAcceleration", this.gWatchHitBoxingAcc);
  176. uni.$off('watchOrientation', this.gWatchOrientation);
  177. uni.$emit("game-unload");
  178. },
  179. methods: {
  180. ...mapMutations(['addlocalCalorie', 'syncRequestEvent', 'onWriteBLEConnectionValue',
  181. 'gCreateFilterObj', 'gUpdateFilter', 'B_OpenRopeSkipping', 'B_CloseRopeSkipping',
  182. 'gStopSimulateBLEUpdate', 'gStartSimulateBLEUpdate', 'gUpdateSandbagAlgorithm',
  183. 'gCreateSandbagAlgorithm', 'onConvertDeviceData', 'addSandbagHitCount', 'uploadUserData_hitCount',
  184. 'jumpIdenModule_onJump6AxisDataUpdate'
  185. ]),
  186. navBack() {
  187. this.sendMessage("onQuit", {
  188. type: "right-button",
  189. msg: '点击右上角按钮退出游戏'
  190. });
  191. uni.showModal({
  192. title: '提示',
  193. content: '是否退出游戏?',
  194. success: (res) => {
  195. //如果游戏需要监听退出,则需要走完游戏端流程,才能退出,否则直接退出
  196. if (this.gameQuitListener) {
  197. this.sendMessage("onQuitModal", {
  198. data: res,
  199. msg: '退出提示'
  200. });
  201. } else {
  202. if (res.confirm) {
  203. uni.getCurrentSubNVue().hide('auto');
  204. }
  205. }
  206. }
  207. })
  208. },
  209. //蓝牙断开连接时候
  210. callbackCloseBLE() {
  211. this.sendMessage("onDeviceClose", {
  212. msg: '设备断开连接。'
  213. });
  214. },
  215. //蓝牙状态回调
  216. callbackBLEState(res) {
  217. // console.log("game callbackBLEState==", res);
  218. // 暂时只返回数据连接错误信息
  219. // uni.$emit("callbackBLEState", {
  220. // state: -1,
  221. // msg: "设备数据错误"
  222. // });
  223. this.sendMessage("onDeviceState", res)
  224. },
  225. BLECallback(data) {
  226. let dataType = 'Box';
  227. // if (data.hasOwnProperty("ax")) {
  228. // dataType = "Accelerometer"
  229. // }else if(data.hasOwnProperty("gx")){
  230. // dataType = "Gyroscope"
  231. // }
  232. // 设备回调后面用这个接口
  233. let BLEState = {
  234. device: this.BLEConnectDevice, //连接的蓝牙设备
  235. instructionState: this.instructionState, //开启的指令状态
  236. currentInstruction: this.currentInstruction, //当前发送的指令记录
  237. dataType: dataType, //回调的数据类型
  238. data: data //蓝牙回调的数据
  239. }
  240. this.sendMessage("onDeviceUpdateData", BLEState);
  241. },
  242. BLEJsonCallback(data) {
  243. let dataType = 'Json';
  244. // 设备回调后面用这个接口
  245. let BLEState = {
  246. device: this.BLEConnectDevice, //连接的蓝牙设备
  247. instructionState: this.instructionState, //开启的指令状态
  248. currentInstruction: this.currentInstruction, //当前发送的指令记录
  249. dataType: dataType, //回调的数据类型
  250. data: data //蓝牙回调的数据
  251. }
  252. this.sendMessage("onDeviceUpdateJson", BLEState);
  253. },
  254. /**
  255. * 图片转化base64
  256. * @param {Object} url
  257. * @param {Object} callback
  258. */
  259. urlToBase64(url, callback) {
  260. let toBase64Url;
  261. uni.request({
  262. url: url,
  263. method: 'GET',
  264. responseType: 'arraybuffer',
  265. success: async res => {
  266. let base64 = uni.arrayBufferToBase64(res.data);
  267. toBase64Url = 'data:image/jpeg;base64,' + base64;
  268. if (callback)
  269. callback(toBase64Url, res.data);
  270. }
  271. })
  272. },
  273. /**
  274. * 初始化发送数据给游戏
  275. */
  276. sendGameInit() {
  277. console.log("this.avatarUrl:", this.avatarUrl);
  278. this.urlToBase64(this.avatarUrl, (toBase64Url) => {
  279. let device = null;
  280. /**
  281. * 卡路里消耗参数
  282. */
  283. let calorieParams = {
  284. hitUnit: 0,
  285. runUnit:puchConfig.getRunCalorie(1),
  286. jumpUnit:puchConfig.getJumpCalorie(1)
  287. };
  288. if (this.BLEConnectDevice) {
  289. // device = {
  290. // cname: this.BLEConnectDevice.cname,
  291. // ename: this.BLEConnectDevice.ename,
  292. // name: this.BLEConnectDevice.name
  293. // };
  294. // calorieParams = {
  295. // runUnit: 0.55, // 比跳的稍微大一点
  296. // jumpUnit: 0.5 // 跳的次数 * 0.5
  297. // };
  298. device = {
  299. cname: this.BLEConnectDevice.cname,
  300. ename: this.BLEConnectDevice.ename,
  301. name: this.BLEConnectDevice.name
  302. };
  303. calorieParams.hitUnit = puchConfig.getBoxingCalorie(1);
  304. } else if (this.ConnectBindingDevice) {
  305. device = {
  306. cname: this.ConnectBindingDevice.cname,
  307. ename: this.ConnectBindingDevice.ename,
  308. name: this.ConnectBindingDevice.name
  309. };
  310. calorieParams.hitUnit=puchConfig.getBoxingCalorie(1);
  311. }
  312. //到时候可能要区分是什么类型
  313. let item = {
  314. id: this.decodeItem.gameId,
  315. name: this.decodeItem.gameName
  316. };
  317. // console.log(this.BLEConnectDevice, device);
  318. let gameData = {
  319. avatarUrl: this.avatarUrl,
  320. avatarBase64Url: toBase64Url,
  321. userName: this.userName,
  322. gender: this.gender,
  323. caloriUnit: 10,
  324. calorieParams: calorieParams,
  325. //蓝牙连接的设备
  326. device: device,
  327. item: item
  328. }
  329. this.sendMessage("onGameInit", gameData);
  330. // console.log("onGameInit =", gameData);
  331. })
  332. },
  333. /**
  334. * 统一发送信息
  335. * @param {Object} gameData
  336. */
  337. sendMessage(functionName, gameData) {
  338. let data = {
  339. "funName": functionName,
  340. "gameData": gameData
  341. }
  342. if (!this.PageStart) {
  343. console.warn("页面未初始化不能传消息", data);
  344. return;
  345. }
  346. let initStr = JSON.stringify(data);
  347. // console.log(functionName);
  348. this.$refs.webview.evalJs("onWebViewMessage(" + initStr + ")");
  349. },
  350. sendMessageToWebview() {
  351. if (!this.PageStart) {
  352. uni.showModal({
  353. title: "暴躁的提示",
  354. content: "只有网页加载成功了才可以传参过去,不然无效哦。。"
  355. })
  356. return false;
  357. }
  358. /**
  359. * 下面的 jsfunction 代码你要自己在你的网页里面写一个方法 大致如下
  360. * 下面就是你基本的html知识了,我就不想说什么了,自己学。
  361. window.jsfunction = function(data){
  362. console.log('data', data);
  363. }
  364. */
  365. this.sendMessage("onDeviceUpdateData", {
  366. H: 1
  367. });
  368. },
  369. handlePostMessage: function(postData) {
  370. console.log("handlePostMessage得到参数", postData.detail);
  371. let temp = postData.detail.data[0];
  372. let gameData = temp.gameData;
  373. if (temp.funName == "uploadInfo") {
  374. //TODO:写入排行榜分数,后面需要在游戏完结里面调用
  375. //TODO: 后面游戏时间,卡路里的需要写入数据库,目前只是处理了分数
  376. // let num = Math.round(Math.random() * 10000);
  377. //1. gameScore 处理上传的分数
  378. // let _temp = {
  379. // gameId: '1595755153789139696',
  380. // score: 1230,
  381. // cityCode: '110101',
  382. // };
  383. //2. calorieBurned 处理卡路里
  384. // 记录卡路里到本地
  385. this.addlocalCalorie(gameData.calorieBurned);
  386. console.log("gameData.calorieBurned", gameData.calorieBurned);
  387. //然后更新上服务器
  388. this.syncRequestEvent({
  389. success: () => {
  390. // uni.$emit('updateArcbarData', '');
  391. let _temp = {
  392. //假如排行id 为空,排行榜就是游戏自己的id
  393. gameId: this.decodeItem.gameRankingId == null ? this.decodeItem
  394. .gameId : this.decodeItem.gameRankingId,
  395. score: gameData.gameScore,
  396. cityCode: this.city.cityCode,
  397. bMaxLimit: true
  398. }
  399. console.log('_temp =====', _temp);
  400. reqUtil.requestData(config.URL.UPLOADRANKING, _temp, "POST").then(res => {
  401. console.log('UPLOADRANKING =====', res);
  402. if (res.code == 0) {
  403. //TODO 后续游戏数据处理
  404. this.sendMessage("onUploadInfo", {
  405. code: 0,
  406. msg: '上传成功'
  407. });
  408. } else {
  409. //TODO 后续游戏数据处理
  410. this.sendMessage("onUploadInfo", {
  411. code: 400,
  412. msg: '上传失败'
  413. });
  414. }
  415. },
  416. e => {
  417. console.log(e)
  418. });
  419. },
  420. fail: () => {
  421. this.sendMessage("onUploadInfo", {
  422. code: 400,
  423. msg: '上传失败'
  424. });
  425. }
  426. });
  427. //3. gameTime 游戏时间
  428. //TODO 后面处理游戏时长
  429. } else if (temp.funName == "gameInit") {
  430. // 获取游戏信息
  431. this.sendGameInit();
  432. } else if (temp.funName == "aiRandomInfo") {
  433. // 获取aiRandomInfo
  434. reqUtil.requestData(config.URL.AIRANDOMINFO, {}).then(res => {
  435. console.log('AIRANDOMINFO =====', res);
  436. if (res.code == 0) {
  437. let data = res.data;
  438. this.urlToBase64(data.aiAvatar, (toBase64Url) => {
  439. let sendData = {
  440. aiId: data.aiId,
  441. aiName: data.aiName,
  442. aiGender: data.aiGender,
  443. aiType: data.aiType,
  444. aiAvatarBase64Url: toBase64Url
  445. }
  446. this.sendMessage("onAiRandomInfo", sendData);
  447. })
  448. }
  449. },
  450. e => {
  451. console.log(e)
  452. });
  453. } else if (temp.funName == "fruitInfo") {
  454. let fruitIndexArray = [0, 0, 0];
  455. if (gameData.hasOwnProperty("calorie")) {
  456. fruitIndexArray = fruit.getFruitIndex(gameData.calorie);
  457. }
  458. this.urlToBase64("https://bbeng-bucket.oss-cn-beijing.aliyuncs.com/cocos/fruitMachine.png", (
  459. toBase64Url) => {
  460. this.sendMessage("onFruitInfo", {
  461. // 水果雪碧图
  462. fruitBase64Url: toBase64Url,
  463. // 雪碧图 单张图片宽高
  464. unitWidth: 100,
  465. unitHeight: 100,
  466. unit: "px",
  467. imageStartPosY: 0,
  468. imageEndPosY: -1200,
  469. fruitIndexArray: fruitIndexArray
  470. });
  471. })
  472. } else if (temp.funName == "urlToBase64") {
  473. if (gameData.url) {
  474. this.urlToBase64(gameData.url, (toBase64Url) => {
  475. this.sendMessage("onUrlToBase64", {
  476. base64: toBase64Url,
  477. });
  478. })
  479. } else {
  480. this.sendMessage("onUrlToBase64", {
  481. base64: {},
  482. });
  483. }
  484. } else if (temp.funName == "openAccelerometer") {
  485. if (this.globalAcc) {
  486. uni.$off('watchAcceleration', this.gWatchAcceleration);
  487. uni.$on('watchAcceleration', this.gWatchAcceleration);
  488. } else {
  489. uni.$emit("bindAcc", {
  490. callback: (accId) => {
  491. console.log("开启的:globalAcc=", accId);
  492. this.bGameOpenListen = true;
  493. this.$store.state.globalAcc = accId;
  494. uni.$off('watchAcceleration', this.gWatchAcceleration);
  495. uni.$on('watchAcceleration', this.gWatchAcceleration);
  496. }
  497. });
  498. }
  499. } else if (temp.funName == "closeAccelerometer") {
  500. uni.$off('watchAcceleration', this.gWatchAcceleration);
  501. uni.$emit("unBindAcc", this.globalAcc);
  502. this.$store.state.globalAcc = null;
  503. } else if (temp.funName == "openOrientation") {
  504. //打开陀螺仪
  505. if (this.globalOri) {
  506. uni.$off('watchOrientation', this.gWatchOrientation);
  507. uni.$on('watchOrientation', this.gWatchOrientation);
  508. } else {
  509. uni.$emit("bindOri", {
  510. callback: (oriId) => {
  511. console.log("开启的:globalOri=", oriId);
  512. this.bGameOpenListen = true;
  513. this.$store.state.globalOri = oriId;
  514. uni.$off('watchOrientation', this.gWatchOrientation);
  515. uni.$on('watchOrientation', this.gWatchOrientation);
  516. }
  517. });
  518. }
  519. } else if (temp.funName == "closeOrientation") {
  520. uni.$off('watchOrientation', this.gWatchOrientation);
  521. uni.$emit("unBindOri", this.globalOri);
  522. this.$store.state.globalOri = null;
  523. } else if (temp.funName == "bindBoxingPost") {
  524. if (this.globalAcc) {
  525. uni.$off('watchAcceleration', this.gWatchBoxingAcc);
  526. uni.$on('watchAcceleration', this.gWatchBoxingAcc);
  527. } else {
  528. uni.$emit("bindAcc", {
  529. callback: (accId) => {
  530. console.log("bindBoxingPost开启的:globalAcc=", accId);
  531. this.bGameOpenListen = true;
  532. this.$store.state.globalAcc = accId;
  533. uni.$off('watchAcceleration', this.gWatchBoxingAcc);
  534. uni.$on('watchAcceleration', this.gWatchBoxingAcc);
  535. }
  536. });
  537. }
  538. } else if (temp.funName == "unbindBoxingPost") {
  539. uni.$off('watchAcceleration', this.gWatchBoxingAcc);
  540. uni.$emit("unBindAcc", this.globalAcc);
  541. this.$store.state.globalAcc = null;
  542. } else if (temp.funName == "setAngleRatio") {
  543. if (gameData) {
  544. this.angleRatio = Number(gameData.angleRatio);
  545. console.log("this.angleRatio:", this.angleRatio);
  546. this.sendMessage("onSetAngleRatio", {
  547. angleRatio: this.angleRatio
  548. });
  549. }
  550. } else if (temp.funName == "bindHitBoxingPost") {
  551. /**
  552. * 返回拳击柱的绑定状态
  553. */
  554. console.log("bindHitBoxingPost");
  555. if (this.ConnectBindingDevice) {
  556. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  557. uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  558. //hotman 沙袋情景
  559. this.gCreateSandbagAlgorithm();
  560. this.gStartSimulateBLEUpdate();
  561. return;
  562. }
  563. if (!this.BLEConnectDevice) return;
  564. //用设备来区分,开启什么加速计;
  565. if (this.BLEConnectDevice.deviceType == "BLEHandle") {
  566. //处理蓝牙设备情况下
  567. //开启设备回调 3/4关闭
  568. this.onWriteBLEConnectionValue({
  569. value: "3"
  570. });
  571. // setTimeout(() => {
  572. // //设置加速计b:20ms a:10ms
  573. // this.onWriteBLEConnectionValue({
  574. // value: config.refreshRate
  575. // });
  576. // }, 2000)
  577. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  578. uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  579. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice.usageMode ==
  580. "general") {
  581. //手机情景或者自由模式
  582. this._createEquipmentBasedOnBoxingPostHit();
  583. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  584. //hotman 沙袋情景
  585. // this.gCreateFilterObj();
  586. this.gCreateSandbagAlgorithm();
  587. }
  588. } else if (this.BLEConnectDevice.deviceType == "mySelf") {
  589. //处理手机本身情况下
  590. if (this.globalAcc) {
  591. uni.$off('watchAcceleration', this.gWatchHitBoxingAcc);
  592. uni.$on('watchAcceleration', this.gWatchHitBoxingAcc);
  593. } else {
  594. //开启手机加速计
  595. uni.$emit("bindAcc", {
  596. callback: (accId) => {
  597. console.log("bindHitBoxingPost开启的:globalAcc=", accId);
  598. this.bGameOpenListen = true;
  599. this.$store.state.globalAcc = accId;
  600. uni.$off('watchAcceleration', this.gWatchHitBoxingAcc);
  601. uni.$on('watchAcceleration', this.gWatchHitBoxingAcc);
  602. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice
  603. .usageMode == "general") {
  604. //手机情景或者自由模式
  605. this._createEquipmentBasedOnBoxingPostHit();
  606. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  607. //hotman 沙袋情景 todo
  608. console.warn("没有处理手机加速计的 gCreateFilterObj");
  609. // this.gCreateFilterObj();
  610. }
  611. }
  612. });
  613. }
  614. } else if (this.BLEConnectDevice.deviceType == "BLERope") {
  615. //todo 开启跳绳模式
  616. this.B_OpenRopeSkipping();
  617. // this._createActionJumpOnBoxingPostHit();
  618. //监听蓝牙回调
  619. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  620. uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  621. }
  622. } else if (temp.funName == "unbindHitBoxingPost") {
  623. /**
  624. * 解绑拳击柱的绑定状态
  625. */
  626. if (this.ConnectBindingDevice) {
  627. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  628. this.gStopSimulateBLEUpdate();
  629. return;
  630. }
  631. if (this.BLEConnectDevice && this.BLEConnectDevice.deviceType == "BLEHandle") {
  632. //开启设备回调 3/4关闭
  633. this.onWriteBLEConnectionValue({
  634. value: "4"
  635. });
  636. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  637. } else if (this.BLEConnectDevice && this.BLEConnectDevice.deviceType == "BLERope") {
  638. console.warn("unbindHitBoxingPost 还没设置关闭跳绳模式")
  639. this.onClearData();
  640. this.B_CloseRopeSkipping();
  641. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  642. } else {
  643. uni.$off('watchAcceleration', this.gWatchHitBoxingAcc);
  644. uni.$emit("unBindAcc", this.globalAcc);
  645. this.$store.state.globalAcc = null;
  646. }
  647. } else if (temp.funName == "closeGame") {
  648. uni.getCurrentSubNVue().hide('auto');
  649. } else if (temp.funName == "addQuitModal") {
  650. this.gameQuitListener = true;
  651. this.sendMessage("onQuitModalListener", {
  652. bListener: this.gameQuitListener
  653. });
  654. } else if (temp.funName == "removeQuitModal") {
  655. this.gameQuitListener = false;
  656. this.sendMessage("onQuitModalListener", {
  657. bListener: this.gameQuitListener
  658. });
  659. } else if (temp.funName == "writeBLEConnectionValue") {
  660. if (!gameData.value || gameData.value == "") return;
  661. if (!this.BLEConnectDevice) return;
  662. //蓝牙写入数据
  663. this.onWriteBLEConnectionValue(gameData);
  664. } else if (temp.funName == "log") {
  665. console.log(gameData);
  666. } else if (temp.funName == "addDeviceUpdateListener") {
  667. uni.$off('updateBLEDeviceData', this.BLECallback);
  668. uni.$on('updateBLEDeviceData', this.BLECallback);
  669. } else if (temp.funName == "closeDeviceUpdateListener") {
  670. uni.$off('updateBLEDeviceData', this.BLECallback);
  671. } else if (temp.funName == "addDeviceJsonUpdateListener") {
  672. uni.$off('updateBLEDeviceJson', this.BLEJsonCallback);
  673. uni.$on('updateBLEDeviceJson', this.BLEJsonCallback);
  674. } else if (temp.funName == "closeDeviceJsonUpdateListener") {
  675. uni.$off('updateBLEDeviceJson', this.BLEJsonCallback);
  676. }
  677. //获取手机原始陀螺仪和加速计数据
  678. else if (temp.funName == "onStartAccAndGyro") {
  679. this.bMyAttitudeListen = true;
  680. this.gStartSimulateBLEUpdate();
  681. //监听蓝牙回调
  682. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  683. uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  684. } else if (temp.funName == "onStopAccAndGyro") {
  685. this.bMyAttitudeListen = false;
  686. this.gStopSimulateBLEUpdate();
  687. //监听蓝牙回调
  688. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  689. }
  690. },
  691. onPageStart: function(e) {
  692. // 监听页面加载成功
  693. this.PageStart = true;
  694. console.log("onPageStart==", e);
  695. setTimeout(() => {
  696. //加载成功后,显示
  697. uni.getCurrentSubNVue().show('fade-in', 250, () => {});
  698. }, 100)
  699. },
  700. onPageFinish: function(e) {
  701. console.log("onPageFinish==", e);
  702. },
  703. onError: function(e) {
  704. // 监听页面加载错误
  705. // this.error = this.url;
  706. console.error(e);
  707. },
  708. gWatchAcceleration: function(a) {
  709. this.sendMessage("onWatchAccelerometer", a);
  710. return;
  711. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  712. this.sendMessage("onWatchAccelerometer", a);
  713. } else if (this.BLEConnectDevice && this.cIndex != -1) {
  714. this.sendMessage("onWatchAccelerometer", a);
  715. }
  716. },
  717. gWatchOrientation: function(o) {
  718. this.sendMessage("onWatchOrientation", o);
  719. return;
  720. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  721. this.sendMessage("onWatchOrientation", o);
  722. } else if (this.BLEConnectDevice && this.cIndex != -1) {
  723. this.sendMessage("onWatchOrientation", o);
  724. }
  725. },
  726. //监听返回计算好的puch
  727. gWatchBoxingAcc: function(a) {
  728. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  729. //计算返回 puch leftPunch rightPunch
  730. this.updateHitData(a);
  731. } else if (this.BLEConnectDevice && this.cIndex != -1) {
  732. this.updateHitData(a);
  733. }
  734. },
  735. //只返回一个hit状态
  736. gWatchHitBoxingAcc: function(a) {
  737. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  738. //计算返回 puch leftPunch rightPunch
  739. this.EquipmentActionObj.updateAcc({
  740. xA: a.xAxis,
  741. zA: a.yAxis,
  742. yA: a.zAxis
  743. })
  744. } else if (this.cIndex != -1 && this.BLEConnectDevice != null) {
  745. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice.usageMode == "general") {
  746. this.EquipmentActionObj.updateAcc({
  747. xA: a.xAxis,
  748. zA: a.yAxis,
  749. yA: a.zAxis,
  750. bLimitRebound: false
  751. })
  752. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  753. //hotman 沙袋情景 todo
  754. //后面处理手机加速计的数据
  755. }
  756. }
  757. },
  758. gWatchBLEUpdate: function(data) {
  759. //如果是模块外面触发,需要离线打包
  760. if (this.bMyAttitudeListen) {
  761. this.sendMessage("updateAccAndGyro", data);
  762. return;
  763. }
  764. if (this.ConnectBindingDevice) {
  765. //hotman 沙袋情景 todo
  766. this.gUpdateSandbagAlgorithm({
  767. data: data,
  768. callback: (res) => {
  769. // console.log(res);
  770. if (res.type == 'hit') {
  771. // console.log('gUpdateFilter callback:',res)
  772. //记录打击次数
  773. this.addSandbagHitCount({
  774. count: 1
  775. });
  776. let temp = {
  777. direction: res.direction,
  778. angle: res.angle,
  779. name: res.name,
  780. ename: res.ename,
  781. value: res.hit,
  782. mass: 10, //质量
  783. hitPower: res.hit ,//计算的力
  784. //新增类型
  785. sendType:"ConnectBindingDevice",
  786. type:"none"
  787. }
  788. this.sendMessage("onBoxingPostHit", temp);
  789. }
  790. }
  791. });
  792. return;
  793. }
  794. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice.usageMode == "general") {
  795. //手机情景或者自由模式
  796. let {
  797. ax,
  798. ay,
  799. az
  800. } = data.acc;
  801. //this.BLEConnectDevice.limitType == "rebound"
  802. //这里是监听 _createEquipmentBasedOnBoxingPostHit 回调
  803. this.EquipmentActionObj.updateTriaxialAcc({
  804. xA: ax * 9.80665,
  805. zA: ay * 9.80665,
  806. yA: az * 9.80665,
  807. bLimitRebound: false
  808. })
  809. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  810. //hotman 沙袋情景 todo
  811. this.gUpdateSandbagAlgorithm({
  812. data: data,
  813. callback: (res) => {
  814. // console.log(res);
  815. if (res.type == 'hit') {
  816. // console.log('gUpdateFilter callback:',res)
  817. //记录打击次数
  818. this.addSandbagHitCount({
  819. count: 1
  820. });
  821. let temp = {
  822. direction: res.direction,
  823. angle: res.angle,
  824. name: res.name,
  825. ename: res.ename,
  826. value: res.hit,
  827. mass: 10, //质量
  828. hitPower: res.hit ,//计算的力
  829. //新增类型
  830. sendType:"hotman",
  831. type:"none"
  832. }
  833. this.sendMessage("onBoxingPostHit", temp);
  834. }
  835. }
  836. });
  837. } else if (this.BLEConnectDevice.usageMode == "ropeSkipping") {
  838. //跳绳蓝牙反馈
  839. this.onBLERopeUpdate(data);
  840. }
  841. },
  842. updateHitData(a) {
  843. this.xA = a.xAxis;
  844. this.yA = a.yAxis;
  845. this.zA = a.zAxis;
  846. if (Math.abs(this.xA) > 8 && this.oldxA != this.xA && !this.bCalculation) {
  847. this.xAccArray.push(this.xA);
  848. this.oldxA = this.xA;
  849. if (this.calTimeout == null) {
  850. this.calTimeout = setTimeout(() => {
  851. this.bCalculation = true;
  852. for (let i = 0; i < this.xAccArray.length; i++) {
  853. if (this.xAccArray[i] < 0 && this.xAccArray[i] < this.xAccArray[this.xMin]) {
  854. this.xMin = i;
  855. } else if (this.xAccArray[i] > 0 && this.xAccArray[i] > this.xAccArray[this
  856. .xMax]) {
  857. this.xMax = i;
  858. }
  859. }
  860. console.log(this.xAccArray, "==", this.xMin, "==", this.xMax);
  861. if (this.xAccArray[this.xMin] < 0 && this.xAccArray[this.xMax] > 0) {
  862. if (this.xMin > this.xMax)
  863. this.onHit("xRCount", "左勾拳", "leftPunch", this.xAccArray[this.xMin], Math.ceil(
  864. Math.abs(this.xAccArray[this.xMin]) *
  865. puchConfig.BOXING_MASS));
  866. else if (this.xMin < this.xMax)
  867. this.onHit("xLCount", "右勾拳", "rightPunch", this.xAccArray[this.xMax], Math
  868. .ceil(Math.abs(this.xAccArray[this
  869. .xMax]) * puchConfig.BOXING_MASS));
  870. } else if (this.xAccArray[this.xMin] < 0) {
  871. this.onHit("xLCount", "右勾拳", "rightPunch", this.xAccArray[this.xMin], Math.ceil(
  872. Math.abs(this.xAccArray[this.xMin]) *
  873. puchConfig.BOXING_MASS));
  874. } else if (this.xAccArray[this.xMax] > 0) {
  875. this.onHit("xRCount", "左勾拳", "leftPunch", this.xAccArray[this.xMax], Math.ceil(Math
  876. .abs(this.xAccArray[this.xMax]) *
  877. puchConfig.BOXING_MASS));
  878. }
  879. setTimeout(() => {
  880. this.onResetCal();
  881. }, 100);
  882. this.calTimeout = null;
  883. }, 200);
  884. }
  885. } else if (this.zA < -10 && this.oldzA != this.zA && this.xAccArray.length == 0) {
  886. console.log("this.zA:", this.zA);
  887. this.oldzA = this.zA;
  888. setTimeout(() => {
  889. this.onResetCal();
  890. }, 200);
  891. this.onHit("zLCount", "直拳", "punch", this.zA, Math.ceil(Math.abs(this.zA) * puchConfig.BOXING_MASS));
  892. }
  893. },
  894. onHit(direction, name, ename, direValue, power) {
  895. // console.log(direction, direValue, power);
  896. let temp = {
  897. direction: direction,
  898. angle: 0,
  899. name: name,
  900. ename: ename,
  901. value: direValue,
  902. mass: this.mass, //质量
  903. hitPower: power ,//计算的力
  904. //新增类型
  905. sendType:"BindBoxingPost",
  906. type:"none"
  907. }
  908. this.sendMessage("onBoxingPostHit", temp);
  909. },
  910. onResetCal() {
  911. this.xAccArray = [];
  912. this.xMax = 0;
  913. this.xMin = 0;
  914. this.bCalculation = false;
  915. this.oldxA = 0;
  916. this.oldzA = 0;
  917. this.calTimeout = null;
  918. },
  919. //创建一个 打击对象
  920. _createEquipmentBasedOnBoxingPostHit() {
  921. this.EquipmentActionObj = new EquipmentAction();
  922. this.EquipmentActionObj.addEventListener("resultantHit", (e) => {
  923. console.log(e);
  924. let temp = {
  925. direction: "allCount",
  926. angle: 0,
  927. name: "击中",
  928. ename: "hit",
  929. value: e.acc,
  930. mass: e.mass, //质量
  931. hitPower: e.power ,//计算的力
  932. //新增类型
  933. sendType:"EquipmentActionObj",
  934. type:"none"
  935. }
  936. this.sendMessage("onBoxingPostHit", temp);
  937. })
  938. },
  939. /**
  940. * 创建一个 跳识别对象
  941. * @deprecated 此方法废弃,将使用c语音识别算法。
  942. */
  943. _createActionJumpOnBoxingPostHit() {
  944. this.actionJumpObj = new ActionJump();
  945. this.actionJumpObj.addEventListener('resultant', (e) => {
  946. let temp = {
  947. direction: "all",
  948. angle: 0,
  949. name: "击中", // this.BLEConnectDevice.name,
  950. ename: "hit", //this.BLEConnectDevice.ename,
  951. value: 10,
  952. mass: 10, //质量
  953. hitPower: 10 ,//计算的力
  954. //新增类型
  955. sendType:"ActionJumpObj",
  956. type:"none"
  957. }
  958. if (e.type == 'stateDataOfJump') {
  959. // console.log("game stateDataOfJump");
  960. this.sendMessage("onBoxingPostHit", temp);
  961. } else if (e.type == 'stop') {
  962. // console.log("game stop");
  963. this.onClearData();
  964. }
  965. })
  966. },
  967. /**
  968. * @param {Object} gameData
  969. * 识别跳部分数据处理
  970. */
  971. onBLERopeUpdate(gameData) {
  972. //调用识别算法
  973. this.jumpIdenModule_onJump6AxisDataUpdate({
  974. data: {
  975. hex: gameData.hex
  976. },
  977. callback: (res) => {
  978. if (res.code == 0) {
  979. if (res.tag != -1) {
  980. // this.jumpIdenModule_listenStateDataOfJump(res);
  981. /**
  982. * @deprecated
  983. */
  984. let temp = {
  985. direction: "all",
  986. angle: 0,
  987. name: "跳", // this.BLEConnectDevice.name,
  988. ename: "hit", //this.BLEConnectDevice.ename,
  989. value: 0, //
  990. mass: 0, //质量
  991. hitPower: 0 ,//计算的力
  992. //新增类型
  993. sendType:"RopeSkipping",
  994. type:"none",
  995. angle:res.tag, //返回一个 tag方向
  996. }
  997. switch (res.tag) {
  998. case 0:
  999. //JumpType.NORMAL = 0
  1000. temp.type = "normal";
  1001. break;
  1002. case 1:
  1003. //right jump
  1004. temp.type = "right";
  1005. break;
  1006. case 2:
  1007. //left jump
  1008. temp.type = "left";
  1009. break;
  1010. case 3:
  1011. //right rotate jump
  1012. temp.type = "right_rotate";
  1013. break;
  1014. case 4:
  1015. //left rotate jump
  1016. temp.type = "left_rotate";
  1017. break;
  1018. default:
  1019. break;
  1020. }
  1021. this.sendMessage("onBoxingPostHit", temp);
  1022. // uni.showToast({
  1023. // title: res.tag + ',' + res.msg,
  1024. // icon: 'none',
  1025. // duration: 1000,
  1026. // mask: true
  1027. // })
  1028. }
  1029. }
  1030. }
  1031. });
  1032. //********陀螺仪角速度********
  1033. let {
  1034. gx,
  1035. gy,
  1036. gz
  1037. } = gameData.gyro;
  1038. let {
  1039. min,
  1040. s,
  1041. ms
  1042. } = gameData;
  1043. //-gameData.acc.ax * 10;
  1044. let _ax = gameData.acc.ax * 10;
  1045. let _ay = gameData.acc.ay * 10;
  1046. let _az = gameData.acc.az * 10;
  1047. //低通滤波分离重力
  1048. let alpha = 0.8;
  1049. this.xA = alpha * this.xA + (1 - alpha) * _ax;
  1050. this.yA = alpha * this.yA + (1 - alpha) * _ay;
  1051. this.zA = alpha * this.zA + (1 - alpha) * _az;
  1052. //高通滤波获取线性速度
  1053. let linear_acceleration_x = _ax - this.xA;
  1054. let linear_acceleration_y = _az - this.zA;
  1055. let linear_acceleration_z = _ay - this.yA;
  1056. // let _temp = {
  1057. // linearAcc: {
  1058. // lAccX: linear_acceleration_x,
  1059. // lAccY: linear_acceleration_y,
  1060. // lAccZ: linear_acceleration_z
  1061. // }, //gameData.acc,
  1062. // oriAcc: {
  1063. // oAccX: _ax,
  1064. // oAccY: _ay,
  1065. // oAccZ: _az
  1066. // },
  1067. // gravityAcc: {
  1068. // gravityX: this.xA,
  1069. // gravityY: this.yA,
  1070. // gravityZ: this.zA
  1071. // },
  1072. // bLimitRebound: false,
  1073. // resultant: Math.sqrt(_ax * _ax +
  1074. // _ay * _ay + _az * _az),
  1075. // runIndex: this.BLEAccIndex,
  1076. // //陀螺仪
  1077. // oriGyro: {
  1078. // oGyroX: gx,
  1079. // oGyroY: gy,
  1080. // oGyroZ: gz
  1081. // },
  1082. // //输入当前轴,旧设备使用Y轴,即为true
  1083. // bYAxis: this.bOldDeviceType ? true : false,
  1084. // };
  1085. // this.actionJumpObj.updateJump(_temp);
  1086. this.BLEAccIndex++;
  1087. },
  1088. onClearData() {
  1089. this.BLEAccIndex = 0;
  1090. },
  1091. }
  1092. }
  1093. </script>
  1094. <style>
  1095. /* #ifdef APP-PLUS */
  1096. .web-view {
  1097. flex: 1;
  1098. flex-direction: column;
  1099. /* background-color: #007AFF; */
  1100. }
  1101. .web-view-child {
  1102. width: 750rpx;
  1103. flex: 1;
  1104. }
  1105. /* #endif */
  1106. /* #ifdef H5 */
  1107. .web-view {
  1108. display: flex;
  1109. flex-direction: column;
  1110. position: absolute;
  1111. bottom: 0;
  1112. top: 0;
  1113. left: 0;
  1114. right: 0;
  1115. }
  1116. .web-view-child {
  1117. position: relative;
  1118. width: 100%;
  1119. height: 100%;
  1120. }
  1121. /* #endif */
  1122. .sendMessage {
  1123. width: 300rpx;
  1124. position: fixed;
  1125. bottom: 100rpx;
  1126. left: 50rpx;
  1127. }
  1128. .web-back {
  1129. position: fixed;
  1130. top: 40px;
  1131. right: 20px;
  1132. width: 160rpx;
  1133. height: 80rpx;
  1134. border-radius: 45px;
  1135. /* border: 1px solid #FFFFFF; */
  1136. /* box-shadow: 0px 0px 1px #FFFFFF; */
  1137. background-color: rgba(0, 0, 0, 1);
  1138. opacity: 0.5;
  1139. /* #ifndef APP-PLUS-NVUE */
  1140. /* z-Index: 999; */
  1141. display: flex;
  1142. /* #endif */
  1143. justify-content: center;
  1144. align-items: center;
  1145. }
  1146. </style>