Login.js 10 KB

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