Login.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import utils from "../Network/netUtils";
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. addCount: 0,
  6. progressTag: false,
  7. startButtonPrefab: cc.Prefab,
  8. loadingProcessPrefab: cc.Prefab
  9. },
  10. onLoad() {
  11. cc.audioEngine.stopAll();
  12. // this.progressTag = false;
  13. // this.oldprogressTag = -99;
  14. // cc.loader.onProgress = (completeCount, totalCount) => {
  15. // if (this.progressTag) {
  16. // this.progress = (1 * completeCount / totalCount).toFixed(1);
  17. // console.log("进度条", this.progress + '%', this.progressNode);
  18. // if (this.progressNode != null) {
  19. // this.progressNode.getComponent(cc.ProgressBar).progress = this.progress;
  20. // }
  21. // }
  22. // };
  23. //如果不是微信环境
  24. if (typeof wx === 'undefined') {
  25. // console.log('cc.sys.platform:', cc.sys.platform);
  26. this.h5();
  27. return;
  28. }
  29. this.wxEnv();
  30. },
  31. start() {
  32. //预加载
  33. // cc.director.preloadScene('MyCityScene - 004', (completedCount, totalCount, item) => {
  34. // // let progress = (completedCount / totalCount).toFixed(1);
  35. // // console.log("进度条", progress + '%');
  36. // // if (this.progressNode != null) {
  37. // // this.progressNode.getComponent(cc.ProgressBar).progress = progress;
  38. // // }
  39. // // if(completedCount / totalCount>0){
  40. // // }
  41. // }, () => {
  42. // //onLoaded
  43. // cc.log('Next scene preloaded');
  44. // // cc.director.loadScene('MyCityScene - 004', function () {
  45. // // this.progressTag = false;
  46. // // }.bind(this));
  47. // });
  48. },
  49. loginButton() {
  50. let self = this;
  51. console.log("loginButton");
  52. // cc.loader.loadRes("prefab/login/playButton", (err, texture) => {
  53. let _startButton = cc.instantiate(this.startButtonPrefab);
  54. _startButton.parent = cc.find("Canvas");
  55. _startButton.zIndex = 999;
  56. _startButton.width *= 2;
  57. _startButton.height *= 2;
  58. _startButton.setPosition(0, -230);
  59. console.log("_startButton", _startButton.position);
  60. _startButton.on(cc.Node.EventType.TOUCH_START, () => {
  61. _startButton.destroy();
  62. //显示载入loading
  63. // cc.loader.loadRes("prefab/loading", (err, texture) => {
  64. let _loading = cc.instantiate(this.loadingProcessPrefab);
  65. _loading.parent = cc.find("Canvas");
  66. _loading.zIndex = 999;
  67. _loading.y = -500;
  68. let bgbar = _loading.getChildByName("bgbar");
  69. this.progressNode = bgbar.getChildByName("progress");
  70. this.progressTag = true;
  71. this.addCount = 0;
  72. });
  73. // })
  74. // });
  75. // UtilsPrefabs.init(self.node).addPrefabs(UtilsPrefabs.getPrefabsPath(UtilsPrefabs.LOGINPREFABSNAME.playButton), null, (node) => {
  76. // console.log("生成button", UtilsPrefabs.LOGINPREFABSNAME.playButton);
  77. // node.width *= 2;
  78. // node.height *= 2;
  79. // node.setPosition(0, -230);
  80. // console.log("button", node.position);
  81. // node.on(cc.Node.EventType.TOUCH_START, () => {
  82. // node.destroy();
  83. // //显示载入loading
  84. // self._showLoading();
  85. // // () => {
  86. // // //获取游戏数据
  87. // // self._gotoScene();//跳转场景
  88. // // }
  89. // })
  90. // })
  91. },
  92. h5() {
  93. let self = this;
  94. UtilsPrefabs.init(self.node).addPrefabs(UtilsPrefabs.getPrefabsPath(UtilsPrefabs.LOGINPREFABSNAME.login), null, (node) => {
  95. // UtilsPrefabs.init(self.node).addPrefabs(UtilsPrefabs.getPrefabsPath(UtilsPrefabs.LOGINPREFABSNAME.playButton), null, (node) => {
  96. // node.width *= 2;
  97. // node.height *= 2;
  98. // node.setPosition(0, -230);
  99. // node.on(cc.Node.EventType.TOUCH_START, () => {
  100. // node.destroy();
  101. // self._showLoading();//显示载入loading
  102. // //全局函数
  103. // //获取游戏数据
  104. // self._gotoScene();//跳转场景
  105. // })
  106. // })
  107. })
  108. },
  109. wxEnv() {
  110. // let self = this;
  111. // cc.audioEngine.stopAll();
  112. // this.progressTag = false;
  113. // this.oldprogressTag = -99;
  114. // cc.loader.onProgress = (completeCount, totalCount) => {
  115. // if (this.progressTag) {
  116. // this.progress = (1 * completeCount / totalCount).toFixed(1);
  117. // // console.log("进度条", this.progress + '%', this.progressNode);
  118. // if (this.progressNode != null) {
  119. // this.progressNode.getComponent(cc.ProgressBar).progress = this.progress;
  120. // }
  121. // }
  122. // };
  123. UtilsPrefabs.init(self.node).addPrefabs(UtilsPrefabs.PrefabsName[2], null, (node) => {
  124. // if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  125. // }
  126. //获取登录按钮ui,用wx 接口获取临时链接
  127. WeChat.onGetPlayButtonPicture((data) => {
  128. // console.log('按钮图片:', data);
  129. self.loginbuttonImageUrl = data.tempFileURL;
  130. self.wxLogin();
  131. }, (err) => {
  132. console.error('获取按钮图片失败,检查网络!');
  133. })
  134. });
  135. },
  136. wxLogin() {
  137. let self = this;
  138. let isOnTap = false;
  139. wx.login({
  140. success: function (res) {
  141. if (res.code) {
  142. console.log("登陆成功,获取到code", res.code);
  143. }
  144. //self.loginbuttonImageUrl
  145. // type: 'text',
  146. // text: '开始游戏',
  147. //image: 'https://7465-test-533989-1259102327.tcb.qcloud.la/pictures/PlayButtonnew.png?sign=545e70218ce3106a7e518fdfb8240c5e&t=1556089089',
  148. let button = wx.createUserInfoButton({
  149. type: 'image',
  150. image: self.loginbuttonImageUrl,
  151. style: {
  152. left: wx.getSystemInfoSync().screenWidth / 2 - 80,
  153. top: wx.getSystemInfoSync().screenHeight - 230,
  154. width: 160,
  155. height: 60,
  156. lineHeight: 40,
  157. // backgroundColor: '#ff0000',
  158. color: '#ffffff',
  159. textAlign: 'center',
  160. fontSize: 16,
  161. borderRadius: 4
  162. }
  163. })
  164. button.show();
  165. button.onTap((res) => {
  166. // console.log(res)
  167. if (res.errMsg == "getUserInfo:ok") {
  168. if (isOnTap) return;
  169. isOnTap = true;
  170. //点击允许,授权
  171. // console.log('已经授权。');
  172. //全局函数
  173. //注册或者登录用户信息
  174. WeChat.onRegisterUser(res.userInfo,
  175. (res) => {
  176. //记录用户信息在全局变量
  177. GlobalD.UserInfo = res.result.data.userinfo;
  178. userData.openId = res.result.data.openid;
  179. // console.log('用户信息:',);
  180. //清除微信授权按钮
  181. button.destroy();
  182. self._showLoading();//显示载入loading
  183. //全局函数
  184. //获取游戏数据
  185. WeChat.onGetGameData((res) => {
  186. console.log('获取的游戏数据:', res);
  187. //记录读取的数据到内存里面
  188. userData.readData = res.data;
  189. self._gotoScene();//跳转场景
  190. })
  191. }, (fail) => {//登录失败
  192. isOnTap = false;
  193. });
  194. } else {
  195. //点击拒绝,没有授权
  196. console.log('不允许登录。');
  197. }
  198. })
  199. }
  200. });
  201. },
  202. update(dt) {
  203. if (this.progressTag) {
  204. this.addCount += dt * Math.random();
  205. let progress = (this.addCount / 0.8);
  206. if (progress >= 1) {
  207. //跳转场景
  208. this._gotoScene();
  209. this.progressTag = false;
  210. }
  211. if (this.progressNode) {
  212. this.progressNode.getComponent(cc.ProgressBar).progress = progress;
  213. }
  214. }
  215. },
  216. _showLoading() {
  217. console.log('显示载入预制');
  218. //显示载入预制
  219. UtilsPrefabs.init(this.node)
  220. .addPrefabs(UtilsPrefabs.PrefabsName[3], null, function (node) {
  221. node.y = -500;
  222. var bgbar = UtilsPrefabs.getNode("bgbar", node);
  223. this.progressNode = UtilsPrefabs.getNode("progress", bgbar);
  224. this.progressTag = true;
  225. this.addCount = 0;
  226. }.bind(this));
  227. },
  228. _gotoScene() {
  229. cc.director.loadScene('MyCityScene - 004', function () {
  230. // this.progressTag = false;
  231. }.bind(this));
  232. // //预加载
  233. // cc.director.preloadScene('MyCityScene - 004', (completedCount, totalCount, item) => {
  234. // let progress = (1 * completedCount / totalCount).toFixed(1);
  235. // console.log("进度条", progress + '%');
  236. // if (this.progressNode != null) {
  237. // this.progressNode.getComponent(cc.ProgressBar).progress = progress;
  238. // }
  239. // }, () => {
  240. // //onLoaded
  241. // cc.log('Next scene preloaded');
  242. // cc.director.loadScene('MyCityScene - 004', function () {
  243. // // this.progressTag = false;
  244. // }.bind(this));
  245. // });
  246. }
  247. });