| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- //<!-- uni 的 SDK,必须引用。 -->
- //<script type="text/javascript" src="//js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
- // uni.postMessage 里面字段:
- // functionName 目前提供的函数名字:
- // "gameInit" ,获取对应的游戏初始化信息,比如头像,性别,等
- // "uploadInfo" , 上传游戏完结数据
- // ***2020/3/25******新增 *******/
- // "aiRandomInfo", 返回一个随机的ai的信息
- // "fruitInfo" , 参数{calorie:(int)},返回一个imageurl 和 对应的水果下标数组
- // 设备回调
- // "onDeviceUpdateData" ,设备回调后面用这个,
- // "onDeviceState", 暂时只返回数据连接错误信息,
- // "onDeviceClose", 和设备断开连接时候回调,
- // gameData 里面按照下面示例给的参考
- // 请求数据时候,给app post 数据时候的格式,
- // uni.postMessage({
- // data:{
- // funName:functionName,
- // gameData:{}
- // }
- // });
- //统一在onMessage 中接收数据
- 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 ==",data.gameData);
- webView.onGameInit(data.gameData);
- }
- else if(name == "onUploadInfo")
- {
- /**
- * 上传分数后的返回,
- * gameData:
- * {}
- */
- console.log("onUploadInfo ==",data.gameData);
- 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 ==",data.gameData);
- webView.onAiRandomInfo(data.gameData);
- }
- else if(name == "onFruitInfo")//获取水果列表
- {
- /**
- * 获取水果图片信息
- * fruitBase64: "",// 水果雪碧图 base64
- * unitWidth: 100,// 雪碧图 单张图片宽
- * unitHeight: 100,// 雪碧图 单张图片高
- * unit: "px",// 雪碧图 单位
- * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
- * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
- * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
- */
- console.log("onFruitInfo ==",data.gameData);
- 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 ==",data.gameData);
- webView.onDeviceUpdateData(data.gameData);
- }
- else if(name == "onDeviceState"){
- /**
- * 暂时只返回设备数据连接错误信息
- * gamedata = {state: -1, msg: "设备数据错误"}
- */
- console.log("onDeviceState ==",data.gameData);
- webView.onDeviceUpdateData(data.gameData);
- }
- else if(name == "onDeviceClose"){
- /**
- * 和设备断开连接时候回调
- * gamedata = {msg: '设备断开连接。'}
- */
- console.log("onDeviceClose ==",data.gameData);
- webView.onDeviceUpdateData(data.gameData);
- }else if(name == "onUrlToBase64"){
- console.log("onUrlToBase64 ==",data.gameData);
- webView.onUrlToBase64(data.gameData);
- }
- };
- let webView = {
- avatarBase64: null,
- avatarUrl:'',
- userName: '',
- gender: 0,
- rivalavatarBase64: null,
- rivalavatarUrl:'',
- rivalUserName: '',
- rivalGender: 1,
- 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);
- }
- },
- init(listener) {
- this.register(listener);//初始化的时候默认直接注册对象,不需要重复注册.
- this.gameInitEvent();
- },
- gameInitEvent() {
- // 向服务器自己获取初始化信息
- uni.postMessage({
- data: {
- funName: "gameInit",
- gameData: {}
- }
- });
- },
- InitLocal(gameData)//初始化自己的头像,姓名等信息
- {
- this.avatarUrl = gameData.avatarUrl;
- this.avatarBase64 = gameData.avatarBase64Url;
- this.userName = gameData.userName;
- this.gender = gameData.gender;
- },
- 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.InitLocal(data);
- webView.dispatchEvent('onGameInit',data);
- },
- onUploadInfo(data) {
- webView.dispatchEvent('onUploadInfo',data);
- },
- onAiRandomInfo(data) {
- this.rivalavatarBase64 = data.aiAvatarBase64Url;
- this.rivalUserName = data.aiName;
- this.rivalGender = data.aiGender;
- webView.dispatchEvent('onAiRandomInfo',data);
- },
- onFruitInfo(data) {
- // this.setImageBase64(data.fruitBase64,function (texture) {
- //
- // });
- webView.dispatchEvent('onFruitInfo',data);
- },
- onDeviceUpdateData(data) {
- // if(!window.gameMode) return;
- //
- // if(data.F == 0)
- // {
- // console.log("F == 0");
- // window.gameMode.pConSt1.speedUp();
- //
- // }else if(data.F == 1)
- // {
- // console.log("F == 1");
- // window.gameMode.pConSt1.speedUp();
- // }
- // else if(data.F == 2)
- // {
- // console.log("F == 2");
- // window.gameMode.pConSt1.jump();
- // }
- 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;
|