subGame.nvue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.BLECallback);
  58. uni.$off('updateBLEDeviceData', _self.gWatchBLEUpdate);
  59. if (_self.BLEConnectDevice && _self.BLEConnectDevice.deviceType == "BLEHandle") {
  60. //开启设备回调 3/4关闭
  61. _self.onWriteBLEConnectionValue({
  62. value: "4"
  63. });
  64. }
  65. _self.$store.state.bGamePlaying = false;
  66. uni.$emit("game-unload", {});
  67. });
  68. //*****注释蓝牙操作******
  69. //监听物理返回按钮
  70. plus.key.addEventListener('backbutton', () => {
  71. this.navBack();
  72. }, false);
  73. // uni.$on("showGame", (option) => {
  74. // this.onShow();
  75. // });
  76. },
  77. onUnload() {
  78. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  79. this.$store.state.bGamePlaying = false;
  80. console.log("subNVue子窗体 onUnload!");
  81. uni.$emit("game-unload");
  82. },
  83. methods: {
  84. ...mapMutations(["onWriteBLEConnectionValue"]),
  85. navBack() {
  86. this.$store.state.bGamePlaying = false;
  87. // uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  88. uni.getCurrentSubNVue().hide('auto');
  89. },
  90. onShow() {
  91. //设置store状态
  92. this.$store.state.bGamePlaying = true;
  93. // uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  94. uni.getCurrentSubNVue().show('fade-in', 250, () => {});
  95. },
  96. //蓝牙断开连接时候
  97. callbackCloseBLE() {
  98. this.sendMessage("onDeviceClose", {
  99. msg: '设备断开连接。'
  100. });
  101. },
  102. //蓝牙状态回调
  103. callbackBLEState(res) {
  104. this.sendMessage("onDeviceState", res)
  105. },
  106. /**
  107. * 统一发送信息
  108. * @param {Object} gameData
  109. */
  110. sendMessage(functionName, gameData) {
  111. let data = {
  112. "funName": functionName,
  113. "gameData": gameData
  114. }
  115. if (!this.PageStart) {
  116. console.warn("页面未初始化不能传消息", data);
  117. return;
  118. }
  119. let initStr = JSON.stringify(data);
  120. // console.log(initStr);
  121. this.$refs.webview.evalJs("onWebViewMessage(" + initStr + ")");
  122. },
  123. handlePostMessage: function(postData) {
  124. console.log("handlePostMessage得到参数", postData.detail);
  125. let temp = postData.detail.data[0];
  126. let gameData = temp.gameData;
  127. if (temp.funName == "onStartAccAndGyro") {
  128. this.bMyAttitudeListen = true;
  129. this.count = 0;
  130. //监听蓝牙回调
  131. // uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  132. // setTimeout(() => {
  133. // uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  134. // }, 500)
  135. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  136. uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
  137. } else if (temp.funName == "onStopAccAndGyro") {
  138. this.bMyAttitudeListen = false;
  139. //监听蓝牙回调
  140. uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
  141. } else if (temp.funName == "addDeviceUpdateListener") {
  142. uni.$off('updateBLEDeviceData', this.BLECallback);
  143. uni.$on('updateBLEDeviceData', this.BLECallback);
  144. } else if (temp.funName == "closeDeviceUpdateListener") {
  145. uni.$off('updateBLEDeviceData', this.BLECallback);
  146. } else if (temp.funName == "writeBLEConnectionValue") {
  147. if (!gameData.value || gameData.value == "") return;
  148. //蓝牙写入数据
  149. this.onWriteBLEConnectionValue(gameData);
  150. } else if (temp.funName == "saveData") {
  151. if (!gameData.value || gameData.value == "") return;
  152. uni.setStorageSync('saveData', gameData.value);
  153. } else if (temp.funName == "getData") {
  154. const saveData = uni.getStorageSync('saveData');
  155. this.sendMessage("saveData", saveData);
  156. }
  157. },
  158. onPageStart: function(e) {
  159. // 监听页面加载成功
  160. this.PageStart = true;
  161. console.log("onPageStart==", e);
  162. setTimeout(() => {
  163. //加载成功后,显示
  164. // uni.getCurrentSubNVue().show('fade-in', 250, () => {});
  165. this.onShow();
  166. }, 100)
  167. },
  168. onPageFinish: function(e) {
  169. console.log("onPageFinish==", e);
  170. },
  171. onError: function(e) {
  172. // 监听页面加载错误
  173. // this.error = this.url;
  174. console.error(e);
  175. },
  176. gWatchBLEUpdate: function(data) {
  177. //如果是模块外面触发,需要离线打包
  178. // if (this.bMyAttitudeListen) {
  179. // }
  180. //添加计数
  181. data.count = ++this.count;
  182. this.sendMessage("updateAccAndGyro", data);
  183. },
  184. BLECallback(data) {
  185. let dataType = 'Box';
  186. // if (data.hasOwnProperty("ax")) {
  187. // dataType = "Accelerometer"
  188. // }else if(data.hasOwnProperty("gx")){
  189. // dataType = "Gyroscope"
  190. // }
  191. // 设备回调后面用这个接口
  192. let BLEState = {
  193. device: this.BLEConnectDevice, //连接的蓝牙设备
  194. instructionState: this.instructionState, //开启的指令状态
  195. currentInstruction: this.currentInstruction, //当前发送的指令记录
  196. dataType: dataType, //回调的数据类型
  197. data: data //蓝牙回调的数据
  198. }
  199. this.sendMessage("onDeviceUpdateData", BLEState);
  200. },
  201. }
  202. }
  203. </script>
  204. <style>
  205. /* #ifdef APP-PLUS */
  206. .web-view {
  207. flex: 1;
  208. flex-direction: column;
  209. /* background-color: #007AFF; */
  210. }
  211. .web-view-child {
  212. width: 750rpx;
  213. flex: 1;
  214. }
  215. /* #endif */
  216. /* #ifdef H5 */
  217. .web-view {
  218. display: flex;
  219. flex-direction: column;
  220. position: absolute;
  221. bottom: 0;
  222. top: 0;
  223. left: 0;
  224. right: 0;
  225. }
  226. .web-view-child {
  227. position: relative;
  228. width: 100%;
  229. height: 100%;
  230. }
  231. /* #endif */
  232. .sendMessage {
  233. width: 300rpx;
  234. position: fixed;
  235. bottom: 100rpx;
  236. left: 50rpx;
  237. }
  238. .web-back {
  239. position: fixed;
  240. top: 40px;
  241. right: 20px;
  242. width: 160rpx;
  243. height: 80rpx;
  244. border-radius: 45px;
  245. /* border: 1px solid #FFFFFF; */
  246. /* box-shadow: 0px 0px 1px #FFFFFF; */
  247. background-color: rgba(0, 0, 0, 1);
  248. opacity: 0.5;
  249. /* #ifndef APP-PLUS-NVUE */
  250. /* z-Index: 999; */
  251. display: flex;
  252. /* #endif */
  253. justify-content: center;
  254. align-items: center;
  255. }
  256. </style>