subGame.nvue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <view>
  3. <view class="web-view">
  4. <web-view class="web-view-child" :src="'http://'+LocationGameUrl" ref="webview" @pagestart="onPageStart"
  5. @onPostMessage="handlePostMessage" @error="onError"></web-view>
  6. </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. var currentWebview;
  14. var orientId = null,
  15. accId = null;
  16. import {
  17. mapState,
  18. mapMutations
  19. } from 'vuex';
  20. export default {
  21. computed: mapState(['LocationGameUrl']),
  22. data() {
  23. return {
  24. url: '',
  25. error: '',
  26. PageStart: false, // 记录网页请求的加载状态,true 加载成功 false 加载失败
  27. //当前选择的item
  28. decodeItem: null,
  29. bMyAttitudeListen: false,
  30. count: 0,
  31. startGame: false,
  32. currentWebview: null,
  33. }
  34. },
  35. onLoad() {
  36. let _self = this;
  37. var pages = getCurrentPages();
  38. var page = pages[pages.length - 1];
  39. currentWebview = page.$getAppWebview();
  40. //监听一次调用setOnceGameOption
  41. console.log("子组件初始化");
  42. uni.$once("setOnceGameOption", (option) => {
  43. //http://192.168.1.12:7456
  44. // if (option) {
  45. // this.decodeItem = JSON.parse(decodeURIComponent(option));
  46. // this.url = this.decodeItem.gameWebUrl;
  47. // }
  48. });
  49. //子窗体onload 后获取父窗体的onload数据
  50. uni.$emit("game-load");
  51. uni.getCurrentSubNVue().addEventListener("hide", function() {
  52. console.log("subNVue子窗体已隐藏!");
  53. //通知游戏,页面退出
  54. _self.sendMessage("onSubHide", {
  55. msg: '退出页面'
  56. });
  57. uni.$off('updateBLEDeviceData', _self.gWatchBLEUpdate);
  58. _self.$store.state.bGamePlaying = false;
  59. uni.$emit("game-unload", {});
  60. });
  61. //*****注释蓝牙操作******
  62. //监听物理返回按钮
  63. plus.key.addEventListener('backbutton', () => {
  64. this.navBack();
  65. }, false);
  66. // uni.$on("showGame", (option) => {
  67. // this.onShow();
  68. // });
  69. },
  70. onUnload() {
  71. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  72. this.$store.state.bGamePlaying = false;
  73. console.log("subNVue子窗体 onUnload!");
  74. uni.$emit("game-unload");
  75. },
  76. methods: {
  77. ...mapMutations([]),
  78. navBack() {
  79. this.$store.state.bGamePlaying = false;
  80. // uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  81. uni.getCurrentSubNVue().hide('auto');
  82. },
  83. onShow() {
  84. //设置store状态
  85. this.$store.state.bGamePlaying = true;
  86. // uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  87. uni.getCurrentSubNVue().show('fade-in', 250, () => {});
  88. },
  89. //蓝牙断开连接时候
  90. callbackCloseBLE() {
  91. this.sendMessage("onDeviceClose", {
  92. msg: '设备断开连接。'
  93. });
  94. },
  95. //蓝牙状态回调
  96. callbackBLEState(res) {
  97. this.sendMessage("onDeviceState", res)
  98. },
  99. /**
  100. * 统一发送信息
  101. * @param {Object} gameData
  102. */
  103. sendMessage(functionName, gameData) {
  104. let data = {
  105. "funName": functionName,
  106. "gameData": gameData
  107. }
  108. if (!this.PageStart) {
  109. console.warn("页面未初始化不能传消息", data);
  110. return;
  111. }
  112. let initStr = JSON.stringify(data);
  113. // console.log(initStr);
  114. this.$refs.webview.evalJs("onWebViewMessage(" + initStr + ")");
  115. },
  116. handlePostMessage: function(postData) {
  117. console.log("handlePostMessage得到参数", postData.detail);
  118. let temp = postData.detail.data[0];
  119. let gameData = temp.gameData;
  120. if (temp.funName == "onStartAccAndGyro") {
  121. this.bMyAttitudeListen = true;
  122. this.count = 0;
  123. //监听蓝牙回调
  124. // uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  125. // setTimeout(() => {
  126. // uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  127. // }, 500)
  128. uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  129. } else if (temp.funName == "onStopAccAndGyro") {
  130. this.bMyAttitudeListen = false;
  131. //监听蓝牙回调
  132. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  133. }
  134. },
  135. onPageStart: function(e) {
  136. // 监听页面加载成功
  137. this.PageStart = true;
  138. console.log("onPageStart==", e);
  139. setTimeout(() => {
  140. //加载成功后,显示
  141. // uni.getCurrentSubNVue().show('fade-in', 250, () => {});
  142. this.onShow();
  143. }, 100)
  144. },
  145. onPageFinish: function(e) {
  146. console.log("onPageFinish==", e);
  147. },
  148. onError: function(e) {
  149. // 监听页面加载错误
  150. // this.error = this.url;
  151. console.error(e);
  152. },
  153. gWatchBLEUpdate: function(data) {
  154. //如果是模块外面触发,需要离线打包
  155. // if (this.bMyAttitudeListen) {
  156. // }
  157. //添加计数
  158. data.count = ++this.count;
  159. this.sendMessage("updateAccAndGyro", data);
  160. }
  161. }
  162. }
  163. </script>
  164. <style>
  165. /* #ifdef APP-PLUS */
  166. .web-view {
  167. flex: 1;
  168. flex-direction: column;
  169. /* background-color: #007AFF; */
  170. }
  171. .web-view-child {
  172. width: 750rpx;
  173. flex: 1;
  174. }
  175. /* #endif */
  176. /* #ifdef H5 */
  177. .web-view {
  178. display: flex;
  179. flex-direction: column;
  180. position: absolute;
  181. bottom: 0;
  182. top: 0;
  183. left: 0;
  184. right: 0;
  185. }
  186. .web-view-child {
  187. position: relative;
  188. width: 100%;
  189. height: 100%;
  190. }
  191. /* #endif */
  192. .sendMessage {
  193. width: 300rpx;
  194. position: fixed;
  195. bottom: 100rpx;
  196. left: 50rpx;
  197. }
  198. .web-back {
  199. position: fixed;
  200. top: 40px;
  201. right: 20px;
  202. width: 160rpx;
  203. height: 80rpx;
  204. border-radius: 45px;
  205. /* border: 1px solid #FFFFFF; */
  206. /* box-shadow: 0px 0px 1px #FFFFFF; */
  207. background-color: rgba(0, 0, 0, 1);
  208. opacity: 0.5;
  209. /* #ifndef APP-PLUS-NVUE */
  210. /* z-Index: 999; */
  211. display: flex;
  212. /* #endif */
  213. justify-content: center;
  214. align-items: center;
  215. }
  216. </style>