subGame.nvue 35 KB

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