subGame.nvue 31 KB

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