| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- cc.Class({
- extends: cc.Component,
- properties: {
- },
- onLoad() {
- let self = this;
- //如果不是微信环境
- if (typeof wx === 'undefined') {
- return;
- }
- cc.audioEngine.stopAll();
- this.progressTag = false;
- this.oldprogressTag = -99;
- cc.loader.onProgress = (completeCount, totalCount) => {
- if (this.progressTag) {
- this.progress = (1 * completeCount / totalCount).toFixed(1);
- // console.log("进度条", this.progress + '%', this.progressNode);
- if (this.progressNode != null) {
- this.progressNode.getComponent(cc.ProgressBar).progress = this.progress;
- }
- }
- };
- UtilsPrefabs.init(self.node).addPrefabs(UtilsPrefabs.PrefabsName[2], null, (node) => {
- // if (cc.sys.platform != cc.sys.WECHAT_GAME) {
- // }
- //获取登录按钮ui,用wx 接口获取临时链接
- WeChat.onGetPlayButtonPicture((data) => {
- // console.log('按钮图片:', data);
- self.loginbuttonImageUrl = data.tempFileURL;
- self.wxLogin();
- }, (err) => {
- console.error('获取按钮图片失败,检查网络!');
- })
- });
- },
- wxLogin() {
- let self = this;
- let isOnTap = false;
- wx.login({
- success: function (res) {
- if (res.code) {
- console.log("登陆成功,获取到code", res.code);
- }
- //self.loginbuttonImageUrl
- // type: 'text',
- // text: '开始游戏',
- //image: 'https://7465-test-533989-1259102327.tcb.qcloud.la/pictures/PlayButtonnew.png?sign=545e70218ce3106a7e518fdfb8240c5e&t=1556089089',
- let button = wx.createUserInfoButton({
- type: 'image',
- image: self.loginbuttonImageUrl,
- style: {
- left: wx.getSystemInfoSync().screenWidth / 2 - 80,
- top: wx.getSystemInfoSync().screenHeight - 230,
- width: 160,
- height: 60,
- lineHeight: 40,
- backgroundColor: '#fb94a9',
- color: '#ffffff',
- textAlign: 'center',
- fontSize: 16,
- borderRadius: 4
- }
- })
- button.show();
- button.onTap((res) => {
- // console.log(res)
- if (res.errMsg == "getUserInfo:ok") {
- if (isOnTap) return;
- isOnTap = true;
- //点击允许,授权
- // console.log('已经授权。');
- //全局函数
- //注册或者登录用户信息
- WeChat.onRegisterUser(res.userInfo,
- (res) => {
- //记录用户信息在全局变量
- GlobalD.UserInfo = res.result.data.userinfo;
- userData.openId = res.result.data.openid;
- // console.log('用户信息:',);
- //清除微信授权按钮
- button.destroy();
- self._showLoading();//显示载入loading
- //全局函数
- //获取游戏数据
- WeChat.onGetGameData((res) => {
- console.log('获取的游戏数据:', res);
- //记录读取的数据到内存里面
- userData.readData = res.data;
- self._gotoScene();//跳转场景
- })
- }, (fail) => {//登录失败
- isOnTap = false;
- });
- } else {
- //点击拒绝,没有授权
- console.log('不允许登录。');
- }
- })
- }
- });
- },
- _showLoading() {
- console.log('显示载入预制');
- //显示载入预制
- UtilsPrefabs.init(this.node)
- .addPrefabs(UtilsPrefabs.PrefabsName[3], null, function (node) {
- node.y = -500;
- var bgbar = UtilsPrefabs.getNode("bgbar", node);
- this.progressNode = UtilsPrefabs.getNode("progress", bgbar);
- this.progressTag = true;
- }.bind(this));
- },
- _gotoScene() {
- cc.director.loadScene('MyCityScene - 004', function () {
- this.progressTag = false;
- }.bind(this));
- }
- });
|