subGame.nvue 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. <template>
  2. <view class="web-view">
  3. <!-- :src="url" src = 'http://192.168.0.112:7456/build/index.html' :src="LocationGameUrl" -->
  4. <!-- @receivedtitle="onReceivedTitle" @pagefinish="onPageFinish"-->
  5. <web-view class="web-view-child" :src="url" ref="webview" @pagestart="onPageStart" @onPostMessage="handlePostMessage"
  6. @error="onError"></web-view>
  7. <view class="web-back" @click="navBack">
  8. <image style="width: 40rpx;height: 40rpx;" src="/static/gameCloseW.png"></image>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. import reqUtil from "@/util/util-js/requstUtil.js";
  14. import config from "@/common/config.js";
  15. import fruit from "@/components/fruitMachine/fruit.js"
  16. // import AccAndOri from "@/util/util-js/AccAndOri.js"
  17. import puchConfig from "@/util/util-js/puchConfig.js"
  18. import EquipmentAction from "@/util/util-js/EquipmentAction.js"
  19. var currentWebview;
  20. var orientId = null,
  21. accId = null;
  22. import {
  23. mapState,
  24. mapMutations
  25. } from 'vuex';
  26. export default {
  27. computed: mapState([
  28. 'avatarUrl', 'gender', 'userName', 'city', 'BLEConnectDevice', 'instructionState', 'currentInstruction',
  29. 'cIndex', 'globalAcc', 'globalOri', 'LocationGameUrl',
  30. 'ConnectBindingDevice',
  31. 'currentModeIndex'
  32. ]),
  33. data() {
  34. return {
  35. url: '',
  36. pagefinish: '',
  37. error: '',
  38. PageStart: false, // 记录网页请求的加载状态,true 加载成功 false 加载失败
  39. gameQuitListener: false, //游戏是否初始化退出监听
  40. xA: 0,
  41. yA: 0,
  42. zA: 0,
  43. oldxA: 0,
  44. oldzA: 0,
  45. bUpdateOnce: false,
  46. bDelayOnce: false,
  47. mass: puchConfig.BOXING_MASS,
  48. //X轴的变化数组值
  49. xAccArray: [],
  50. xMax: 0,
  51. maxTimeoutId: null,
  52. bMaxPause: false,
  53. xMin: 0,
  54. minTimeoutId: null,
  55. bMinPause: false,
  56. bCalculation: false,
  57. calTimeout: null,
  58. //角度比 z/x
  59. angleRatio: 1, //
  60. //当前选择的item
  61. decodeItem: null,
  62. //拳击数据判断对象
  63. EquipmentActionObj: null,
  64. BLENum: 0,
  65. BLEACX: 0,
  66. BLEACYL: 0,
  67. //是否是游戏里面开启的监听,如果是的话,退出游戏界面时候需要关闭相应的监听,如加速计和陀螺仪
  68. bGameOpenListen: false,
  69. }
  70. },
  71. onLoad() {
  72. let _self = this;
  73. var pages = getCurrentPages();
  74. var page = pages[pages.length - 1];
  75. currentWebview = page.$getAppWebview();
  76. //监听一次调用setOnceGameOption
  77. console.log("子组件初始化");
  78. uni.$once("setOnceGameOption", (option) => {
  79. console.log("setOnceGameOption=", option);
  80. if (option && option.item) {
  81. this.decodeItem = JSON.parse(decodeURIComponent(option.item));
  82. this.url = this.decodeItem.gameWebUrl;
  83. let temp = {
  84. fObjectId: this.decodeItem.gameId,
  85. recentlyType: 0
  86. }
  87. // console.log("game temp:", config.URL.RECENTLYPLAYINGADD);
  88. //添加最近在玩的游戏
  89. reqUtil.requestData(config.URL.RECENTLYPLAYINGADD, temp).then(res => {
  90. console.log('RECENTLYPLAYINGADD =====', res);
  91. if (res.code == 0) {}
  92. },
  93. e => {
  94. console.log(e)
  95. });
  96. }
  97. });
  98. //子窗体onload 后获取父窗体的onload数据
  99. uni.$emit("game-load");
  100. uni.getCurrentSubNVue().addEventListener("hide", function() {
  101. console.log("subNVue子窗体已隐藏!");
  102. //通知游戏,页面退出
  103. _self.sendMessage("onSubHide", {
  104. msg: '退出页面'
  105. });
  106. uni.$off('updateBLEDeviceData', _self.BLECallback);
  107. //json
  108. uni.$off('updateBLEDeviceJson', _self.BLEJsonCallback);
  109. // console.log(_self.BLEConnectDevice);
  110. //如果连接了蓝牙设备是手柄
  111. if (_self.BLEConnectDevice && _self.BLEConnectDevice.deviceType == "BLEHandle") {
  112. //开启设备回调 3/4关闭
  113. _self.onWriteBLEConnectionValue({
  114. value: "4"
  115. });
  116. //关闭json 回调
  117. _self.onWriteBLEConnectionValue({
  118. value: "6"
  119. });
  120. // _self.onWriteBLEConnectionValue({
  121. // value: "4",
  122. // getSuccess:()=>{
  123. // // 发送获取mac
  124. // _self.onWriteBLEConnectionValue({
  125. // value: "6"
  126. // });
  127. // }
  128. // });
  129. uni.$off('updateBLEDeviceData', _self.gWatchBLEUpdate);
  130. }
  131. uni.$emit("game-unload", {
  132. globalAcc: _self.globalAcc,
  133. globalOri: _self.globalOri,
  134. bGameOpenListen: _self.bGameOpenListen
  135. });
  136. });
  137. //*****注释蓝牙操作******
  138. // 监听事件
  139. // uni.$on('callbackCloseBLE', this.callbackCloseBLE);
  140. // uni.$on('callbackBLEState', this.callbackBLEState);
  141. // uni.$on('updateBLEDeviceData', this.BLECallback);
  142. //监听物理返回按钮
  143. plus.key.addEventListener('backbutton', () => {
  144. this.navBack();
  145. }, false);
  146. },
  147. onUnload() {
  148. console.log("subNVue子窗体 onUnload!");
  149. //*****注释蓝牙操作******
  150. // uni.$off('callbackCloseBLE', this.callbackCloseBLE);
  151. // uni.$off('callbackBLEState', this.callbackBLEState);
  152. //取消相应的绑定事件
  153. uni.$off('watchAcceleration', this.gWatchAcceleration);
  154. uni.$off("watchAcceleration", this.gWatchBoxingAcc);
  155. uni.$off("watchAcceleration", this.gWatchHitBoxingAcc);
  156. uni.$off('watchOrientation', this.gWatchOrientation);
  157. uni.$emit("game-unload");
  158. },
  159. methods: {
  160. ...mapMutations(['addlocalCalorie', 'syncRequestEvent', 'onWriteBLEConnectionValue',
  161. 'gCreateFilterObj', 'gUpdateFilter'
  162. ]),
  163. navBack() {
  164. this.sendMessage("onQuit", {
  165. type: "right-button",
  166. msg: '点击右上角按钮退出游戏'
  167. });
  168. uni.showModal({
  169. title: '提示',
  170. content: '是否退出游戏?',
  171. success: (res) => {
  172. //如果游戏需要监听退出,则需要走完游戏端流程,才能退出,否则直接退出
  173. if (this.gameQuitListener) {
  174. this.sendMessage("onQuitModal", {
  175. data: res,
  176. msg: '退出提示'
  177. });
  178. } else {
  179. if (res.confirm) {
  180. uni.getCurrentSubNVue().hide('auto');
  181. }
  182. }
  183. }
  184. })
  185. },
  186. //蓝牙断开连接时候
  187. callbackCloseBLE() {
  188. this.sendMessage("onDeviceClose", {
  189. msg: '设备断开连接。'
  190. });
  191. },
  192. //蓝牙状态回调
  193. callbackBLEState(res) {
  194. // console.log("game callbackBLEState==", res);
  195. // 暂时只返回数据连接错误信息
  196. // uni.$emit("callbackBLEState", {
  197. // state: -1,
  198. // msg: "设备数据错误"
  199. // });
  200. this.sendMessage("onDeviceState", res)
  201. },
  202. BLECallback(data) {
  203. let dataType = 'Box';
  204. // if (data.hasOwnProperty("ax")) {
  205. // dataType = "Accelerometer"
  206. // }else if(data.hasOwnProperty("gx")){
  207. // dataType = "Gyroscope"
  208. // }
  209. // 设备回调后面用这个接口
  210. let BLEState = {
  211. device: this.BLEConnectDevice, //连接的蓝牙设备
  212. instructionState: this.instructionState, //开启的指令状态
  213. currentInstruction: this.currentInstruction, //当前发送的指令记录
  214. dataType: dataType, //回调的数据类型
  215. data: data //蓝牙回调的数据
  216. }
  217. this.sendMessage("onDeviceUpdateData", BLEState);
  218. },
  219. BLEJsonCallback(data) {
  220. let dataType = 'Json';
  221. // 设备回调后面用这个接口
  222. let BLEState = {
  223. device: this.BLEConnectDevice, //连接的蓝牙设备
  224. instructionState: this.instructionState, //开启的指令状态
  225. currentInstruction: this.currentInstruction, //当前发送的指令记录
  226. dataType: dataType, //回调的数据类型
  227. data: data //蓝牙回调的数据
  228. }
  229. this.sendMessage("onDeviceUpdateJson", BLEState);
  230. },
  231. /**
  232. * 图片转化base64
  233. * @param {Object} url
  234. * @param {Object} callback
  235. */
  236. urlToBase64(url, callback) {
  237. let toBase64Url;
  238. uni.request({
  239. url: url,
  240. method: 'GET',
  241. responseType: 'arraybuffer',
  242. success: async res => {
  243. let base64 = uni.arrayBufferToBase64(res.data);
  244. toBase64Url = 'data:image/jpeg;base64,' + base64;
  245. if (callback)
  246. callback(toBase64Url, res.data);
  247. }
  248. })
  249. },
  250. /**
  251. * 初始化发送数据给游戏
  252. */
  253. sendGameInit() {
  254. this.urlToBase64(this.avatarUrl, (toBase64Url) => {
  255. let device = null;
  256. /**
  257. * 卡路里消耗参数
  258. */
  259. let calorieParams = null;
  260. if (this.BLEConnectDevice) {
  261. // device = {
  262. // cname: this.BLEConnectDevice.cname,
  263. // ename: this.BLEConnectDevice.ename,
  264. // name: this.BLEConnectDevice.name
  265. // };
  266. // calorieParams = {
  267. // runUnit: 0.55, // 比跳的稍微大一点
  268. // jumpUnit: 0.5 // 跳的次数 * 0.5
  269. // };
  270. device = {
  271. cname: this.BLEConnectDevice.cname,
  272. ename: this.BLEConnectDevice.ename,
  273. name: this.BLEConnectDevice.name
  274. };
  275. calorieParams = {
  276. hitUnit: puchConfig.getBoxingCalorie(1)
  277. };
  278. } else if (this.ConnectBindingDevice) {
  279. device = {
  280. cname: this.ConnectBindingDevice.cname,
  281. ename: this.ConnectBindingDevice.ename,
  282. name: this.ConnectBindingDevice.name
  283. };
  284. calorieParams = {
  285. hitUnit: puchConfig.getBoxingCalorie(1)
  286. };
  287. }
  288. //到时候可能要区分是什么类型
  289. let item = {
  290. id: this.decodeItem.gameId,
  291. name: this.decodeItem.gameName
  292. };
  293. // console.log(this.BLEConnectDevice, device);
  294. let gameData = {
  295. avatarUrl: this.avatarUrl,
  296. avatarBase64Url: toBase64Url,
  297. userName: this.userName,
  298. gender: this.gender,
  299. caloriUnit: 10,
  300. calorieParams: calorieParams,
  301. //蓝牙连接的设备
  302. device: device,
  303. item: item
  304. }
  305. this.sendMessage("onGameInit", gameData);
  306. // console.log("onGameInit =", gameData);
  307. })
  308. },
  309. /**
  310. * 统一发送信息
  311. * @param {Object} gameData
  312. */
  313. sendMessage(functionName, gameData) {
  314. let data = {
  315. "funName": functionName,
  316. "gameData": gameData
  317. }
  318. if (!this.PageStart) {
  319. console.warn("页面未初始化不能传消息", data);
  320. return;
  321. }
  322. let initStr = JSON.stringify(data);
  323. this.$refs.webview.evalJs("onWebViewMessage(" + initStr + ")");
  324. },
  325. sendMessageToWebview() {
  326. if (!this.PageStart) {
  327. uni.showModal({
  328. title: "暴躁的提示",
  329. content: "只有网页加载成功了才可以传参过去,不然无效哦。。"
  330. })
  331. return false;
  332. }
  333. /**
  334. * 下面的 jsfunction 代码你要自己在你的网页里面写一个方法 大致如下
  335. * 下面就是你基本的html知识了,我就不想说什么了,自己学。
  336. window.jsfunction = function(data){
  337. console.log('data', data);
  338. }
  339. */
  340. this.sendMessage("onDeviceUpdateData", {
  341. H: 1
  342. });
  343. },
  344. handlePostMessage: function(postData) {
  345. console.log("handlePostMessage得到参数", postData.detail);
  346. let temp = postData.detail.data[0];
  347. let gameData = temp.gameData;
  348. if (temp.funName == "uploadInfo") {
  349. //TODO:写入排行榜分数,后面需要在游戏完结里面调用
  350. //TODO: 后面游戏时间,卡路里的需要写入数据库,目前只是处理了分数
  351. // let num = Math.round(Math.random() * 10000);
  352. //1. gameScore 处理上传的分数
  353. // let _temp = {
  354. // gameId: '1595755153789139696',
  355. // score: 1230,
  356. // cityCode: '110101',
  357. // };
  358. //2. calorieBurned 处理卡路里
  359. // 记录卡路里到本地
  360. this.addlocalCalorie(gameData.calorieBurned);
  361. console.log("gameData.calorieBurned", gameData.calorieBurned);
  362. //然后更新上服务器
  363. this.syncRequestEvent({
  364. success: () => {
  365. // uni.$emit('updateArcbarData', '');
  366. let _temp = {
  367. //假如排行id 为空,排行榜就是游戏自己的id
  368. gameId: this.decodeItem.gameRankingId == null ? this.decodeItem.gameId : this.decodeItem.gameRankingId,
  369. score: gameData.gameScore,
  370. cityCode: this.city.cityCode,
  371. bMaxLimit: true
  372. }
  373. console.log('_temp =====', _temp);
  374. reqUtil.requestData(config.URL.UPLOADRANKING, _temp, "POST").then(res => {
  375. console.log('UPLOADRANKING =====', res);
  376. if (res.code == 0) {
  377. //TODO 后续游戏数据处理
  378. this.sendMessage("onUploadInfo", {
  379. code: 0,
  380. msg: '上传成功'
  381. });
  382. } else {
  383. //TODO 后续游戏数据处理
  384. this.sendMessage("onUploadInfo", {
  385. code: 400,
  386. msg: '上传失败'
  387. });
  388. }
  389. },
  390. e => {
  391. console.log(e)
  392. });
  393. },
  394. fail: () => {
  395. this.sendMessage("onUploadInfo", {
  396. code: 400,
  397. msg: '上传失败'
  398. });
  399. }
  400. });
  401. //3. gameTime 游戏时间
  402. //TODO 后面处理游戏时长
  403. } else if (temp.funName == "gameInit") {
  404. // 获取游戏信息
  405. this.sendGameInit();
  406. } else if (temp.funName == "aiRandomInfo") {
  407. // 获取aiRandomInfo
  408. reqUtil.requestData(config.URL.AIRANDOMINFO, {}).then(res => {
  409. console.log('AIRANDOMINFO =====', res);
  410. if (res.code == 0) {
  411. let data = res.data;
  412. this.urlToBase64(data.aiAvatar, (toBase64Url) => {
  413. let sendData = {
  414. aiId: data.aiId,
  415. aiName: data.aiName,
  416. aiGender: data.aiGender,
  417. aiType: data.aiType,
  418. aiAvatarBase64Url: toBase64Url
  419. }
  420. this.sendMessage("onAiRandomInfo", sendData);
  421. })
  422. }
  423. },
  424. e => {
  425. console.log(e)
  426. });
  427. } else if (temp.funName == "fruitInfo") {
  428. let fruitIndexArray = [0, 0, 0];
  429. if (gameData.hasOwnProperty("calorie")) {
  430. fruitIndexArray = fruit.getFruitIndex(gameData.calorie);
  431. }
  432. this.urlToBase64("https://bbeng-bucket.oss-cn-beijing.aliyuncs.com/cocos/fruitMachine.png", (toBase64Url) => {
  433. this.sendMessage("onFruitInfo", {
  434. // 水果雪碧图
  435. fruitBase64Url: toBase64Url,
  436. // 雪碧图 单张图片宽高
  437. unitWidth: 100,
  438. unitHeight: 100,
  439. unit: "px",
  440. imageStartPosY: 0,
  441. imageEndPosY: -1200,
  442. fruitIndexArray: fruitIndexArray
  443. });
  444. })
  445. } else if (temp.funName == "urlToBase64") {
  446. if (gameData.url) {
  447. this.urlToBase64(gameData.url, (toBase64Url) => {
  448. this.sendMessage("onUrlToBase64", {
  449. base64: toBase64Url,
  450. });
  451. })
  452. } else {
  453. this.sendMessage("onUrlToBase64", {
  454. base64: {},
  455. });
  456. }
  457. } else if (temp.funName == "openAccelerometer") {
  458. //打开加速计s
  459. // if (this.globalGameAcc) {
  460. // uni.$on('watchAcceleration', this.gWatchAcceleration);
  461. // } else {
  462. // AccAndOri.bindAcc((accId) => {
  463. // this.$store.state.globalGameAcc = accId;
  464. // console.log("开启的:globalGameAcc=",this.globalGameAcc);
  465. // uni.$on('watchAcceleration', this.gWatchAcceleration);
  466. // });
  467. // }
  468. if (this.globalAcc) {
  469. uni.$on('watchAcceleration', this.gWatchAcceleration);
  470. } else {
  471. uni.$emit("bindAcc", {
  472. callback: (accId) => {
  473. console.log("开启的:globalAcc=", accId);
  474. this.bGameOpenListen = true;
  475. this.$store.state.globalAcc = accId;
  476. uni.$on('watchAcceleration', this.gWatchAcceleration);
  477. }
  478. });
  479. }
  480. } else if (temp.funName == "closeAccelerometer") {
  481. uni.$off('watchAcceleration', this.gWatchAcceleration);
  482. uni.$emit("unBindAcc", this.globalAcc);
  483. this.$store.state.globalAcc = null;
  484. } else if (temp.funName == "openOrientation") {
  485. //打开陀螺仪
  486. if (this.globalOri) {
  487. uni.$on('watchOrientation', this.gWatchOrientation);
  488. } else {
  489. // AccAndOri.bindOri((oriId) => {
  490. // this.$store.state.globalOri = oriId;
  491. // uni.$on('watchOrientation', this.gWatchOrientation);
  492. // });
  493. uni.$emit("bindOri", {
  494. callback: (oriId) => {
  495. console.log("开启的:globalOri=", oriId);
  496. this.bGameOpenListen = true;
  497. this.$store.state.globalOri = oriId;
  498. uni.$on('watchOrientation', this.gWatchOrientation);
  499. }
  500. });
  501. }
  502. } else if (temp.funName == "closeOrientation") {
  503. // uni.$off('watchOrientation', this.gWatchOrientation);
  504. uni.$off('watchOrientation', this.gWatchOrientation);
  505. uni.$emit("unBindOri", this.globalOri);
  506. this.$store.state.globalOri = null;
  507. } else if (temp.funName == "bindBoxingPost") {
  508. if (this.globalAcc) {
  509. uni.$on('watchAcceleration', this.gWatchBoxingAcc);
  510. } else {
  511. uni.$emit("bindAcc", {
  512. callback: (accId) => {
  513. console.log("bindBoxingPost开启的:globalAcc=", accId);
  514. this.bGameOpenListen = true;
  515. this.$store.state.globalAcc = accId;
  516. uni.$on('watchAcceleration', this.gWatchBoxingAcc);
  517. }
  518. });
  519. }
  520. } else if (temp.funName == "unbindBoxingPost") {
  521. uni.$off('watchAcceleration', this.gWatchBoxingAcc);
  522. uni.$emit("unBindAcc", this.globalAcc);
  523. this.$store.state.globalAcc = null;
  524. } else if (temp.funName == "setAngleRatio") {
  525. if (gameData) {
  526. this.angleRatio = Number(gameData.angleRatio);
  527. console.log("this.angleRatio:", this.angleRatio);
  528. this.sendMessage("onSetAngleRatio", {
  529. angleRatio: this.angleRatio
  530. });
  531. }
  532. } else if (temp.funName == "bindHitBoxingPost") {
  533. console.log("bindHitBoxingPost");
  534. if(!this.BLEConnectDevice)return;
  535. //用设备来区分,开启什么加速计;
  536. if (this.BLEConnectDevice.deviceType == "BLEHandle") {
  537. //处理蓝牙设备情况下
  538. //开启设备回调 3/4关闭
  539. this.onWriteBLEConnectionValue({
  540. value: "3"
  541. });
  542. //开启设备频率 20ms
  543. this.onWriteBLEConnectionValue({
  544. value: "b"
  545. });
  546. // this.onWriteBLEConnectionValue({
  547. // value: "3",
  548. // getSuccess:()=>{
  549. // // 发送获取mac
  550. // this.onWriteBLEConnectionValue({
  551. // value: "b"
  552. // });
  553. // }
  554. // });
  555. uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  556. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice.usageMode == "general") {
  557. //手机情景或者自由模式
  558. this._createEquipmentBasedOnBoxingPostHit();
  559. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  560. //hotman 沙袋情景
  561. this.gCreateFilterObj();
  562. }
  563. console.log("BLEHandle");
  564. } else if (this.BLEConnectDevice.deviceType == "mySelf") {
  565. //处理手机本身情况下
  566. if (this.globalAcc) {
  567. uni.$on('watchAcceleration', this.gWatchHitBoxingAcc);
  568. } else {
  569. //开启手机加速计
  570. uni.$emit("bindAcc", {
  571. callback: (accId) => {
  572. console.log("bindHitBoxingPost开启的:globalAcc=", accId);
  573. this.bGameOpenListen = true;
  574. this.$store.state.globalAcc = accId;
  575. uni.$on('watchAcceleration', this.gWatchHitBoxingAcc);
  576. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice.usageMode == "general") {
  577. //手机情景或者自由模式
  578. this._createEquipmentBasedOnBoxingPostHit();
  579. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  580. //hotman 沙袋情景 todo
  581. console.warn("没有处理手机加速计的 gCreateFilterObj");
  582. // this.gCreateFilterObj();
  583. }
  584. }
  585. });
  586. }
  587. }
  588. } else if (temp.funName == "unbindHitBoxingPost") {
  589. if (this.BLEConnectDevice&&this.BLEConnectDevice.deviceType == "BLEHandle") {
  590. //开启设备回调 3/4关闭
  591. this.onWriteBLEConnectionValue({
  592. value: "4"
  593. });
  594. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  595. } else {
  596. uni.$off('watchAcceleration', this.gWatchHitBoxingAcc);
  597. uni.$emit("unBindAcc", this.globalAcc);
  598. this.$store.state.globalAcc = null;
  599. }
  600. } else if (temp.funName == "closeGame") {
  601. uni.getCurrentSubNVue().hide('auto');
  602. } else if (temp.funName == "addQuitModal") {
  603. this.gameQuitListener = true;
  604. this.sendMessage("onQuitModalListener", {
  605. bListener: this.gameQuitListener
  606. });
  607. } else if (temp.funName == "removeQuitModal") {
  608. this.gameQuitListener = false;
  609. this.sendMessage("onQuitModalListener", {
  610. bListener: this.gameQuitListener
  611. });
  612. } else if (temp.funName == "writeBLEConnectionValue") {
  613. if (!gameData.value || gameData.value == "") return;
  614. if(!this.BLEConnectDevice)return;
  615. //蓝牙写入数据
  616. this.onWriteBLEConnectionValue(gameData);
  617. } else if (temp.funName == "log") {
  618. console.log(gameData);
  619. } else if (temp.funName == "addDeviceUpdateListener") {
  620. uni.$on('updateBLEDeviceData', this.BLECallback);
  621. } else if (temp.funName == "closeDeviceUpdateListener") {
  622. uni.$off('updateBLEDeviceData', this.BLECallback);
  623. } else if (temp.funName == "addDeviceJsonUpdateListener") {
  624. uni.$on('updateBLEDeviceJson', this.BLEJsonCallback);
  625. } else if (temp.funName == "closeDeviceJsonUpdateListener") {
  626. uni.$off('updateBLEDeviceJson', this.BLEJsonCallback);
  627. }
  628. },
  629. onPageStart: function(e) {
  630. // 监听页面加载成功
  631. this.PageStart = true;
  632. console.log("onPageStart==", e);
  633. setTimeout(()=>{
  634. //加载成功后,显示
  635. uni.getCurrentSubNVue().show('fade-in', 250, () => {});
  636. },100)
  637. },
  638. onPageFinish: function(e) {
  639. console.log("onPageFinish==", e);
  640. },
  641. onError: function(e) {
  642. // 监听页面加载错误
  643. // this.error = this.url;
  644. console.error(e);
  645. },
  646. gWatchAcceleration: function(a) {
  647. this.sendMessage("onWatchAccelerometer", a);
  648. return;
  649. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  650. this.sendMessage("onWatchAccelerometer", a);
  651. } else if (this.BLEConnectDevice && this.cIndex != -1) {
  652. this.sendMessage("onWatchAccelerometer", a);
  653. }
  654. },
  655. gWatchOrientation: function(o) {
  656. this.sendMessage("onWatchOrientation", o);
  657. return;
  658. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  659. this.sendMessage("onWatchOrientation", o);
  660. } else if (this.BLEConnectDevice && this.cIndex != -1) {
  661. this.sendMessage("onWatchOrientation", o);
  662. }
  663. },
  664. //监听返回计算好的puch
  665. gWatchBoxingAcc: function(a) {
  666. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  667. //计算返回 puch leftPunch rightPunch
  668. this.updateHitData(a);
  669. } else if (this.BLEConnectDevice && this.cIndex != -1) {
  670. this.updateHitData(a);
  671. }
  672. },
  673. //只返回一个hit状态
  674. gWatchHitBoxingAcc: function(a) {
  675. if (this.ConnectBindingDevice && this.ConnectBindingDevice.deviceType == 1) {
  676. //计算返回 puch leftPunch rightPunch
  677. this.EquipmentActionObj.updateAcc({
  678. xA: a.xAxis,
  679. zA: a.yAxis,
  680. yA: a.zAxis
  681. })
  682. } else if (this.cIndex != -1 && this.BLEConnectDevice != null) {
  683. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice.usageMode == "general") {
  684. this.EquipmentActionObj.updateAcc({
  685. xA: a.xAxis,
  686. zA: a.yAxis,
  687. yA: a.zAxis,
  688. bLimitRebound: false
  689. })
  690. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  691. //hotman 沙袋情景 todo
  692. //后面处理手机加速计的数据
  693. }
  694. }
  695. },
  696. gWatchBLEUpdate: function(data) {
  697. if (this.BLEConnectDevice.usageMode == "phone" || this.BLEConnectDevice.usageMode == "general") {
  698. //手机情景或者自由模式
  699. let {
  700. ax,
  701. ay,
  702. az
  703. } = data.acc;
  704. //this.BLEConnectDevice.limitType == "rebound"
  705. //这里是监听 _createEquipmentBasedOnBoxingPostHit 回调
  706. this.EquipmentActionObj.updateTriaxialAcc({
  707. xA: ax * 9.80665,
  708. zA: ay * 9.80665,
  709. yA: az * 9.80665,
  710. bLimitRebound: false
  711. })
  712. } else if (this.BLEConnectDevice.usageMode == "hotman") {
  713. //hotman 沙袋情景 todo
  714. this.gUpdateFilter({
  715. data: data,
  716. callback: (res) => {
  717. // console.log(res);
  718. if (res.type == 'hit') {
  719. // console.log('gUpdateFilter callback:',res)
  720. let temp = {
  721. direction: "allCount",
  722. name: "击中",
  723. ename: "hit",
  724. value: res.hit,
  725. mass: 10, //质量
  726. hitPower: res.hit //计算的力
  727. }
  728. this.sendMessage("onBoxingPostHit", temp);
  729. }
  730. }
  731. });
  732. }
  733. },
  734. updateHitData(a) {
  735. this.xA = a.xAxis;
  736. this.yA = a.yAxis;
  737. this.zA = a.zAxis;
  738. if (Math.abs(this.xA) > 8 && this.oldxA != this.xA && !this.bCalculation) {
  739. this.xAccArray.push(this.xA);
  740. this.oldxA = this.xA;
  741. if (this.calTimeout == null) {
  742. this.calTimeout = setTimeout(() => {
  743. this.bCalculation = true;
  744. for (let i = 0; i < this.xAccArray.length; i++) {
  745. if (this.xAccArray[i] < 0 && this.xAccArray[i] < this.xAccArray[this.xMin]) {
  746. this.xMin = i;
  747. } else if (this.xAccArray[i] > 0 && this.xAccArray[i] > this.xAccArray[this.xMax]) {
  748. this.xMax = i;
  749. }
  750. }
  751. console.log(this.xAccArray, "==", this.xMin, "==", this.xMax);
  752. if (this.xAccArray[this.xMin] < 0 && this.xAccArray[this.xMax] > 0) {
  753. if (this.xMin > this.xMax)
  754. this.onHit("xRCount", "左勾拳", "leftPunch", this.xAccArray[this.xMin], Math.ceil(Math.abs(this.xAccArray[this.xMin]) *
  755. puchConfig.BOXING_MASS));
  756. else if (this.xMin < this.xMax)
  757. this.onHit("xLCount", "右勾拳", "rightPunch", this.xAccArray[this.xMax], Math.ceil(Math.abs(this.xAccArray[this
  758. .xMax]) * puchConfig.BOXING_MASS));
  759. } else if (this.xAccArray[this.xMin] < 0) {
  760. this.onHit("xLCount", "右勾拳", "rightPunch", this.xAccArray[this.xMin], Math.ceil(Math.abs(this.xAccArray[this.xMin]) *
  761. puchConfig.BOXING_MASS));
  762. } else if (this.xAccArray[this.xMax] > 0) {
  763. this.onHit("xRCount", "左勾拳", "leftPunch", this.xAccArray[this.xMax], Math.ceil(Math.abs(this.xAccArray[this.xMax]) *
  764. puchConfig.BOXING_MASS));
  765. }
  766. setTimeout(() => {
  767. this.onResetCal();
  768. }, 100);
  769. this.calTimeout = null;
  770. }, 200);
  771. }
  772. } else if (this.zA < -10 && this.oldzA != this.zA && this.xAccArray.length == 0) {
  773. console.log("this.zA:", this.zA);
  774. this.oldzA = this.zA;
  775. setTimeout(() => {
  776. this.onResetCal();
  777. }, 200);
  778. this.onHit("zLCount", "直拳", "punch", this.zA, Math.ceil(Math.abs(this.zA) * puchConfig.BOXING_MASS));
  779. }
  780. },
  781. onHit(direction, name, ename, direValue, power) {
  782. // console.log(direction, direValue, power);
  783. let temp = {
  784. direction: direction,
  785. name: name,
  786. ename: ename,
  787. value: direValue,
  788. mass: this.mass, //质量
  789. hitPower: power //计算的力
  790. }
  791. this.sendMessage("onBoxingPostHit", temp);
  792. },
  793. onResetCal() {
  794. this.xAccArray = [];
  795. this.xMax = 0;
  796. this.xMin = 0;
  797. this.bCalculation = false;
  798. this.oldxA = 0;
  799. this.oldzA = 0;
  800. this.calTimeout = null;
  801. },
  802. //创建一个 打击对象
  803. _createEquipmentBasedOnBoxingPostHit() {
  804. this.EquipmentActionObj = new EquipmentAction();
  805. this.EquipmentActionObj.addEventListener("resultantHit", (e) => {
  806. console.log(e);
  807. let temp = {
  808. direction: "allCount",
  809. name: "击中",
  810. ename: "hit",
  811. value: e.acc,
  812. mass: e.mass, //质量
  813. hitPower: e.power //计算的力
  814. }
  815. this.sendMessage("onBoxingPostHit", temp);
  816. })
  817. }
  818. }
  819. }
  820. </script>
  821. <style>
  822. /* #ifdef APP-PLUS */
  823. .web-view {
  824. flex: 1;
  825. flex-direction: column;
  826. /* background-color: #007AFF; */
  827. }
  828. .web-view-child {
  829. width: 750rpx;
  830. height: 100%;
  831. flex: 1;
  832. }
  833. /* #endif */
  834. /* #ifdef H5 */
  835. .web-view {
  836. display: flex;
  837. flex-direction: column;
  838. position: absolute;
  839. bottom: 0;
  840. top: 0;
  841. left: 0;
  842. right: 0;
  843. }
  844. .web-view-child {
  845. position: relative;
  846. width: 100%;
  847. height: 100%;
  848. }
  849. /* #endif */
  850. .sendMessage {
  851. width: 300rpx;
  852. position: fixed;
  853. bottom: 100rpx;
  854. left: 50rpx;
  855. }
  856. .web-back {
  857. position: fixed;
  858. top: 40px;
  859. right: 20px;
  860. width: 160rpx;
  861. height: 80rpx;
  862. border-radius: 45px;
  863. /* border: 1px solid #FFFFFF; */
  864. /* box-shadow: 0px 0px 1px #FFFFFF; */
  865. background-color: rgba(0, 0, 0, 1);
  866. opacity: 0.5;
  867. /* #ifndef APP-PLUS-NVUE */
  868. z-Index: 10;
  869. display: flex;
  870. /* #endif */
  871. justify-content: center;
  872. align-items: center;
  873. }
  874. </style>