| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- window.onWebViewMessage = function(data){
- let name= data.funName;
- //比如下面请求了gameInit ,这里接收app 处理返回的数据
- if(name == "onGameInit")
- {
- /**
- * onGameInit
- * 初始化游戏数据
- * gameData :
- * avatarUrl:"",//头像url
- * avatarBase64Url:"",//base64字符串头像,后面以base64为主
- * userName:"", //名字
- * gender: 0,//"0:男,1:女"
- * calorieParams:{runUnit:10,jumpUnit:20}, //每次跑/跳动消耗的卡路里
- * device:null 或 {cname:'中文名',ename:'英文名',name:'设备原有名字'}//'当前选择的设备信息,没选择就为空'
- */
- console.log("onGameInit ==");
- webView.onGameInit(data.gameData);
- }
- else if(name == "onUploadInfo")
- {
- /**
- * 上传分数后的返回,
- * gameData:
- * {}
- */
- console.log("onUploadInfo ==");
- webView.onUploadInfo(data.gameData);
- }
- else if(name == "onAiRandomInfo")//获取随机AI 头像 姓名等信息
- {
- /**
- * 获取ai信息的数据回调
- * aiId: 0, //ai的id
- * aiName: "",//ai 的名字
- * aiGender: 0,//"0:男,1:女"
- * aiType: "",//网名类型
- * aiAvatarBase64: ""//base64字符串,头像
- */
- console.log("onAiRandomInfo ==");
- webView.onAiRandomInfo(data.gameData);
- }
- else if(name == "onFruitInfo")//获取水果列表
- {
- /**
- * 获取水果图片信息
- * fruitBase64Url: "",// 水果雪碧图 base64
- * unitWidth: 100,// 雪碧图 单张图片宽
- * unitHeight: 100,// 雪碧图 单张图片高
- * unit: "px",// 雪碧图 单位
- * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
- * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
- * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
- */
- console.log("onFruitInfo ==");
- webView.onFruitInfo(data.gameData);
- }
- else if(name == "onDeviceUpdateData")//蹦床每次操作后传回来的指令
- {
- /**
- * 设备连接成功后的返回值
- * 蹦床的返回数值,有可能两种格式
- * gameData:{F:2} , {H:-1,T:0}; //F:0:左 1:右 2:起跳 3:降落 -1: 无状态 T 0--无时间 -1:超时 H 0:左手 1:右手
- */
- // console.log("onDeviceUpdateData ==");
- webView.onDeviceUpdateData(data.gameData);
- }
- else if(name == "onDeviceState"){
- /**
- * 暂时只返回设备数据连接错误信息
- * gamedata = {state: -1, msg: "设备数据错误"}
- */
- console.log("onDeviceState ==");
- webView.onDeviceUpdateData(data.gameData);
- }
- else if(name == "onDeviceClose"){
- /**
- * 和设备断开连接时候回调
- * gamedata = {msg: '设备断开连接。'}
- */
- console.log("onDeviceClose ==");
- webView.onDeviceUpdateData(data.gameData);
- }else if(name == "onUrlToBase64"){
- console.log("onUrlToBase64 ==");
- webView.onUrlToBase64(data.gameData);
- }
- };
- let webView = {
- listenerArr: [],
- register(listener)//注册后实现对应的回调函数 可以监听到回调
- {
- //TODO
- // 要判断对象是否已经注册过,如果存在不注册
- this.listenerArr.push(listener);
- },
- unRegister(listener) {
- //TODO
- // 要判断对象是否已经注册过,如果存在才可以删除
- this.remove(this.listenerArr, listener);
- },
- dispatchEvent(eventName, data)//把所有的监听事件分发给所有接收到回调的节点
- {
- for (let i = 0; i < this.listenerArr.length; i++) {
- this.listenerArr[i].emit(eventName, data);
- }
- },
- gameInitEvent() {
- // 向服务器自己获取初始化信息
- uni.postMessage({
- data: {
- funName: "gameInit",
- gameData: {}
- }
- });
- },
- postMessage(score, gameTime, calorie) {//最后胜利把信息发给服务器记录和统计
- uni.postMessage({
- data: {
- funName: "uploadInfo",
- gameData: {
- gameScore: score == "" ? 100 : score,//游戏得分
- gameTime: gameTime == "" ? 100 : gameTime,//单位秒
- calorieBurned: calorie == "" ? 1000 : calorie,//消耗的卡路里
- }
- }
- });
- },
- getAiInfo(callback) {//获取随机AI信息
- this.callback = callback;
- uni.postMessage({
- data: {
- funName: "aiRandomInfo",
- gameData: {}
- }
- })
- },
- getFruitInfo(calorie) {//获取水果
- uni.postMessage({
- data: {
- funName: "fruitInfo",
- gameData: {
- calorie: 1000
- }
- }
- })
- },
- getBase64(url){
- uni.postMessage({
- data:{
- funName:"urlToBase64",
- gameData:{
- url:url
- }
- }
- })
- },
- indexOf(arr, item) {//判断元素在数组第几位
- for (let i = 0; i < arr.length; i++) {
- if (arr[i] == item) return i;
- }
- return -1;
- },
- remove(arr, item) {//移除数组中制定元素
- let index = arr.indexOf(item);
- if (index > -1) {
- arr.splice(index, 1);
- }
- },
- // callBack
- onGameInit(data) {
- webView.dispatchEvent('onGameInit',data);
- },
- onUploadInfo(data) {
- webView.dispatchEvent('onUploadInfo',data);
- },
- onAiRandomInfo(data) {
- webView.dispatchEvent('onAiRandomInfo',data);
- },
- onFruitInfo(data) {
- webView.dispatchEvent('onFruitInfo',data);
- },
- onDeviceUpdateData(data) {
- webView.dispatchEvent('onDeviceUpdateData',data);
- },
- onDeviceState(data) {
- webView.dispatchEvent('onDeviceState',data);
- },
- onDeviceClose(data) {
- webView.dispatchEvent('onDeviceClose',data);
- },
- onUrlToBase64(data)
- {
- webView.dispatchEvent('onUrlToBase64',data);
- },
- };
- module.exports = webView;
|