Login.js 9.0 KB

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