WebView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. window.onWebViewMessage = function(data){
  2. let name= data.funName;
  3. //比如下面请求了gameInit ,这里接收app 处理返回的数据
  4. if(name == "onGameInit")
  5. {
  6. /**
  7. * onGameInit
  8. * 初始化游戏数据
  9. * gameData :
  10. * avatarUrl:"",//头像url
  11. * avatarBase64Url:"",//base64字符串头像,后面以base64为主
  12. * userName:"", //名字
  13. * gender: 0,//"0:男,1:女"
  14. * caloriUnit:10,//每次跳动消耗的卡路里
  15. * calorieParams:{runUnit:10,jumpUnit:20}, //蹦床时候的参数
  16. * device:null 或 {cname:'中文名',ename:'英文名',name:'设备原有名字'}//'当前选择的设备信息,没选择就为空'
  17. */
  18. console.log("onGameInit ==",data.gameData);
  19. webView.onGameInit(data.gameData);
  20. }
  21. else if(name == "onUploadInfo")
  22. {
  23. /**
  24. * 上传分数后的返回,
  25. * gameData:
  26. * {}
  27. */
  28. // console.log("onUploadInfo ==",data.gameData);
  29. webView.onUploadInfo(data.gameData);
  30. }
  31. else if(name == "onAiRandomInfo")//获取随机AI 头像 姓名等信息
  32. {
  33. /**
  34. * 获取ai信息的数据回调
  35. * aiId: 0, //ai的id
  36. * aiName: "",//ai 的名字
  37. * aiGender: 0,//"0:男,1:女"
  38. * aiType: "",//网名类型
  39. * aiAvatarBase64: ""//base64字符串,头像
  40. */
  41. // console.log("onAiRandomInfo ==",data.gameData);
  42. webView.onAiRandomInfo(data.gameData);
  43. }
  44. else if(name == "onFruitInfo")//获取水果列表
  45. {
  46. /**
  47. * 获取水果图片信息
  48. * fruitBase64: "",// 水果雪碧图 base64
  49. * unitWidth: 100,// 雪碧图 单张图片宽
  50. * unitHeight: 100,// 雪碧图 单张图片高
  51. * unit: "px",// 雪碧图 单位
  52. * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
  53. * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
  54. * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
  55. */
  56. // console.log("onFruitInfo ==",data.gameData);
  57. webView.onFruitInfo(data.gameData);
  58. }
  59. else if(name == "onDeviceUpdateData")//蹦床每次操作后传回来的指令
  60. {
  61. /**
  62. * 设备连接成功后的返回值
  63. * 蹦床的返回数值,有可能两种格式
  64. * gameData:{F:2} , {H:-1,T:0}; //F:0:左 1:右 2:起跳 3:降落 -1: 无状态 T 0--无时间 -1:超时 H 0:左手 1:右手
  65. */
  66. // console.log("onDeviceUpdateData ==",data.gameData);
  67. webView.onDeviceUpdateData(data.gameData);
  68. }
  69. else if(name == "onDeviceState"){
  70. /**
  71. * 暂时只返回设备数据连接错误信息
  72. * gamedata = {state: -1, msg: "设备数据错误"}
  73. */
  74. // console.log("onDeviceState ==",data.gameData);
  75. webView.onDeviceUpdateData(data.gameData);
  76. }
  77. else if(name == "onDeviceClose"){
  78. /**
  79. * 和设备断开连接时候回调
  80. * gamedata = {msg: '设备断开连接。'}
  81. */
  82. // console.log("onDeviceClose ==",data.gameData);
  83. webView.onDeviceUpdateData(data.gameData);
  84. }
  85. else if(name == "onUrlToBase64"){
  86. // console.log("onUrlToBase64 ==",data.gameData);
  87. webView.onUrlToBase64(data.gameData);
  88. }
  89. else if (name == "onWatchAccelerometer") {
  90. /**
  91. * 返回加速计的数据
  92. * {
  93. * xAxis
  94. * yAxis
  95. * zAxis
  96. * }
  97. */
  98. // console.log("onWatchAcc == ", data.gameData);
  99. webView.onWatchAccelerometer(data.gameData);
  100. }
  101. else if (name == "onWatchOrientation") {
  102. /**
  103. * 返回加速计的数据
  104. * {
  105. * xAxis
  106. * yAxis
  107. * zAxis
  108. * }
  109. */
  110. // console.log("onWatchOri == ", data.gameData);
  111. webView.onWatchOrientation(data.gameData);
  112. }
  113. else if(name == "onBoxingPostHit"){
  114. console.log("onBoxingPostHit == ", data.gameData);
  115. webView.onBoxingPostHit(data.gameData);
  116. }
  117. else if (name == "onQuit") {
  118. console.log("onQuit == ", data.gameData);
  119. webView.onQuit(data.gameData);
  120. } else if (name == "onQuitModal") {
  121. console.log("onQuitModal == ", data.gameData);
  122. webView.onQuitModal(data.gameData);
  123. }
  124. else if( name == "onQuitModalListener"){
  125. console.log("onQuitModalListener == ", data.gameData);
  126. }
  127. };
  128. let webView = {
  129. bValid:false,
  130. avatarBase64: null,
  131. avatarSpriteFrame: null,
  132. avatarUrl:'',
  133. userName: '',
  134. gender: 0,
  135. rivalavatarBase64: null,
  136. rivalAvatarSpriteFrame: null,
  137. rivalavatarUrl:'',
  138. rivalUserName: '',
  139. rivalGender: 1,
  140. listenerArr: [],
  141. videoName:'',
  142. kCalUnit:0,
  143. register(listener)//注册后实现对应的回调函数 可以监听到回调
  144. {
  145. //TODO
  146. // 要判断对象是否已经注册过,如果存在不注册
  147. if(this.indexOf(this.listenerArr, listener) != -1) return;
  148. this.listenerArr.push(listener);
  149. },
  150. unRegister(listener) {
  151. //TODO
  152. // 要判断对象是否已经注册过,如果存在才可以删除
  153. if(this.indexOf(this.listenerArr, listener) != -1)
  154. {
  155. this.remove(this.listenerArr, listener);
  156. }
  157. },
  158. dispatchEvent(eventName, data)//把所有的监听事件分发给所有接收到回调的节点
  159. {
  160. for (let i = 0; i < this.listenerArr.length; i++) {
  161. this.listenerArr[i].emit(eventName, data);
  162. }
  163. },
  164. init(listener,callback) {
  165. if(this.indexOf(this.listenerArr, listener) != -1) return;
  166. this.callback = callback;
  167. this.bValid = true;
  168. this.register(listener);//初始化的时候默认直接注册对象,不需要重复注册.
  169. this.gameInitEvent();
  170. },
  171. gameInitEvent() {
  172. // 向服务器自己获取初始化信息
  173. uni.postMessage({
  174. data: {
  175. funName: "gameInit",
  176. gameData: {}
  177. }
  178. });
  179. },
  180. InitLocal(gameData)//初始化自己的头像,姓名等信息
  181. {
  182. this.avatarUrl = gameData.avatarUrl;
  183. this.avatarBase64 = gameData.avatarBase64Url;
  184. this.userName = gameData.userName;
  185. this.gender = gameData.gender;
  186. this.videoName = gameData.item.name;
  187. this.kCalUnit = gameData.calorieParams.hitUnit;
  188. this.callback();
  189. },
  190. uploadInfo(score, gameTime, calorie) {//最后胜利把信息发给服务器记录和统计
  191. uni.postMessage({
  192. data: {
  193. funName: "uploadInfo",
  194. gameData: {
  195. gameScore: score == "" ? 100 : score,//游戏得分
  196. gameTime: gameTime == "" ? 100 : gameTime,//单位秒
  197. calorieBurned: calorie == "" ? 1000 : calorie,//消耗的卡路里
  198. }
  199. }
  200. });
  201. },
  202. getAiInfo(callback) {//获取随机AI信息
  203. this.callback = callback;
  204. uni.postMessage({
  205. data: {
  206. funName: "aiRandomInfo",
  207. gameData: {}
  208. }
  209. })
  210. },
  211. getFruitInfo(calorie) {//获取水果
  212. uni.postMessage({
  213. data: {
  214. funName: "fruitInfo",
  215. gameData: {
  216. calorie: 1150
  217. }
  218. }
  219. })
  220. },
  221. getBase64(url){
  222. uni.postMessage({
  223. data:{
  224. funName:"urlToBase64",
  225. gameData:{
  226. url:url
  227. }
  228. }
  229. })
  230. },
  231. onBind() {
  232. uni.postMessage({
  233. data: {
  234. funName: "openAccelerometer",
  235. gameData: {}
  236. }
  237. })
  238. },
  239. onUnBind() {
  240. uni.postMessage({
  241. data: {
  242. funName: "closeAccelerometer",
  243. gameData: {}
  244. }
  245. })
  246. },
  247. onBindBoxingPost(){
  248. uni.postMessage({
  249. data: {
  250. funName: "bindBoxingPost",
  251. gameData: {}
  252. }
  253. })
  254. },
  255. onModify(){
  256. uni.postMessage({
  257. data: {
  258. funName: "setAngleRatio",
  259. gameData: {
  260. angleRatio:0.5 //z / x 值越小,代表直拳的判断方位越大
  261. }
  262. }
  263. })
  264. },
  265. onBindHitBoxingPost(){
  266. uni.postMessage({
  267. data: {
  268. funName: "bindHitBoxingPost",
  269. gameData: {}
  270. }
  271. })
  272. },
  273. onUnbindHitBoxingPost(){
  274. uni.postMessage({
  275. data: {
  276. funName: "unbindHitBoxingPost",
  277. gameData: {}
  278. }
  279. })
  280. },
  281. closeGame(){
  282. uni.postMessage({
  283. data: {
  284. funName: "closeGame",
  285. gameData: {}
  286. }
  287. });
  288. },
  289. onAddQuitModalListener(){
  290. uni.postMessage({
  291. data: {
  292. funName: "addQuitModal",
  293. gameData: {}
  294. }
  295. });
  296. },
  297. onRemoveQuitModalListener(){
  298. uni.postMessage({
  299. data: {
  300. funName: "removeQuitModal",
  301. gameData: {}
  302. }
  303. });
  304. },
  305. indexOf(arr, item) {//判断元素在数组第几位
  306. for (let i = 0; i < arr.length; i++) {
  307. if (arr[i] == item) return i;
  308. }
  309. return -1;
  310. },
  311. remove(arr, item) {//移除数组中制定元素
  312. let index = arr.indexOf(item);
  313. if (index > -1) {
  314. arr.splice(index, 1);
  315. }
  316. },
  317. // callBack
  318. onGameInit(data) {
  319. webView.InitLocal(data);
  320. webView.dispatchEvent('onGameInit',data);
  321. },
  322. onUploadInfo(data) {
  323. webView.dispatchEvent('onUploadInfo',data);
  324. },
  325. onAiRandomInfo(data) {
  326. this.rivalavatarBase64 = data.aiAvatarBase64Url;
  327. this.rivalUserName = data.aiName;
  328. this.rivalGender = data.aiGender;
  329. webView.dispatchEvent('onAiRandomInfo',data);
  330. },
  331. onFruitInfo(data) {
  332. // this.setImageBase64(data.fruitBase64,function (texture) {
  333. //
  334. // });
  335. webView.dispatchEvent('onFruitInfo',data);
  336. },
  337. onDeviceUpdateData(data) {
  338. // if(!window.gameMode) return;
  339. //
  340. // if(data.F == 0)
  341. // {
  342. // console.log("F == 0");
  343. // window.gameMode.pConSt1.speedUp();
  344. //
  345. // }else if(data.F == 1)
  346. // {
  347. // console.log("F == 1");
  348. // window.gameMode.pConSt1.speedUp();
  349. // }
  350. // else if(data.F == 2)
  351. // {
  352. // console.log("F == 2");
  353. // window.gameMode.pConSt1.jump();
  354. // }
  355. webView.dispatchEvent('onDeviceUpdateData',data);
  356. },
  357. onDeviceState(data) {
  358. webView.dispatchEvent('onDeviceState',data);
  359. },
  360. onDeviceClose(data) {
  361. webView.dispatchEvent('onDeviceClose',data);
  362. },
  363. onUrlToBase64(data)
  364. {
  365. webView.dispatchEvent('onUrlToBase64',data);
  366. },
  367. onWatchAccelerometer(data) {
  368. /**
  369. * 返回加速计的数据
  370. * {
  371. * xAxis
  372. * yAxis
  373. * zAxis
  374. * }
  375. */
  376. webView.dispatchEvent('onWatchAccelerometer',data);
  377. },
  378. onWatchOrientation(data)
  379. {
  380. /**
  381. * 返回加速计的数据
  382. * {
  383. * xAxis
  384. * yAxis
  385. * zAxis
  386. * }
  387. */
  388. webView.dispatchEvent('onWatchOrientation',data);
  389. },
  390. onBoxingPostHit(data)
  391. {
  392. webView.dispatchEvent('onBoxingPostHit',data);
  393. },
  394. onQuit(data)
  395. {
  396. webView.dispatchEvent('onQuit',data);
  397. },
  398. onQuitModal(data)
  399. {
  400. webView.dispatchEvent('onQuitModal',data);
  401. }
  402. };
  403. module.exports = webView;