subGame.nvue 34 KB

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