Commit 75546213 authored by Edwise's avatar Edwise 🍷

123

parent 10126358
'use strict'; 'use strict';
import { import {
Main Main
} from './output'; } from './output';
import API from '../../api'; import API from '../../api';
import mock from '../../mock';
const app = getApp(); const app = getApp();
const { const {
tbcc tbcc
} = app; } = app;
const { const {
commonToast, commonToast,
getAuthUserInfo, getAuthUserInfo,
navigateToOutside, navigateToOutside,
getSystemInfo getSystemInfo
} = tbcc.tb; } = tbcc.tb;
const checkSystem = async () => { const checkSystem = async () => {
const { const {
version version
} = await getSystemInfo(); } = await getSystemInfo();
console.log('version', version); console.log('version', version);
return tbcc.utils.versionCompare(version, '9.8.0'); return tbcc.utils.versionCompare(version, '9.8.0');
} }
Page({ Page({
data: { data: {
messageModalVisible: false, messageModalVisible: false,
messageModalData: {}, messageModalData: {},
ruleModalVisible: false, ruleModalVisible: false,
levelupModalVisible: false levelupModalVisible: false,
}, selectcatModalVisible: false,
catnameModalVisible: false
},
onLoad(props) { onLoad(props) {
this.setData({}); this.setData({});
checkSystem().then(r => { checkSystem().then(r => {
if (!r) { if (!r) {
my.showToast({ my.showToast({
content: "请升级最新版本的淘宝", content: "请升级最新版本的淘宝",
duration: 5000 duration: 5000
});
}
}); });
}
});
}, },
onReady() {
console.log('on ready')
//授权和登录先搞
// this.getAuth();
setTimeout(() => {
// this.onMessage({
// data: {
// netName: 'mine.showModal',
// parameter: {
// name: 'levelupModal'
// }
// }
// });
// this.onMessage({
// data: {
// netName: 'mine.showModal',
// parameter: {
// name: 'messageModal',
// data: { catName: '喵喵1122' }
// }
// }
// });
// this.onMessage({ onReady() {
// data: { console.log('on ready')
// netName: 'mine.navigateToPage', var that = this;
// parameter: { setTimeout(()=>{
// name: 'pageshop' var parameter = {name:"catnameModal"}
// } var data = {netName:"mine.showModal",parameter}
// } that.onMessage({data:data})
// }); },2000)
}, 1000); //授权和登录先搞
// this.getAuth();
my.__onMessage__ = (e) => {
}, this.onMessage(e)
}
},
init() { init() {
console.log('do init'); console.log('do init');
this.spark_startEngine(); this.spark_startEngine();
},
},
spark_startEngine() { spark_startEngine() {
console.log('spark_startEngine') console.log('spark_startEngine')
var self = this; var self = this;
my.createCanvas({ my.createCanvas({
id: 'canvas', id: 'canvas',
success: ccc => { success: ccc => {
const dpr = my.getSystemInfoSync().pixelRatio; const dpr = my.getSystemInfoSync().pixelRatio;
const windowWidth = my.getSystemInfoSync().windowWidth; const windowWidth = my.getSystemInfoSync().windowWidth;
const windowHeight = my.getSystemInfoSync().windowHeight; const windowHeight = my.getSystemInfoSync().windowHeight;
ccc.width = windowWidth * dpr + dpr; //重新修改会有误差 ccc.width = windowWidth * dpr + dpr; //重新修改会有误差
ccc.height = windowHeight * dpr + dpr; ccc.height = windowHeight * dpr + dpr;
if (!this.main) { if (!this.main) {
this.main = new Main(ccc); //添加事件,为了和main里的通信 this.main = new Main(ccc); //添加事件,为了和main里的通信
app.canvasMain = this.main; app.canvasMain = this.main;
this.main.addGlobalEvent("onMessage", self.onMessage, self); this.main.addGlobalEvent("onMessage", self.onMessage, self);
this.main.addGlobalEvent("showToast", self.showToast, self); this.main.addGlobalEvent("showToast", self.showToast, self);
} }
} }
}); });
}, },
onCanvasReady() { onCanvasReady() {
console.log('onCanvasReady') console.log('onCanvasReady')
//授权和登录先搞 //授权和登录先搞
checkSystem().then(r => { checkSystem().then(r => {
if (r) { if (r) {
this.getAuth(); this.getAuth();
} }
}); });
}, },
showToast(e) { showToast(e) {
my.showToast({ my.showToast({
content: e.data || "网络异常,请稍后重试~", content: e.data || "网络异常,请稍后重试~",
duration: 2000 duration: 2000
}); });
}, },
postMessage(netName, data) { postMessage(netName, data) {
this.main && this.main.dispatchGlobalEvent({ this.main && this.main.dispatchGlobalEvent({
type: netName type: netName
}, data); //以防复用事件event串了,就用这种方式 }, data); //以防复用事件event串了,就用这种方式
}, },
//和main通信的方法,接收main的信息 //和main通信的方法,接收main的信息
onMessage(e) { onMessage(e) {
//接口名字 //接口名字
let netName = e.data.netName; //接口参数 let netName = e.data.netName; //接口参数
let parameter = e.data.parameter; //暂时发现3个需要用户操作的接口 let parameter = e.data.parameter; //暂时发现3个需要用户操作的接口
//定制的授权 //定制的授权
if (netName.indexOf("mine") == 0) { if (netName.indexOf("mine") == 0) {
var data = { var data = {
success: true success: true
}; };
switch (netName) { switch (netName) {
case "mine.getAppData": case "mine.getAppData":
//获取信息 //获取信息
data = { data = {
success: true, success: true,
data: { data: {
activityId: app.activityId, activityId: app.activityId,
nickName: app.nickName, nickName: app.nickName,
avatar: app.avatar, avatar: app.avatar,
openId: app.openId openId: app.openId
}
};
break;
case "mine.navigateToPage": //切换page
data = {
success: true,
data: {}
};
my.navigateTo({
url: `/pages/${parameter.name}/${parameter.name}`
});
break;
case "mine.showModal": //切换page
data = {
success: true,
data: {}
};
const key = `${parameter.name}Visible`;
const datakey = `${parameter.name}Data`;
const toSetData = {};
toSetData[key] = true;
toSetData[datakey] = parameter.data;
this.setData(toSetData);
break;
case 'test':
console.log('测试一下', parameter.hint);
break;
} }
};
break;
case "mine.navigateToPage": //切换page
data = {
success: true,
data: {}
};
my.navigateTo({
url: `/pages/${parameter.name}/${parameter.name}`
});
break;
case "mine.showModal": //切换page
data = {
success: true,
data: {}
};
const key = `${parameter.name}Visible`;
const datakey = `${parameter.name}Data`;
const toSetData = {};
toSetData[key] = true;
toSetData[datakey] = parameter.data;
this.setData(toSetData);
break;
case 'test':
console.log('测试一下', parameter.hint);
break;
}
this.postMessage(netName, data); this.postMessage(netName, data);
} else { } else {
const { const {
function: fc function: fc
} = app.cloud; } = app.cloud;
const [myCloudName, handler] = netName.split("."); //待写, const [myCloudName, handler] = netName.split("."); //待写,
parameter.activityId = app.activityId; if (app.requestType == 'yapi') {
fc.invoke(myCloudName, parameter, handler).then(res => { console.log('mock[handler]', mock[handler])
this.postMessage(netName, res); this.postMessage(netName, mock[handler]);
}); return;
} }
},
//鼠标事件 parameter.activityId = app.activityId;
onMouseEvent(e) { fc.invoke(myCloudName, parameter, handler).then(res => {
if (this.main) this.main.stage.onMouseEvent(e); this.postMessage(netName, res);
}, });
}
},
onShow() { //鼠标事件
// 页面显示 onMouseEvent(e) {
if (this.main) { if (this.main) this.main.stage.onMouseEvent(e);
this.main.run(); },
this.main.dispatchGlobalEvent("onShow");
}
},
onHide() { onShow() {
// 页面隐藏 // 页面显示
if (this.main) { if (this.main) {
this.main.pause(); this.main.run();
this.main.dispatchGlobalEvent("onHide"); this.main.dispatchGlobalEvent("onShow");
} }
}, },
/** onHide() {
* 获取用户授权信息 // 页面隐藏
*/ if (this.main) {
async getAuth() { this.main.pause();
const userInfo = await getAuthUserInfo().catch(err => { this.main.dispatchGlobalEvent("onHide");
console.log('未授权成功', err); }
}); },
if (userInfo) {
console.log('userInfo', userInfo);
const {
nickName,
avatar
} = userInfo;
app.nickName = nickName;
app.avatar = avatar;
this.login();
}
},
/**
* 登录接口
*/
async login() {
const {
nickName,
avatar
} = app;
const {
success
} = await API.login({
userNick: nickName,
avatar
}).catch(res => {
commonToast(res && res.message);
}) || {};
if (success) { /**
this.init(); * 获取用户授权信息
} */
}, async getAuth() {
closeLevelupModal() { const userInfo = await getAuthUserInfo().catch(err => {
this.setData({ console.log('未授权成功', err);
levelupModalVisible: false });
}); if (userInfo) {
}, console.log('userInfo', userInfo);
closeRuleModal() { const {
this.setData({ nickName,
ruleModalVisible: false avatar
}); } = userInfo;
}, app.nickName = nickName;
closeMessageModal() { app.avatar = avatar;
this.setData({ this.init();
messageModalVisible: false // this.login();
});
} }
},
/**
* 登录接口
*/
async login() {
const {
nickName,
avatar
} = app;
const {
success
} = await API.login({
userNick: nickName,
avatar
}).catch(res => {
commonToast(res && res.message);
}) || {};
}); if (success) {
\ No newline at end of file this.init();
}
},
closeLevelupModal() {
this.setData({
levelupModalVisible: false
});
},
closeRuleModal() {
this.setData({
ruleModalVisible: false
});
},
closeMessageModal() {
this.setData({
messageModalVisible: false
});
},
closeCatnameModal() {
this.setData({
catnameModalVisible: false
})
},
closeSelectcatModal() {
this.setData({
selectcatModalVisible: false
})
},
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment