WebView.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * calorieParams:{runUnit:10,jumpUnit:20}, //每次跑/跳动消耗的卡路里
  15. * device:null 或 {cname:'中文名',ename:'英文名',name:'设备原有名字'}//'当前选择的设备信息,没选择就为空'
  16. */
  17. console.log("onGameInit ==");
  18. webView.onGameInit(data.gameData);
  19. }
  20. else if(name == "onUploadInfo")
  21. {
  22. /**
  23. * 上传分数后的返回,
  24. * gameData:
  25. * {}
  26. */
  27. console.log("onUploadInfo ==");
  28. webView.onUploadInfo(data.gameData);
  29. }
  30. else if(name == "onAiRandomInfo")//获取随机AI 头像 姓名等信息
  31. {
  32. /**
  33. * 获取ai信息的数据回调
  34. * aiId: 0, //ai的id
  35. * aiName: "",//ai 的名字
  36. * aiGender: 0,//"0:男,1:女"
  37. * aiType: "",//网名类型
  38. * aiAvatarBase64: ""//base64字符串,头像
  39. */
  40. console.log("onAiRandomInfo ==");
  41. webView.onAiRandomInfo(data.gameData);
  42. }
  43. else if(name == "onFruitInfo")//获取水果列表
  44. {
  45. /**
  46. * 获取水果图片信息
  47. * fruitBase64Url: "",// 水果雪碧图 base64
  48. * unitWidth: 100,// 雪碧图 单张图片宽
  49. * unitHeight: 100,// 雪碧图 单张图片高
  50. * unit: "px",// 雪碧图 单位
  51. * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
  52. * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
  53. * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
  54. */
  55. console.log("onFruitInfo ==");
  56. webView.onFruitInfo(data.gameData);
  57. }
  58. else if(name == "onDeviceUpdateData")//蹦床每次操作后传回来的指令
  59. {
  60. /**
  61. * 设备连接成功后的返回值
  62. * 蹦床的返回数值,有可能两种格式
  63. * gameData:{F:2} , {H:-1,T:0}; //F:0:左 1:右 2:起跳 3:降落 -1: 无状态 T 0--无时间 -1:超时 H 0:左手 1:右手
  64. */
  65. // console.log("onDeviceUpdateData ==");
  66. webView.onDeviceUpdateData(data.gameData);
  67. }
  68. else if(name == "onDeviceState"){
  69. /**
  70. * 暂时只返回设备数据连接错误信息
  71. * gamedata = {state: -1, msg: "设备数据错误"}
  72. */
  73. console.log("onDeviceState ==");
  74. webView.onDeviceUpdateData(data.gameData);
  75. }
  76. else if(name == "onDeviceClose"){
  77. /**
  78. * 和设备断开连接时候回调
  79. * gamedata = {msg: '设备断开连接。'}
  80. */
  81. console.log("onDeviceClose ==");
  82. webView.onDeviceUpdateData(data.gameData);
  83. }else if(name == "onUrlToBase64"){
  84. console.log("onUrlToBase64 ==");
  85. webView.onUrlToBase64(data.gameData);
  86. }
  87. };
  88. let webView = {
  89. listenerArr: [],
  90. register(listener)//注册后实现对应的回调函数 可以监听到回调
  91. {
  92. //TODO
  93. // 要判断对象是否已经注册过,如果存在不注册
  94. this.listenerArr.push(listener);
  95. },
  96. unRegister(listener) {
  97. //TODO
  98. // 要判断对象是否已经注册过,如果存在才可以删除
  99. this.remove(this.listenerArr, listener);
  100. },
  101. dispatchEvent(eventName, data)//把所有的监听事件分发给所有接收到回调的节点
  102. {
  103. for (let i = 0; i < this.listenerArr.length; i++) {
  104. this.listenerArr[i].emit(eventName, data);
  105. }
  106. },
  107. gameInitEvent() {
  108. // 向服务器自己获取初始化信息
  109. uni.postMessage({
  110. data: {
  111. funName: "gameInit",
  112. gameData: {}
  113. }
  114. });
  115. },
  116. postMessage(score, gameTime, calorie) {//最后胜利把信息发给服务器记录和统计
  117. uni.postMessage({
  118. data: {
  119. funName: "uploadInfo",
  120. gameData: {
  121. gameScore: score == "" ? 100 : score,//游戏得分
  122. gameTime: gameTime == "" ? 100 : gameTime,//单位秒
  123. calorieBurned: calorie == "" ? 1000 : calorie,//消耗的卡路里
  124. }
  125. }
  126. });
  127. },
  128. getAiInfo(callback) {//获取随机AI信息
  129. this.callback = callback;
  130. uni.postMessage({
  131. data: {
  132. funName: "aiRandomInfo",
  133. gameData: {}
  134. }
  135. })
  136. },
  137. getFruitInfo(calorie) {//获取水果
  138. uni.postMessage({
  139. data: {
  140. funName: "fruitInfo",
  141. gameData: {
  142. calorie: 1000
  143. }
  144. }
  145. })
  146. },
  147. getBase64(url){
  148. uni.postMessage({
  149. data:{
  150. funName:"urlToBase64",
  151. gameData:{
  152. url:url
  153. }
  154. }
  155. })
  156. },
  157. indexOf(arr, item) {//判断元素在数组第几位
  158. for (let i = 0; i < arr.length; i++) {
  159. if (arr[i] == item) return i;
  160. }
  161. return -1;
  162. },
  163. remove(arr, item) {//移除数组中制定元素
  164. let index = arr.indexOf(item);
  165. if (index > -1) {
  166. arr.splice(index, 1);
  167. }
  168. },
  169. // callBack
  170. onGameInit(data) {
  171. webView.dispatchEvent('onGameInit',data);
  172. },
  173. onUploadInfo(data) {
  174. webView.dispatchEvent('onUploadInfo',data);
  175. },
  176. onAiRandomInfo(data) {
  177. webView.dispatchEvent('onAiRandomInfo',data);
  178. },
  179. onFruitInfo(data) {
  180. webView.dispatchEvent('onFruitInfo',data);
  181. },
  182. onDeviceUpdateData(data) {
  183. webView.dispatchEvent('onDeviceUpdateData',data);
  184. },
  185. onDeviceState(data) {
  186. webView.dispatchEvent('onDeviceState',data);
  187. },
  188. onDeviceClose(data) {
  189. webView.dispatchEvent('onDeviceClose',data);
  190. },
  191. onUrlToBase64(data)
  192. {
  193. webView.dispatchEvent('onUrlToBase64',data);
  194. },
  195. };
  196. module.exports = webView;