WeChatSDKManager.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import HTTPS, { NetPost } from "../HTTPS";
  2. export class WeChatSDKManager {
  3. private loginData = {};
  4. private logincode = '';
  5. login(callBack: Function) {
  6. let wx = (window as any)["wx"];
  7. let self = this;
  8. wx.login({
  9. success: (login_res) => {
  10. self.logincode = login_res.code;
  11. console.log("得到登录凭证code");
  12. console.log(login_res.code);
  13. let sysInfo = wx.getSystemInfoSync();
  14. let width = sysInfo.screenWidth;
  15. let height = sysInfo.screenHeight;
  16. wx.getSetting({
  17. success(getSetting_res) {
  18. console.log("wx.getSetting获取用户配置成功");
  19. if (getSetting_res.authSetting['scope.userInfo']) {
  20. wx.getUserInfo({
  21. success: function (res) {
  22. console.log("得到用户信息数据");
  23. console.log(getSetting_res);
  24. self.loginData = res.userInfo;
  25. self.requestGameServer()
  26. callBack?.(res);
  27. }
  28. })
  29. } else {
  30. let button = wx.createUserInfoButton({
  31. type: 'text',
  32. text: '',
  33. style: {
  34. left: 0,
  35. top: 0,
  36. width: width,
  37. height: height,
  38. lineHeight: height,
  39. // backgroundColor: '#ff0000',
  40. color: '#000000',
  41. textAlign: 'center',
  42. fontSize: 18,
  43. borderRadius: 4
  44. }
  45. })
  46. // _disNode.active = true;
  47. button.onTap((res) => {
  48. // 用户同意授权后回调,通过回调可获取用户头像昵称信息
  49. console.log("用户点击后_得到用户信息数据");
  50. console.log(res);
  51. self.loginData = res.userInfo;
  52. self.requestGameServer()
  53. callBack?.(res);
  54. button.hide();
  55. })
  56. button.show();
  57. }
  58. },
  59. // 失败回调
  60. fail(res) {
  61. console.log("wx.getSetting获取用户配置失败");
  62. console.log(res);
  63. },
  64. // 结束回调(调用成功,失败都会执行)
  65. complete(res) {
  66. console.log("wx.getSetting获取用户配置结束");
  67. console.log(res);
  68. }
  69. })
  70. }
  71. });
  72. }
  73. requestGameServer() {
  74. HTTPS.Instance.post(NetPost.Login,
  75. {
  76. platform: 'wechatmini',
  77. code: this.logincode,
  78. avatar: this.loginData['avatarUrl'],
  79. nickname: this.loginData['nickName'],
  80. }).then((resp) => {
  81. console.error('999999999999');
  82. console.error(resp);
  83. })
  84. }
  85. showShareMenu() {
  86. let wx = (window as any)["wx"];
  87. wx.showShareMenu({
  88. menus: ['shareAppMessage', "shareTimeline"],
  89. })
  90. wx.onShareAppMessage(function () {
  91. return {
  92. title: '你能挑战过吗?',
  93. imageUrl: ""
  94. }
  95. })
  96. if (wx.onShareTimeline != null) {
  97. wx.onShareTimeline(function () {
  98. return {
  99. title: '你能挑战过吗?',
  100. imageUrl: ""
  101. }
  102. })
  103. }
  104. }
  105. }