WebView.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //<!-- uni 的 SDK,必须引用。 -->
  2. //<script type="text/javascript" src="//js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
  3. // uni.postMessage 里面字段:
  4. // functionName 目前提供的函数名字:
  5. // "gameInit" ,获取对应的游戏初始化信息,比如头像,性别,等
  6. // "uploadInfo" , 上传游戏完结数据
  7. // ***2020/3/25******新增 *******/
  8. // "aiRandomInfo", 返回一个随机的ai的信息
  9. // "fruitInfo" , 参数{calorie:(int)},返回一个imageurl 和 对应的水果下标数组
  10. // 设备回调
  11. // "onDeviceUpdateData" ,设备回调后面用这个,
  12. // "onDeviceState", 暂时只返回数据连接错误信息,
  13. // "onDeviceClose", 和设备断开连接时候回调,
  14. // gameData 里面按照下面示例给的参考
  15. // 请求数据时候,给app post 数据时候的格式,
  16. // uni.postMessage({
  17. // data:{
  18. // funName:functionName,
  19. // gameData:{}
  20. // }
  21. // });
  22. //统一在onMessage 中接收数据
  23. window.onWebViewMessage = function(data){
  24. let name= data.funName;
  25. //比如下面请求了gameInit ,这里接收app 处理返回的数据
  26. if(name == "onGameInit")
  27. {
  28. /**
  29. * onGameInit
  30. * 初始化游戏数据
  31. * gameData :
  32. * avatarUrl:"",//头像url
  33. * avatarBase64Url:"",//base64字符串头像,后面以base64为主
  34. * userName:"", //名字
  35. * gender: 0,//"0:男,1:女"
  36. * calorieParams:{runUnit:10,jumpUnit:20}, //每次跑/跳动消耗的卡路里
  37. * device:null 或 {cname:'中文名',ename:'英文名',name:'设备原有名字'}//'当前选择的设备信息,没选择就为空'
  38. */
  39. console.log("onGameInit ==",data.gameData);
  40. webView.onGameInit(data.gameData);
  41. }
  42. else if(name == "onUploadInfo")
  43. {
  44. /**
  45. * 上传分数后的返回,
  46. * gameData:
  47. * {}
  48. */
  49. console.log("onUploadInfo ==",data.gameData);
  50. webView.onUploadInfo(data.gameData);
  51. }
  52. else if(name == "onAiRandomInfo")//获取随机AI 头像 姓名等信息
  53. {
  54. /**
  55. * 获取ai信息的数据回调
  56. * aiId: 0, //ai的id
  57. * aiName: "",//ai 的名字
  58. * aiGender: 0,//"0:男,1:女"
  59. * aiType: "",//网名类型
  60. * aiAvatarBase64: ""//base64字符串,头像
  61. */
  62. console.log("onAiRandomInfo ==",data.gameData);
  63. webView.onAiRandomInfo(data.gameData);
  64. }
  65. else if(name == "onFruitInfo")//获取水果列表
  66. {
  67. /**
  68. * 获取水果图片信息
  69. * fruitBase64: "",// 水果雪碧图 base64
  70. * unitWidth: 100,// 雪碧图 单张图片宽
  71. * unitHeight: 100,// 雪碧图 单张图片高
  72. * unit: "px",// 雪碧图 单位
  73. * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
  74. * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
  75. * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
  76. */
  77. console.log("onFruitInfo ==",data.gameData);
  78. webView.onFruitInfo(data.gameData);
  79. }
  80. else if(name == "onDeviceUpdateData")//蹦床每次操作后传回来的指令
  81. {
  82. /**
  83. * 设备连接成功后的返回值
  84. * 蹦床的返回数值,有可能两种格式
  85. * gameData:{F:2} , {H:-1,T:0}; //F:0:左 1:右 2:起跳 3:降落 -1: 无状态 T 0--无时间 -1:超时 H 0:左手 1:右手
  86. */
  87. console.log("onDeviceUpdateData ==",data.gameData);
  88. webView.onDeviceUpdateData(data.gameData);
  89. }
  90. else if(name == "onDeviceState"){
  91. /**
  92. * 暂时只返回设备数据连接错误信息
  93. * gamedata = {state: -1, msg: "设备数据错误"}
  94. */
  95. console.log("onDeviceState ==",data.gameData);
  96. webView.onDeviceUpdateData(data.gameData);
  97. }
  98. else if(name == "onDeviceClose"){
  99. /**
  100. * 和设备断开连接时候回调
  101. * gamedata = {msg: '设备断开连接。'}
  102. */
  103. console.log("onDeviceClose ==",data.gameData);
  104. webView.onDeviceUpdateData(data.gameData);
  105. }else if(name == "onUrlToBase64"){
  106. console.log("onUrlToBase64 ==",data.gameData);
  107. webView.onUrlToBase64(data.gameData);
  108. }
  109. };
  110. let webView = {
  111. avatarBase64: null,
  112. avatarUrl:'',
  113. userName: '',
  114. gender: 0,
  115. rivalavatarBase64: null,
  116. rivalavatarUrl:'',
  117. rivalUserName: '',
  118. rivalGender: 1,
  119. listenerArr: [],
  120. register(listener)//注册后实现对应的回调函数 可以监听到回调
  121. {
  122. //TODO
  123. // 要判断对象是否已经注册过,如果存在不注册
  124. this.listenerArr.push(listener);
  125. },
  126. unRegister(listener) {
  127. //TODO
  128. // 要判断对象是否已经注册过,如果存在才可以删除
  129. this.remove(this.listenerArr, listener);
  130. },
  131. dispatchEvent(eventName, data)//把所有的监听事件分发给所有接收到回调的节点
  132. {
  133. for (let i = 0; i < this.listenerArr.length; i++) {
  134. this.listenerArr[i].emit(eventName, data);
  135. }
  136. },
  137. init(listener) {
  138. this.register(listener);//初始化的时候默认直接注册对象,不需要重复注册.
  139. this.gameInitEvent();
  140. },
  141. gameInitEvent() {
  142. // 向服务器自己获取初始化信息
  143. uni.postMessage({
  144. data: {
  145. funName: "gameInit",
  146. gameData: {}
  147. }
  148. });
  149. },
  150. InitLocal(gameData)//初始化自己的头像,姓名等信息
  151. {
  152. this.avatarUrl = gameData.avatarUrl;
  153. this.avatarBase64 = gameData.avatarBase64Url;
  154. this.userName = gameData.userName;
  155. this.gender = gameData.gender;
  156. },
  157. postMessage(score, gameTime, calorie) {//最后胜利把信息发给服务器记录和统计
  158. uni.postMessage({
  159. data: {
  160. funName: "uploadInfo",
  161. gameData: {
  162. gameScore: score == "" ? 100 : score,//游戏得分
  163. gameTime: gameTime == "" ? 100 : gameTime,//单位秒
  164. calorieBurned: calorie == "" ? 1000 : calorie,//消耗的卡路里
  165. }
  166. }
  167. });
  168. },
  169. getAiInfo(callback) {//获取随机AI信息
  170. this.callback = callback;
  171. uni.postMessage({
  172. data: {
  173. funName: "aiRandomInfo",
  174. gameData: {}
  175. }
  176. })
  177. },
  178. getFruitInfo(calorie) {//获取水果
  179. uni.postMessage({
  180. data: {
  181. funName: "fruitInfo",
  182. gameData: {
  183. calorie: 1000
  184. }
  185. }
  186. })
  187. },
  188. getBase64(url){
  189. uni.postMessage({
  190. data:{
  191. funName:"urlToBase64",
  192. gameData:{
  193. url:url
  194. }
  195. }
  196. })
  197. },
  198. indexOf(arr, item) {//判断元素在数组第几位
  199. for (let i = 0; i < arr.length; i++) {
  200. if (arr[i] == item) return i;
  201. }
  202. return -1;
  203. },
  204. remove(arr, item) {//移除数组中制定元素
  205. let index = arr.indexOf(item);
  206. if (index > -1) {
  207. arr.splice(index, 1);
  208. }
  209. },
  210. // callBack
  211. onGameInit(data) {
  212. webView.InitLocal(data);
  213. webView.dispatchEvent('onGameInit',data);
  214. },
  215. onUploadInfo(data) {
  216. webView.dispatchEvent('onUploadInfo',data);
  217. },
  218. onAiRandomInfo(data) {
  219. this.rivalavatarBase64 = data.aiAvatarBase64Url;
  220. this.rivalUserName = data.aiName;
  221. this.rivalGender = data.aiGender;
  222. webView.dispatchEvent('onAiRandomInfo',data);
  223. },
  224. onFruitInfo(data) {
  225. // this.setImageBase64(data.fruitBase64,function (texture) {
  226. //
  227. // });
  228. webView.dispatchEvent('onFruitInfo',data);
  229. },
  230. onDeviceUpdateData(data) {
  231. // if(!window.gameMode) return;
  232. //
  233. // if(data.F == 0)
  234. // {
  235. // console.log("F == 0");
  236. // window.gameMode.pConSt1.speedUp();
  237. //
  238. // }else if(data.F == 1)
  239. // {
  240. // console.log("F == 1");
  241. // window.gameMode.pConSt1.speedUp();
  242. // }
  243. // else if(data.F == 2)
  244. // {
  245. // console.log("F == 2");
  246. // window.gameMode.pConSt1.jump();
  247. // }
  248. webView.dispatchEvent('onDeviceUpdateData',data);
  249. },
  250. onDeviceState(data) {
  251. webView.dispatchEvent('onDeviceState',data);
  252. },
  253. onDeviceClose(data) {
  254. webView.dispatchEvent('onDeviceClose',data);
  255. },
  256. onUrlToBase64(data)
  257. {
  258. webView.dispatchEvent('onUrlToBase64',data);
  259. },
  260. };
  261. module.exports = webView;