Login.js 8.5 KB

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